GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Jan-2009, 03:18
vikashkumar051 vikashkumar051 is offline
New Member
 
Join Date: Dec 2008
Posts: 11
vikashkumar051 is an unknown quantity at this point

Finding alternate paths using Dijkstra algorithm


I am pasting here code for finding path using Dijkstra algorithm

CPP / C++ / C Code:

#include <stdio.h>
#include <limits.h>
#include <assert.h>

typedef enum {false, true} bool; /* The order is rather important to
				    get the boolean values right. */
typedef char *string;


#define oo UINT_MAX /* infinity is represented as the maximum unsigned int. */

typedef unsigned int value;

typedef enum { C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12, C13, C14, nonexistent} vertex;

const vertex first_vertex = C1;
const vertex last_vertex = C14;

#define no_vertices 14

string name[] = {"C1","C2","C3","C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13", "C14"};

value weight[no_vertices][no_vertices] =
 {
/*  C1	C2   C3	  C4    C5   C6   C7  C8   C9  C10  C11 C12  C13  C14 */
  { 0,	10,  oo,  oo,   10,  oo,  oo, oo,  oo,  oo,  oo, oo,  oo,  oo	}, // C1
  { 10,	0,   10,  oo,   10,  10,  oo, oo,  oo,	oo,  oo, oo,  oo,  oo	}, // C2
  { oo,	10,   0,  10,   oo,  10,  10, oo,  oo,	oo,  oo, oo,  oo,  oo	}, // C3
  { oo,	oo,  10,   0,   oo,  oo,  10, oo,  oo,	oo,  oo, oo,  oo,  oo	}, // C4
  { 10,	10,  oo,  oo,    0,  10,  oo, 10,  10,	oo,  oo, oo,  oo,  oo	}, // C5
  { oo,	10,  10,  oo,   10,   0,  10, oo,  10,	10,  oo, oo,  oo,  oo	}, // C6
  { oo,	oo,  10,  10,   oo,  10,   0, oo,  oo,	10,  10, oo,  oo,  oo	}, // C7
  { oo,	oo,  oo,  oo,   10,  oo,  oo,  0,  10,	oo,  oo, 10,  oo,  oo	}, // C8
  { oo,	oo,  oo,  oo,   10,  10,  oo, 10,   0,	10,  oo, 10,  10,  oo	}, // C9
  { oo,	oo,  oo,  oo,   oo,  10,  10, oo,  10,	 0,  10, oo,  10,  10	}, // C10
  { oo,	oo,  oo,  oo,   oo,  oo,  10, oo,  oo,	10,  0,	 oo,  oo,  10	}, // C11
  { oo,	oo,  oo,  oo,   oo,  oo,  oo, 10,  10,	oo,  oo,  0,  10,  oo	}, // C12
  { oo,	oo,  oo,  oo,   oo,  oo,  oo, oo,  10,	10,  oo, 10,   0,  10	}, // C13
  { oo,	oo,  oo,  oo,   oo,  oo,  oo, oo,  oo,	10,  10, oo,  10,   0	}  // C14
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * The implementation of Dijkstra's algorithm starts here. *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


value D[no_vertices];
bool tight[no_vertices];
vertex predecessor[no_vertices];


vertex minimal_nontight() {

  vertex j, u;

  for (j = first_vertex; j <= last_vertex; j++)
     if (!tight[j])
	break;

  assert(j <= last_vertex);

  u = j;

  for (j++; j <= last_vertex; j++)
     if (!tight[j]  &&  D[j] < D[u])
	  u = j;

  return u;
}


bool successor(vertex u, vertex z) {
  return (weight[u][z] != oo  &&  u != z);
}


void dijkstra(vertex s) {
  vertex z, u;
  int i;

  D[s] = 0;

  for (z = first_vertex; z <= last_vertex; z++) {

    if (z != s)
      D[z] = oo;

    tight[z] = false;
    predecessor[z] = nonexistent;
  }

  for (i = 0; i < no_vertices; i++) {

    u = minimal_nontight();
    tight[u] = true;

    if (D[u] == oo)
      continue; /* to next iteration ofthe for loop */

    for (z = first_vertex; z <= last_vertex; z++)
      if (successor(u,z) && !tight[z] && D[u] + weight[u][z] < D[z]) {
	D[z] = D[u] + weight[u][z]; /* Shortcut found. */
	predecessor[z] = u;
      }
  }
}


#define stack_limit 10000 /* Arbitrary choice. Has to be big enough to
			     accomodate the largest path. */

vertex stack[stack_limit];
unsigned int sp = 0; /* Stack pointer. */

void push(vertex u) {
  assert(sp < stack_limit);
  stack[sp] = u;
  sp++;
}

bool stack_empty() {
  return (sp == 0);
}

vertex pop() {
  assert(!stack_empty());
  sp--;
  return stack[sp];
}

/* End of stack digression and back to printing paths. */

void print_shortest_path(vertex origin, vertex destination) {

  vertex v;

  assert(origin != nonexistent  &&  destination != nonexistent);

  dijkstra(origin);

  printf("The shortest path from %s to %s is:\n\n",
	 name[origin], name[destination]);

  for (v = destination; v != origin; v = predecessor[v])
    if (v == nonexistent) {
      printf("nonexistent (because the graph is disconnected).\n");
      return;
    }
    else
      push(v);

  push(origin);

  while (!stack_empty())
    printf(" %s",name[pop()]);

  printf(".\n\n");
}


/* We now run an example. */

int main() {

  print_shortest_path(C1,C14);

  return 0; /* Return gracefully to the caller of the program
	       (provided the assertions above haven't failed). */
}

/* End of program. */


This is the output you get when you run the program:
Code:
The shortest path from C1 to C14 is: C1 C2 C6 C10 C14.

I want other paths which are also shortest should be printed.
that is, below path should also be printed

Code:
C1 C5 C6 C10 C14 C1 C5 C9 C10 C14 C1 C5 C9 C13 C14


Thanks in advance
Vikash
  #2  
Old 08-Jan-2009, 08:13
cpit cpit is offline
Junior Member
 
Join Date: Jan 2006
Posts: 73
cpit is on a distinguished road

Re: Finding alternate paths using Dijkstra algorithm


Firstly, you have to find all paths from "source" to "destination", after you have to select all paths with minimum number of hops.
  #3  
Old 08-Jan-2009, 08:43
vikashkumar051 vikashkumar051 is offline
New Member
 
Join Date: Dec 2008
Posts: 11
vikashkumar051 is an unknown quantity at this point

Re: Finding alternate paths using Dijkstra algorithm


Thanks for your reply, but I know this that I have to find out all paths from "source" to "destination", then I have to select all paths with minimum number of hops.

Question is what change should be made to the code pasted above to achieve the desired result ?

Thanks
Vikash
  #4  
Old 08-Jan-2009, 23:57
vikashkumar051 vikashkumar051 is offline
New Member
 
Join Date: Dec 2008
Posts: 11
vikashkumar051 is an unknown quantity at this point

Re: Finding alternate paths using Dijkstra algorithm


Here, I am attaching a picture of the matrix shown in code. Please help me out in solving the above scenario.


Thanks
Vikash
Attached Images
File Type: jpg matrixImage.jpg (11.7 KB, 28 views)
  #5  
Old 09-Jan-2009, 23:09
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 255
Blake is a jewel in the roughBlake is a jewel in the rough

Re: Finding alternate paths using Dijkstra algorithm


If you're looking for every alternate shortest path, you're out of luck. The reason is that the number of shortest paths can be exponential in the number of edges in the graph. (Or even infinite if there are zero weight cycles). If you want to find a subset of all the shortest paths, given any shortest path, you know that an alternate shortest path must omit at least one edge that is on your original shortest path. Thus you can remove one edge at a time from one of the original shortest paths in your graph, re-run the algorithm, and see if the new path you get has the same length. If you only get longer paths each time, the shortest path you've found must be unique. Otherwise, you will have a new shortest path.
__________________
www.blake-foster.com
  #6  
Old 10-Jan-2009, 08:22
vikashkumar051 vikashkumar051 is offline
New Member
 
Join Date: Dec 2008
Posts: 11
vikashkumar051 is an unknown quantity at this point

Re: Finding alternate paths using Dijkstra algorithm


I am looking for every alternate shortest path, as I have explaned in my first post. that is,

Code:
This is the output you get when you run the program: The shortest path from C1 to C14 is: C1 C2 C6 C10 C14. I want other paths which are also shortest should be printed. that is, below path should also be printed C1 C5 C6 C10 C14 C1 C5 C9 C10 C14 C1 C5 C9 C13 C14

Suppose, I have a static graph as shown in the matrix in the above code, & I don't want to re-run code everytime by removing one edge at a time. Just it should print all the alternate paths.


Thanks
Vikash
  #7  
Old 10-Jan-2009, 08:33
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 255
Blake is a jewel in the roughBlake is a jewel in the rough

Re: Finding alternate paths using Dijkstra algorithm


If you don't want to re-run the code every time, you should just use depth-first search and cut off the search when the distance reaches the length of the shortest path. I suggested re-running Dijkstra because you asked how to modify the existing code.

Normally, depth-first search will keep track of visited nodes and cut off when you come to a node you have already visited. In this case you should not do that, since you want every path.

Just keep in mind that due to the (possibly) exponential number of shortest paths, on some inputs this algorithm will still be running when we're all dead and buried.
__________________
www.blake-foster.com
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
all paths on Dijkstra - handle vector cpit C++ Forum 1 13-Jul-2006 09:23
all shortest paths with dijkstra cpit C++ Forum 0 05-Jun-2006 11:07
Algorithm Help Please! daking_09 C++ Forum 0 24-May-2006 20:12
bug finding in searching algorithm! robin1977 C Programming Language 0 24-Mar-2005 21:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:02.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.