-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdirected acyclic graph (shortest path)
79 lines (77 loc) · 1.61 KB
/
directed acyclic graph (shortest path)
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
78
79
#include<iostream>
#include<vector>
#include<list>
using namespace std;
// taking graph
int arr[5][5]={{0,10,0,5,0},{0,0,1,2,0},{0,0,0,0,4},{0,3,9,0,2},{7,0,6,0,0}};
list<int> r;
int time=0;
void dfs(int i,int stime[],int ftime[],int parent[],int color[]);
int main()
{
int v;
cout<<"enter number of vertices\n";
cin>>v;
int stime[v],ftime[v],parent[v],color[v];
int i,j;
int key[v];
// taking three arrays color ,parent,key
for(i=0;i<v;i++)
{
key[i]=INT_MAX;
}
// key denotes shortest path from source vertex
key[0]=0;
// denoting color to denote status of anode
for(i=0;i<v;i++)
{
color[i]=0;
parent[i]=-1;
}
// parent denotes the parent of a node
for(i=0;i<v;i++)
{
if(color[i]==0)
{
dfs(i,stime,ftime,parent,color);
}
}
list<int>::iterator it;
for(it=r.begin();it!=r.end();it++)
{
for(i=0;i<v;i++)
{
if(arr[*it][i]!=0)
{
if(key[i]>key[*it]+arr[*it][i])
{
key[i]=key[*it]+arr[*it][i];
}
}
}
}
// printing shortest path
for(i=1;i<v;i++)
{
cout<<key[i]<<endl;
}
}
// implementing depth first search
void dfs(int i,int stime[],int ftime[],int parent[],int color[])
{
time++;
color[i]=1;
stime[i]=time;
int j;
for(j=0;j<5;j++)
{
if(arr[i][j]!=0 && color[j]==0)
{
dfs(j,stime,ftime,parent,color);
}
}
time++;
ftime[i]=time;
color[i]=2;
r.push_front(i);
}