User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 375,199 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 2,153 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 Pascal and Delphi advertiser:
Views: 896 | Replies: 4
Reply
Join Date: May 2008
Posts: 4
Reputation: johnkurtz is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
johnkurtz johnkurtz is offline Offline
Newbie Poster

dijkstra algorithm

  #1  
May 6th, 2008
Hey, could anybody lend me dijkstra's algorithm in pascal? (commented please). I have searched for it on Internet and I haven't find it.

Thanks.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,738
Reputation: Duoas is a name known to all Duoas is a name known to all Duoas is a name known to all Duoas is a name known to all Duoas is a name known to all Duoas is a name known to all 
Rep Power: 10
Solved Threads: 173
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: dijkstra algorithm

  #2  
May 6th, 2008
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: johnkurtz is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
johnkurtz johnkurtz is offline Offline
Newbie Poster

Re: dijkstra algorithm

  #3  
May 10th, 2008
Sorry guys. I was too selfish.
I have tried to implement Dijkstra's algorithm. The next code gives me a problem with the procedure "print". In the while, "I" never takes the value of "parfirst" and I don't know what to do to solve the problem.

  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. type
  9. matrix=array[1..5,1..5]of integer;
  10. tedge=record
  11. a,b:integer;
  12. c:integer;
  13. end;
  14. matrix1=array[1..5]of integer;
  15.  
  16. var
  17. edges:array[1..5] of tedge;
  18. weight:matrix; {matrix for the weight of each edge}
  19. weightmax,first,last:integer; {infinity}
  20. distance:matrix1; {distances between the first a node and each vertex}
  21. node:matrix1; {previous vertex list}
  22.  
  23.  
  24. procedure inicialize (var paredges:array of tedge;var parweight:matrix;var parfirst,parlast:integer);
  25. var
  26. I,J:integer;
  27.  
  28. begin
  29. paredges[1].a:=1;
  30. paredges[1].b:=2;
  31. paredges[1].c:=3;
  32. paredges[2].a:=2;
  33. paredges[2].b:=3;
  34. paredges[2].c:=5;
  35. paredges[3].a:=3;
  36. paredges[3].b:=4;
  37. paredges[3].c:=6;
  38. paredges[4].a:=1;
  39. paredges[4].b:=5;
  40. paredges[4].c:=5;
  41. paredges[5].a:=5;
  42. paredges[5].b:=4;
  43. paredges[5].c:=4;
  44.  
  45. weightmax:=1;
  46.  
  47. for I:=1 to 5 do weightmax:=weightmax + paredges[i].c;
  48.  
  49. for I:=1 to 5 do
  50. for J:=1 to 5 do
  51. parweight[I,J]:=weightmax;
  52.  
  53. parweight[1,2]:=3;
  54. parweight[2,1]:=3;
  55. parweight[2,3]:=5;
  56. parweight[3,2]:=5;
  57. parweight[3,4]:=6;
  58. parweight[4,3]:=6;
  59. parweight[4,5]:=4;
  60. parweight[5,4]:=4;
  61. parweight[1,5]:=5;
  62. parweight[5,1]:=5;
  63.  
  64. writeln;
  65. write('First node: ');
  66. readln(parfirst);
  67. write('Last node: ');
  68. readln(parlast);
  69. end;
  70.  
  71.  
  72. procedure calculate(parweight:matrix; var pardistance, parnode:array of integer;weightmax,parfirst,parlast:integer);
  73. var
  74. I,K,S,J,dist,Dmin,Kmin:integer;
  75. nodebis:array[1..5]of integer;
  76.  
  77. begin
  78. for I:=1 to 5 do begin
  79. parnode[i]:=0;
  80. pardistance[i]:=weightmax;
  81. end;
  82.  
  83. {for now, only the first node is in the list}
  84. parnode[parfirst]:=parfirst;
  85. pardistance[parfirst]:=0;
  86. S:=1;
  87. nodebis[1]:=parfirst;
  88.  
  89. repeat
  90. {new i: minimun distance}
  91. Kmin:=S;
  92. I:=nodebis[S];
  93. Dmin:=pardistance[i];
  94.  
  95. for K:=1 to S-1 do begin
  96. J:=nodebis[K];
  97. Dist:=pardistance[J];
  98.  
  99. if (Dist<Dmin) then begin
  100. Kmin:=K;
  101. I:=J;
  102. Dmin:=Dist;
  103. end;
  104. end;
  105.  
  106. if (I<>last) then begin
  107. nodebis[Kmin]:=nodebis[S];
  108. S:=S-1;
  109. {see other nodes around i}
  110. for J:=1 to 5 do
  111. if (pardistance[i] + parweight[I,J]<pardistance[J])then begin
  112. if (pardistance[J]=weightmax) then begin
  113. S:=S+1; {add j to the list}
  114. nodebis[S]:=J;
  115. end;
  116. {update}
  117. pardistance[J]:=pardistance[i]+parweight[I,J];
  118. parnode[J]:=I;
  119. end;
  120. end;
  121. until (I=parlast) or (S=0);
  122. end;
  123.  
  124.  
  125. procedure print(parnode:array of integer;parfirst,parlast:integer);
  126. var
  127. I,path:integer;
  128. pathnode:array[1..5]of integer;
  129.  
  130. begin
  131. path:=1;
  132. pathnode[1]:=parlast;
  133. I:=parlast;
  134.  
  135. while(I<>parfirst) do begin {print the nodes turning them}
  136. I:=parnode[i];
  137. path:=path+1;
  138. pathnode[path]:=I;
  139. end;
  140.  
  141. writeln;
  142. writeln('The nodes of the shortest path: ');
  143. For I:=path downto 1 do begin
  144. write(pathnode[i]);
  145. end;
  146. end;
  147.  
  148. begin
  149.  
  150. inicialize(edges,weight,first,last);
  151. calculate(weight,distance,node,weightmax,first,last);
  152. print(node,first,last);
  153. readln;
  154. end.
Reply With Quote  
Join Date: Oct 2006
Posts: 3
Reputation: luis_ramos is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
luis_ramos luis_ramos is offline Offline
Newbie Poster

Re: dijkstra algorithm

  #4  
May 26th, 2008
Not repeat the use of i, j , that cause the error
Reply With Quote  
Join Date: Oct 2006
Posts: 3
Reputation: luis_ramos is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
luis_ramos luis_ramos is offline Offline
Newbie Poster

Re: dijkstra algorithm

  #5  
May 26th, 2008
Not repeat the use of i, j , that cause the error
Reply With Quote  
Reply

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

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Pascal and Delphi Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Pascal and Delphi Forum

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