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 363,783 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 4,519 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:
Dec 17th, 2004
Views: 3,710
This is a program I just recently completed for a computer science course. Given a 2D grid, where every path between two gridpoints has a weight (path length) associated with it, this program computes the shortest path from (1,1) to (m,n). You are only allowed to move up and to the right (so no, it's not Dijstra.)
Last edited : Dec 24th, 2006.
cplusplus Syntax | 4 stars
  1. // DANI HOROWITZ
  2. // NOVEMBER 2004
  3.  
  4. /***************************************************************************/
  5. /***************************************************************************/
  6.  
  7. // CSC 155 Project 2 Dynamic Programming
  8. // Assigned 11/02/2004
  9. // Due : 11/16/2004
  10. //
  11. //
  12.  
  13. #include <iostream.h>
  14. int *Rectangular_Shortest_Path(int , int , int , int , int[][6], int[][6]);
  15.  
  16. int *Rectangular_Shortest_Path(int startx, int starty, int endx, int endy,
  17. int r[][6], int a[][6])
  18. {
  19. // I could find no easy way to dynamically create a 2D array in C++
  20. int distance[10][10]; // keep track of distances to get to each point
  21. int path[10][10]; // keep track of the best paths
  22.  
  23. /***************************************************************************/
  24. /***************************************************************************/
  25.  
  26. int size = (endx-startx)+(endy-starty); // size of our shortest path array
  27. int* shortest_path = new int[size]; // it will take 8 steps to get from (0,0)->(5,5)
  28. cout << "It will take " << size << " steps" << endl;
  29.  
  30. /***************************************************************************/
  31. /***************************************************************************/
  32.  
  33. // populate our starting place
  34. distance[startx][starty] = 0;
  35.  
  36. // populate bottom distance row
  37. for (int x=startx+1; x<=endx; x++)
  38. {
  39. distance[x][starty] = distance[x-1][starty] + r[x-1][starty];
  40. cout << "We went right to get to (" << x << "," << starty << ") in " << distance[x][starty] << " units" << endl;
  41. }
  42.  
  43. // populate leftmost distance row
  44. for (int y=starty+1; y<=endy; y++)
  45. {
  46. distance[startx][y] = distance[startx][y-1] + a[startx][y-1];
  47. cout << "We went up to get to (" << startx << "," << y << ") in " << distance[startx][y] << " units" << endl;
  48. }
  49.  
  50. /***************************************************************************/
  51. /***************************************************************************/
  52.  
  53. cout << endl;
  54. for (int y=starty+1; y<=endy; y++) // go down each column
  55. {
  56. for (int x=startx+1; x<=endx; x++) // go across each row
  57. {
  58. int distance_up = distance[x][y-1] + a[x][y-1]; // calculate weight to get to (x,y) from coordinate below it
  59. int distance_right = distance[x-1][y] + r[x-1][y]; // calculate weight to get to (x,y) from coordinate to left of it
  60. if (distance_up < distance_right) // it's better to move up
  61. {
  62. distance[x][y] = distance_up; // populate our distance array
  63. path[x][y] = 1; // populate our path array
  64. cout << "If we go up to get to (" << x << "," << y << ") we can do it in a weight of " << distance[x][y] << " units" << endl;
  65. }
  66. else // it's better to move right
  67. {
  68. distance[x][y] = distance_right; // populate our distance array
  69. path[x][y] = 0; // populate our path array
  70. cout << "If we go right to get to (" << x << "," << y << ") we can do it in a weight of " << distance[x][y] << " units" << endl;
  71. }
  72. }
  73. }
  74.  
  75. /***************************************************************************/
  76. /***************************************************************************/
  77.  
  78. x = endx;
  79. y = endy;
  80. int n=0;
  81. cout << endl << "Working backwards ..." << endl;
  82. // now that we have all of the smaller problems solved to calculate the optimal weight to get to every point on the grid,
  83. // let's figure out how to get from (5,5)->(1,1)
  84.  
  85. while (x >= startx && y >= starty) // loop so long as both coordinates are >= 1
  86. {
  87. if (x > startx && y > starty)
  88. {
  89. // we are not working in the first row or first column,
  90. // which means that we have an opportunity of going left or down, whichever is best
  91.  
  92. if ((distance[x-1][y]+r[x-1][y]) < (distance[x][y-1]+a[x][y-1]))
  93. {
  94. cout << "Go left from (" << x << "," << y << ") for " << distance[x][y] << " units left" << endl;
  95. x--; // move one unit left
  96. }
  97. else
  98. {
  99. cout << "Go down from (" << x << "," << y << ") for " << distance[x][y] << " units left" << endl;
  100. y--; // move one unit down
  101. }
  102. }
  103. else
  104. {
  105. if (x == startx) // we are somewhere in the left column
  106. {
  107. // we don't have a choice, we can only move down
  108. cout << "Go down from (" << x << "," << y << ") for " << distance[x][y] << " units left" << endl;
  109. y--;
  110. }
  111. else // we are somewhere in the bottom row
  112. {
  113. // we don't have a choice, we can only move left
  114. cout << "Go left from (" << x << "," << y << ") for " << distance[x][y] << " units left" << endl;
  115. x--;
  116. }
  117. }
  118. shortest_path[n] = path[x][y]; // populate our shortest path array - this line has an error ??
  119. n++;
  120. }
  121.  
  122. cout << endl;
  123.  
  124. /***************************************************************************/
  125. /***************************************************************************/
  126.  
  127. return shortest_path; // return the shortest path BACKWARDS!
  128.  
  129. }
  130.  
  131.  
  132. int main()
  133. {
  134.  
  135. int Right[6][6]; // entry Right[i][j] contains the link distance
  136. // between grid point (i,j) to (i+1, j);
  137. int Above[6][6]; // entry Above[i][j] contains the link distance
  138. // between grid point (i,j) to (i, j+1);
  139.  
  140. int *shortest_path; // the array for storing the shortest path
  141.  
  142. //
  143. // read the inputs for the 2-dimensional arrays Right and Above
  144. //
  145.  
  146. for (int i=1; i< 6; i++)
  147. {
  148. for (int j=1; j < 6; j++)
  149. {
  150. cout << "Please enter the link distance : Right[" << i << "," << j << "]\n";
  151. cin >> Right[i][j];
  152. cout << "Please enter the link distance : Above[" << i << "," << j << "]\n";
  153. cin >> Above[i][j];
  154. }
  155.  
  156. }
  157. shortest_path = Rectangular_Shortest_Path(1,1,5,5, Right, Above);
  158.  
  159. return (0);
  160. }
Comments (Newest First)
cscgal | The Queen of DaniWeb | Sep 20th, 2005
I just looked it up to double check ... Dijstra is undirected and weighted. This algorithm provided is directed and weighted.
1o0oBhP | Posting Pro in Training | Dec 31st, 2004
Sorry to be picky Dijstra isnt the matrix algorithm with weights - it is primm's matrix algorithm :p
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 10:44 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC