-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarshall_algorithm.cpp
More file actions
50 lines (41 loc) · 1.22 KB
/
warshall_algorithm.cpp
File metadata and controls
50 lines (41 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
using namespace std;
#define MAX 1000000000
int main() {
cout << "Enter number of vertices : ";
int v;
cin >> v;
int matrix[v][v];
cout << "Enter the matrix : \n";
int shortest_path_matrix[v][v];
for(int i = 0; i < v; i++) {
for(int j = 0; j < v; j++) {
cin >> matrix[i][j];
if(matrix[i][j] == 0) shortest_path_matrix[i][j] = MAX;
else shortest_path_matrix[i][j] = matrix[i][j];
}
}
// calculate shortest path matrix :
for(int k = 0; k < v; k++) {
cout << endl << endl;
for(int i = 0; i < v; i++) {
for(int j = 0; j < v; j++) {
cout << shortest_path_matrix[i][j] << " ";
}
cout << endl;
}
for(int i = 0; i < v; i++) {
for(int j = 0; j < v; j++) {
shortest_path_matrix[i][j] = min(shortest_path_matrix[i][j], (shortest_path_matrix[i][k]+shortest_path_matrix[k][j]));
}
}
}
cout << "\nShortest path matrix : \n";
for(int i = 0; i < v; i++) {
for(int j = 0; j < v; j++) {
cout << shortest_path_matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}