Guest User

Untitled

a guest
Dec 14th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. // C Program for Floyd Warshall Algorithm
  2. #include<stdio.h>
  3.  
  4. // Number of vertices in the graph
  5. #define V 4
  6.  
  7. /* Define Infinite as a large enough value. This value will be used
  8. for vertices not connected to each other */
  9. #define INF 99999
  10.  
  11. // A function to print the solution matrix
  12. void printSolution(int dist[][V]);
  13.  
  14. // Solves the all-pairs shortest path problem using Floyd Warshall algorithm
  15. void floydWarshall (int graph[][V])
  16. {
  17. /* dist[][] will be the output matrix that will finally have the shortest
  18. distances between every pair of vertices */
  19. int dist[V][V], i, j, k;
  20.  
  21. /* Initialize the solution matrix same as input graph matrix. Or
  22. we can say the initial values of shortest distances are based
  23. on shortest paths considering no intermediate vertex. */
  24. for (i = 0; i < V; i++)
  25. for (j = 0; j < V; j++)
  26. dist[i][j] = graph[i][j];
  27.  
  28. /* Add all vertices one by one to the set of intermediate vertices.
  29. ---> Before start of a iteration, we have shortest distances between all
  30. pairs of vertices such that the shortest distances consider only the
  31. vertices in set {0, 1, 2, .. k-1} as intermediate vertices.
  32. ----> After the end of a iteration, vertex no. k is added to the set of
  33. intermediate vertices and the set becomes {0, 1, 2, .. k} */
  34. for (k = 0; k < V; k++)
  35. {
  36. // Pick all vertices as source one by one
  37. for (i = 0; i < V; i++)
  38. {
  39. // Pick all vertices as destination for the
  40. // above picked source
  41. for (j = 0; j < V; j++)
  42. {
  43. // If vertex k is on the shortest path from
  44. // i to j, then update the value of dist[i][j]
  45. if (dist[i][k] + dist[k][j] < dist[i][j])
  46. dist[i][j] = dist[i][k] + dist[k][j];
  47. }
  48. }
  49. }
  50.  
  51. // Print the shortest distance matrix
  52. printSolution(dist);
  53. }
  54.  
  55. /* A utility function to print solution */
  56. void printSolution(int dist[][V])
  57. {
  58. printf ("Following matrix shows the shortest distances"
  59. " between every pair of vertices \n");
  60. for (int i = 0; i < V; i++)
  61. {
  62. for (int j = 0; j < V; j++)
  63. {
  64. if (dist[i][j] == INF)
  65. printf("%7s", "INF");
  66. else
  67. printf ("%7d", dist[i][j]);
  68. }
  69. printf("\n");
  70. }
  71. }
  72.  
  73. // driver program to test above function
  74. int main()
  75. {
  76. /* Let us create the following weighted graph
  77. 10
  78. (0)------->(3)
  79. | /|\
  80. 5 | |
  81. | | 1
  82. \|/ |
  83. (1)------->(2)
  84. 3 */
  85. int graph[V][V] = { {0, 5, INF, 10},
  86. {INF, 0, 3, INF},
  87. {INF, INF, 0, 1},
  88. {INF, INF, INF, 0}
  89. };
  90.  
  91. // Print the solution
  92. floydWarshall(graph);
  93. return 0;
  94. }
Add Comment
Please, Sign In to add comment