std::string Paths<NodeType,F>::getRouteDot(NodeType a,NodeType b)const { std::stringstream stream; RouteType r=getRoute(a,b); NodeType p; DirectedGraphType gg=_graph; stream<<"digraph G\n{\n"; stream<<"\tedge [color=red]\n"; for(typename RouteType::iterator n=r.begin();n!=r.end();n++) { if(n==r.begin()) { } else { stream<<"\t"<<p<<"->"<<*n<<";\n"; typename DirectedGraphType::iterator di=std::find(gg.begin(),gg.end(),EdgeType(p,*n,NULL)); gg.erase(di); } p=*n; } stream<<"\tedge [color=black]\n"; for(typename DirectedGraphType::iterator e=gg.begin();e!=gg.end();e++) { stream<<"\t"<<e->first<<"->"<<e->second<<";\n"; } stream<<"}"; return stream.str(); }
void EdmondMatching::solveEdmondMatching() { // they've already called addEdge() for all edges // interface with C library assert(graphSize < MAX); initGraph(graphSize); //Since C++ arrays are 0..n-1 and input 1..n , subtractions for (unsigned i=0; i<Edges.size(); i++){ //are made for better memory usage. //scanf(" %d %d",&a,&b); unsigned a = Edges.at(i).first; unsigned b = Edges.at(i).second; //printf("edge: %d %d\n",a,b); if (a!=b) { g[a-1][b-1]=g[b-1][a-1]=unmatched; } } findMaximumMatching(graphSize); for (unsigned i=0; i<graphSize; i++){ for (unsigned j=i+1; j<graphSize; j++) { if (g[i][j]==matched) { Matches.push_back(EdgeType(i+1,j+1)); //printf("match: %d %d\n",i+1,j+1); } } } }
void create_rand_ho_edges(std::vector< EdgeType >& edges, const float EDGE_PROB, const int NUM_NODES_R, const int NUM_NODES_C, const int MAX_RADIUS) { edges.clear(); // iterate over all nodes and try to put long range edges to the neighbors // in a certain distance for (int node_id1 = 0; node_id1 < (NUM_NODES_R * NUM_NODES_C); ++node_id1) { int r = node_id1 % NUM_NODES_R; int c = node_id1 / NUM_NODES_R; int start_r = (r-MAX_RADIUS >= 0) ? r-MAX_RADIUS : 0; // for each node put edges in its neighborhood in northern hemisphere for (int ri = start_r; ri <= r; ++ri) { int start_c = (c-MAX_RADIUS >= 0) ? c-MAX_RADIUS : 0; int end_c = (c+MAX_RADIUS < NUM_NODES_C) ? c+MAX_RADIUS : NUM_NODES_C-1; for (int ci = start_c; ci <= end_c; ++ci) { // in case the neighbor is on the left row or is the same node itself // (the neighbors on the left would be covered when the node on left // search for neighbors to its right) if (ri == r && ci <= c) continue; int node_id2 = NUM_NODES_R * ci + ri; if (rand() <= RAND_MAX * EDGE_PROB) { edges.push_back(EdgeType(node_id1, node_id2)); //std::cout << "(" << node_id1 << ", " << node_id2 << ")" << std::endl; } } } } }
F Paths<NodeType,F>::parse(NodeType a,NodeType b,F f)const { RouteType r=getRoute(a,b); NodeType p; for(typename RouteType::iterator n=r.begin();n!=r.end();n++) { if(n==r.begin()) { } else { typename DirectedGraphType::const_iterator di=std::find(_graph.begin(),_graph.end(),EdgeType(p,*n,NULL)); EdgeType edge=*di; f=edge(f); } p=*n; } return f; }
void create_rand_edges(std::vector< EdgeType >& edges, const float EDGE_PROB, const int NUM_NODES_R, const int NUM_NODES_C) { std::vector<bool> connected(NUM_NODES_R * NUM_NODES_C, false); const float NE_EXTRA_PROB = 0.3; edges.clear(); bool created_se, created_prv_se = false; for (int node_id = 0; node_id < (NUM_NODES_R * NUM_NODES_C - NUM_NODES_R); ++node_id) { created_se = false; if (node_id % NUM_NODES_R == 0) { // only S, E, SE if (rand() <= RAND_MAX * EDGE_PROB) { edges.push_back(EdgeType(node_id, node_id + 1)); connected[edges.back().first] = connected[edges.back().second] = true; } if (rand() <= RAND_MAX * EDGE_PROB) { edges.push_back(EdgeType(node_id, node_id + NUM_NODES_R)); connected[edges.back().first] = connected[edges.back().second] = true; } if (rand() <= RAND_MAX * EDGE_PROB) { edges.push_back(EdgeType(node_id, node_id + NUM_NODES_R + 1)); connected[edges.back().first] = connected[edges.back().second] = true; created_se = true; } } else if (node_id % NUM_NODES_R > 0 && node_id % NUM_NODES_R < NUM_NODES_R - 1) { // S, NE, E, SE if (rand() <= RAND_MAX * EDGE_PROB) { edges.push_back(EdgeType(node_id, node_id + 1)); connected[edges.back().first] = connected[edges.back().second] = true; } if (rand() <= RAND_MAX * (EDGE_PROB + NE_EXTRA_PROB) && !created_prv_se) { edges.push_back(EdgeType(node_id, node_id + NUM_NODES_R - 1)); connected[edges.back().first] = connected[edges.back().second] = true; } if (rand() <= RAND_MAX * EDGE_PROB) { edges.push_back(EdgeType(node_id, node_id + NUM_NODES_R)); connected[edges.back().first] = connected[edges.back().second] = true; } if (rand() <= RAND_MAX * EDGE_PROB) { edges.push_back(EdgeType(node_id, node_id + NUM_NODES_R + 1)); connected[edges.back().first] = connected[edges.back().second] = true; created_se = true; } } else if (node_id % NUM_NODES_R == NUM_NODES_R - 1) { // NE, E if (rand() <= RAND_MAX * (EDGE_PROB + NE_EXTRA_PROB) && !created_prv_se) { edges.push_back(EdgeType(node_id, node_id + NUM_NODES_R - 1)); connected[edges.back().first] = connected[edges.back().second] = true; } if (rand() <= RAND_MAX * EDGE_PROB) { edges.push_back(EdgeType(node_id, node_id + NUM_NODES_R)); connected[edges.back().first] = connected[edges.back().second] = true; } } created_prv_se = created_se; } /* add south edges on the last column */ for (int node_id = (NUM_NODES_R * NUM_NODES_C - NUM_NODES_R); node_id < (NUM_NODES_R * NUM_NODES_C - 1); ++node_id) { // S if (rand() <= RAND_MAX * EDGE_PROB) { edges.push_back(EdgeType(node_id, node_id + 1)); connected[edges.back().first] = connected[edges.back().second] = true; } } int num_dc = 0; /* iterate over all nodes to see which are disconnected */ for (unsigned int node_id = 0; node_id < connected.size(); ++node_id) { if (!connected[node_id]) { int n_e_s_w_offset[] = {-1, NUM_NODES_R, +1, -NUM_NODES_R}; bool n_e_s_w[] = {true, true, true, true}; if (node_id % NUM_NODES_R == 0) n_e_s_w[0] = false; if (node_id / NUM_NODES_R == NUM_NODES_C - 1) n_e_s_w[1] = false; if (node_id % NUM_NODES_R == NUM_NODES_R - 1) n_e_s_w[2] = false; if (node_id / NUM_NODES_R == 0) n_e_s_w[3] = false; for (unsigned int direc = 0; direc < 4; ++direc) { if (n_e_s_w[direc]) { edges.push_back(EdgeType(node_id, node_id + n_e_s_w_offset[direc])); connected[edges.back().first] = connected[edges.back().second] = true; break; } } } } /* sort edges */ std::sort(edges.begin(), edges.end(), sort_pred); assert(num_dc == 0); }
void EdmondMatching::addEdge(unsigned v1, unsigned v2) { graphSize = std::max(graphSize, v1); graphSize = std::max(graphSize, v2); Edges.push_back(EdgeType(v1, v2)); }