Skip to content

Commit 9a454c9

Browse files
committed
Added Kosaraju Algorithm in C
1 parent dcd2d40 commit 9a454c9

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
#include <stdlib.h>
4+
#include <assert.h>
5+
#define N 100
6+
#define I __INT_MAX__
7+
8+
typedef long long int ll;
9+
10+
typedef struct Node
11+
{
12+
ll vertex;
13+
struct Node *next;
14+
} Node;
15+
16+
void display(Node *first);
17+
void push(struct Node *first, ll a);
18+
bool isEmpty(struct Node *first);
19+
ll pop(struct Node *first);
20+
21+
void display(Node *first)
22+
{
23+
24+
struct Node *temp;
25+
temp = first->next;
26+
27+
while (temp)
28+
{
29+
printf("%lld ", temp->vertex);
30+
temp = temp->next;
31+
}
32+
printf("\n");
33+
}
34+
35+
void push(struct Node *first, ll a)
36+
{
37+
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
38+
temp->vertex = a;
39+
temp->next = first->next;
40+
first->next = temp;
41+
}
42+
bool isEmpty(struct Node *first)
43+
{
44+
if (first->next)
45+
return 0;
46+
else
47+
return 1;
48+
}
49+
50+
ll pop(struct Node *first)
51+
{
52+
53+
if (isEmpty(first))
54+
printf("The stack is empty\n");
55+
else
56+
{
57+
58+
Node *p = first;
59+
p = p->next;
60+
first->next = p->next;
61+
ll temp = p->vertex;
62+
free(p);
63+
return temp;
64+
}
65+
}
66+
67+
void dfsL(Node *list, ll x, ll visited[], Node *st)
68+
{
69+
70+
if (visited[x] == 0)
71+
{
72+
visited[x] = 1;
73+
Node *temp;
74+
temp = (list + x)->next;
75+
while (temp)
76+
{
77+
dfsL(list, temp->vertex, visited, st);
78+
temp = temp->next;
79+
}
80+
push(st, x);
81+
}
82+
}
83+
void dfsR(Node *list, ll x, ll visited[])
84+
{
85+
86+
if (visited[x] == 0)
87+
{
88+
visited[x] = 1;
89+
printf("%lld ", x);
90+
Node *temp;
91+
temp = (list + x)->next;
92+
while (temp)
93+
{
94+
dfsR(list, temp->vertex, visited);
95+
temp = temp->next;
96+
}
97+
}
98+
}
99+
100+
ll main()
101+
{
102+
// for kosarajus
103+
104+
ll n = 0, X = 0;
105+
scanf("%lld %lld", &n, &X);
106+
Node *adjList = (Node *)malloc((n + 1) * sizeof(Node));
107+
Node *adjListR = (Node *)malloc((n + 1) * sizeof(Node));
108+
Node *stack = (Node *)malloc(sizeof(Node));
109+
stack->next = NULL;
110+
stack->vertex = 0;
111+
for (ll i = 0; i <= n; i++)
112+
{
113+
(adjList + i)->next = NULL;
114+
(adjList + i)->vertex = 0;
115+
(adjListR + i)->next = NULL;
116+
(adjListR + i)->vertex = 0;
117+
}
118+
ll temp1, temp2;
119+
for (ll i = 1; i <= X; i++)
120+
{
121+
scanf("%lld %lld", &temp1, &temp2);
122+
push(adjList + temp1, temp2);
123+
push(adjListR + temp2, temp1);
124+
}
125+
ll visited[N] = {0};
126+
for (ll i = 1; i <= n; i++)
127+
{
128+
if (visited[i] == 0)
129+
dfsL(adjList, i, visited, stack);
130+
}
131+
ll visitedR[N] = {0};
132+
while (!isEmpty(stack))
133+
{
134+
ll i = pop(stack);
135+
if (visitedR[i] == 0)
136+
{
137+
dfsR(adjListR, i, visitedR);
138+
printf("\n");
139+
}
140+
}
141+
return 0;
142+
}

0 commit comments

Comments
 (0)