Path Reconstruction using DFS
Hi Guys,
I asked an initial question about how to implement an all possible pathway algorithm, and I was told that I should look into DFS or BFS. I managed to program a [semi?]working module for DFS but I am still unsure as how to proceed about reconstructing the path.
Right now my input is a simple square. My starting point is 0 and the end point is 3
0--3
| |
| |
1--2
What I want is the program to output 0--1--2--3 AND 0--3 in preferably two separate lines.
So far I have the following code(
Code:
#include "stdio.h"
#define N 4
int q[N],top=-1,front=-1,rear=-1,stack[N];
int vis[N]={0};
int a[N][N]={
{0,1,0,1},
{1,0,1,0},
{0,1,0,1},
{1,0,1,0}
};
void dfs(int s,int n);
void push(int item);
int pop();
int main()
{
dfs(0,N);
return 0;
}
void dfs(int s,int n)
{
int i,k;
push(s);
vis[s]=1;
k=pop();
if(k!=N)
printf(" %d ",k);
while(k!=N)
{
for(i=0;i< N;i++)
if((a[k][i]!=0)&&(vis[i]==0))
{
push(i);
vis[i]=1;
}
k=pop();
if(k!=N)
printf(" %d ",k);
}
// for(i=0;i< N;i++)
// if(vis[i]==0)
// dfs(i,n);
}
void push(int item)
{
if(top==N)
printf("Stack overflow ");
else
stack[++top]=item;
}
int pop()
{
int k;
if(top==-1)
return(N);
else
{
k=stack[top--];
return(k);
}
}
when I run this I get the following output: 0 3 2 1
Any Ideas as how to go proceed next? I am completely confused. :/
I am bit of a C noob so I could use a little detailed help. The program is part of a sub routine that I am writing for my research project.
Thanks for all the help guys!