From d4d0020be95cfcead06893d2166c43190797782f Mon Sep 17 00:00:00 2001 From: juan Date: Mon, 28 Nov 2022 16:32:09 +0800 Subject: [PATCH] Add part1 of pset7 --- semester3/pset7/7-1-Minimum-Spanning-Tree.c | 131 ++++++++++++++++++++ semester3/pset7/7-2-DFS.c | 74 +++++++++++ 2 files changed, 205 insertions(+) create mode 100644 semester3/pset7/7-1-Minimum-Spanning-Tree.c create mode 100644 semester3/pset7/7-2-DFS.c diff --git a/semester3/pset7/7-1-Minimum-Spanning-Tree.c b/semester3/pset7/7-1-Minimum-Spanning-Tree.c new file mode 100644 index 0000000..ee14b6b --- /dev/null +++ b/semester3/pset7/7-1-Minimum-Spanning-Tree.c @@ -0,0 +1,131 @@ +#include +#define SIZE 10 +#define INF 9999 + +void createLine(int mat[SIZE][SIZE], char start, char end, int weight) { + mat[start - 'A'][end - 'A'] = weight; + mat[end - 'A'][start - 'A'] = weight; +} + +int getMinNode(int* dist, int* visited) { + int minDist = INF, minEnd; + for (int i = 0; i < SIZE; i++) { + if (dist[i] < minDist && !visited[i]) { + minDist = dist[i]; + minEnd = i; + } + } + return minEnd; +} + +int min(int l, int r) { + return (l < r)? l : r; +} + +void sort(int* ans, int size) { + // init bucket sort + int max = -1; + for (int i = 0; i < size; i++) { + if (ans[i] > max) { + max = ans[i]; + } + } + + int bucket[max + 1]; + for (int i = 0; i < max + 1; i++) { + bucket[i] = 0; + } + + // bucket sort + for (int i = 0; i < size; i++) { + bucket[ans[i]]++; + } + + int count = 0; + int sorted[size]; + for (int i = 0; i < max + 1; i++) { + while (bucket[i]) { + sorted[count++] = i; + bucket[i]--; + } + } + + for (int i = 0; i < size; i++) { + ans[i] = sorted[i]; + } +} + +void createGraph(int mat[SIZE][SIZE]) { + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + mat[i][j] = INF; + } + } + + createLine(mat, 'A', 'B', 3); + createLine(mat, 'A', 'E', 4); + createLine(mat, 'A', 'D', 4); + createLine(mat, 'B', 'A', 3); + createLine(mat, 'B', 'E', 2); + createLine(mat, 'B', 'F', 3); + createLine(mat, 'B', 'C', 10); + createLine(mat, 'C', 'F', 6); + createLine(mat, 'C', 'G', 1); + createLine(mat, 'D', 'E', 5); + createLine(mat, 'D', 'H', 6); + createLine(mat, 'E', 'F', 11); + createLine(mat, 'E', 'H', 2); + createLine(mat, 'E', 'I', 1); + createLine(mat, 'E', 'D', 5); + createLine(mat, 'F', 'E', 11); + createLine(mat, 'F', 'B', 3); + createLine(mat, 'F', 'C', 6); + createLine(mat, 'F', 'G', 2); + createLine(mat, 'F', 'J', 11); + createLine(mat, 'F', 'I', 3); + createLine(mat, 'G', 'J', 8); + createLine(mat, 'H', 'I', 4); + createLine(mat, 'I', 'J', 7); +} + +int main(void) { + int mat[SIZE][SIZE]; + createGraph(mat); + + char start, end; + int weight; + scanf("%c,%c,%d", &start, &end, &weight); + mat[start - 'A'][end - 'A'] = weight; // the matrix is undirectional + mat[end - 'A'][start - 'A'] = weight; + + int ans[SIZE - 1]; + int dist[SIZE] = {}; + for (int i = 1; i < SIZE; i++) { + dist[i] = INF; + } + int top = 0; + + int cur; + int visited[SIZE] = {}; + + while (top != SIZE) { + cur = getMinNode(dist, visited); + visited[cur] = 1; + + ans[top++] = dist[cur]; + + for (int j = 0; j < SIZE; j++) { + dist[j] = min(dist[j], mat[cur][j]); + // printf("%c -> %c: %d\n", cur + 'A', j + 'A', dist[j]); + } + } + + ans[0] = ans[top - 1]; // top == SIZE + + sort(ans, SIZE - 1); + for (int i = 0; i < SIZE - 1; i++) { + printf("%d,", ans[i]); + } + + return 0; +} diff --git a/semester3/pset7/7-2-DFS.c b/semester3/pset7/7-2-DFS.c new file mode 100644 index 0000000..ac5f136 --- /dev/null +++ b/semester3/pset7/7-2-DFS.c @@ -0,0 +1,74 @@ +#include +#include +#define SIZE 9 + +typedef struct stack { + int top; + int *arr; +} stack; + +stack *newStack() { + stack *st = malloc(sizeof(stack)); + st->top = 0; + st->arr = malloc(sizeof(int) * SIZE * 3); + + return st; +} + +void push(stack *st, int i) { st->arr[st->top++] = i; } + +int pop(stack *st) { return st->arr[--st->top]; } + +int isEmpty(stack *st) { return (st->top == 0); } + +int main(void) { + int mat[SIZE][SIZE] = {}; + + mat[1][2] = 1; + mat[1][3] = 1; + mat[2][1] = 1; + mat[2][4] = 1; + mat[2][5] = 1; + mat[3][1] = 1; + mat[3][6] = 1; + mat[3][7] = 1; + mat[4][2] = 1; + mat[4][8] = 1; + mat[5][2] = 1; + mat[5][8] = 1; + mat[6][3] = 1; + mat[6][8] = 1; + mat[7][3] = 1; + mat[7][8] = 1; + mat[8][4] = 1; + mat[8][5] = 1; + mat[8][6] = 1; + mat[8][7] = 1; + + int visited[SIZE] = {}; + + int start; + scanf("%d", &start); + + stack *st = newStack(); + + push(st, start); + + int cur; + + while (!isEmpty(st)) { + cur = pop(st); + if (!visited[cur]) { + visited[cur] = 1; + printf("%d,", cur); + } + + for (int i = SIZE - 1; i > 0; i--) { + if (!visited[i] && mat[cur][i] == 1) { + push(st, i); + } + } + } + + return 0; +}