1/**//*
2 그래프 검색(깊이우선)
3*/
4#include <stdio.h>
5
6#define N 8 //정점 수
7
8int graph[N+1][N+1] = //인접 행렬
9...{
10 ...{0, 0, 0, 0, 0, 0, 0, 0, 0},
11 ...{0, 0, 1, 0, 0, 0, 0, 0, 0},
12 ...{0, 1, 0, 1, 1, 0, 0, 0, 0},
13 ...{0, 0, 1, 0, 0, 0, 0, 1, 0},
14 ...{0, 0, 1, 0, 0, 1, 0, 0, 0},
15 ...{0, 0, 0, 0, 1, 0, 1, 0, 0},
16 ...{0, 0, 0, 0, 0, 1, 0, 1, 1},
17 ...{0, 0, 0, 1, 0, 0, 1, 0, 1},
18 ...{0, 0, 0, 0, 0, 0, 1, 1, 0},
19};
20
21int v[N + 1]; //방문 플래그
22
23void Visit(int);
24
25void main(void)
26...{
27 int i;
28 for(i = 1;i <= N;i++)
29 ...{
30 v[i] = 0;
31 }
32 Visit(1);
33 printf("\n");
34}
35
36void Visit(int i)
37...{
38 int j;
39 v[i] = 1;
40 for(j = 1;j <= N;j++)
41 ...{
42 if(graph[i][j] == 1 && v[j] == 0)
43 ...{
44 printf("%d->%d ", i, j);
45 Visit(j);
46 }
47 }
48}