943,571 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 249
  • C++ RSS
Apr 29th, 2009
-1

This confuses me so much that I don't even know what to call the problem

Expand Post »
NOTE:This is a correction from my previous 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.  
  223. for(int i = 0; i < edgedata[value].edgecount; i++)
  224. {
  225. if(!done.in(edgedata[value].path[i].vertexto))
  226. {
  227. total = edgedata[value].path[i].weight + shortpath[value].distance;
  228.  
  229. if(total < shortpath[i].distance)
  230. {
  231. shortpath[i].distance = total;
  232. shortpath[i].via = value;
  233.  
  234. }
  235. }
  236. }
  237. }
  238.  
  239. //--------------------------------------------------------------------------
  240. // 'calculate' initialized the Set done, adds the starting vertex to done,
  241. // then performs a loop finding the next shortest path, adding it to the
  242. // set, and updating the shortpath table
  243. //--------------------------------------------------------------------------
  244.  
  245. void
  246. Paths::calculate (int addnode)
  247. {
  248. int loop;
  249.  
  250. done.initialize();
  251. done.add(addnode);
  252. updatepaths (addnode);
  253.  
  254. for (loop = 1; loop < VERTICES; loop++)
  255. {
  256. addnode = findshortest( );
  257. if (addnode != TOOLARGE)
  258. {
  259. done.add(addnode);
  260. updatepaths(addnode);
  261. }
  262. }
  263. }
  264.  
  265. //--------------------------------------------------------------------------
  266. // 'printpaths' displays a table showing the starting vertex then every
  267. // vertex, its final weight (distance) and how to get there (via)
  268. // this last information is in the Shortpath table
  269. //--------------------------------------------------------------------------
  270. void
  271. Paths::printpaths(int value)
  272. {
  273. cout << "Vertex: " << " " << "Distance: " << " " << "Via: " << endl;
  274. for(int i = 0; i < 5; i++)
  275. {
  276. cout << i+1 << " " << shortpath[i].distance << " " << shortpath[i].via+1 << endl;
  277. }
  278. }
  279.  
  280. //------------------------- main routine -------------------------
  281.  
  282. int
  283. main()
  284. {
  285. Paths findall;
  286. int startvertex, addnode;
  287. char response;
  288. char file[25];
  289.  
  290. do
  291. {
  292. cout << "Is the data being input from a file (y/n) ? ";
  293. cin >> response;
  294. if (tolower(response) == 'y')
  295. {
  296. cin.get();
  297. cout << "Please enter the filename : "<<flush;
  298. cin.getline(file, 25);
  299. findall.filldatafile(file);
  300. }
  301. else
  302. findall.filldata();
  303. findall.printdata();
  304. cout << "Is the data correct (y/n) ? ";
  305. cin >> response;
  306. }
  307. while (tolower(response) == 'n');
  308.  
  309. do
  310. {
  311. cout << "\nWhat is your starting vertex number? ";
  312. cin >> startvertex;
  313.  
  314. addnode = startvertex - 1;
  315. findall.initialize (addnode);
  316. findall.calculate (addnode);
  317.  
  318. findall.printpaths (startvertex);
  319.  
  320. cout << "\nWould you like to see the distances starting from another vertex (y/n) ? ";
  321. cin >> response;
  322. }
  323. while ( tolower(response) == 'y' );
  324.  
  325. system("pause");
  326. return 0;
  327. }

Here is the data being used:
3
2 5
3 3
5 2
3
1 3
3 2
4 1
2
2 1
4 3
0
3
2 6
3 10
4 7

Thanks!
Similar Threads
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: Difficult programming challenge
Next Thread in C++ Forum Timeline: throwing corba exception in c++





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


Follow us on Twitter


© 2011 DaniWeb® LLC