-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path방향그래프_1.c
189 lines (146 loc) · 3.06 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable : 4996)
#define SIZE 100
typedef struct {
char elem[SIZE];
int front, rear;
}QueueType;
typedef struct IncidentEdge {
char aName;
struct IncidentEdge* next;
}IncidentEdge;
typedef struct Vertex {
char vName;
IncidentEdge* iHead;
struct Vertex* next;
}Vertex;
typedef struct {
Vertex* vHead;
int vCount;
}Graph;
void initQueue(QueueType* Q) {
Q->front = Q->rear = 0;
}
void init(Graph* G) {
G->vHead = NULL;
G->vCount = 0;
}
int isEmpty(QueueType* Q) {
return Q->rear == Q->front;
}
int isFull(QueueType* Q) {
return (Q->rear + 1) % SIZE == Q->front;
}
void enqueue(QueueType* Q, char vName) {
Q->rear = (Q->rear + 1) % SIZE;
Q->elem[Q->rear] = vName;
}
char dequeue(QueueType* Q) {
Q->front = (Q->front + 1) % SIZE;
return Q->elem[Q->front];
}
void makeVertex(Graph* G, char vName) {
Vertex* v = (Vertex*)malloc(sizeof(Vertex));
v->vName = vName;
v->iHead = NULL;
v->next = NULL;
G->vCount++;
Vertex* q = G->vHead;
if (q == NULL)
G->vHead = v;
else {
while (q->next != NULL)
q = q->next;
q->next = v;
}
}
void makeIncidentEdge(Vertex* v, char aName) {
IncidentEdge* p = (IncidentEdge*)malloc(sizeof(IncidentEdge));
p->aName = aName;
p->next = NULL;
IncidentEdge* q = v->iHead;
if (q != NULL)
p->next = q;
v->iHead = p;
}
Vertex* findVertex(Graph* G, char vName) {
Vertex* p = G->vHead;
while (p->vName != vName)
p = p->next;
return p;
}
void insertEdge(Graph* G, char v1, char v2) {
Vertex* v = findVertex(G, v1);
makeIncidentEdge(v, v2);
}
int nameToNum(char node[], char Name) {
int i = 0;
while (node[i] != Name)
i++;
return i;
}
char numToName(char node[], int N) {
return node[N];
}
void inDegree(Graph* G, int* in, char node[]) {
Vertex* p = G->vHead;
IncidentEdge* q;
for (; p != NULL; p = p->next)
for (q = p->iHead; q != NULL; q = q->next)
in[nameToNum(node, q->aName)]++;
}
void topologicalSort(Graph* G, int* in, int N, char node[]) {
QueueType Q;
initQueue(&Q);
char* topOrder = (char*)malloc(N + 1 * sizeof(char));
int rank = 1;
Vertex* p;
IncidentEdge* q;
inDegree(G, in, node);
for (int i = 0; i < G->vCount; i++) {
if (in[i] == 0)
enqueue(&Q, numToName(node, i));
}
while (!isEmpty(&Q)) {
char vName = dequeue(&Q);
topOrder[rank++] = vName;
p = findVertex(G, vName);
q = p->iHead;
while (q != NULL) {
in[nameToNum(node, q->aName)]--;
if (in[nameToNum(node, q->aName)] == 0)
enqueue(&Q, q->aName);
q = q->next;
}
}
if (rank <= N)
printf("0");
else
for (int i = 1; i <= N; i++)
printf("%c ", topOrder[i]);
}
int main() {
Graph G;
init(&G);
int N, M;
char vName1, vName2;
scanf("%d", &N);
char* node = (char*)malloc(N * sizeof(char));
int* in = (int*)calloc(N, sizeof(int));
for (int i = 0; i < N; i++) {
getchar();
scanf("%c", &vName1);
node[i] = vName1;
makeVertex(&G, vName1);
}
scanf("%d", &M);
for (int i = 0; i < M; i++) {
getchar();
scanf("%c %c", &vName1, &vName2);
insertEdge(&G, vName1, vName2);
}
topologicalSort(&G, in, N, node);
return 0;
}