-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDP-FloydsAlgorithm.cpp
63 lines (60 loc) · 1 KB
/
DP-FloydsAlgorithm.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;
int main()
{
const int INF = 1e9;
int n;
cout<<"Input the number of vertices : ";
cin>>n;
int graph[n][n],dist[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i!=j)
graph[i][j]=INF;
else
graph[i][j]=0;
}
}
cout<<"Input the number of edges of directed graph : ";
int e;
cin>>e;
for(int i=0;i<e;i++)
{
int a,b;
cout<<"Enter the 2 vertices of an edge(start to end) : ";
cin>>a>>b;
cout<<"Enter the weight for graph["<<a<<"]["<<b<<"] : ";
cin>>graph[a][b];
}
cout<<"WEIGHTED ADJACENCY MATRIX IS : "<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
dist[i][j]=graph[i][j];
cout<<graph[i][j]<<" ";
}
cout<<endl;
}
for(int k=0;k<n;k++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
dist[i][j] = min(dist[i][j] , dist[i][k] + dist[k][j]);
}
}
}
cout<<"\nSHORTEST PATH MATRIX IS : "<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<dist[i][j]<<" ";
}
cout<<endl;
}
}