Question: how do i find two distinct paths from a starting vertices to and ending vertices in an undirected graph using adjacency list in c++. Psuedo
how do i find two distinct paths from a starting vertices to and ending vertices in an undirected graph using adjacency list in c++. Psuedo code is fine.
some member variables i use are
private:
vector > adj; //adjacency lists of the ugraph
vector distance; //for bfs
vector parents; //fo
vector colors; //for dfs
vector stamps; //for dfs
struct edge {
int neighbor;
int w;
edge(){
neighbor = 0;
w = 0;
}
edge(int i, int j){
neighbor = i;
w = j;
};
};
bool graph::distinctPaths(int u, int v){
bool *visited = new bool[(int)adj.size()];
int *path = new int[(int)adj.size()];
int pathIndex = 0;
bfsm(u);
if(distance[v] == INT_MAX)
return false;
for(int i = 0; i <(int)adj.size();i++)
visited[i] = false;
bfsm(u);
if(distance[v] == INT_MAX)
return false;
int count = 0;
printPaths(u,v,path,pathIndex,visited,count);
if(count<2)
return false;
for(int i = 0; i
if(path[i] == v){
if(path[i+1] != path[0] && ((i+1)!=pathIndex))
return false;
}
}
for(int i = 0; i < pathIndex ; i++){
cout<
if(path[i] == v){
cout<
}
}
return true;
}
void graph::printPaths(int u, int v, int path[],int &pathIndex,bool visited[],int &count){
if(count<2){
visited[pathIndex] = 1;
path[pathIndex] = u;
pathIndex++;
if(u == v){
count++;
if(count == 1){
visited[path[0]] = false;
for(int i = 1; i < pathIndex-1;i++){
cout<<"check"<
visited[i+1] = false;
}
printPaths(path[0],v,path,pathIndex,visited,count);
}
}
else
{
for(int i = 0; i < (int)adj[u].size();i++){
if(!visited[i]){
printPaths(i,v,path,pathIndex,visited,count);}
}
if(count<2)
pathIndex--;
visited[u] = 0;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
