-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.cc
247 lines (207 loc) · 4.1 KB
/
graph.cc
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
#include<iostream>
#include<cstdlib>
#include<stack>
#include<queue>
#include<list>
using namespace std;
class Graph
{
public:
int V;
list<int> *array;
Graph(int V){
this->V=V;
array = new list<int>[V];
}
void addEdge(int src, int dest){
array[src].push_back(dest);
array[dest].push_back(src);
}
void addDirectedEdge(int src, int dest){
array[src].push_back(dest);
}
void printGraph(){
int v;
for(v=0;v<V;++v){
cout<<v<<"->";
list<int>::iterator it = array[v].begin();
while(it != array[v].end()){
cout<<*it<<" ";
++it;
}
cout<<endl;
}
}
void dfs(){
cout<<"The Depth First Search \n";
bool *explored = new bool[V]();
for(int i = 0; i < V;i++){
explored[i] = false;
}
for(int i = 0; i < V ; i++){
if(explored[i] == false){
stack<int> s;
s.push(i);
explored[i] = true;
while(!s.empty()){
int vert = s.top();
s.pop();
cout<<vert<<"\t";
list<int>::iterator it = array[vert].begin();
while(it != array[vert].end()){
//cout<<"explored["<<temp->dest<<"] = " << explored[temp->dest]<<"\n";
if(!explored[*it]){
s.push(*it);
explored[*it] = true;
}
++it;
}
}
cout<<"\n";
}
}
cout<<"\n";
}
void dfs2(){
bool *visited = new bool[V];
for(int i=0;i<V;i++){
visited[i] = false;
}
stack<int> stack;
int s = 0;
visited[s] = true;
stack.push(s);
list<int>::iterator i;
while(!stack.empty()){
s = stack.top();
cout<<s<<" ";
stack.pop();
for(i = array[s].begin();i != array[s].end();++i){
if(!visited[*i]){
visited[*i] = true;
stack.push(*i);
}
}
}
cout<<"\n";
}
void dfsutil(){
}
void bfs(){
bool *visited = new bool[V];
for(int i=0;i<V;i++){
visited[i] = false;
}
for(int i =0 ;i<V;i++){
if(visited[i] == false){
queue<int> queue;
int s = i;
visited[s] = true;
queue.push(s);
list<int>::iterator i;
while(!queue.empty()){
s = queue.front();
cout<<s<<" ";
queue.pop();
for(i = array[s].begin();i != array[s].end();++i){
if(!visited[*i]){
visited[*i] = true;
queue.push(*i);
}
}
}
cout<<"\n";
}
}
}
};
list<int>* createReverseArray(list<int> *array, int size){
list<int> *array2 = new list<int>[size];
for(int i = 0;i<size;i++){
list<int>::iterator it;
for(it = array[i].begin();it != array[i].end();++it){
array2[*it].push_back(i);
}
}
return array2;
}
Graph createReverseGraph(Graph g){
Graph gNew(g.V);
gNew.array = createReverseArray(g.array,g.V);
return gNew;
}
void dfs(Graph g, int i, bool visited[], int leader[], int s,int &t,int f[]){
visited[i] = true;
leader[i] = s;
list<int>::iterator it = g.array[i].begin();
for(it = g.array[i].begin();it != g.array[i].end();++it){
int a = *it;
if(!visited[*it]){
dfs(g,*it, visited, leader, s,t,f);
}
}
f[i] = t++;
}
void printArray(int *f, int size){
for(int i = 0;i<size;i++){
cout<<i<<" "<<f[i]<<"\n";
}
}
int* dfsLoop(Graph g, int f[],bool reverse){
int t = 0;
int s;
int *leader = new int[g.V];
bool *visited = new bool[g.V];
int n = g.V;
for(int i = 0;i<n;i++){
visited[i] = false;
}
Graph G = g;
printArray(f,g.V);
if(reverse == true){
G = createReverseGraph(g);
list<int> *arr = G.array;
for(int i = 0;i < n; i++){
G.array[i] = arr[f[i]];
}
}
for(int i = n - 1 ;i >= 0; i--){
if(!visited[i]){
s = i;
dfs(G,i,visited, leader,s,t,f);
}
}
return leader;
}
void kosaraju(Graph g){
int *f = new int[g.V];
for(int i = 0;i<g.V;i++){
f[i] = i;
}
dfsLoop(g, f, false);
int *leader = dfsLoop(g, f, true);
for(int i=0;i<g.V;i++){
for(int j = 0 ; j <g.V;j++){
if(leader[i] == f[j]){
cout<<j<<" ";
}
}
}
cout<<"\n";
}
int main(){
Graph gh(9);
gh.addDirectedEdge(0,6);
gh.addDirectedEdge(3,0);
gh.addDirectedEdge(6,3);
gh.addDirectedEdge(6,8);
gh.addDirectedEdge(2,8);
gh.addDirectedEdge(8,5);
gh.addDirectedEdge(5,2);
gh.addDirectedEdge(5,7);
gh.addDirectedEdge(7,1);
gh.addDirectedEdge(1,4);
gh.addDirectedEdge(4,7);
kosaraju(gh);
return 0;
}