dataStructure@ Find if there is a path between two vertices in a directed graph

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

dataStructure@ Find if there is a path between two vertices in a directed graph

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

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

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<limits>
#include<vector>
using namespace std;
const int maxn = ;
struct edge{
int to, cost;
edge(int t){
this->to = t; this->cost = ;
}
};
void addEdge(vector<edge> &edgelist, vector<vector<int> > &G, int from, int to){
edgelist.push_back(edge(to));
G[from].push_back(edgelist.size()-);
}
void addDoubleEdge(vector<edge> &edgelist, vector<vector<int> > &G, int from, int to){
addEdge(edgelist,G,from,to);
addEdge(edgelist,G,to,from);
}
bool isCyclic(vector<edge> edgelist, vector<vector<int> > G,vector<bool> vis, vector<bool> RecStack, int v){
for(int i=;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;
RecStack[e.to] = false;
}
}
return false;
}
void isCyclicUtil(vector<edge> edgelist, vector<vector<int> > G){// find all cycles.
vector<bool> vis(G.size());
vector<bool> RecStack(G.size());
for(int i=;i<vis.size();++i) vis[i]=false;
for(int i=;i<RecStack.size();++i) RecStack[i]=false; for(int i=;i<G.size();++i){
if(!vis[i]){
vis[i] = true; RecStack[i] = true;
if(isCyclic(edgelist,G,vis,RecStack,i)){
cout<<i<<" starts a cycle"<<endl;
}
RecStack[i] = false;
}
}
}
bool dfs(vector<edge> edgelist, vector<vector<int> > G, vector<bool> vis, int from, int to){
if(from == to) return true;
for(int i=;i<G[from].size();++i){
edge e = edgelist[G[from][i]];
if(e.to == to) return true;
if(!vis[e.to]){
vis[e.to] = true;
if(dfs(edgelist, G, vis, e.to, to)) return true;
}
}
return false;
}
void isReachable(vector<edge> edgelist, vector<vector<int> > G, int from, int to){
vector<bool> vis(G.size());
for(int i=;i<vis.size();++i) vis[i] = false;
vis[from] = true;
if(dfs(edgelist, G, vis, from, to)) cout<<from<<" and "<<to<<" are reachable to each other"<<endl;
else cout<<from<<" and "<<to<<" are not reachable to each other"<<endl;
}
void buildMap(vector<edge> &edgelist, vector<vector<int> > &G){
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
}
int main(){
vector<edge> edgelist;
vector<vector<int> > G(maxn); buildMap(edgelist,G); isCyclicUtil(edgelist, G); isReachable(edgelist, G, , ); return ;
}
上一篇:nginx 支持pathinfo的配置文件


下一篇:小草手把手教你 LabVIEW 串口仪器控制——VISA 串口配置