Finding alternate paths using Dijkstra algorithm

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 8
Reputation: vikashkumar051 is an unknown quantity at this point 
Solved Threads: 0
vikashkumar051 vikashkumar051 is offline Offline
Newbie Poster

Finding alternate paths using Dijkstra algorithm

 
0
  #1
Jan 11th, 2009
Hi,

I am pasting here code for finding path using Dijkstra algorithm.

#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:
  1.  
  2. The shortest path from C1 to C14 is:
  3.  
  4. C1 C2 C6 C10 C14.


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


  1.  
  2. C1 C5 C6 C10 C14
  3. C1 C5 C9 C10 C14
  4. C1 C5 C9 C13 C14


Thanks in advance
Vikash
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Finding alternate paths using Dijkstra algorithm

 
0
  #2
Jan 11th, 2009
The whole point of Dijkstra's algorithm is to find a least cost (or shortest, in your case) path.

Once you have a first path, eliminate one of the nodes (C2, C5, or C10 in your example) on that path from the graph, and run Dijkstra's algorithm again. If the resultant path has a higher cost than your first one, put the node back, eliminate another node, and try again until the resultant path has same (or similar) cost to the first. Repeat this as often as necessary.

This isn't exactly inexpensive for large graphs (necessary to run Dijkstra's algorithm a number of times) and needs to be done systematically, but it will work.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: vikashkumar051 is an unknown quantity at this point 
Solved Threads: 0
vikashkumar051 vikashkumar051 is offline Offline
Newbie Poster

Re: Finding alternate paths using Dijkstra algorithm

 
0
  #3
Jan 11th, 2009
Can you please paste here exact code for doing it.

Thanks
Vikash
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Finding alternate paths using Dijkstra algorithm

 
0
  #4
Jan 11th, 2009
Originally Posted by vikashkumar051 View Post
Can you please paste here exact code for doing it
You have a description of one possible approach; consider coding it up as an exercise.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: vikashkumar051 is an unknown quantity at this point 
Solved Threads: 0
vikashkumar051 vikashkumar051 is offline Offline
Newbie Poster

Re: Finding alternate paths using Dijkstra algorithm

 
0
  #5
Jan 12th, 2009
I am unable to go ahead, I'm stuck. Can you please paste here exact code for solving the above scenario.

Thanks
Vikash
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 949
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is online now Online
Posting Shark

Re: Finding alternate paths using Dijkstra algorithm

 
0
  #6
Jan 12th, 2009
uhmm.. No.
Learn to code, or at least use a search engine.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: vikashkumar051 is an unknown quantity at this point 
Solved Threads: 0
vikashkumar051 vikashkumar051 is offline Offline
Newbie Poster

Re: Finding alternate paths using Dijkstra algorithm

 
0
  #7
Jan 13th, 2009
ok, no problem. I thought that may be anyone of you can solve the scenario. First I always try to use search engine, as I was not able to get the solution thats why I posted my problem here, but its ok no problem.


Thanks
Vikash
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC