-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path그래프순회_1.c
115 lines (93 loc) · 1.92 KB
/
그래프순회_1.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)
#define FALSE 0
#define TRUE 1
typedef struct IncidentEdge {
int aName;
struct IncidentEdge* next;
}IncidentEdge;
typedef struct Vertex {
int vName;
int isVisit;
IncidentEdge* iHead;
struct Vertex* next;
}Vertex;
typedef struct {
Vertex* vHead;
}GraphType;
void initGraph(GraphType* G) {
G->vHead = NULL;
}
void makeVertex(GraphType* G, int vName) {
Vertex* v = (Vertex*)malloc(sizeof(Vertex));
v->vName = vName;
v->isVisit = FALSE;
v->next = NULL;
v->iHead = (IncidentEdge*)malloc(sizeof(IncidentEdge));
v->iHead->next = NULL;
Vertex* p = G->vHead;
if (p == NULL)
G->vHead = v;
else {
while (p->next != NULL)
p = p->next;
p->next = v;
}
}
Vertex* findVertex(GraphType* G, int vName) {
Vertex* v = G->vHead;
while (v->vName != vName)
v = v->next;
return v;
}
void makeIncidentEdge(Vertex* v, int aName) {
IncidentEdge* i = (IncidentEdge*)malloc(sizeof(IncidentEdge));
i->aName = aName;
i->next = NULL;
IncidentEdge* p = v->iHead;
if (p->next == NULL)
v->iHead->next = i;
else {
while (p->next != NULL && p->next->aName < aName)
p = p->next;
if (p->next != NULL) {
i->next = p->next;
p->next = i;
}
else
p->next = i;
}
}
void insertEdge(GraphType* G, int v1, int v2) {
Vertex* v = findVertex(G, v1);
makeIncidentEdge(v, v2);
v = findVertex(G, v2);
makeIncidentEdge(v, v1);
}
void rDfs(GraphType* G, int vName) {
Vertex* v = findVertex(G, vName);
if (v->isVisit == TRUE)
return;
IncidentEdge* p;
v->isVisit = TRUE;
printf("%d\n", v->vName);
for (p = v->iHead->next; p != NULL; p = p->next)
rDfs(G, p->aName);
}
int main() {
GraphType G;
initGraph(&G);
int N, M, S;
scanf("%d %d %d", &N, &M, &S);
for (int i = 1; i <= N; i++)
makeVertex(&G, i);
int v1, v2;
for (int i = 0; i < M; i++) {
scanf("%d %d", &v1, &v2);
insertEdge(&G, v1, v2);
}
rDfs(&G, S);
return 0;
}