int main() { ListDigraph test; ListDigraph::Node a = test.addNode(); ListDigraph::Node b = test.addNode(); ListDigraph::Node c = test.addNode(); ListDigraph::Node d = test.addNode(); ListDigraph::Node e = test.addNode(); ListDigraph::Node f = test.addNode(); ListDigraph::Arc ac = test.addArc(a, c); ListDigraph::Arc bc = test.addArc(b, c); ListDigraph::Arc cd = test.addArc(c, d); ListDigraph::Arc bf = test.addArc(b, f); ListDigraph::Arc de = test.addArc(d, e); ListDigraph::Arc df = test.addArc(d, f); ListDigraph::ArcMap<int> costs(test); costs[ac] = 1; costs[bc] = 2; costs[cd] = 2; costs[bf] = 32; costs[de] = 16; costs[df] = 8; solve_via_minflow(test, costs); return 0; }
int read_pcpath(string input_file){ std::ifstream kinput; kinput.open(input_file.c_str()); if (!kinput) return 0; kinput >> n >> m; std::map<std::string, ListDigraph::Node> ref; for ( int i=0; i<n; i++ ){ string tmp; double r; kinput >> tmp >> r; ListDigraph::Node n = g.addNode(); node_names[n] = tmp; prizes[n] = r; ref[tmp]=n; } for ( int i=0; i<m; i++){ string v1, v2; double c_tmp; kinput >> v1 >> v2 >> c_tmp; ListDigraph::Arc a = g.addArc(ref[v1], ref[v2]); //source, target costs[a] = c_tmp; } return 1; }
/* The function purpose is to check if the node is exist, if not it create it and add it to the map*/ void nodeValidation(int input, CrossRefMap<ListDigraph, ListDigraph::Node, int> & ids, ListDigraph::Node & node, ListDigraph & g){ node = ids(input); if (node == INVALID) { // Check if node is inavlid node = g.addNode(); ids.set(node, input); // make a connection between the node and the string } }
//splits graph into connected components void split_graph(ListDigraph& g, vector<ListDigraph*>& graphs){ Undirector<ListDigraph> undirected(g); ListDigraph::NodeMap<int> components(g); stronglyConnectedComponents(undirected, components); int num_subgraphs = 0; for(ListDigraph::NodeIt n(g); n != INVALID; ++n){ if(components[n] > num_subgraphs) num_subgraphs = components[n]; } num_subgraphs++; ListDigraph::NodeMap<ListDigraph::Node> map(g); for(int i = 0; i < num_subgraphs; i++){ ListDigraph temp; for(ListDigraph::NodeIt n(g); n != INVALID; ++n){ if(components[n] == i){ map[n] = temp.addNode(); } } for(ListDigraph::NodeIt n(g); n != INVALID; ++n){ if(components[n] == i){ for(ListDigraph::OutArcIt o(g, n); o != INVALID; ++o){ temp.addArc(map[g.source(o)], map[g.target(o)]); } } } graphs.push_back(&temp); } }
//Generate a random complete euclidean ListGraph bool GenerateRandomEuclideanListDigraph(ListDigraph &g, DNodeStringMap &vname, // node name DNodePosMap &px, // x-position of the node DNodePosMap &py, // y-position of the node ArcValueMap & weight, // weight of edges int n, // number of nodes double SizeX, // coordinate x is a random number in [0,SizeX) double SizeY) // coordinate y is a random number in [0,SizeY) { DNode *V; V = new DNode[n]; if (V==NULL){ cout << "Memory allocation error, number of nodes " << n << " too large\n"; exit(0);} for (int i=0;i<n;i++) { // insert nodes (random points in [0,100] x [0,100] ) V[i] = g.addNode(); // new node px[V[i]] = SizeX*drand48(); py[V[i]] = SizeY*drand48(); vname[V[i]] = IntToString(i+1);// name of node is i+1 } for (int i=0;i<n;i++) for (int j=i+1;j<n;j++) { Arc e = g.addArc(V[i],V[j]); // generate new arc from v_i to v_j weight[e] = sqrt(pow(px[V[i]]-px[V[j]],2) + pow(py[V[i]]-py[V[j]],2)); Arc f = g.addArc(V[j],V[i]); // generate new arc from v_j to v_i weight[f] = weight[e]; } delete[] V; return(true); }
void createMACGraph(ListDigraph& g, int num_paths, int path_length, ListDigraph::ArcMap<int>& demands){ srand(time(NULL)); ListDigraph::Node prev[num_paths]; ListDigraph::Node current[num_paths]; for (int i = 0; i < num_paths; ++i) { prev[i] = g.addNode(); } for (int i = 0; i < path_length-1; ++i) { for (int j = 0; j < num_paths; ++j) { current[j] = g.addNode(); g.addArc(prev[j], current[j]); } for (int j = 0; j < num_paths; ++j) { if(rand()%100 < 50){ int targetIndex = j; while(targetIndex != j){ targetIndex = rand()%num_paths; } g.addArc(prev[j], current[targetIndex]); } } for (int j = 0; j < num_paths; ++j) { prev[j] = current[j]; } } //this splits every node and sets demand for the edge between the halves to 1 for (ListDigraph::NodeIt n(g); n != INVALID; ++n){ ListDigraph::Node new_node = g.split(n, false); ListDigraph::Arc new_edge = g.addArc(n, new_node); demands[new_edge] = 1; } }
ListDigraph::Node addSink(ListDigraph& g){ ListDigraph::Node t = g.addNode(); for(ListDigraph::NodeIt n(g); n != INVALID; ++n){ if(countOutArcs(g, n) == 0 && n != t){ g.addArc(n, t); } } return t; }
ListDigraph::Node addSource(ListDigraph& g){ ListDigraph::Node s = g.addNode(); for(ListDigraph::NodeIt n(g); n != INVALID; ++n){ if(countInArcs(g, n) == 0 && n != s){ g.addArc(s, n); } } return s; }
int main() { ListDigraph g; ListDigraph::Node u = g.addNode(); ListDigraph::Node v = g.addNode(); ListDigraph::Arc a = g.addArc( u, v ); cout << "Hello World! This is LEMON library here." << endl; cout << "We have a directed graph with " << countNodes( g ) << " nodes " << "and " << countArcs( g ) << " arc." << endl; return 0; }
void createKPathGraph(ListDigraph& g, int k, int n, int m, ListDigraph::ArcMap<int>& weights, ListDigraph::ArcMap<int>& demands){ srand(time(NULL)); ListDigraph::Node* nodes[k]; for (int i = 0; i < k; ++i) { nodes[i] = (ListDigraph::Node*) calloc(n, sizeof(ListDigraph::Node)); } for (int i = 0; i < k; ++i) { for (int j = 0; j < n; ++j) { nodes[i][j] = g.addNode(); if(j != 0) g.addArc(nodes[i][j-1], nodes[i][j]); } } for (int i = 0; i < m; ++i) { int k1 = rand()%k; int k2 = rand()%k; int n1 = rand()%(n-1); int n2 = (rand()%(n-n1-1))+n1+1; if(findArc(g, nodes[k1][n1], nodes[k2][n2]) == INVALID){ g.addArc(nodes[k1][n1], nodes[k2][n2]); } } for (ListDigraph::ArcIt a(g); a != INVALID; ++a) { weights[a] = rand()%1000; } //this splits every node and sets demand for the edge between the halves to 1 for (ListDigraph::NodeIt n(g); n != INVALID; ++n){ ListDigraph::Node new_node = g.split(n, false); ListDigraph::Arc new_edge = g.addArc(n, new_node); demands[new_edge] = 1; } }
//Simple function for generating acyclic graphs void createRandomGraph(ListDigraph& g, int num_nodes, float edge_prob){ srand(time(NULL)); ListDigraph::NodeMap<int> labels(g); for(int i=0; i<num_nodes; i++){ ListDigraph::Node new_node = g.addNode(); labels[new_node] = i; } for(ListDigraph::NodeIt n(g); n != INVALID; ++n){ for(ListDigraph::NodeIt v(g); v != INVALID; ++v){ //no edges from bigger nodes to smaller to ensure acyclicity, //and no edges from node to itself //+ an attempt to create longer graphs if(labels[n] >= labels[v] || labels[n] < labels[v]-20) continue; if(rand()%100 <= edge_prob*100){ g.addArc(n, v); } } } }
// Generate a triangulated ListDigraph, building the Delaunay // triangulation of random points. Each edge of the Delaunay triangulation // leads to two arcs (in both senses) // Uses the geompack program, available in // http://people.sc.fsu.edu/~jburkardt/cpp_src/geompack/geompack.html bool GenerateTriangulatedListDigraph(ListDigraph &g, DNodeStringMap &vname, // name of the nodes DNodePosMap &px, // x-position of the nodes DNodePosMap &py, // y-position of the nodes ArcValueMap & weight, // weight of edges int n, // number of nodes double SizeX, // coordinate x is a random number in [0,SizeX) double SizeY) // coordinate y is a random number in [0,SizeY) { int i; // n=number of nodes DNode *V = new DNode[n]; double *p = new double[2*n+2];// node coodinates are (x;y) = ( p[2*i] ; p[2*i+1] ) int *tri = new int[6*n]; // Each 3 elements are the indexes of a triangle int ntri; int *tri_nabe = new int[6*n]; if ((V==NULL)||(p==NULL)||(tri==NULL)||(tri_nabe==NULL)){ cout << "Memory allocation error, number of nodes " << n << " too large\n"; exit(0);} for (i=0;i<n;i++) { // insere os vértices (pontos aleatórios no plano [0,SizeX]x[0,SizeY] ) V[i] = g.addNode(); // gera um vértice nó do grafo px[V[i]] = SizeX*drand48(); // nodes are random points py[V[i]] = SizeY*drand48(); p[2*i] = px[V[i]]; p[2*i+1] = py[V[i]]; vname[V[i]] = IntToString(i+1);// name of node is i+1 } if (r8tris2 ( n, p, &ntri, tri, tri_nabe )) { printf("ERROR\n");Pause(); } for (i=0;i<ntri;i++) { int a,b,c; a = tri[3*i]-1; b = tri[3*i+1]-1; c = tri[3*i+2]-1; // each triangle if formed with nodes V[a] , V[b] , V[c] // insert arcs without duplications in both senses if (findArc(g,V[a],V[b])==INVALID){ Arc e = g.addArc(V[a],V[b]); weight[e] = sqrt(pow(px[V[a]]-px[V[b]],2) + pow(py[V[a]]-py[V[b]],2)); } if (findArc(g,V[b],V[a])==INVALID){ Arc e = g.addArc(V[b],V[a]); weight[e] = sqrt(pow(px[V[b]]-px[V[a]],2) + pow(py[V[b]]-py[V[a]],2)); } if (findArc(g,V[a],V[c])==INVALID){ Arc e = g.addArc(V[a],V[c]); weight[e] = sqrt(pow(px[V[a]]-px[V[c]],2) + pow(py[V[a]]-py[V[c]],2)); } if (findArc(g,V[c],V[a])==INVALID){ Arc e = g.addArc(V[c],V[a]); weight[e] = sqrt(pow(px[V[c]]-px[V[a]],2) + pow(py[V[c]]-py[V[a]],2)); } if (findArc(g,V[b],V[c])==INVALID) { Arc e = g.addArc(V[b],V[c]); weight[e] = sqrt(pow(px[V[b]]-px[V[c]],2) + pow(py[V[b]]-py[V[c]],2)); } if (findArc(g,V[c],V[b])==INVALID) { Arc e = g.addArc(V[c],V[b]); weight[e] = sqrt(pow(px[V[c]]-px[V[b]],2) + pow(py[V[c]]-py[V[b]],2)); } } delete[] V; delete[] p; delete[] tri; delete[] tri_nabe; return(true); }
bool ReadListDigraph(string filename, ListDigraph &g, DNodeStringMap &vname, ArcValueMap &weight, DNodePosMap &posx, DNodePosMap & posy, DNodeBoolMap &is_terminal, const bool dupla) { ifstream ifile; int i,n,m; double peso; Arc a; char nomeu[100],nomev[100]; string STR; DNode u,v; #if __cplusplus >= 201103L std::unordered_map<string,DNode> string2node,test; #else std::tr1::unordered_map<string,DNode> string2node; #endif ifile.open(filename.c_str()); if (!ifile) {cout << "File '" << filename << "' does not exist.\n"; exit(0);} PulaBrancoComentario(ifile); ifile >> n; ifile >> m; // first line have number of nodes and number of arcs if (m<0 || ifile.eof()) { cout<<"File "<<filename<<" is not a digraph given by arcs.\n"; exit(0);} for (i=0;i<n;i++) { getline(ifile,STR); if (ifile.eof()) {cout<<"Reached unexpected end of file "<<filename<<".\n";exit(0);} while (STR=="") getline(ifile,STR); { string token; istringstream ins; // Declare an input string stream. ins.str(STR); // Specify string to read. int nt = 0; while( getline(ins, token, ' ') ) { // format: <node_name> <pos_x> <pos_y> <is_terminal> if (nt==0) { auto test = string2node.find(token); if (test!=string2node.end()){cout<<"ERROR: Repeated node: "<<nomev<<endl;exit(0);} v = g.addNode(); string2node[token] = v; vname[v] = token;} else if (nt==1) { posx[v] = atof(token.c_str());} else if (nt==2) { posy[v] = atof(token.c_str());} else if (nt==3) { if(atoi(token.c_str()) == 1) is_terminal[v] = true; else is_terminal[v] = false; } nt++; } } } for (i=0;i<m;i++) { // format: <node_source> <node_target> <arc_weight> ifile >> nomeu; ifile >> nomev; ifile >> peso; if (ifile.eof()) {cout << "Reached unexpected end of file " <<filename << ".\n"; exit(0);} auto test = string2node.find(nomeu); if (test == string2node.end()) {cout<<"ERROR: Unknown node: "<<nomeu<<endl;exit(0);} else u = string2node[nomeu]; test = string2node.find(nomev); if (test == string2node.end()) {cout<<"ERROR: Unknown node: "<<nomev<<endl;exit(0);} else v = string2node[nomev]; a = g.addArc(u,v); weight[a] = peso; if (dupla) {a = g.addArc(v,u); weight[a] = peso;} } ifile.close(); return(true); }
#include <lemon/list_graph.h> #include "../decomposition.h" #include "../MPC.h" #include <stdlib.h> #include <time.h> #include "catch.hpp" #include <lemon/bfs.h> using namespace std; using namespace lemon; TEST_CASE("MACs are found in an easy test case") { ListDigraph g; ListDigraph::Node s = g.addNode(); ListDigraph::Node t = g.addNode(); ListDigraph::Node a = g.addNode(); ListDigraph::Node b = g.addNode(); ListDigraph::Node c = g.addNode(); ListDigraph::Node d = g.addNode(); ListDigraph::Arc sa = g.addArc(s, a); ListDigraph::Arc ab = g.addArc(a, b); ListDigraph::Arc bt = g.addArc(b, t); ListDigraph::Arc sc = g.addArc(s, c); ListDigraph::Arc cd = g.addArc(c, d); ListDigraph::Arc dt = g.addArc(d, t); ListDigraph::NodeMap<int> labels(g); int counter = 0;