forked from gaurav03kr/hello-hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS_Visit.c
155 lines (132 loc) · 2.71 KB
/
DFS_Visit.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
#include <stdio.h>
#include <stdlib.h>
#define MAXNUM_VERTICES 100
typedef struct
{
int n_vertices;
int n_edges;
int adjacency_matrix[MAXNUM_VERTICES][MAXNUM_VERTICES];
} Graph;
typedef enum
{
WHITE,
GRAY,
BLACK
} COLOR;
#define Inf 100000000
int finish_time[MAXNUM_VERTICES];
int time = 0;
int topology[MAXNUM_VERTICES];
void inisialisasi_graph(Graph *g, int n_v, int n_e)
{
int i, j;
g->n_vertices = n_v;
g->n_edges = n_e;
for (i = 0; i < MAXNUM_VERTICES; i++)
{
for (j = 0; j < MAXNUM_VERTICES; j++)
{
if (i < n_v && j < n_v)
{
g->adjacency_matrix[i][j] = 0;
}
else
{
g->adjacency_matrix[i][j] = -1;
}
}
}
}
void print_adjacency_matrix(Graph *g)
{
int i, j;
for (i = 0; i < g->n_vertices; i++)
{
printf("\t%d", i);
}
printf("\n");
for (i = 0; i < g->n_vertices; i++)
{
printf("%d", i);
for (j = 0; j < g->n_vertices; j++)
{
printf("\t%d", g->adjacency_matrix[i][j]);
}
printf("\n");
}
}
int cek = 0;
void DFS_visit(Graph *g, COLOR *vertex_colors, int v)
{
int i;
printf("%d ", v);
vertex_colors[v] = GRAY;
for (i = 0; i < g->n_vertices; i++)
{
if (g->adjacency_matrix[v][i] == 1 && vertex_colors[i] != WHITE)
{
cek = 1;
}
if (g->adjacency_matrix[v][i] == 1 && vertex_colors[i] == WHITE)
{
DFS_visit(g, vertex_colors, i);
}
}
vertex_colors[v] = BLACK;
time = time + 1;
finish_time[v] = time;
topology[time] = v;
}
void DFS(Graph *g)
{
COLOR vertex_colors[MAXNUM_VERTICES];
int i;
for (i = 0; i < g->n_vertices; i++)
{
vertex_colors[i] = WHITE;
}
for (i = 0; i < g->n_vertices; i++)
{
if (vertex_colors[i] == WHITE)
{
DFS_visit(g, vertex_colors, i);
}
}
printf("\n");
}
int cmp(const void *a, const void *b)
{
return (*(int *)b - *(int *)a);
}
int main()
{
int n_v = 0;
int n_e = 0;
int i, j;
scanf("%d %d", &n_v, &n_e);
Graph g;
inisialisasi_graph(&g, n_v, n_e);
for (i = 0; i < n_e; i++)
{
int a, b;
scanf("%d %d", &a, &b);
g.adjacency_matrix[a][b] = 1;
}
print_adjacency_matrix(&g);
DFS(&g);
if (cek == 1)
{
printf("Cycle\n");
}
else
{
qsort(finish_time, g.n_vertices, sizeof(int), cmp);
printf("Hasil Topology : ");
for (int i = 0; i < g.n_vertices; i++)
{
printf("%d ", topology[finish_time[i]]);
}
}
printf("\n");
return 0;
}