943,866 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2891
  • C++ RSS
Jan 11th, 2009
0

Finding alternate paths using Dijkstra algorithm

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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


C++ Syntax (Toggle Plain Text)
  1.  
  2. C1 C5 C6 C10 C14
  3. C1 C5 C9 C10 C14
  4. C1 C5 C9 C13 C14


Thanks in advance
Vikash
Reputation Points: 6
Solved Threads: 0
Newbie Poster
vikashkumar051 is offline Offline
9 posts
since Dec 2008
Jan 11th, 2009
0

Re: Finding alternate paths using Dijkstra algorithm

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.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Jan 11th, 2009
0

Re: Finding alternate paths using Dijkstra algorithm

Can you please paste here exact code for doing it.

Thanks
Vikash
Reputation Points: 6
Solved Threads: 0
Newbie Poster
vikashkumar051 is offline Offline
9 posts
since Dec 2008
Jan 11th, 2009
0

Re: Finding alternate paths using Dijkstra algorithm

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.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Jan 12th, 2009
0

Re: Finding alternate paths using Dijkstra algorithm

I am unable to go ahead, I'm stuck. Can you please paste here exact code for solving the above scenario.

Thanks
Vikash
Reputation Points: 6
Solved Threads: 0
Newbie Poster
vikashkumar051 is offline Offline
9 posts
since Dec 2008
Jan 12th, 2009
0

Re: Finding alternate paths using Dijkstra algorithm

uhmm.. No.
Learn to code, or at least use a search engine.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jan 13th, 2009
0

Re: Finding alternate paths using Dijkstra algorithm

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
Reputation Points: 6
Solved Threads: 0
Newbie Poster
vikashkumar051 is offline Offline
9 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: A quick Survey (The More Responces the Better)
Next Thread in C++ Forum Timeline: adding array values





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC