943,608 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 404
  • C++ RSS
Apr 29th, 2009
0

Problem with finding shortest path

Expand Post »
Hey,
So I am having this problem with this code. I think the problem is that shortpath[0].distance is being set to 0, and so never going into the if statement of updatepaths() therefore messing up the whole program. But I don't know why it is being set to 0.

Here is the code:


C++ Syntax (Toggle Plain Text)
  1. //*****************************************************************************************
  2. // This program finds the shortest path from one vertex of a 'dgraph' to all others.
  3. // It uses: a 'Set' to keep track of the shortest path vertices;
  4. // a table (of type Vertexinfo) that, for each vertex keeps track of its edges,
  5. // where each edge goes (to which vertex) and each edge's weight
  6. // - this information is supplied -;
  7. // a final table (of type Shortpath) that keeps track of each vertex's overall
  8. // weight (from the starting vertex) and how to get to it
  9. // - this information must be calculated and is the program's goal -.
  10. // filename: graph1.cpp
  11. // Author: Kimberlie Davis
  12. //*****************************************************************************************
  13.  
  14. #include <iostream>
  15. #include <fstream>
  16. #include <iomanip>
  17. #include <cctype>
  18. #include "sets.cpp"
  19.  
  20. int const VERTICES = 5;
  21. int const TOOLARGE = 30000;
  22.  
  23. using namespace std;
  24.  
  25. struct Pathinfo
  26. {
  27. int vertexto;
  28. int weight;
  29. };
  30.  
  31. struct Vertexinfo
  32. {
  33. int edgecount;
  34. Pathinfo path[VERTICES - 1];
  35. };
  36.  
  37. struct Shortpath
  38. {
  39. int distance;
  40. int via;
  41. };
  42.  
  43.  
  44. class Paths
  45. {
  46. private:
  47. Vertexinfo edgedata[VERTICES];
  48. Shortpath shortpath[VERTICES];
  49. Set done;
  50. public:
  51. void filldata(void);
  52. void filldatafile(char []);
  53. void printdata(void);
  54. void initialize(int);
  55. int findshortest(void);
  56. void updatepaths(int);
  57. void printpaths(int);
  58. void calculate(int);
  59. };
  60.  
  61. //--------------------------------------------------------------------------
  62. // 'filldata' for each vertex, receives a count of the number of edges,
  63. // then for each edge finds out where it goes (vertex to) and the weight.
  64. //
  65. // A problem exists in that the user names the vertices from 1 up, but
  66. // they will be used as subscripts (which start at 0); so 1 is subtracted
  67. // from the vertex number being input
  68. //--------------------------------------------------------------------------
  69.  
  70. void
  71. Paths::filldata ( )
  72. {
  73. int vertex, edge;
  74.  
  75. cout << "For each vertex first enter the number of edges to it\n";
  76. cout << "then for each edge input the vertex it goes to and its weight\n\n";
  77.  
  78. for (vertex = 0; vertex < VERTICES; vertex++)
  79. {
  80. cout << " For vertex number " << vertex+1 << '\n';
  81. cout << " How many edges : ";
  82. cin >> edgedata[vertex].edgecount;
  83.  
  84. for (edge = 0; edge < edgedata[vertex].edgecount; edge++)
  85. {
  86. cout << " Edge number " << edge+1 << '\n';
  87. cout << " To vertex # ";
  88. cin >> edgedata[vertex].path[edge].vertexto;
  89. edgedata[vertex].path[edge].vertexto--; // <---- turn into subscript
  90. cout << " Weight ";
  91. cin >> edgedata[vertex].path[edge].weight;
  92. }
  93. }
  94. }
  95.  
  96. //--------------------------------------------------------------------------
  97. // 'filldatafile' for each vertex, receives a count of the number of edges,
  98. // then for each edge finds out where it goes (vertex to) and the weight.
  99. // the data comes from a file, the filename is asked of the user
  100.  
  101. // A problem exists in that the user creates the file using vertices
  102. // numbered from 1 up, but they will be used as subscripts (which start at 0)
  103. // so 1 is subtracted from the vertex number being input
  104. //--------------------------------------------------------------------------
  105.  
  106. void
  107. Paths::filldatafile (char filename[])
  108. {
  109. int vertex, edge;
  110. ifstream infile;
  111.  
  112. infile.open(filename);
  113. if (!infile.fail())
  114. {
  115. for (vertex = 0; vertex < VERTICES; vertex++)
  116. {
  117. infile >> edgedata[vertex].edgecount;
  118.  
  119. for (edge = 0; edge < edgedata[vertex].edgecount; edge++)
  120. {
  121. infile >> edgedata[vertex].path[edge].vertexto;
  122. edgedata[vertex].path[edge].vertexto--; // <---- turn into subscript
  123. infile >> edgedata[vertex].path[edge].weight;
  124. }
  125. }
  126. }
  127. else
  128. {
  129. for (vertex = 0; vertex < VERTICES; vertex++)
  130. edgedata[vertex].edgecount = 0;
  131. }
  132.  
  133. }
  134.  
  135. //--------------------------------------------------------------------------
  136. // 'printdata' displays the entered vertex information so the user can make
  137. // sure the data is entered correctly. To compensate for the vertices
  138. // being used as subscripts, 1 is added to the vertex display. 1 is also
  139. // added to the edge for display since edge is also used as a subscript
  140. //--------------------------------------------------------------------------
  141.  
  142. void
  143. Paths::printdata ( )
  144. {
  145. int vertex, edge;
  146.  
  147. cout << "\nThe following data was input\n\n";
  148.  
  149.  
  150. for (vertex = 0; vertex < VERTICES; vertex++)
  151. {
  152. cout << " Vertex number " << vertex+1 << " ";
  153. cout << edgedata[vertex].edgecount <<" edges\n";
  154.  
  155. for (edge = 0; edge < edgedata[vertex].edgecount; edge++)
  156. {
  157. cout << " Edge number " << edge+1 ;
  158. cout << " To vertex # " << edgedata[vertex].path[edge].vertexto+1;;
  159. cout << " Weight " << edgedata[vertex].path[edge].weight << '\n';
  160. }
  161.  
  162. cout << '\n';
  163.  
  164. }
  165. }
  166.  
  167. //--------------------------------------------------------------------------
  168. // 'initialize' takes the starting vertex and stores it as the the 'via'
  169. // for each vertex. It also sets the distance to a very large number;
  170. // except the starting vertex where the distance is set to 0
  171. //--------------------------------------------------------------------------
  172.  
  173. void
  174. Paths::initialize (int startnode)
  175. {
  176. int i;
  177. for (i = 0; i < VERTICES; i++)
  178. {
  179. shortpath[i].distance = TOOLARGE;
  180. shortpath[i].via = startnode;
  181. }
  182.  
  183. shortpath[startnode].distance = 0;
  184. }
  185.  
  186.  
  187.  
  188. //--------------------------------------------------------------------------
  189. // 'findshortest' searches the distance/via table for the smallest weight
  190. // of a vertex that hasn't yet been completed (i.e. not in the Set)
  191. //--------------------------------------------------------------------------
  192.  
  193. int
  194. Paths::findshortest ( )
  195. {
  196. int vertex, smalldist, smallvertex;
  197.  
  198. smalldist = TOOLARGE;
  199. smallvertex = TOOLARGE;
  200.  
  201. for (vertex = 0; vertex < VERTICES; vertex++)
  202. if (!done.in(vertex) && shortpath[vertex].distance < smalldist)
  203. {
  204. smalldist = shortpath[vertex].distance;
  205. smallvertex = vertex;
  206. }
  207.  
  208. return smallvertex;
  209. }
  210.  
  211. //--------------------------------------------------------------------------
  212. // 'updatepaths' uses the newest vertex which was added to the Set to modify
  213. // the distances of the remaining vertices (if smaller)
  214. // in addition to the newly added vertex, it uses the Set, the Vertexinfo
  215. // and the Shortpath tables
  216. //--------------------------------------------------------------------------
  217. void
  218. Paths::updatepaths(int value)
  219. {
  220. int total;
  221. int subscript;
  222. for(int i = 0; i < edgedata[value].edgecount; i++)
  223. {
  224. if(!done.in(edgedata[value].path[i].vertexto))
  225. {
  226. total = edgedata[value].path[i].weight + shortpath[value].distance;
  227.  
  228. if(total < shortpath[i].distance)
  229. {
  230. shortpath[i].distance = total;
  231. shortpath[i].via = value;
  232.  
  233. }
  234. }
  235. }
  236. }
  237.  
  238. //--------------------------------------------------------------------------
  239. // 'calculate' initialized the Set done, adds the starting vertex to done,
  240. // then performs a loop finding the next shortest path, adding it to the
  241. // set, and updating the shortpath table
  242. //--------------------------------------------------------------------------
  243.  
  244. void
  245. Paths::calculate (int addnode)
  246. {
  247. int loop;
  248.  
  249. done.initialize();
  250. done.add(addnode);
  251. updatepaths (addnode);
  252.  
  253. for (loop = 1; loop < VERTICES; loop++)
  254. {
  255. addnode = findshortest( );
  256. if (addnode != TOOLARGE)
  257. {
  258. done.add(addnode);
  259. updatepaths(addnode);
  260. }
  261. }
  262. }
  263.  
  264. //--------------------------------------------------------------------------
  265. // 'printpaths' displays a table showing the starting vertex then every
  266. // vertex, its final weight (distance) and how to get there (via)
  267. // this last information is in the Shortpath table
  268. //--------------------------------------------------------------------------
  269. void
  270. Paths::printpaths(int value)
  271. {
  272. cout << "Vertex: " << " " << "Distance: " << " " << "Via: " << endl;
  273. for(int i = 0; i < 5; i++)
  274. {
  275. cout << i+1 << " " << shortpath[i].distance << " " << shortpath[i].via+1 << endl;
  276. }
  277. }
  278.  
  279. //------------------------- main routine -------------------------
  280.  
  281. int
  282. main()
  283. {
  284. Paths findall;
  285. int startvertex, addnode;
  286. char response;
  287. char file[25];
  288.  
  289. do
  290. {
  291. cout << "Is the data being input from a file (y/n) ? ";
  292. cin >> response;
  293. if (tolower(response) == 'y')
  294. {
  295. cin.get();
  296. cout << "Please enter the filename : "<<flush;
  297. cin.getline(file, 25);
  298. findall.filldatafile(file);
  299. }
  300. else
  301. findall.filldata();
  302. findall.printdata();
  303. cout << "Is the data correct (y/n) ? ";
  304. cin >> response;
  305. }
  306. while (tolower(response) == 'n');
  307.  
  308. do
  309. {
  310. cout << "\nWhat is your starting vertex number? ";
  311. cin >> startvertex;
  312.  
  313. addnode = startvertex - 1;
  314. findall.initialize (addnode);
  315. findall.calculate (addnode);
  316.  
  317. findall.printpaths (startvertex);
  318.  
  319. cout << "\nWould you like to see the distances starting from another vertex (y/n) ? ";
  320. cin >> response;
  321. }
  322. while ( tolower(response) == 'y' );
  323.  
  324. system("pause");
  325. return 0;
  326. }
Similar Threads
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Apr 29th, 2009
0

Re: Problem with finding shortest path

But I don't know why it is being set to
> Did you actually write this code yourself?

> If yes, you could try using a debugger

> BTW, Avoid the use of system("PAUSE"); (look at this excellent information written by WaltP )
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 29th, 2009
0

Re: Problem with finding shortest path

I wrote some of it. I did use a debugger, and still couldn't figure it out. I got it down to shortpath[0] is being set to 0 in the updatepaths() method though.
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008

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: SpecialFolder::System
Next Thread in C++ Forum Timeline: Binary Files and Seekg





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


Follow us on Twitter


© 2011 DaniWeb® LLC