| | |
Finding alternate paths using Dijkstra algorithm
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
Hi,
I am pasting here code for finding path using Dijkstra algorithm.
This is the output you get when you run the program:
I want other paths which are also shortest should be printed.
that is, below path should also be printed
Thanks in advance
Vikash
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)
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
C++ Syntax (Toggle Plain Text)
C1 C5 C6 C10 C14 C1 C5 C9 C10 C14 C1 C5 C9 C13 C14
Thanks in advance
Vikash
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
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.
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: A quick Survey (The More Responces the Better)
- Next Thread: adding array values
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





