Advertisement
Guest User

Untitled

a guest
May 24th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. // Napon Krassner (Jett)
  2. // CSC 3430
  3. // HW 4
  4. // Description: Dijkstra's shortest path algorithm. The program is for adjacency matrix
  5. // representation of the graph.
  6.  
  7. #include <stdio.h>
  8. #include <limits.h>
  9.  
  10. // Number of vertices in the graph
  11. #define V 5
  12.  
  13. // Function declaration
  14. void dijkstra(int graph[V][V], int start);
  15. int minDistance(int dist[], bool sptSet[]);
  16. void printSolution(int dist[], int n, int parent[], int start);
  17. void printPath(int parent[], int j);
  18.  
  19.  
  20. // Main program with given graph to run dijkstra shortest path algorithm
  21. int main()
  22. {
  23. // Graph copied from provided GraphMatrix.txt
  24. int graph[V][V] = {
  25. { 0, 1, 0, 6, 5 },
  26. { 1, 0, 4, 0, 0 },
  27. { 0, 4, 0, 2, 7 },
  28. { 6, 0, 2, 0, 3 },
  29. { 5, 0, 7, 3, 0 },
  30. };
  31.  
  32. dijkstra(graph, 0);
  33.  
  34. return 0;
  35. }
  36.  
  37.  
  38. // Parameters: 2-D array of int - graph matrix
  39. // int - starting point
  40. // Return: nothing
  41. // Description: Funtion for Dijkstra's shortest path algorithm for a matrix graph.
  42. void dijkstra(int graph[V][V], int start)
  43. {
  44. int dist[V]; // The output array, shortest distance from start to i
  45. bool sptSet[V]; // true if vertex i in shortest path tree or shortest distance from start to i is finalized
  46. int parent[V]; // Parent array to store shortest path tree
  47.  
  48. // Initialize all distances as INFINITE and stpSet[] as false
  49. for (int i = 0; i < V; i++)
  50. {
  51. parent[start] = -1; // initialize starting point
  52. dist[i] = INT_MAX;
  53. sptSet[i] = false;
  54. }
  55.  
  56. // Distance of starting point from starting point is always 0
  57. dist[start] = 0;
  58.  
  59. // Find shortest path for all vertices
  60. for (int count = 0; count < V - 1; count++)
  61. {
  62. // Pick the minimum distance vertex from the set of
  63. // vertices not yet processed. u is always equal to starting point
  64. // in first iteration.
  65. int u = minDistance(dist, sptSet);
  66.  
  67. // Mark the picked vertex as processed
  68. sptSet[u] = true;
  69.  
  70. // Update dist value of the adjacent vertices of the
  71. // picked vertex.
  72. for (int v = 0; v < V; v++)
  73.  
  74. // Update dist[v] only if is not in sptSet, there is
  75. // an edge from u to v, and total weight of path from
  76. // starting point to v through u is smaller than current value of
  77. // dist[v]
  78. if (!sptSet[v] && graph[u][v] &&
  79. dist[u] + graph[u][v] < dist[v])
  80. {
  81. parent[v] = u;
  82. dist[v] = dist[u] + graph[u][v];
  83. }
  84. }
  85.  
  86. // print the constructed distance array
  87. printSolution(dist, V, parent, start);
  88. }
  89.  
  90.  
  91. // Parameters: int array - output array for shortest distance
  92. // bool array - shortest path tree
  93. // Return: int - index of minimum distance vertex
  94. // Description: function to find the vertex with minimum distance
  95. // value, from the set of vertices not yet included in shortest
  96. // path tree
  97. int minDistance(int dist[], bool sptSet[])
  98. {
  99. // Initialize min value
  100. int min = INT_MAX;
  101. int min_index; // used to return the index
  102.  
  103. for (int v = 0; v < V; v++)
  104. if (sptSet[v] == false && dist[v] <= min)
  105. min = dist[v], min_index = v;
  106.  
  107. return min_index;
  108. }
  109.  
  110.  
  111. // Parameters: int array - output array of distance for all vertices
  112. // int - number of vertices
  113. // int array - array of shortest path tree
  114. // Return: nothing
  115. // Description: Print the constructed distance array
  116. void printSolution(int dist[], int n, int parent[], int start)
  117. {
  118. printf("Vertex\t Distance from Source: %d", start);
  119. for (int i = 0; i < V; i++)
  120. {
  121. printf("\n%d \t\t %d\t\tPath: %d", i, dist[i], start);
  122. printPath(parent, i);
  123. }
  124. printf("\n");
  125. }
  126.  
  127.  
  128. // Parameters: int array - array of shortest path tree
  129. // int - end point to print
  130. // Return: nothing
  131. // Description: Function to print shortest path from source to j using parent array
  132. void printPath(int parent[], int j)
  133. {
  134. // Base Case : If j is source
  135. if (parent[j] == -1)
  136. return;
  137.  
  138. printPath(parent, parent[j]);
  139.  
  140. printf("-->%d", j);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement