Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. #include "Queue.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <deque>
  6.  
  7. using namespace std;
  8. using namespace cop4530;
  9.  
  10. int search(int* graph, int size, int s, int d);
  11.  
  12. //This is the airline program: it uses the queue class to create
  13. //a breadth-first search.
  14. int main(int argc, char* argv[])
  15. {
  16. ifstream infile;
  17. int size;
  18. int* graph;
  19. int s, d, sum;
  20. string* citynames;
  21. string selection = "y";
  22. string source;
  23. string destination;
  24. deque<int> path;
  25.  
  26. //Checks to see if there was a file passed in. If so, open it
  27. if(argc != 2)
  28. {
  29. cout << "Usage: project2 airline_file";
  30. return 0;
  31. }
  32. else
  33. {
  34. //Opens the file, gets everything out of it, closes it.
  35. infile.open(argv[1]);
  36. infile >> size;
  37. cout << size << " cities:" << endl;
  38.  
  39. //Gets rid of any left over whitespace; there somethimes is
  40. while(infile.peek() == '\n' || infile.peek() == ' ')
  41. infile.get();
  42.  
  43. //Creates an array for names and cost matrix, stored as a 1-d array
  44. graph = new int[size*size];
  45. citynames = new string[size];
  46.  
  47. for(int i = 0; i < size; i++)
  48. {//gets city names
  49. getline(infile, citynames[i], '\n');
  50. cout << " " << citynames[i] << endl;
  51. }
  52.  
  53. for(int i = 0; i < size*size; i++)//gets graph, puts it into 1-d matrix
  54. infile >> graph[i];
  55.  
  56. //Outputs the flight things...
  57. cout << "\nDirect flights between cities:\n" << "------------------------\n";
  58. for(int i = 0; i < size; i++)
  59. {
  60. cout << citynames[i] << ":" << endl;
  61. for(int j = 0; j < size; j++)
  62. if(graph[i*size + j] > 0)
  63. cout << " " << citynames[j] << ", $" << graph[i*size + j] << endl;
  64. }
  65. cout << "-----------------------\n";
  66.  
  67. infile.close();
  68. }
  69.  
  70. //Main Program loop
  71. while(selection != "n" && selection != "N")
  72. {
  73. path.clear(); //Clears the path so that it does not conflict with future queries
  74. cout << "\nSource City : ";
  75. getline(cin,source, '\n');
  76. cout << "Destination City : ";
  77. getline(cin,destination, '\n');
  78. cout << "finding min_hop route...\n";
  79.  
  80. s = -1; //Sets both destination and source to be non-true, then checks for truth
  81. d = -1;
  82. for(int i = 0; i < size; i++)
  83. {
  84. if(citynames[i] == source)
  85. s = i;
  86. if(citynames[i] == destination)
  87. d = i;
  88. }
  89.  
  90. //If destination or source is not a city, say so
  91. if(s == -1)
  92. cout << "\tpath not found, source city, " << source << ", not on map";
  93. else if(d == -1)
  94. cout << "\tpath not found, destination city, " << destination << ", not on map";
  95.  
  96. //If both destination and source are a city, then look for a path using a path finding function search
  97. if(s != -1 && d != -1)
  98. {
  99. //If source is destination, put only it on the deque
  100. if (s == d)
  101. path.push_back(s);
  102. else //puts the destination on the back
  103. path.push_back(d);
  104.  
  105. //Search returns the node pointing to the destination given, so if you call it over and over
  106. //with that pointing node as the new destination, it will give you the path. Not even close to
  107. //an efficient way of doing it but it works....plus I don't have to save EVERY SINGLE PATH i've
  108. //ever used in memory.
  109. while(s != d && d != -1)
  110. {
  111. d = search(graph, size, s, d);
  112. path.push_front(d);
  113. }
  114. if(d == -1)
  115. cout << "There is no path from " << source << " to " << destination;
  116. else
  117. {//Whole next segment shows the path and crap if there is one
  118. cout << "\n ";
  119. sum = 0;
  120. for(unsigned int i = 0; i < path.size(); i++)
  121. {//Ouputs the path and price of path
  122. if(i > 0)
  123. sum = sum + graph[path[i -1]*size + path[i]];
  124. cout << citynames[path[i]];
  125. if(i < path.size() - 1)
  126. cout << " -> ";
  127. else
  128. cout << ", $" << sum;
  129. }
  130. }
  131. }
  132.  
  133. do{//Repeats until user puts a correct choice
  134. cout << "\n\nSearch another route? (Y/N): ";
  135. cin >> selection;
  136. }while(selection != "y" && selection != "Y" && selection != "n" && selection != "N");
  137.  
  138. cin.get(); //Gets the somehow leftover newline at the end...
  139. }
  140.  
  141. delete graph;
  142. //delete citynames;
  143.  
  144. return 0;
  145. }
  146.  
  147. //Search using breadth first, returns the second to last element on the path
  148. int search(int* graph, int size, int s, int d)
  149. {
  150. //Base case, if source is destination, send it back
  151. if(s == d)
  152. return d;
  153.  
  154. //element is the node that is currently being visited
  155. int element;
  156. bool* visited = new bool[size];
  157. for(int i = 0; i < size; i++)
  158. visited[i] = 0;
  159.  
  160. //Path is the queue that performs the breadth first search.
  161. //Track keeps track of the second to last node that was visited on the path
  162. Queue<int> path;
  163. deque<int> track;
  164. track.clear();
  165.  
  166. //Adds source city to queue and marks as visited
  167. path.push(s);
  168. visited[s] = 1;
  169.  
  170. //Breadth first search based off of what we did in class
  171. while(!(path.empty()))
  172. {
  173. element = path.front();
  174. track.push_back(element); //Adds node to track, pops off after neighbors are visited
  175.  
  176. for(int i = 0; i < size; i++)
  177. {//Finds neighbors of current node, adds them to queue
  178. if(graph[element*size + i] > 0 && visited[i] == 0)
  179. {//If there is an unvisited node that is a neighbor, add it to queue and count it as visited
  180. visited[i] = 1;
  181. path.push(i);
  182.  
  183. if(i == d)
  184. {//If destination is found, return track that holds previous node
  185. path.pop();
  186. return track[track.size() - 1];
  187. //track.push_back(i);
  188. //break;
  189. }
  190. }
  191. }
  192. track.pop_back();
  193. path.pop();
  194. }
  195. return -1;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement