need help with dijkstra program

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

Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

need help with dijkstra program

 
0
  #1
Aug 12th, 2009
I am doing a program to print out the shortest path between locations. I implemented it using a 2D array for the adjacency matrix.

However, when I run it and input the start and end locations, nothing else happens.

  1. #include <iostream>
  2. #include <limits.h>
  3. #include <assert.h>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7.  
  8. #define INF UINT_MAX //define INF as infinity value
  9. #define locationCount 9 //define number of locations = 9
  10. #define stack_limit 10000 //stack limit
  11.  
  12. typedef enum location {changi, punggol, sembawang, woodland, bukitbatok, clementi, outram, kallang, bedok, notExist};
  13.  
  14. const location first_location = changi;
  15. const location last_location = bedok;
  16.  
  17. char* name[] = { "changi", "punggol", "sembawang", "woodland", "bukit batok",
  18. "clementi", "outram", "kallang", "bedok" };
  19.  
  20.  
  21. //Adjacency Matrix
  22. unsigned int weightedGraph[locationCount][locationCount] =
  23. {
  24. // ch pu sem wl bb cle out kll bed
  25. {INF, 19 , INF, INF, INF, INF, INF, INF, 10 },//changi
  26. {19 , INF, 17 , INF, INF, INF, INF, 17 , INF },//punggol
  27. {INF, 17 , INF, 5 , INF, INF, INF, 22 , INF },//sembawang
  28. {INF, INF, 5 , INF, 14 , INF, INF, 24 , INF },//woodland
  29. {INF, INF, INF, 14 , INF, 5 , INF, 25 , INF },//bukit batok
  30. {INF, INF, INF, INF, 5 , INF, 10 , 17 , INF },//clementi
  31. {INF, INF, INF, INF, INF, 10 , INF, 7 , 14 },//outram
  32. {INF, 17 , 22, 24, 25 , 17 , 7 , INF, 10 },//kallang
  33. {10 , INF, INF, INF, INF, INF, 14, 10, INF } //bedok
  34.  
  35. };
  36.  
  37.  
  38. unsigned int overEst[locationCount];
  39. bool tight[locationCount];
  40. location predecessor[locationCount];
  41.  
  42.  
  43. location minNonTight()
  44. {
  45. location i, j;
  46.  
  47. for(i = first_location; i <= last_location; i+1)
  48. {
  49. if(!tight[i])
  50. break;
  51. }
  52.  
  53. assert(j <= last_location);
  54.  
  55. j = i;
  56.  
  57.  
  58. for(i+1; i<= last_location; i+1)
  59. {
  60. if(!tight[i] && overEst[i] < overEst[j])
  61. j = i;
  62. }
  63.  
  64. return j;
  65. }
  66.  
  67.  
  68. bool successor(int i, int j)
  69. {
  70. return (weightedGraph[i][j] != INF && i != j);
  71. }
  72.  
  73.  
  74. void dijkstraAlg(location s)
  75. {
  76. location i, j;
  77. overEst[s] = 0;
  78.  
  79. for(i = first_location; i <= last_location; i+1)
  80. {
  81. if(i != s)
  82. overEst[i] = INF;
  83.  
  84. tight[i] = false;
  85. predecessor[i] = notExist;
  86. }
  87.  
  88. for(int x = 0; x < locationCount; x++)
  89. {
  90. j = minNonTight();
  91. tight[j] = true;
  92.  
  93. if(overEst[j] = INF)
  94. continue;
  95.  
  96. for(i = first_location; i <= last_location; i+1)
  97. {
  98. if(successor(i,j) && !tight[i] &&
  99. overEst[j] + weightedGraph[i][j] < overEst[j])
  100. {
  101. overEst[i] = overEst[j] + weightedGraph[i][j];
  102. predecessor[i] = j;
  103. }
  104. }
  105.  
  106.  
  107.  
  108. }
  109.  
  110. }
  111.  
  112.  
  113.  
  114. /*stack for Djikstra shortest path */
  115.  
  116. location stack[stack_limit];
  117. unsigned int stackTop;
  118.  
  119. void push(location l)
  120. {
  121. assert(stackTop < stack_limit);
  122. stack[stackTop] = l;
  123. stackTop++;
  124.  
  125. }
  126.  
  127.  
  128. location pop()
  129. {
  130. stackTop--;
  131. return stack[stackTop];
  132. }
  133.  
  134.  
  135. bool emptyStack()
  136. {
  137. return(stackTop == 0);
  138. }
  139.  
  140.  
  141. void print_shortest_path(location origin, location destination) {
  142.  
  143. location v;
  144.  
  145. assert(origin != notExist && destination != notExist);
  146.  
  147.  
  148. dijkstraAlg(origin);
  149.  
  150. cout << "The shortest path from " << name[origin] << " to "
  151. << name[destination] << " :" <<endl;
  152.  
  153. for (v = destination; v != origin; v = predecessor[v])
  154. if (v == notExist) {
  155. cout << "Path does not exist" << endl;
  156. return;
  157. }
  158. else
  159. push(v);
  160.  
  161. push(origin);
  162.  
  163. while (!emptyStack())
  164. cout << name[pop()] << endl;
  165.  
  166. }
  167.  
  168.  
  169.  
  170. //MAIN
  171. int main()
  172. {
  173. int start, end;
  174.  
  175. cout << "changi, punggol, sembawang, woodland, bukitbatok, clementi, outram, kallang, bedok" << endl;
  176.  
  177. cout << "Enter a starting location: " << endl;
  178. cin >> start;
  179.  
  180. cout << "Enter an ending location: " << endl;
  181. cin >> end;
  182.  
  183. print_shortest_path((location)start, (location)end);
  184.  
  185. system("pause");
  186.  
  187. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

Re: need help with dijkstra program

 
0
  #2
Aug 12th, 2009
by the way this is my main when I run the program for testing

  1. int main()
  2. {
  3. print_shortest_path(changi, bedok);
  4.  
  5. system("pause");
  6.  
  7. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: need help with dijkstra program

 
0
  #3
Aug 12th, 2009
If this is a duplicate forgive me. I posted but it didn't show up!

This is a mistake! It's in two places in your code!
  1. // for(i = first_location; i <= last_location; i+1)
  2. for(i = first_location; i <= last_location; i++)
i+1 is not an expression! Use either i++ or i+= 1
  1. for(i+1; i<= last_location; i+1)
  2. ^ ^
Also set 0's as your diagonal when source=destination!
Last edited by wildgoose; Aug 12th, 2009 at 11:41 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

Re: need help with dijkstra program

 
0
  #4
Aug 13th, 2009
i++ or i+= will not work because the compiler doesnt allow such operations for enum iterations
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,219
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 150
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: need help with dijkstra program

 
0
  #5
Aug 13th, 2009
Originally Posted by number87 View Post
i++ or i+= will not work because the compiler doesnt allow such operations for enum iterations
what do you mean. Isn't 'i' a int variable declared inside the for loop?
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: need help with dijkstra program

 
0
  #6
Aug 13th, 2009
Enum math isn't available so you have to do a work around.

You have to use integers for the loop, but cast it into the enum!
  1. for( int i = first_location; i <= last_location; i++)
  2. {
  3. location eLoc = static_cast <location> (i);
  4. or
  5. location eLoc = (location) i;
  6.  
  7.  
  8. }
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC