/** * Copy the solution (Graph, Tour, Edges, and Headings) into those given. * @note Existing nodes and edges are cleared. */ void DubinsPathPlanner::copySolution(ogdf::Graph &G, ogdf::GraphAttributes &GA, ogdf::List<ogdf::node> &Tour, ogdf::List<ogdf::edge> &Edges, NodeArray<double> &Headings, double &cost) { DPP_ASSERT(m_haveSolution); // Copy the graph and attributes NodeArray<node> nodeCopyTable(m_G); EdgeArray<edge> edgeCopyTable(m_G); int n = copyGraph(m_G, m_GA, G, GA, nodeCopyTable, edgeCopyTable); // Copy the tour ogdf::ListIterator<ogdf::node> tourIter; for ( tourIter = m_Tour.begin(); tourIter != m_Tour.end(); tourIter++ ) { node u = *tourIter; node ucopy = nodeCopyTable(u); Tour.pushBack(ucopy); } // Copy the edge list ogdf::ListIterator<ogdf::edge> edgeIter; for ( edgeIter = m_Edges.begin(); edgeIter != m_Edges.end(); edgeIter++ ) { edge e = *edgeIter; edge ecopy = edgeCopyTable(e); Edges.pushBack(ecopy); } // Copy the headings Headings.init(G); node u; forall_nodes(u,m_G) { node ucopy = nodeCopyTable(u); Headings(ucopy) = m_Headings(u); }
int main(int argc, char* argv[]){ int i, n=8; List S = newList(); Graph G = newGraph(n); Graph T=NULL, C=NULL; for(i=1; i<=n; i++) append(S, i); addArc(G, 1,2); addArc(G, 1,5); addArc(G, 2,5); addArc(G, 2,6); addArc(G, 3,2); addArc(G, 3,4); addArc(G, 3,6); addArc(G, 3,7); addArc(G, 3,8); addArc(G, 6,5); addArc(G, 6,7); addArc(G, 8,4); addArc(G, 8,7); printGraph(stdout, G); DFS(G, S); fprintf(stdout, "\n"); fprintf(stdout, "x: d f p\n"); for(i=1; i<=n; i++){ fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(G, i), getFinish(G, i), getParent(G, i)); } fprintf(stdout, "\n"); printList(stdout, S); fprintf(stdout, "\n"); T = transpose(G); C = copyGraph(G); fprintf(stdout, "\n"); printGraph(stdout, C); fprintf(stdout, "\n"); printGraph(stdout, T); fprintf(stdout, "\n"); DFS(T, S); fprintf(stdout, "\n"); fprintf(stdout, "x: d f p\n"); for(i=1; i<=n; i++){ fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(T, i), getFinish(T, i), getParent(T, i)); } fprintf(stdout, "\n"); printList(stdout, S); fprintf(stdout, "\n"); freeList(&S); freeGraph(&G); freeGraph(&T); freeGraph(&C); return(0); }
MyGraph MyGraph::graph_difference(MyGraph const& g2) const{ MyGraph output = copyGraph(); graph_hashmap::const_iterator g_iter = g2.thegraph.begin(); while( g_iter != g2.thegraph.end()){ if(output.set_of_vertices.find(g_iter->first) != output.set_of_vertices.end()) output.removeVertex(g_iter->first); *g_iter++; } return output; }
void copyGraph( unordered_map<UndirectedGraphNode *, UndirectedGraphNode *> &mapping, UndirectedGraphNode *vertex) { if (mapping.find(vertex) == mapping.end()) { mapping.emplace(vertex, new UndirectedGraphNode(vertex->label)); for (auto *neighbor : vertex->neighbors) { copyGraph(mapping, neighbor); } } }
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (!node) return nullptr; unordered_map<UndirectedGraphNode *, UndirectedGraphNode *> mapping; copyGraph(mapping, node); connectNeighbors(mapping); return mapping[node]; }
MyGraph MyGraph::graph_union(const MyGraph& g2) const { MyGraph output = copyGraph(); graph_hashmap::const_iterator g2_it = g2.thegraph.begin(); while(g2_it != this->thegraph.end()){ if ( output.thegraph.find(g2_it->first) == output.thegraph.end()) output.insertVertex(g2_it->first); //cycle through all the edges contained in the node hashmap node_hashmap::const_iterator node_iter = g2_it->second.begin(); while (node_iter != g2_it->second.end() ){ if (!output.areAdjacent(g2_it->first,node_iter->first)) output.insertEdge(g2_it->first,node_iter->first); *node_iter++; } *g2_it++; } return output; }
MyGraph MyGraph::graph_complement() const{ MyGraph output = copyGraph(); graph_hashmap::const_iterator g_iter = thegraph.begin(); my_set::const_iterator g2_iter; while (g_iter != thegraph.end()){ g2_iter = set_of_vertices.begin(); while ( g2_iter != set_of_vertices.end()){ if (strcmp(g_iter->first.c_str(),(*g2_iter).c_str()) ){ //avoid self-loops if( areAdjacent(g_iter->first, *g2_iter) ) output.removeEdge(g_iter->first, *g2_iter); else if (!output.areAdjacent(g_iter->first, *g2_iter)) output.insertEdge(g_iter->first, *g2_iter); } *g2_iter++; } *g_iter++; } return output; }
Graph maxStream(VertexType source, VertexType sink, Graph G) { Index S = findVertex(source, G); Index E = findVertex(sink, G); if(G->TheCells[S].Info != Legitimate || G->TheCells[E].Info != Legitimate) { fprintf(stderr, "vertex %s or %s does not exist", source, sink); return NULL; } /*准备好残余图和流图*/ Graph Gr = intializeGraph(G->vertex); Gr = copyGraph(Gr, G); Graph Gf = intializeGraph(G->vertex); copyVertex(Gf, G); maxStream(S, E, Gf, Gr); DestroyGraph(Gr); return Gf; }
Graph NFAGenerator::evaluate(string postFix) { string splited[100]; int index = 0; std::string delimiter = " "; size_t pos = 0; std::string token; while ((pos = postFix.find(delimiter)) != std::string::npos) { token = postFix.substr(0, pos); splited[index] = token; //std::cout << token << std::endl; postFix.erase(0, pos + delimiter.length()); index++; } splited[index] = postFix; std::stack<Graph> evaStack; for ( int i = 0 ; i < index ; i++){ if(splited[i].at(0)=='+'){ Graph firstOperand = evaStack.top(); evaStack.pop(); vector<Node*> destNodes; vector<Node*>::iterator it= destNodes.begin(); map<char,vector<Node*>>::iterator mapit = firstOperand.end->edges.begin(); it = destNodes.insert(it,firstOperand.start); if(firstOperand.start->edges.find(0) != firstOperand.start->edges.end()){ it = firstOperand.end->edges.at(0).begin(); firstOperand.end->edges.at(0).insert(it,firstOperand.start); }else{ //firstOperand.end->edges[0]=destNodes; //mapit = firstOperand.start->edges.insert(mapit,pair<char,vector<Node*>>(0,destNodes)); mapit = firstOperand.end->edges.insert(mapit,pair<char,vector<Node*>>(0,destNodes)); } evaStack.push(firstOperand); }else if( splited[i].at(0)=='|') { Graph firstOperand = evaStack.top(); evaStack.pop(); Graph secondOperand = evaStack.top(); evaStack.pop(); //Graph newGraph; Node* newStart = new Node(); Node* newEnd = new Node(); vector<Node*> destNodes; vector<Node*>::iterator it= destNodes.begin(); it = destNodes.insert(it,firstOperand.start); it = destNodes.insert(it,secondOperand.start); newStart->edges[0]=destNodes; vector<Node*> destNodes1; vector<Node*>::iterator it1= destNodes1.begin(); if(firstOperand.end->edges.find(0) != firstOperand.end->edges.end()){ it1 = firstOperand.end->edges.at(0).begin(); firstOperand.end->edges.at(0).insert(it1,newEnd); }else{ it1 = destNodes1.insert(it1,newEnd); firstOperand.end->edges[0]=destNodes1; } vector<Node*> destNodes2; vector<Node*>::iterator it2= destNodes2.begin(); if(secondOperand.end->edges.find(0) != secondOperand.end->edges.end()){ it2 = secondOperand.end->edges.at(0).begin(); secondOperand.end->edges.at(0).insert(it2,newEnd); }else{ it2 = destNodes2.insert(it2,newEnd); secondOperand.end->edges[0]=destNodes2; } firstOperand.start = newStart; firstOperand.end = newEnd; evaStack.push(firstOperand); }else if (splited[i].at(0)=='*'){ Graph firstOperand = evaStack.top(); evaStack.pop(); Node* newStart=new Node(); Node* newEnd=new Node(); vector<Node*> destNodes; vector<Node*>::iterator it; if(firstOperand.end->edges.find(0) != firstOperand.end->edges.end()){ it = firstOperand.end->edges.at(0).begin(); it = firstOperand.end->edges.at(0).insert(it,firstOperand.start); it = firstOperand.end->edges.at(0).insert(it,newEnd); }else{ it = destNodes.begin(); it = destNodes.insert(it,firstOperand.start); it = destNodes.insert(it,newEnd); firstOperand.end->edges[0]=destNodes; } vector<Node*> newStartVec; it = newStartVec.begin(); it = newStartVec.insert(it,firstOperand.start); it = newStartVec.insert(it,newEnd); newStart->edges[0]=newStartVec; firstOperand.end = newEnd; firstOperand.start = newStart; evaStack.push(firstOperand); }else if(splited[i].at(0)=='&'){ Graph secondOperand = evaStack.top(); evaStack.pop(); Graph firstOperand = evaStack.top(); evaStack.pop(); vector<Node*> destNodes; vector<Node*>::iterator it= destNodes.begin(); if(firstOperand.end->edges.find(0) != firstOperand.end->edges.end()){ it = firstOperand.end->edges.at(0).begin(); firstOperand.end->edges.at(0).insert(it,secondOperand.start); }else{ it = destNodes.insert(it,secondOperand.start); firstOperand.end->edges[0]=destNodes; } firstOperand.end = secondOperand.end; evaStack.push(firstOperand); }else{ if( regDef.find(splited[i]) != regDef.end()){ map<char,vector<Node*>>::iterator m1 = regDef.at(splited[i]).start->edges.begin(); map<char,vector<Node*>>::iterator m2 = regDef.at(splited[i]).end->edges.begin(); Node* start = copyGraph(regDef.at(splited[i]).start,regDef.at(splited[i]).end->nodeNum,regDef.at(splited[i]).start->nodeNum); Graph g;//(start,&end); *(g.start) = *start; g.end = end; end= new Node(); //g.end->edges = endedges; //g.end->nodeNum = end.nodeNum; //g.end->state = end.state; evaStack.push(g); }else{ size_t found = splited[i].find('-'); if( found != string::npos && splited[i].size() > 2 ){ // range if( splited[i].size() == 3 ){ evaStack.push(createRangeGraph(splited[i].at(0),splited[i].at(2))); } }else{ //m[yy]=l' bool lastSlash = false; string temp=""; for(int m = 0 ; m < splited[i].size() ; m++){ if(lastSlash && splited[i].at(m) == '\\'){ temp+='\\'; } else if(splited[i].at(m) == '\\'){ lastSlash = true; continue; } else{ temp+=splited[i].at(m); } lastSlash = false; } splited[i] = temp; Graph keyword; Node* current=new Node(); Node* next=new Node(); vector<Node*> destNodes; vector<Node*>::iterator it= destNodes.begin(); vector<Node*> destNodes2; vector<Node*>::iterator it2= destNodes2.begin(); it = destNodes.insert(it,current); //keyword.start->edges.insert(pair<char,vector<Node*>>(splited[i].at(0),destNodes)); keyword.start->edges[splited[i].at(0)]=destNodes; givenInputs.push_back(splited[i].at(0)); for (int k = 1 ; k < splited[i].size()-1 ; k++ ){ destNodes = vector<Node*>(); it = destNodes.begin(); it = destNodes.insert(it,next); //current->edges.insert(pair<char,vector<Node*>>(splited[i].at(k),destNodes)); current->edges[splited[i].at(k)]=destNodes; givenInputs.push_back(splited[i].at(k)); current = next; next = new Node(); } if(splited[i].size() == 1){ it2 = destNodes2.begin(); it2 = destNodes2.insert(it2,keyword.end); keyword.start->edges[splited[i].at(0)]=destNodes2; } else if(splited[i].size() > 1){ destNodes = vector<Node*>(); it = destNodes.begin(); it = destNodes.insert(it,keyword.end); //current->edges.insert(pair<char,vector<Node*>>(splited[i].at(splited[i].size()-1),destNodes)); current->edges[splited[i].at(splited[i].size()-1)]=destNodes; givenInputs.push_back(splited[i].at(splited[i].size()-1)); } //insert keyword into regEx vector evaStack.push(keyword); } } } } if( evaStack.size() != 1 ) throw exception(); return evaStack.top(); }
int main(int argc, char* argv[]){ int order = 8; //the number of nodes in our Graph G Graph G = newGraph(order); List S = newList(); //insert numbers into List S for(int i=1; i<=8; i++){ append(S, i); } printf("Here is our Graph G when first initialized\n"); printGraph(stdout, G); printf("Here is our List S\n"); printList(stdout, S); printf("\n\n"); //calling addArc() to assign edges to our Graph G addArc(G, 1, 2); addArc(G, 2, 3); addArc(G, 3, 4); addArc(G, 4, 3); addArc(G, 4, 8); addArc(G, 8, 8); addArc(G, 7, 8); addArc(G, 3, 7); addArc(G, 7, 6); addArc(G, 6, 7); addArc(G, 2, 6); addArc(G, 5, 6); addArc(G, 5, 1); addArc(G, 2, 5); printf("Here is the adjacency list representation of G after adding our directed edges\n"); printGraph(stdout, G); printf("\n"); //Test that transpose() works properly Graph T = transpose(G); printf("Here is the transpose of G:\n"); printGraph(stdout, T); printf("The order of G is: %d\n", getOrder(G)); printf("The size of G is: %d\n", getSize(G)); printf("The parent of node 2 is: %d\n", getParent(G, 2)); printf("The discover time of node 2 is: %d\n", getDiscover(G, 2)); printf("The finish tim of node 2 is: %d\n\n\n", getFinish(G, 2)); DFS(G,S); printf("Here is our List S after running DFS():\n"); printList(stdout, S); printf("\n"); //Test that discover time, finish time, and parent fields correct after call to DFS() for(int i = 1; i<=order; i++){ printf("Node %d: discovery time = %d ; finish time = %d ; parent = %d\n", i, getDiscover(G, i), getFinish(G, i), getParent(G, i)); } //Test that copyGraph() works properly Graph C = copyGraph(G); printf("Here is the copy of G:\n"); printGraph(stdout, C); for(int i = 1; i<=order; i++){ printf("Node %d: discovery time = %d ; finish time = %d ; parent = %d\n", i, getDiscover(C, i), getFinish(C, i), getParent(C, i)); } freeList(&S); freeGraph(&G); freeGraph(&T); freeGraph(&C); return(0); }
int main(int argc, char* argv[]){ int i, n=8; List S = newList(); Graph G = newGraph(n); Graph T=NULL, C=NULL; Graph G_undir = newGraph(35); for(i=1; i<35; i++) { if( i%7!=0 ) addEdge(G_undir, i, i+1); if( i<=28 ) addEdge(G_undir, i, i+7); } printf("The Order of this undirected graph is: %d\n", getOrder(G_undir)); printf("The Size of this undirected graph is: %d\n", getSize(G_undir)); printf("Adjacency list representation of G_undir: \n"); printGraph(stdout, G_undir); printf("\n"); for(i=1; i<=n; i++) append(S, i); addArc(G, 1,2); addArc(G, 1,5); addArc(G, 2,5); addArc(G, 2,6); addArc(G, 3,2); addArc(G, 3,4); addArc(G, 3,6); addArc(G, 3,7); addArc(G, 3,8); addArc(G, 6,5); addArc(G, 6,7); addArc(G, 8,4); addArc(G, 8,7); printf("The Order of this graph is: %d\n", getOrder(G)); printf("The Size of this graph is: %d\n", getSize(G)); printf("Adjacency list representation of G: \n"); printGraph(stdout, G); DFS(G, S); fprintf(stdout, "\n"); fprintf(stdout, "x: d f p\n"); for(i=1; i<=n; i++){ fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(G, i), getFinish(G, i), getParent(G, i)); } fprintf(stdout, "\n"); printList(stdout, S); fprintf(stdout, "\n"); T = transpose(G); C = copyGraph(G); fprintf(stdout, "\n"); printGraph(stdout, C); fprintf(stdout, "\n"); printGraph(stdout, T); fprintf(stdout, "\n"); DFS(T, S); fprintf(stdout, "\n"); fprintf(stdout, "x: d f p\n"); for(i=1; i<=n; i++){ fprintf(stdout, "%d: %2d %2d %2d\n", i, getDiscover(T, i), getFinish(T, i), getParent(T, i)); } fprintf(stdout, "\n"); printList(stdout, S); fprintf(stdout, "\n"); freeList(&S); freeGraph(&G); freeGraph(&T); freeGraph(&C); return(0); }
// {{{ elapsedTime stat_t elapsedTime (int n, int m, int c, int nb_iter) { int i=0; stat_t result; GTimer *timer = g_timer_new(); memset(&result, 0, sizeof(stat_t)); for (i=0; i< nb_iter; i++) { // Graph Initialization Graph * lcLab = allocListGraph(n); Graph * lcFIFO = allocListGraph(n); randFill(lcLab,m,c,1,lcFIFO); Graph *lcDinic = copyGraph(lcLab); int f,g,h; // FIFO Algorithm Graph * lfFIFO = allocGraph(lcFIFO); Graph * ldFIFO = copyGraph(lcFIFO); // printf ("Entrée FIFO\n"); g_timer_start(timer); h=algoFIFO (lcFIFO,ldFIFO,lfFIFO,0,1); g_timer_stop(timer); // printf ("Sortie FIFO\n"); freeGraph(ldFIFO); freeGraph(lfFIFO); if (h) { result._st1 += (double) g_timer_elapsed(timer, NULL); g_timer_reset(timer); // High Label Algorithm Graph * lfLab = allocGraph(lcLab); Graph * ldLab = copyGraph(lcLab); // printf ("Entrée High Label\n"); g_timer_start(timer); g=algoLabel (lcLab,ldLab,lfLab,0,1); g_timer_stop(timer); result._st2 += (double) g_timer_elapsed(timer, NULL); g_timer_reset(timer); // printf ("Sortie High Label\n"); freeGraph(lfLab); freeGraph(ldLab); // Dinic Algorithm Graph * lfDinic = allocGraph(lcLab); // printf("Entrée Dinic\n"); g_timer_start(timer); f=algoDinic (lcDinic,lfDinic,0,1); g_timer_stop(timer); result._st3 += (double) g_timer_elapsed(timer, NULL); g_timer_reset(timer); // printf("Sortie Dinic\n"); freeGraph (lfDinic); } else { printf ("Flot nul.\n"); g_timer_reset(timer); i --; } freeGraph (lcFIFO); freeGraph (lcLab); freeGraph (lcDinic); } result._st1 /= nb_iter; result._st2 /= nb_iter; result._st3 /= nb_iter; result._st4 /= nb_iter; g_timer_destroy(timer); return result; }