Guest User

Untitled

a guest
Dec 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3. #include "linked-list.h"
  4. using namespace std;
  5.  
  6. // infinity means there is no path!
  7. const int INFINITY = 999999999;
  8.  
  9. template <int N>
  10. class Graph {
  11. public:
  12. Graph( ) {
  13. count = 0;
  14. }
  15.  
  16. // insert an edge (both directions)
  17. void insertEdge(const char fromcity[], const char tocity[], int cost) {
  18. int from = lookup(fromcity);
  19. int to = lookup(tocity);
  20.  
  21. Edge e;
  22. e.to = to;
  23. e.cost = cost;
  24. nodes[from].edges.addAtEnd(e);
  25. e.to = from;
  26. nodes[to].edges.addAtEnd(e);
  27. }
  28.  
  29. // return the cost of an edge or 0 for none
  30. int getCost(int from, int to) const {
  31. Edge e;
  32. e.to = to;
  33. e.cost = INFINITY;
  34. return nodes[from].edges.find(e).cost;
  35. }
  36.  
  37. // add a node
  38. void insertNode(const char name[]) {
  39. strcpy(nodes[count].data, name);
  40. count++;
  41. }
  42.  
  43. // return the node index of a city name
  44. int lookup(const char name[]) const {
  45. for(int i = 0; i < count; i++) {
  46. if(strcmp(nodes[i].data, name) == 0) {
  47. return i;
  48. }
  49. }
  50.  
  51. return -1;
  52. }
  53.  
  54. // return the name of a node index
  55. const char* name(int index) const {
  56. return nodes[index].data;
  57. }
  58.  
  59. private:
  60. // an edge has a destination and cost
  61. struct Edge {
  62. int to, cost;
  63.  
  64. bool operator==(const Edge& other) {
  65. return to == other.to;
  66. }
  67. };
  68.  
  69. // a node has some data and a list of edges
  70. struct Node {
  71. char data[100];
  72. LinkedList<Edge> edges;
  73. };
  74.  
  75. // the graph is an array of nodes
  76. Node nodes[N];
  77. int count;
  78. };
  79.  
  80. // use Dijkstra's algorithm to find a shortest path
  81. template<int N>
  82. int shortestPath(const Graph<N>& graph, const char startcity[], const char endcity[]) {
  83. // find the city names indices
  84. int start = graph.lookup(startcity);
  85. int end = graph.lookup(endcity);
  86.  
  87. // make an array of distances from start to the given node
  88. int tentative[N];
  89.  
  90. // set all distances to infinity except start which is 0
  91. for(int i = 0; i < N; i++) {
  92. tentative[i] = INFINITY;
  93. }
  94. tentative[start] = 0;
  95.  
  96. // make an array of which nodes we've seen
  97. bool visited[N];
  98. for(int i = 0; i < N; i++) {
  99. visited[i] = false;
  100. }
  101.  
  102. // start at the start node
  103. int current = start;
  104.  
  105. // loop until all nodes are visited
  106. while(current != -1) {
  107. cout << "Considering " << graph.name(current) << endl;
  108. // for each neighbor of current
  109. for(int i = 0; i < N; i++) {
  110. // if it's connected
  111. if(graph.getCost(current, i) < INFINITY) {
  112. // calculate the distance from start to i via current
  113. int distance = tentative[current] + graph.getCost(current, i);
  114.  
  115. // if this distance is less than the tentative distance from start to i we have, replace it
  116. // also mark i as unvisited since we now have to reconsider it
  117. if(distance < tentative[i]) {
  118. cout << "Distance from " << graph.name(start) << " to " << graph.name(i) << " is " << distance << endl;
  119. tentative[i] = distance;
  120. visited[i] = false;
  121. }
  122. }
  123. }
  124.  
  125. // mark current node as visited
  126. visited[current] = true;
  127. cout << endl;
  128.  
  129. // set current node to unvisited one with the smallest cost
  130. int min = INFINITY;
  131. current = -1;
  132. for(int i = 0; i < N; i++) {
  133. if(!visited[i] && (tentative[i] < min)) {
  134. min = tentative[i];
  135. current = i;
  136. }
  137. }
  138. }
  139.  
  140. // we now have the shortest path to ecery node in the graph
  141. // return the one we were interested in!
  142. return tentative[end];
  143. }
  144.  
  145. int main( ) {
  146. // create a graph with the cities
  147. Graph<11> graph;
  148. graph.insertNode("Alexandria");
  149. graph.insertNode("Blacksburg");
  150. graph.insertNode("Charlottesville");
  151. graph.insertNode("Danville");
  152. graph.insertNode("Fredericksburg");
  153. graph.insertNode("Harrisonburg");
  154. graph.insertNode("Lynchburg");
  155. graph.insertNode("Newport News");
  156. graph.insertNode("Richmond");
  157. graph.insertNode("Roanoke");
  158. graph.insertNode("Virginia Beach");
  159.  
  160. graph.insertEdge("Alexandria", "Fredericksburg", 50);
  161. graph.insertEdge("Fredericksburg", "Richmond", 60);
  162. graph.insertEdge("Richmond", "Charlottesville", 70);
  163. graph.insertEdge("Richmond", "Lynchburg", 110);
  164. graph.insertEdge("Richmond", "Danville", 145);
  165. graph.insertEdge("Richmond", "Newport News", 70);
  166. graph.insertEdge("Newport News", "Virginia Beach", 35);
  167. graph.insertEdge("Virginia Beach", "Danville", 210);
  168. graph.insertEdge("Danville", "Lynchburg", 70);
  169. graph.insertEdge("Lynchburg", "Charlottesville", 70);
  170. graph.insertEdge("Lynchburg", "Roanoke", 65);
  171. graph.insertEdge("Roanoke", "Blacksburg", 40);
  172. graph.insertEdge("Blacksburg", "Harrisonburg", 140);
  173. graph.insertEdge("Harrisonburg", "Alexandria", 135);
  174. graph.insertEdge("Harrisonburg", "Charlottesville", 50);
  175.  
  176. cout << endl << "Distance is " << shortestPath(graph, "Alexandria", "Danville") << endl;
  177. graph.insertEdge("Fredericksburg", "Richmond", 40); //<-- attempt at changing the value from 60 to 40//
  178. cout << endl << "Distance is " << shortestPath(graph, "Alexandria", "Danville") << endl;// the output is still the same,and the edge weight did not change//
  179.  
  180. return 0;
  181. }
Add Comment
Please, Sign In to add comment