|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +#define MAX_VERTICES 50 |
| 5 | + |
| 6 | +typedef struct GraphNode { |
| 7 | + int vertex; |
| 8 | + struct GraphNode* link; |
| 9 | +} GraphNode; |
| 10 | + |
| 11 | +typedef struct GraphType { |
| 12 | + int n; |
| 13 | + GraphNode* adj_list[MAX_VERTICES]; |
| 14 | +} GraphType; |
| 15 | + |
| 16 | + |
| 17 | +void graph_init(GraphType* g) { |
| 18 | + g->n = 0; |
| 19 | + for (int v = 0; v < MAX_VERTICES; v++) { |
| 20 | + g->adj_list[v] = NULL; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +void insert_vertext(GraphType* g, int v) { |
| 26 | + if ((g->n + 1) > MAX_VERTICES) { |
| 27 | + fprintf(stderr, "그래프: 정점의 개수 초과"); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + g->n++; |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +void insert_edge(GraphType* g, int u, int v) { |
| 36 | + GraphNode* node; |
| 37 | + if (u >= g->n || v >= g->n) { |
| 38 | + fprintf(stderr, "그래프: 정점 번호 오류"); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + node = (GraphNode*)malloc(sizeof(GraphNode)); |
| 43 | + node->vertex = v; |
| 44 | + node->link = g->adj_list[u]; |
| 45 | + g->adj_list[u] = node; |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +#define MAX_STACK_SIZE 100 |
| 50 | +typedef int element; |
| 51 | +typedef struct { |
| 52 | + element stack[MAX_STACK_SIZE]; |
| 53 | + int top; |
| 54 | +} StackType; |
| 55 | + |
| 56 | + |
| 57 | +void stack_init(StackType* s) { |
| 58 | + s->top = -1; |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +int is_empty(StackType* s) { |
| 63 | + return (s->top == -1); |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +int is_full(StackType* s) { |
| 68 | + return (s->top == (MAX_STACK_SIZE - 1)); |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +void push(StackType* s, element item) { |
| 73 | + if (is_full(s)) { |
| 74 | + fprintf(stderr, "스택 포화 에러\n"); |
| 75 | + return; |
| 76 | + } |
| 77 | + else{ |
| 78 | + s->stack[++(s->top)] = item; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +element pop(StackType* s) { |
| 84 | + if (is_empty(s)) { |
| 85 | + fprintf(stderr, "스택 공백 에러\n"); |
| 86 | + exit(1); |
| 87 | + } |
| 88 | + else { |
| 89 | + return s->stack[(s->top)--]; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +int topo_sort(GraphType* g) { |
| 95 | + int i; |
| 96 | + StackType s; |
| 97 | + GraphNode* node; |
| 98 | + |
| 99 | + int* in_degree = (int*)malloc(g->n * sizeof(int)); |
| 100 | + for (i = 0; i < g->n; i++) { |
| 101 | + in_degree[i] = 0; |
| 102 | + } |
| 103 | + |
| 104 | + for (i = 0; i < g->n; i++) { |
| 105 | + GraphNode* node = g->adj_list[i]; |
| 106 | + while (node != NULL) { |
| 107 | + in_degree[node->vertex]++; |
| 108 | + node = node->link; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + stack_init(&s); |
| 113 | + |
| 114 | + for (i = 0; i < g->n; i++) { |
| 115 | + if (in_degree[i] == 0) { |
| 116 | + push(&s, i); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + while (!is_empty(&s)) { |
| 121 | + int w = pop(&s); |
| 122 | + printf("v[%d] -> ", w); |
| 123 | + node = g->adj_list[w]; |
| 124 | + while (node != NULL) { |
| 125 | + int u = node->vertex; |
| 126 | + in_degree[u]--; |
| 127 | + |
| 128 | + if (in_degree[u] == 0) { |
| 129 | + push(&s, u); |
| 130 | + } |
| 131 | + |
| 132 | + node = node->link; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + free(in_degree); |
| 137 | + printf("X\n"); |
| 138 | + |
| 139 | + return (i == g->n); |
| 140 | +} |
| 141 | + |
| 142 | +int main(void) { |
| 143 | + GraphType g; |
| 144 | + |
| 145 | + graph_init(&g); |
| 146 | + insert_vertext(&g, 0); |
| 147 | + insert_vertext(&g, 1); |
| 148 | + insert_vertext(&g, 2); |
| 149 | + insert_vertext(&g, 3); |
| 150 | + insert_vertext(&g, 4); |
| 151 | + insert_vertext(&g, 5); |
| 152 | + |
| 153 | + insert_edge(&g, 0, 2); |
| 154 | + insert_edge(&g, 0, 3); |
| 155 | + |
| 156 | + insert_edge(&g, 1, 3); |
| 157 | + insert_edge(&g, 1, 4); |
| 158 | + |
| 159 | + insert_edge(&g, 2, 3); |
| 160 | + insert_edge(&g, 2, 5); |
| 161 | + |
| 162 | + insert_edge(&g, 3, 5); |
| 163 | + |
| 164 | + insert_edge(&g, 4, 5); |
| 165 | + |
| 166 | + topo_sort(&g); |
| 167 | + |
| 168 | + return 0; |
| 169 | +} |
0 commit comments