Advertisement
Guest User

Untitled

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