struct digraph *Warshall (struct digraph *g) { vertex i, j, k; /* make a new digraph for g' */ struct digraph *tc_g = new_digraph (g->n); assert (tc_g->n == g->n); /* copy all the edges over to the new matrix */ for (j=0; j<g->n; j++) for (i=0; i<g->n; i++) if (edge_exists (g, i, j)) insert_edge (tc_g, i, j); /* make sure there is an edge from v to v for all v (reflexive property) */ for (i=0; i<g->n; i++) insert_edge (tc_g, i, i); /* Warshall's algorithm */ for (k=0; k<g->n; k++) for (i=0; i<g->n; i++) for (j=0; j<g->n; j++) if (edge_exists (tc_g, i, k) && edge_exists (tc_g, k, j)) insert_edge (tc_g, i, j); return tc_g; }
/** Add an edge. * @param edge edge to add * @param mode edge add mode * @param allow_existing if true allow an edge with the given parameters * to exist. In that case the method silently returns. Note that you might * loose edge properties in that case. If false, an exception is thrown if * a similar edge exists. * @throw Exception thrown if an edge for the same nodes already exist unless * @p allow_existing is set true, then simply returns. * Takes directed edges into account. So if a directed edge from p1 to p2 * exists, it is ok to add a (directed or undirected) edge from p2 to p1. */ void NavGraph::add_edge(const NavGraphEdge &edge, NavGraph::EdgeMode mode, bool allow_existing) { if (edge_exists(edge)) { if (allow_existing) return; else throw Exception("Edge from %s to %s already exists", edge.from().c_str(), edge.to().c_str()); } else { switch (mode) { case EDGE_NO_INTERSECTION: edge_add_no_intersection(edge); break; case EDGE_SPLIT_INTERSECTION: edge_add_split_intersection(edge); break; case EDGE_FORCE: edges_.push_back(edge); edges_.back().set_nodes(node(edge.from()), node(edge.to())); break; } reachability_calced_ = false; notify_of_change(); } }
auto Graph<T, E>::minimum_connection(const std::vector<Node<T, E>>& x, const std::vector<Node<T, E>>& y) const { const T* min_node1 = nullptr; const T* min_node2 = nullptr; auto min_edge = E{}; for (const auto& outer_node : x) { for (const auto& inner_node : y) { const auto exists = edge_exists(outer_node.value, inner_node.value); if (!exists) { continue; } const auto edge = get_edge(outer_node.value, inner_node.value); if (min_node1 == nullptr || edge < min_edge) { min_node1 = &outer_node.value; min_node2 = &inner_node.value; min_edge = edge; } } } return std::make_tuple(*min_node1, *min_node2, min_edge); }
void Graph::add_edge(const string &v1, const string &v2) { neighbor_list *neighbors; string *vertex; //cout << "Adding edge " << v1 << " to " << v2 << endl; if (edge_exists(v1, v2)) { //cout << "Edge already exists" << endl; return; } adjacency_list::iterator it = adj_list.find(v1); if (it == adj_list.end()) { neighbors = new neighbor_list(); adj_list[v1] = neighbors; } else neighbors = adj_list[v1]; vertex = new string(v2); neighbors->insert(make_pair(*vertex, true)); nodes[v1] = true; nodes[*vertex] = true; if (!directed) { add_edge(v2, v1); } }
void DFS (struct digraph *g, vertex v, int discovered[]) { vertex w; if (v > g->n) return; discovered[v] = 1; for (w=0; w<g->n; w++) if (!discovered[w] && edge_exists (g, v, w)) DFS (g, w, discovered); }
void print_adj_matrix (struct digraph *g) { int i, j; for (j=0; j<g->n; j++) { printf ("[ "); for (i=0; i<g->n; i++) { if (edge_exists (g, j, i)) printf ("1 "); else printf ("0 "); } printf ("]\n"); } }
static void add_path_to_graph(pkt_t *pkt) { uint8_t count=0; uint8_t hops=pkt->hops; node_id_t node1,node2; if(hops>MAX_PATH_LEN) { LOG("WARN: response with truncated path\r\n"); return; } /* Add ourselves as the last hop in the path */ pkt->path[hops++] = this_node_id; for(count=hops-1;count>0;count--){ node1=pkt->path[count]; node2=pkt->path[count-1]; LOG("adding rf link: "); LOGP("%d <-> %d\r\n", node1, node2); if(!edge_exists(network.edges[node1],node2,network.degree[node1])){ network.edges[node1][network.degree[node1]].v=node2; network.edges[node1][network.degree[node1]].weight=1; network.degree[node1]++; } if(!edge_exists(network.edges[node2],node1,network.degree[node2])){ network.edges[node2][network.degree[node2]].v=node1; network.edges[node2][network.degree[node2]].weight=1; network.degree[node2]++; } } }
void DFS2 (struct digraph *g, vertex v, int discovered[], struct list_node **L) { vertex w; if (v > g->n) return; discovered[v] = 1; //for (w=0; w<g->n; w++) { for (w=g->n-1; w>=0; w--) { if (!discovered[w] && edge_exists (g, v, w)) DFS2 (g, w, discovered, L); } /* we are finished with node v; push it onto the list */ struct list_node *e = malloc (sizeof (struct list_node)); e->next = *L; e->x = v; *L = e; }
int add_directed_edge(t_net *g, size_t id1, size_t id2) { t_node *node_a; t_node *node_b; t_slist *node_a_edges; node_a = NULL; node_b = NULL; if (!(node_a = find_node(g, id1)) || !(node_b = find_node(g, id2))) return (FALSE & my_perror("add_edge couldnt find specified node(s)")); if ((edge_exists(node_a, node_b))) return (TRUE); if ((node_a->edges = add_slist_elem(node_a->edges)) == NULL) return (FALSE & my_perror("malloc failed (add_slist_elem) !")); node_a_edges = goto_last_slist(node_a->edges); node_a_edges->data = node_b; g->nb_links++; return (TRUE); }
static void addPEdges (channel* cp, maze* mp) { int i,j; /* dir[1,2] are used to figure out whether we should use prev * pointers or next pointers -- 0 : decrease, 1 : increase */ int dir; /* number of hops along the route to get to the deciding points */ pair hops; /* precedences of the deciding points : same convention as * seg_cmp function */ int prec1, prec2; pair p; rawgraph* G = cp->G; segment** segs = cp->seg_list; for(i=0;i+1<cp->cnt;i++) { for(j=i+1;j<cp->cnt;j++) { if (!edge_exists(G,i,j) && !edge_exists(G,j,i)) { if (is_parallel(segs[i], segs[j])) { /* get_directions */ if(segs[i]->prev==0) { if(segs[j]->prev==0) dir = 0; else dir = 1; } else if(segs[j]->prev==0) { dir = 1; } else { if(segs[i]->prev->comm_coord==segs[j]->prev->comm_coord) dir = 0; else dir = 1; } p = decide_point(segs[i], segs[j], 0, dir); hops.a = p.a; prec1 = p.b; p = decide_point(segs[i], segs[j], 1, 1-dir); hops.b = p.a; prec2 = p.b; switch(prec1) { case -1 : set_parallel_edges (segs[j], segs[i], dir, 0, hops.a, mp); set_parallel_edges (segs[j], segs[i], 1-dir, 1, hops.b, mp); if(prec2==1) removeEdge (segs[i], segs[j], 1-dir, mp); break; case 0 : switch(prec2) { case -1: set_parallel_edges (segs[j], segs[i], dir, 0, hops.a, mp); set_parallel_edges (segs[j], segs[i], 1-dir, 1, hops.b, mp); break; case 0 : set_parallel_edges (segs[i], segs[j], 0, dir, hops.a, mp); set_parallel_edges (segs[i], segs[j], 1, 1-dir, hops.b, mp); break; case 1: set_parallel_edges (segs[i], segs[j], 0, dir, hops.a, mp); set_parallel_edges (segs[i], segs[j], 1, 1-dir, hops.b, mp); break; } break; case 1 : set_parallel_edges (segs[i], segs[j], 0, dir, hops.a, mp); set_parallel_edges (segs[i], segs[j], 1, 1-dir, hops.b, mp); if(prec2==-1) removeEdge (segs[i], segs[j], 1-dir, mp); break; } } } } } }
/* sets the edges for a series of parallel segments along two edges starting * from segment i, segment j. It is assumed that the edge should be from * segment i to segment j - the dependency is appropriately propagated */ static void set_parallel_edges (segment* seg1, segment* seg2, int dir1, int dir2, int hops, maze* mp) { int x; channel* chan; channel* nchan; segment* prev1; segment* prev2; if (seg1->isVert) chan = chanSearch(mp->vchans, seg1); else chan = chanSearch(mp->hchans, seg1); insert_edge(chan->G, seg1->ind_no, seg2->ind_no); for (x=1;x<=hops;x++) { prev1 = next_seg(seg1, dir1); prev2 = next_seg(seg2, dir2); if(!seg1->isVert) { nchan = chanSearch(mp->vchans, prev1); if(prev1->comm_coord==seg1->p.p1) { if(seg1->l1==B_UP) { if(edge_exists(chan->G, seg1->ind_no, seg2->ind_no)) insert_edge(nchan->G, prev2->ind_no, prev1->ind_no); else insert_edge(nchan->G, prev1->ind_no, prev2->ind_no); } else { if(edge_exists(chan->G, seg1->ind_no, seg2->ind_no)) insert_edge(nchan->G, prev1->ind_no, prev2->ind_no); else insert_edge(nchan->G, prev2->ind_no, prev1->ind_no); } } else { if(seg1->l2==B_UP) { if(edge_exists(chan->G, seg1->ind_no, seg2->ind_no)) insert_edge(nchan->G,prev1->ind_no, prev2->ind_no); else insert_edge(nchan->G,prev2->ind_no, prev1->ind_no); } else { if(edge_exists(chan->G, seg1->ind_no, seg2->ind_no)) insert_edge(nchan->G, prev2->ind_no, prev1->ind_no); else insert_edge(nchan->G, prev1->ind_no, prev2->ind_no); } } } else { nchan = chanSearch(mp->hchans, prev1); if(prev1->comm_coord==seg1->p.p1) { if(seg1->l1==B_LEFT) { if(edge_exists(chan->G, seg1->ind_no, seg2->ind_no)) insert_edge(nchan->G, prev1->ind_no, prev2->ind_no); else insert_edge(nchan->G, prev2->ind_no, prev1->ind_no); } else { if(edge_exists(chan->G, seg1->ind_no, seg2->ind_no)) insert_edge(nchan->G, prev2->ind_no, prev1->ind_no); else insert_edge(nchan->G, prev1->ind_no, prev2->ind_no); } } else { if(seg1->l2==B_LEFT) { if(edge_exists(chan->G, seg1->ind_no, seg2->ind_no)) insert_edge(nchan->G, prev2->ind_no, prev1->ind_no); else insert_edge(nchan->G, prev1->ind_no, prev2->ind_no); } else { if(edge_exists(chan->G, seg1->ind_no, seg2->ind_no)) insert_edge(nchan->G, prev1->ind_no, prev2->ind_no); else insert_edge(nchan->G, prev2->ind_no, prev1->ind_no); } } } chan = nchan; seg1 = prev1; seg2 = prev2; } }