-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDFS.cpp
281 lines (235 loc) · 7.78 KB
/
DFS.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* DFS.cpp
*
* Created on: Apr 4, 2014
* Author: Santhosh Ramaiah
*/
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <limits>
#include <fstream>
#include <cstring>
#include <sstream>
#include <cstdlib>
#define MAX 300 // maximum number of nodes or edges
using namespace std;
enum colors {BLACK, WHITE, GRAY}; // description for node state
int color[MAX], d[MAX], p[MAX], f[MAX],ftemp[MAX], t, vertex,checkDirected;
int edge,k=0,snode,cont=0, roat=0, contrev=0;
vector<int> bkeeping[MAX]={}; // book keeping for edges
int NIL = numeric_limits<int>::min();// 0 value to NIL
bool bflag=false,sflag=true; // bflag- flag for back edge represents cyclic or not. sflag- flag for edge repeat.
bool sccflag=false; // Strongly connected component flag
vector<int> contgra[MAX]={}; // Connected graph book keeping.
vector<int> contgrarev[MAX]={}; //reverse Connected graph book keeping.
void DFS(vector<int>[]); // DFS
void DFS_VISIT(vector<int>[],int, int);// DFS visit
int main(void)
{
string line,filename;
char filename1[40];
int intArray[MAX][MAX]={}; // temporary variable which stores node and its adjacent edges
int temp; // checks if the graph is directed or not.
std::vector<int> adjList[MAX]; // list of node and its adjacent edges
std::vector<int> revadjList[MAX]; // reverse adjacency list for Graph transpose.
cout<<"Enter the name of the graph file: "<<endl;
cin>>filename;
strcpy(filename1, filename.c_str()); //string to character store to pass
ifstream myfile; // read the file.
myfile.open(filename1);// open the file
ifstream resourcefile; // read the file
resourcefile.open("RESOURCE_FILE.txt"); // open the resource file
if(resourcefile.is_open()){// further expansion for reading resource file nedded.
resourcefile.close();
}else{
cout<<"resource file not available"<<endl; // close resource file // used later on.
}
if (myfile.is_open()) // check if the file is open.
{
while ( getline (myfile,line) ) // get every line and store it to line
{
std::stringstream ss(line); // converts string to int
int templength1=line.length();
for(int idx = 0; idx < templength1; idx++)
{
ss>>intArray[k][idx]; // store the int to array
}
k++;
}
myfile.close();
vertex=intArray[1][0]; // store the vertex
checkDirected=intArray[0][0]; // store the directed/undirected graph
cout<<"Enter the first node to start"<<"(0 to "<<(vertex-1)<<"):"<<endl;
cin>>snode; // selected node or source node
for(int e=0; e<vertex; e++) { // read all the vertices
for(int ed=0; ed<intArray[e+2][2];ed++){ // read all the edges
temp=intArray[e+2][ed+3];
adjList[e].push_back(temp); //for directed graph
revadjList[temp].push_back(e); //for SCC graph transpose
if(checkDirected==(-1)){
adjList[temp].push_back(e);//for undirected graph.
}
}
}
cout<<endl;
cout<<"Classification of edges"<<endl;
DFS(adjList); // do DFS visit
cout<<endl;
cout<<"Vertex (Discovery Time/Finish Time)"<<endl;
int vert=vertex-1, swap;
for(int v=0; v<vertex; v++) { // print all the vertices and their discovery/finish time
printf("v%d (%d/%d)\n", v, d[v], f[v]);
ftemp[v]=(f[v]); // store the finish times for sorting
//bkeeping[0].push_back(v);
}
// bubble sort the finish times
for (int c = 0 ; c < vertex; c++)
{
for (int d = 0 ; d < vertex - c - 1; d++)
{
if (ftemp[d] < ftemp[d+1]) /* For decreasing order use < */
{
swap = ftemp[d];
ftemp[d] = ftemp[d+1];
ftemp[d+1] = swap;
}
}
}
// finish times sorted.
cout<<endl;
if (checkDirected==0){ // graph is directed
if (bflag){ // graph is cyclic
cout<<"Graph is Directed Cyclic."<<endl;
cout<<"No Topological Sorting"<<endl;
}
else{
cout<<"Graph is Directed Acyclic."<<endl;
cout<<"The Topological Sorting is:"<<endl;
for (int i = 0; i <= vert; i++) {
for (int j = 0; j <= vert; j++){
if(ftemp[i]==f[j]){
cout <<j<< " ";
}
}
}
cout<<endl;
}
cout<<endl;
sccflag=true;
DFS(revadjList);
cout<<"Number of Strongly Connected Components are: "<<contrev<<endl;
cout<<"Strongly Connected Components are:"<<endl;
for (int i = 0; i < contrev; i++) {
int tempsize1=contgrarev[i].size();
for (int j = 0; j < tempsize1; j++){
cout<<contgrarev[i][j]<<" ";
}cout<<endl;
}
}
else if (checkDirected==(-1)){ // graph is undirected
if(bflag){ // graph is cyclic
cout<<"Graph is Undirected Cyclic."<<endl;
}
else{
cout<<"Graph is Undirected Acyclic."<<endl;
}
cout<<"Number of Connected Components are: "<<cont<<endl;
cout<<"Connected Components are:"<<endl;
for (int i = 0; i < cont; i++) {
int tempsize2=contgra[i].size();
for (int j = 0; j < tempsize2; j++){
cout<<contgra[i][j]<<" ";
}cout<<endl;
}
}
else {cout<<"File Read Error.";}
}
else cout << "Unable to open file";
return 0;
}
void DFS(vector<int> G[]) {
for(int u=0; u<vertex; u++) { // initially paint all nodes white
color[u] = WHITE;
p[u] = NIL;
}
t = 0; // reset time
if(sccflag==false){ // do if not of strongly connected graph
if(color[snode] == WHITE) { // do DFS visit for source node
contgra[cont].push_back(snode);
DFS_VISIT(G,snode,cont);
cont++;
}
for(int u=0; u<vertex; u++) { // do DFS visit for all nodes other than source node.
if(u==snode){continue; } // not needed as it is backed by color.
else{
if(color[u] == WHITE) {
contgra[cont].push_back(u);
DFS_VISIT(G,u,cont);
cont++;
}
}
}
}
else{ // for SCC graph transpose node sending in reverse of finish times
for (int i = 0; i < vertex; i++) {
for (int u = 0; u < vertex; u++){
if(ftemp[i]==f[u]){
if(color[u] == WHITE) {
contgrarev[contrev].push_back(u);
DFS_VISIT(G,u,contrev);
contrev++;
}
}
}
}
}
}
void DFS_VISIT(vector<int> G[], int u, int contg) {
t = t + 1; // increment time
d[u] = t; // put discovery time for node
color[u] = GRAY; // paint gray to node
int tempsize3=G[u].size();
for(int v=0; v<tempsize3; v++) { // do for all adjacent nodes
for (int sj=0;sj<roat;sj++){ //check for edge repetition.
if(((bkeeping[sj][1]==u)&&(bkeeping[sj][0]==G[u][v]))||((bkeeping[sj][1]==u)&&(bkeeping[sj][0]==G[u][v]))){
sflag=false;
break;
}
else{
sflag=true;
}
}
bkeeping[roat].push_back(u); //put edge in book keeping array.
bkeeping[roat].push_back(G[u][v]); //put edge in book keeping array.
roat++;
if(color[G[u][v]] == WHITE) { // check for nodes which are undiscovered
p[G[u][v]] = u;
if(sccflag==false){// check for SCC
contgra[contg].push_back(G[u][v]);
cout<<"Edge "<<u<<"----"<<G[u][v]<<" Tree Edge"<<endl;
}
else{
contgrarev[contg].push_back(G[u][v]);
}
DFS_VISIT(G,G[u][v],contg);
}
else if(color[G[u][v]] == GRAY&&sflag==true&&sccflag==false){ //check for back edge
cout<<"Edge "<<u<<"----"<<G[u][v]<<" Back Edge"<<endl;
bflag=true;
}
else if(color[G[u][v]] == BLACK&&sflag==true&&sccflag==false){ // check for forward/cross edge
if(d[u]<d[v]){
cout<<"Edge "<<u<<"----"<<G[u][v]<<" Forward Edge"<<endl;
}
else{
cout<<"Edge "<<u<<"----"<<G[u][v]<<" Cross Edge"<<endl;
}
}
sflag=false;
}
color[u] = BLACK; // paint node black
t = t + 1; //increment time
f[u] = t; // put finish time for node
}