943,514 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 710
  • C++ RSS
Aug 12th, 2009
0

need help with dijkstra program

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
Aug 12th, 2009
0

Re: need help with dijkstra program

by the way this is my main when I run the program for testing

C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. print_shortest_path(changi, bedok);
  4.  
  5. system("pause");
  6.  
  7. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
Aug 12th, 2009
0

Re: need help with dijkstra program

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!
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Aug 13th, 2009
0

Re: need help with dijkstra program

i++ or i+= will not work because the compiler doesnt allow such operations for enum iterations
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
Aug 13th, 2009
0

Re: need help with dijkstra program

Click to Expand / Collapse  Quote originally posted by number87 ...
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?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 13th, 2009
0

Re: need help with dijkstra program

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!
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009

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: seed and random number generator
Next Thread in C++ Forum Timeline: include problem with md5sum.h, pls help





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


Follow us on Twitter


© 2011 DaniWeb® LLC