User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,586 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,621 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1882 | Replies: 0
Reply
Join Date: Jun 2007
Posts: 14
Reputation: kpillsb39 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
kpillsb39 kpillsb39 is offline Offline
Newbie Poster

Help Dijkstra's Algorithm help

  #1  
Nov 3rd, 2007
Hi,

I have built a program for Dijkstra's algorithm, but not sure why it does not display the shortest path thru the matrix. Would someone please point me in the right direction to get the code to work.

Thanks,
code follows, no errors, but no display of path vertices for shortest path.

  1. #include<iostream> //cout, cin, cerr, ...
  2. #include<cmath> //math
  3. #include<queue> //queue
  4. using namespace std;
  5. class queue
  6. {
  7. private:
  8. int q[21];
  9. int front, rear;
  10. protected:
  11. queue()
  12. {
  13. front=rear=-1;
  14. }
  15. int isempty()
  16. {
  17. if((front==-1 && rear==-1) || (front>rear))
  18. {
  19. front=rear=-1;
  20. return 1;
  21. }
  22. return 0;
  23. }
  24. int push(int a)
  25. {
  26. if(isempty())
  27. front++;
  28. q[++rear]=a;
  29. }
  30. int del()
  31. {
  32. return q[front++];
  33. }
  34. };
  35. class dijkstra
  36. {
  37. private:
  38. int ajMat[21][21];
  39. int set[21],predecessor[21],mark[21],pathestimate[21];
  40. int source;
  41. int SIZE;
  42. public:
  43. int minimum();
  44. void initialize();
  45. void printpath(int);
  46. void algorithm();
  47. void output();
  48. };
  49. void dijkstra::initialize()
  50. {
  51. for(int i=1;i<=SIZE;i++)
  52. {
  53. mark[i]=0;
  54. pathestimate[i]=999;
  55. predecessor[i]=0;
  56. }
  57. pathestimate[source]=0;
  58. }
  59. void dijkstra::algorithm()
  60. {
  61. initialize();
  62. int count=0;
  63. int i;
  64. int j;
  65. while(count<SIZE)
  66. {
  67. j=minimum();
  68. set[++count]=j;
  69. mark[j]=1;
  70. for(i=1;i<=SIZE;i++)
  71. {
  72. if(ajMat[i][j]>0)
  73. {
  74. if(mark[i]!=1)
  75. {
  76. if(pathestimate[i]>pathestimate[j]+ajMat[i][j])
  77. {
  78. pathestimate[i]=pathestimate[j]+ajMat[i][j];
  79. predecessor[i]=j;
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. void dijkstra::printpath(int i)
  87. {
  88. if(i==source)
  89. {
  90. cout<<source;
  91. }
  92. else if(predecessor[i]==0)
  93. cout<<"no path from "<<source<<" to "<<i<<endl;
  94. else
  95. {
  96. printpath(predecessor[i]);
  97. cout<<".."<<i<<endl;
  98. }
  99. }
  100. void dijkstra::output()
  101. {
  102. for(int i=1;i<=SIZE;i++)
  103. {
  104. printpath(i);
  105. if(pathestimate[i]!=999)
  106. cout<<"->("<<pathestimate[i]<<")"<<endl;
  107. }
  108. }
  109. int dijkstra::minimum()
  110. {
  111. int min=999;
  112. int i,t;
  113. for(i=1;i<=SIZE;i++)
  114. {
  115. if(mark[i]!=1)
  116. {
  117. if(min>=pathestimate[i])
  118. {
  119. min=pathestimate[i];
  120. t=i;
  121. }
  122. }
  123. }
  124. return t;
  125. };
  126. void main()
  127. {
  128. const int SIZE = 21; //establish ajacency matrix int size
  129. int ajMat[21][21]= {
  130. { 0,14, 0, 0, 9, 0, 0,14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  131. {14,-1,13, 0, 7, 3,13, 0,12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  132. { 0,13,-1,11, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  133. { 0, 0,11,-1, 0, 0,11, 0, 0, 0, 0,11, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  134. { 9, 7, 0, 0,-1, 0, 0, 2, 2, 0, 0, 0,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},
  150. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8,13, 0, 0, 8,-1}};
  151.  
  152. char nodeName ='&'; //establish char node
  153. for(int i=0;i<SIZE;i++) //establish outer loop
  154. {
  155. nodeName = 'A'+i; //establish node name
  156. cout<<" Node "<<static_cast<char>(nodeName)//display node name
  157. <<" Neighbors are: "; //enter neighbors names
  158. for(int j=0; j<SIZE; j++) //establish inner loop
  159. {
  160. if(ajMat[i][j]>0) //if number greater than 0
  161. {
  162. nodeName = 'A'+j; //node name plus neighbor
  163. cout<<" Node "<<static_cast<char>(nodeName)//display node name
  164. <<","; //place comma between neighbors
  165. }
  166. }
  167. cout<<endl;
  168. }
  169. {
  170. dijkstra s;
  171. s.algorithm();
  172. s.output();
  173. }
  174. }
Last edited by WaltP : Nov 3rd, 2007 at 4:06 pm. Reason: Removed obnoxious COLOR tags
AddThis Social Bookmark Button
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:32 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC