-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDFS.c
More file actions
73 lines (58 loc) · 1.35 KB
/
DFS.c
File metadata and controls
73 lines (58 loc) · 1.35 KB
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
#include <stdio.h>
#include <stdbool.h>
#define MAX_VERTICES 50
typedef struct GraphType {
int n;
int adj_mat[MAX_VERTICES][MAX_VERTICES];
} GraphType;
bool visited[MAX_VERTICES];
void init(GraphType* g) {
int r, c;
g->n = 0;
for (r = 0; r < MAX_VERTICES; r++) {
for (c = 0; c < MAX_VERTICES; c++) {
g->adj_mat[r][c] = 0;
}
}
}
void insert_vertex(GraphType* g, int v) {
if (((g->n) + 1 > MAX_VERTICES)) {
printf("그래프: 정점 개수 초과");
return;
}
g->n++;
}
void insert_edge(GraphType* g, int start, int end) {
if (start >= g->n || end >= g->n) {
printf("그래프: 정점 번호 오류");
return;
}
g->adj_mat[start][end] = 1;
g->adj_mat[end][start] = 1;
}
void dfs(GraphType* g, int v) {
int w;
visited[v] = true;
printf("정점 %d -> ", v);
for (w = 0; w < g->n; w++) {
if (g->adj_mat[v][w] && !visited[w]) {
dfs(g, w);
}
}
}
int main(void) {
GraphType* g = (GraphType*)malloc(sizeof(GraphType));
init(g);
for (int i = 0; i < 4; i++) {
insert_vertex(g, i);
}
insert_edge(g, 0, 1);
insert_edge(g, 0, 2);
insert_edge(g, 0, 3);
insert_edge(g, 1, 2);
insert_edge(g, 2, 3);
printf("깊이 우선 탐색\n");
dfs(g, 0);
free(g);
return 0;
}