UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (!node) return node; if(hash.find(node) == hash.end()) { hash[node] = new UndirectedGraphNode(node -> label); for (int i=0;i<(node->neighbors).size();i++) { (hash[node] -> neighbors).push_back( cloneGraph((node->neighbors)[i]) ); } } return hash[node]; }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (!node) return NULL; if (m.find(node) == m.end()) { m[node] = new UndirectedGraphNode(node->label); for (UndirectedGraphNode* neighbor : node->neighbors) (m[node]->neighbors).push_back(cloneGraph(neighbor)); } return m[node]; }
UndirectedGraphNode* Solution::cloneGraph(UndirectedGraphNode *node) { if (!node) return node; if(hash.find(node) == hash.end()) { hash[node] = new UndirectedGraphNode(node -> label); for (auto x : node -> neighbors) { (hash[node] -> neighbors).push_back( cloneGraph(x) ); } } return hash[node]; }
/* cloneG: */ Agraph_t *cloneG(Agraph_t * g, char* name) { Agraph_t* ng; if (!name || (*name == '\0')) name = agnameof (g); ng = openG(name, g->desc); if (ng) { copyAttr((Agobj_t*)g, (Agobj_t*)ng); cloneGraph(ng, g); } return ng; }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode* node, map<int, UndirectedGraphNode*> &recs) { if (node == NULL) return NULL; if (recs.count(node->label) > 0) return recs[node->label]; UndirectedGraphNode *copy = new UndirectedGraphNode(node->label); recs[node->label] = copy; for (int i = 0; i < node->neighbors.size(); i++) { UndirectedGraphNode *neighbor = cloneGraph(node->neighbors[i], recs); copy->neighbors.push_back(neighbor); } return copy; }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if(!node) return node; if(mp.find(node->label) == mp.end()) { UndirectedGraphNode *x = new UndirectedGraphNode(node->label); mp[node->label] = x; for(auto& nb: node->neighbors) { x->neighbors.push_back(cloneGraph(nb)); } return x; } else { return mp[node->label]; } }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if(!node) { return node; } //the node isnot in the hashtable if(hash.find(node) == hash.end()) { hash[node] = new UndirectedGraphNode(node->label); for(auto x : node->neighbors) { (hash[node] -> neighbors).push_back(cloneGraph(x)); } } return hash[node]; }
flatbuffers::Offset<FlatResult> GraphHolder::execute(Nd4jLong graphId, flatbuffers::FlatBufferBuilder &builder, const FlatInferenceRequest* request) { if (!hasGraph(graphId)) throw unknown_graph_exception(graphId); lockRead(graphId); auto graph = cloneGraph(graphId); auto res = GraphExecutioner::execute(graph, builder, request); delete graph; unlockRead(graphId); return res; }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node, unordered_map<int, UndirectedGraphNode *>& track) { if(!node) return NULL; if(track.count(node->label)) return track[node->label]; UndirectedGraphNode *new_node = new UndirectedGraphNode(node->label); new_node->neighbors.resize(node->neighbors.size()); track[node->label] = new_node; for(int i = 0; i < node->neighbors.size(); i++) { new_node->neighbors[i] = cloneGraph(node->neighbors[i], track); } return new_node; }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *start, map<UndirectedGraphNode*, UndirectedGraphNode*>& m) { if (start == NULL) return NULL; UndirectedGraphNode* new_start; if (m.find(start) != m.end()) { return m[start]; } else { new_start = new UndirectedGraphNode(start->label); m[start] = new_start; } for (int i=0; i<start->neighbors.size(); ++i) { UndirectedGraphNode* new_neighbor = cloneGraph(start->neighbors[i], m); new_start->neighbors.push_back(new_neighbor); } return new_start; }
UndirectedGraphNode* cloneGraph(UndirectedGraphNode* node) { if (node == NULL) return NULL; static map<UndirectedGraphNode*, UndirectedGraphNode*> visited; UndirectedGraphNode* result; if (visited.count(node) != 0) { return visited[node]; } else { result = new UndirectedGraphNode(node->label); visited[node] = result; } for (int i = 0; i < node->neighbors.size(); i++) { result->neighbors.push_back(cloneGraph(node->neighbors[i])); } return result; }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (node == NULL) { return NULL; } UndirectedGraphNode* d = new UndirectedGraphNode(node->label); finished[node->label] = d; for (auto p : node->neighbors) { auto f = finished.find(p->label); if (f != finished.end()) { d->neighbors.push_back(f->second); } else { d->neighbors.push_back(cloneGraph(p)); } } return d; }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (node == NULL) return NULL; auto it = nodeMap_.find(node->label); if (it != nodeMap_.end()) { return it->second; } //allocate my node UGN *myNode = new UGN(*node); nodeMap_.insert(make_pair(myNode->label, myNode)); //even though self-reference not established yet, this is ok! for (unsigned int i=0; i<node->neighbors.size(); i++) { myNode->neighbors[i] = cloneGraph(node->neighbors[i]); //self reference established automatically if needed } return myNode; }
/* clone: * Create new object of type AGTYPE(obj) with all of its * attributes and substructure. * If obj is an edge, end nodes are cloned if necessary. * If obj is a graph, if g is null, create a clone top-level * graph. Otherwise, create a clone subgraph of g. * Assume obj != NULL. */ Agobj_t *clone(Agraph_t * g, Agobj_t * obj) { Agobj_t *nobj = 0; Agedge_t *e; Agnode_t *h; Agnode_t *t; int kind = AGTYPE(obj); char *name; if ((kind != AGRAPH) && !g) { exerror("NULL graph with non-graph object in clone()"); return 0; } switch (kind) { case AGNODE: /* same as copy node */ name = agnameof(obj); nobj = (Agobj_t *) openNode(g, name); if (nobj) copyAttr(obj, nobj); break; case AGRAPH: name = agnameof(obj); if (g) nobj = (Agobj_t *) openSubg(g, name); else nobj = (Agobj_t *) openG(name, ((Agraph_t *) obj)->desc); if (nobj) { copyAttr(obj, nobj); cloneGraph((Agraph_t *) nobj, (Agraph_t *) obj); } break; case AGINEDGE: case AGOUTEDGE: e = (Agedge_t *) obj; t = (Agnode_t *) clone(g, OBJ(agtail(e))); h = (Agnode_t *) clone(g, OBJ(aghead(e))); name = agnameof (AGMKOUT(e)); nobj = (Agobj_t *) openEdge(g, t, h, name); if (nobj) copyAttr(obj, nobj); break; } return nobj; }
void cloneGraph(UndirectedGraphNode *parent, UndirectedGraphNode* node) { UndirectedGraphNode *tmp = new UndirectedGraphNode(node->label); nodes.push_back(tmp); parent->neighbors.push_back(tmp); for (int i = 0; i < node->neighbors.size(); i++) { bool finded = false; for (int j = 0; j < nodes.size(); j++) { if (nodes[j]->label == node->neighbors[i]->label) { tmp->neighbors.push_back(nodes[j]); finded = true; break; } } if (!finded) { cloneGraph(tmp, node->neighbors[i]); } } }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if(node == nullptr) return NULL; UndirectedGraphNode * ret = new UndirectedGraphNode(node->label); origin_labels.insert(node); res_labels.insert(ret); for(int i = 0; i < node->neighbors.size(); ++i) { unordered_set<UndirectedGraphNode *>::iterator tmp = origin_labels.find(node->neighbors[i]); if(tmp == origin_labels.end()) { ret->neighbors.push_back(cloneGraph(node->neighbors[i])); } else { unordered_set<UndirectedGraphNode*>::iterator it = res_labels.begin(); for(; it != res_labels.end(); ++it) { if((*it)->label == node->neighbors[i]->label) break; } ret->neighbors.push_back(*it); } } return ret; }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (node == NULL) { return NULL; } root = new UndirectedGraphNode(node->label); nodes.push_back(root); for (int i = 0; i < node->neighbors.size(); i++) { bool finded = false; for (int j = 0; j < nodes.size(); j++) { if (nodes[j]->label == node->neighbors[i]->label) { root->neighbors.push_back(nodes[j]); finded = true; break; } } if (!finded) { cloneGraph(root, node->neighbors[i]); } } return root; }
int main(int argc, char const *argv[]) { UndirectedGraphNode * node = new UndirectedGraphNode(0); // UndirectedGraphNode * node1 = new UndirectedGraphNode(1); // UndirectedGraphNode * node2 = new UndirectedGraphNode(2); // // UndirectedGraphNode * node3 = new UndirectedGraphNode(3); // // UndirectedGraphNode * node4 = new UndirectedGraphNode(4); // // UndirectedGraphNode * node5 = new UndirectedGraphNode(5); // node->neighbors.push_back(node1); // node->neighbors.push_back(node2); // node1->neighbors.push_back(node); // node1->neighbors.push_back(node2); // node2->neighbors.push_back(node2); UndirectedGraphNode * res = cloneGraph(node); return 0; }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (NULL == node) { return NULL; } UndirectedGraphNode* new_node = NULL; map<UndirectedGraphNode*, UndirectedGraphNode*>::iterator it = old_new_map.find(node); if (it == old_new_map.end()) { new_node = new UndirectedGraphNode(node->label); old_new_map.insert(make_pair(node, new_node)); for (size_t i = 0; i < node->neighbors.size(); ++i) { UndirectedGraphNode* tmp = cloneGraph(node->neighbors[i]); if (tmp != NULL) { new_node->neighbors.push_back(tmp); } } return new_node; } else { new_node = it->second; } return new_node; }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (node == NULL) return NULL; map<int, UndirectedGraphNode*> recs; return cloneGraph(node, recs); }
/** * Min cuts randomized contraction algorithm. It does not always return the * correct value, should be repeated. For n^2 * log n the probaility to fail * is < 1 / n * * @param graph The graph to calculate min cuts. * @return Min cuts count. */ int randomizedContraction(const Graph graph) { Graph g = cloneGraph(graph); int randomVertex, randomLinkedVertexIndex, randomLinkedVertex, graphNoOfNodes = graph.n, k, returnValue = -1; Node *node, *node2, *node3, *node3Prev; int* randomlyChoosed = (int*) malloc(sizeof(int) * graph.n); int i; for (i = 0; i < graph.n; i++) { randomlyChoosed[i] = 0; } while (graphNoOfNodes > 2) { /* pickup a random edge */ do { srand(time(NULL)); randomVertex = rand() % graph.n; } while (randomlyChoosed[randomVertex]); randomlyChoosed[randomVertex] = 1; srand(time(NULL)); randomLinkedVertexIndex = rand() % g.edgesCount[randomVertex]; node = g.edges[randomVertex]; k = 0; while (NULL != node && k < randomLinkedVertexIndex) { node = node->next; k++; } /* contract randomVertex into randomLinkedVertex */ if (NULL != node) { randomLinkedVertex = node->vertex; /* modify globally edges to point to the new combined vertex */ for (i = 0; i < graph.n; i++) { if (!randomlyChoosed[i]) { node2 = g.edges[i]; while (NULL != node2) { if (node2->vertex == randomVertex) { node2->vertex = randomLinkedVertex; } node2 = node2->next; } } } /* add randomVertex 's edges to the randomLinkedVertex 's edges */ node2 = g.edges[randomVertex]; if (NULL != node2) { node3 = g.edges[randomLinkedVertex]; if (NULL != node3) { while (NULL != node3->next) { node3 = node3->next; } node3->next = cloneNode(node2); } else { g.edges[randomLinkedVertex] = cloneNode(node2); } } g.edgesCount[randomLinkedVertex] += g.edgesCount[randomVertex]; /* eliminate self pointing edges of randomLinkedVertex */ node3 = g.edges[randomLinkedVertex]; node3Prev = NULL; while (NULL != node3) { if (node3->vertex == randomLinkedVertex || node3->vertex == randomVertex) { if (NULL != node3Prev) { node3Prev->next = node3->next; } else { g.edges[randomLinkedVertex] = node3->next; } node3 = node3->next; g.edgesCount[randomLinkedVertex]--; } else { node3Prev = node3; node3 = node3->next; } } } graphNoOfNodes--; } /* 2 vertices left, return the number of edges of the first one found */ for (i = 0; i < graph.n; i++) { if (!randomlyChoosed[i]) { returnValue = g.edgesCount[i]; break; } } freeGraph(&g); free(randomlyChoosed); return returnValue; }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { unordered_map<int, UndirectedGraphNode*> track; return cloneGraph(node, track); }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { // Note: The Solution object is instantiated only once and is reused by each test case. map<UndirectedGraphNode*, UndirectedGraphNode*> m; return cloneGraph(node, m); }