-
Notifications
You must be signed in to change notification settings - Fork 11
/
question9.c
180 lines (143 loc) · 3.47 KB
/
question9.c
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
/*
Kosaraju algorithm for strongly connected components
Time complexity: O(v+e), this happens thrice
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int stack[100];
int top = -1;
int isStackEmpty(){
return top == -1;
}
void push(int data){
stack[++top] = data;
}
int pop(){
return stack[top--];
}
struct adjListNode{
int data;
struct adjListNode *next;
};
struct adjList{
struct adjListNode *head;
};
struct graph{
int vertices;
struct adjList *arr;
};
struct adjListNode *newNode(int data){
struct adjListNode *temp = (struct adjListNode *)malloc(sizeof(struct adjListNode));
temp->data = data;
temp->next = NULL;
return temp;
}
void createEdge(int start, int end, struct graph *newGraph){
struct adjListNode *temp = newNode(end);
if(newGraph->arr[start].head){
temp->next = newGraph->arr[start].head;
newGraph->arr[start].head = temp;
}else{
newGraph->arr[start].head = temp;
}
}
void display(struct graph *newGraph){
int i;
struct adjListNode *temp;
for(i=0;i<newGraph->vertices;i++){
temp = newGraph->arr[i].head;
printf("%d ---> ", i);
while(temp){
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
}
void dfs(int index, struct graph *newGraph, int *visited){
visited[index] = 1;
printf("%d ", index);
struct adjListNode *temp = newGraph->arr[index].head;
while(temp){
if(visited[temp->data] == 0){
dfs(temp->data, newGraph, visited);
}
temp = temp->next;
}
push(index);
}
void dfsReverse(int index, struct graph *reverse, int *visited){
visited[index] = 1;
printf("%d ", index);
struct adjListNode *temp = reverse->arr[index].head;
while(temp){
if(visited[temp->data] == 0){
dfsReverse(temp->data, reverse, visited);
}
temp = temp->next;
}
}
void traverseGraphAndReverse(struct graph *reverse, struct graph *newGraph){
int i;
struct adjListNode *temp;
for(i=0;i<newGraph->vertices;i++){
temp = newGraph->arr[i].head;
while(temp){
createEdge(temp->data, i, reverse);
temp = temp->next;
}
}
}
void init(int *visited, int vertices){
int i;
for(i=0;i<vertices;i++){
visited[i] = 0;
}
}
void displayStronglyConnected(struct graph *newGraph, int *visited){
//do dfs and push elements on stack in the decreasing order of their finishing time
int i;
for(i=0;i<newGraph->vertices;i++){
if(visited[i] == 0){
dfs(i, newGraph, visited);
printf("\n");
}
}
//reverse the graph
struct graph *reverse = (struct graph *)malloc(sizeof(struct graph));
reverse->vertices = newGraph->vertices;
reverse->arr = (struct adjList *)calloc(newGraph->vertices, sizeof(struct adjList));
traverseGraphAndReverse(reverse, newGraph);
printf("\n printing reverse \n");
display(reverse);
//do dfs as per stack on the reverse graph
printf("\n printing components\n");
init(visited, newGraph->vertices);
while(!isStackEmpty()){
int index = pop();
if(visited[index] == 0){
dfsReverse(index, reverse, visited);
printf("\n");
}
}
}
int main(){
struct graph *newGraph = (struct graph *)malloc(sizeof(struct graph));
int vertices = 7;
newGraph->vertices = 7;
newGraph->arr = (struct adjList *)calloc(vertices, sizeof(struct adjList));
createEdge(0,1, newGraph);
createEdge(1,2, newGraph);
createEdge(2,0, newGraph);
createEdge(2,4, newGraph);
createEdge(4,5, newGraph);
createEdge(5,3, newGraph);
createEdge(3,4, newGraph);
createEdge(6,5, newGraph);
display(newGraph);
int visited[vertices];
init(visited, vertices);
displayStronglyConnected(newGraph, visited);
return 0;
}