-
Notifications
You must be signed in to change notification settings - Fork 0
/
29.graphDfs.cpp
114 lines (99 loc) · 1.84 KB
/
29.graphDfs.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//bfs
//adjacency matrix
//queue
//path from source to destination
#include<iostream>
#include<cstdio>
#define MAX 100
int adj[MAX][MAX];
int vertex;
int color[MAX];
int parent[MAX];
int discovery[MAX];
int finish[MAX];
#define NIL -1
int time=0;
using namespace std;
void output(int n){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
cout<<adj[i][j]<<" ";
}
cout<<"\n";
}
}
void input(int n){
int v1,v2,wt,edge;
cout<<"\nNo edges: ";
cin>>edge;
while(edge--){
cin>>v1>>v2;
adj[v1][v2]=1;
}
}
void displayState(int n){
int i,j;
cout<<"\n color: ";
for(i=0;i<n;i++){
cout<<color[i]<<" ";
}
cout<<"\n";
}
void initial(int n){
int i,j;
//initial adj matrix
for(i=0;i<n;i++){
for(j=0;j<n;j++){
adj[i][j]=0;
color[i]=0;
}
}
}
void dfs(int i,int n){
cout<<i<<" ";
time += 1;
discovery[i]= time;
color[i]=1;
int j;
for(j=0;j<n;j++){
if(adj[i][j]==1 && color[i]==1){
color[i]=1;
parent[j]=i;
dfs(j,n);
}
}
color[i]=2;
time +=1 ;
finish[i]=time;
}
void dfsTraversal(int n){
int i,start;
for(i=0;i<n;i++){
color[i]=0;
parent[i]=NIL;
}
cout<<"start:";
cin>>start;
for(i=start;i<n;i++){
if(color[i]==0){
dfs(i,n);
}
}
}
int main(){
int n;
cout<<"No of vertex:";
cin>>n;
//input weights
input(n);
cout<<"weights:\n";
output(n);
//bfs traverse
dfsTraversal(n);
cout<<"\nstart finish\n";
for(int i=0;i<n;i++){
cout<<discovery[i]<<" "<<finish[i]<<"\n";
}
return 0;
}