Click to See Complete Forum and Search --> : Graph theory: all paths between 2 vertices


Squeezy
January 4th, 2009, 02:53 PM
Good time of day, everyone!
I'm stuck at my task on data structures and looks like I need your help. The task goes as follows:

For a given unweighted directed graph (represented as adjacent matrix) and two its vertices, find all the paths between these vertices so that any pair of paths do not cross on vertices.

I only came up with an idea of recursive extension of standard Breadth-First Search procedure but I'm not sure how to implement it.
I used no structures since all vertices are numbered and information about them (color, predecessor etc.) is stored in respective arrays. I added a boolean array visited that indicates whether the respective vertex has been visited to keep the property satisfied; there's also an array of queues routes storing the route from each vertex. When the vertex adjacent to current one is found, it is marked as visited, gets added to the respective queue and becomes a starting point for a recursive call of BFS.
So my first question is whether this scheme is actually gonna work?

If it is, here's my simple implementation of extended BFS:

void bfs(int adj[][N], bool visited[], int start, int dest, int d[], int pi[], short color[]){
for(i=0; i<N; i++){
color[i]=WHITE;
d[i]=INT_MAX;
pi[i]=-1;
routes[i]=NULL;
}

int q[N]={},rear=-1,front=-1;
q[++rear]=start;
visited[start]=1;
d[start]=0;
color[start]=GRAY;

while(rear!=front){
start=q[++front];

for(i=0;i<N;i++){
if(adj[start][i] && !visited[i]){
enqueue(routes[start], i);
if(color[i]==WHITE){
color[i]=GRAY;
d[i]=d[start]+1;
pi[i]=start;
q[++rear]=i;
visited[i]=true;
}
color[i]=BLACK;
bfs(adj,visited, i, dest, d, pi, color);
if (i==dest) return;
}
}
}
}

As you can see, the problem is that every recursive call 'corrupts' values of variables and I need to make them local (when the call is over, the procedure must get back to the old values). Since I'm new to programming, I've no clue how to fix the code.

Any help would be greatly appreciated!