datastructure@ Find If there is a path between the vertices in a directed graph

Source: Internet
Author: User

Given a Directed Graph and both vertices in it, check whether there are a path from the first Given vertex to second. For example, in the following graph, there are a path from vertex 1 to 3. As another example, there is no path from 3 to 0.

We can either use Breadth First search (BFS) or Depth first search (DFS) to find path between and vertices. Take the first vertex as source in a BFS (or Dfs), follow the standard BFS (or DFS). If we see the second vertex in our traversal and then return true. Else return FALSE.

Following is C + + code, uses BFS for finding reachability of second vertex from first vertex.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <limits>5#include <vector>6 using namespacestd;7 Const intMAXN =4;8 structedge{9     intto, cost;TenEdgeintt) { One          This->to = t; This->cost =0; A     } - }; - voidAddedge (vector<edge> &edgelist, vector<vector<int> > &g,int  from,intTo ) { the Edgelist.push_back (Edge (To)); -g[ from].push_back (Edgelist.size ()-1); - } - voidAdddoubleedge (vector<edge> &edgelist, vector<vector<int> > &g,int  from,intTo ) { +Addedge (Edgelist,g, from, to); -Addedge (Edgelist,g,to, from); + } A BOOLIscyclic (vector<edge> edgelist, vector<vector<int> > g,vector<BOOL> Vis, vector<BOOL> Recstack,intv) { at      for(intI=0; I<g[v].size (); + +i) { -Edge E =Edgelist[g[v][i]]; -         if(Recstack[e.to])return true; -         if(!Vis[e.to]) { -Vis[e.to] =true; Recstack[e.to] =true; -             if(Iscyclic (edgelist,g,vis,recstack,e.to))return true; inRecstack[e.to] =false; -         } to     } +     return false; - } the voidIscyclicutil (vector<edge> edgelist, vector<vector<int> > G) {//find all cycles. *vector<BOOL>Vis (G.size ()); $vector<BOOL>Recstack (G.size ());Panax Notoginseng      for(intI=0; I<vis.size (); ++i) vis[i]=false; -      for(intI=0; I<recstack.size (); ++i) recstack[i]=false; the      +      for(intI=0; I<g.size (); + +i) { A         if(!Vis[i]) { theVis[i] =true; Recstack[i] =true; +             if(Iscyclic (edgelist,g,vis,recstack,i)) { -cout<<i<<"starts a cycle"<<Endl; $             } $Recstack[i] =false; -         } -     } the } - BOOLDFS (vector<edge> edgelist, vector<vector<int> > G, vector<BOOL> Vis,int  from,intTo ) {Wuyi     if( from= = to)return true; the      for(intI=0; i<g[ from].size (); + +i) { -Edge E = edgelist[g[ from][i]]; Wu         if(e.to = = to)return true; -         if(!Vis[e.to]) { AboutVis[e.to] =true; $             if(Dfs (edgelist, G, Vis, e.to, to))return true; -         } -     } -     return false; A } + voidIsreachable (vector<edge> edgelist, vector<vector<int> > G,int  from,intTo ) { thevector<BOOL>Vis (G.size ()); -      for(intI=0; I<vis.size (); ++i) Vis[i] =false; $vis[ from] =true; the     if(Dfs (edgelist, G, Vis, from, to)) cout<< from<<" and"<<to<<"is reachable to all other"<<Endl; the     Elsecout<< from<<" and"<<to<<"is not reachable"<<Endl; the } the voidBuildmap (vector<edge> &edgelist, vector<vector<int> > &G) { -Addedge (Edgelist,g,0,1); inAddedge (Edgelist,g,0,2); theAddedge (Edgelist,g,2,0); theAddedge (Edgelist,g,1,2); AboutAddedge (Edgelist,g,2,3); theAddedge (Edgelist,g,3,3); the } the intMain () { +Vector<edge>edgelist; -vector<vector<int> >G (MAXN); the     Bayi Buildmap (edgelist,g); the      the Iscyclicutil (edgelist, G); -      -Isreachable (Edgelist, G,1,1); the      the     return 0; the}
View Code

[email protected] Find If there is a path between the vertices in a directed graph

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.