I am trying to print all paths from source to destination in a graph which uses adjacency list.It is weighted and directed. I'm trying to do it in BFS.Thanks for the help. I am getting only one path. How do I get to print other paths?
Here is BFS function:
void BFS(struct Graph *G,QUEUE *q)
{
int j,i=0;
while(!isEmpty(q))
{
int source = dequeue(q);
printf("%d ",source);
path[i]=source;
i++;
if(source==bitis)//source==end
{
for(j=0;j<i;j++)
printf("%d ",path[j]);
}
struct node *head = G->adjList[source]->next;
while(head)
{
if(G->adjList[head->source]->marked)
{
head = head->next;
continue;
}
G->adjList[head->source]->marked = 1;
enqueue(head->source,q);
head = G->adjList[head->source]->next;
}
}
}
Here is structs:
struct node{
int source;
int w;
int marked;
struct node *next;
};
struct Graph{
int V;
int E;
struct node **adjList;
};
Here is adjList:
[0]->[2]->[1]
[1]->[2]
[2]->[3]->[1]
[3]->[0]
[4]->[3]->[2]
Output:4 3 0
- 5 node, 9 edge (A=0,B=1,C=2,D=3,E=4)
- start node: 4 end node: 0
- 5 node, 8 edge (A=0,B=1,C=2,D=3,E=4)
- start node: 4 end node: 0
I want all paths between the two values entered by the user. If user enter 3 and 2
I want the output to be this way:
3 -> 0 -> 2
3 -> 0 -> 1 -> 2
I hope I could express my question. My english so bad. Thank you.