-
Notifications
You must be signed in to change notification settings - Fork 0
/
All pairs shortest path.CPP
77 lines (73 loc) · 1.18 KB
/
All pairs shortest path.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*All pairs shortest path*/
#include<iostream.h>
#include<conio.h>
int c[10][10],kay[10][10],adj[10][10],n;
void apsp();
void shortest(int ,int) ;
void outputpath(int ,int);
void findckay();
void main()
{
clrscr();
cout<<"enter no of vertices"<<endl;
cin>>n;
cout<<"enter adjacency matrix"<<endl;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>adj[i][j];
c[i][j]=adj[i][j];
kay[i][j]=0;
}
apsp();
getch();
}
void apsp()
{
findckay();
cout<<"All pair shortest paths :"<<endl;
for(int i=1;i<=n;i++)
{
getch();
cout<<endl<<"Shortest path from vertex"<<i;
for(int j=1;j<=n;j++)
shortest(i,j);
}
}
void findckay()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++)
{
int t1=c[i][k];
int t2=c[k][j];
int t3=c[i][j];
if(t1!=0&&t2!=0&&(t3==0||t1+t2<t3))
{
c[i][j]=t1+t2;
kay[i][j]=k;
}
}
}
void shortest(int i,int j)
{
if(c[i][j]==0)
{
cout<<"\n There is no path from "<<i<<"to"<<j;
return;
}
cout<<endl<<i<<" ";
outputpath(i,j);
}
void outputpath(int i,int j)
{
if(i==j)
return;
if(kay[i][j]==0)
cout<<j<<" ";
else
{ outputpath(i,kay[i][j]);
outputpath(kay[i][j],j);
}
}