|
| 1 | +#include <iostream> |
| 2 | +#include <limits.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +//Wrapper class for storing a graph |
| 8 | +class Graph |
| 9 | +{ |
| 10 | +public: |
| 11 | +int vertexNum; |
| 12 | +int **edges; |
| 13 | + |
| 14 | +//Constructs a graph with V vertices and E edges |
| 15 | +Graph(int V) |
| 16 | +{ |
| 17 | +this->vertexNum = V; |
| 18 | +this->edges = (int **)malloc(V * sizeof(int *)); |
| 19 | +for (int i = 0; i < V; i++) |
| 20 | +{ |
| 21 | +this->edges[i] = (int *)malloc(V * sizeof(int)); |
| 22 | +for (int j = 0; j < V; j++) |
| 23 | +this->edges[i][j] = INT_MAX; |
| 24 | +this->edges[i][i] = 0; |
| 25 | +} |
| 26 | +} |
| 27 | + |
| 28 | +//Adds the given edge to the graph |
| 29 | +void addEdge(int src, int dst, int weight) |
| 30 | +{ |
| 31 | +this->edges[src][dst] = weight; |
| 32 | +} |
| 33 | +}; |
| 34 | + |
| 35 | +//Utility function to print distances |
| 36 | +void print(int dist[], int V) |
| 37 | +{ |
| 38 | +cout << "\nThe Distance matrix for Floyd - Warshall" << endl; |
| 39 | +for (int i = 0; i < V; i++) |
| 40 | +{ |
| 41 | +for (int j = 0; j < V; j++) |
| 42 | +{ |
| 43 | + |
| 44 | +if (dist[i * V + j] != INT_MAX) |
| 45 | +cout << dist[i * V + j] << "\t"; |
| 46 | +else |
| 47 | +cout << "INF" |
| 48 | + << "\t"; |
| 49 | +} |
| 50 | +cout << endl; |
| 51 | +} |
| 52 | +} |
| 53 | + |
| 54 | +//The main function that finds the shortest path from a vertex |
| 55 | +//to all other vertices using Floyd-Warshall Algorithm. |
| 56 | +void FloydWarshall(Graph graph) |
| 57 | +{ |
| 58 | +int V = graph.vertexNum; |
| 59 | +int dist[V][V]; |
| 60 | + |
| 61 | +//Initialise distance array |
| 62 | +for (int i = 0; i < V; i++) |
| 63 | +for (int j = 0; j < V; j++) |
| 64 | +dist[i][j] = graph.edges[i][j]; |
| 65 | + |
| 66 | +//Calculate distances |
| 67 | +for (int k = 0; k < V; k++) |
| 68 | +//Choose an intermediate vertex |
| 69 | + |
| 70 | +for (int i = 0; i < V; i++) |
| 71 | +//Choose a source vertex for given intermediate |
| 72 | + |
| 73 | +for (int j = 0; j < V; j++) |
| 74 | +//Choose a destination vertex for above source vertex |
| 75 | + |
| 76 | +if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k] + dist[k][j] < dist[i][j]) |
| 77 | +//If the distance through intermediate vertex is less than direct edge then update value in distance array |
| 78 | +dist[i][j] = dist[i][k] + dist[k][j]; |
| 79 | + |
| 80 | +//Convert 2d array to 1d array for print |
| 81 | +int dist1d[V * V]; |
| 82 | +for (int i = 0; i < V; i++) |
| 83 | +for (int j = 0; j < V; j++) |
| 84 | +dist1d[i * V + j] = dist[i][j]; |
| 85 | + |
| 86 | +print(dist1d, V); |
| 87 | +} |
| 88 | + |
| 89 | +//Driver Function |
| 90 | +int main() |
| 91 | +{ |
| 92 | +int V, E; |
| 93 | +int src, dst, weight; |
| 94 | +cout << "Enter number of vertices: "; |
| 95 | +cin >> V; |
| 96 | +cout << "Enter number of edges: "; |
| 97 | +cin >> E; |
| 98 | +Graph G(V); |
| 99 | +for (int i = 0; i < E; i++) |
| 100 | +{ |
| 101 | +cout << "\nEdge " << i + 1 << "\nEnter source: "; |
| 102 | +cin >> src; |
| 103 | +cout << "Enter destination: "; |
| 104 | +cin >> dst; |
| 105 | +cout << "Enter weight: "; |
| 106 | +cin >> weight; |
| 107 | +G.addEdge(src, dst, weight); |
| 108 | +} |
| 109 | +FloydWarshall(G); |
| 110 | + |
| 111 | +return 0; |
| 112 | +} |
0 commit comments