Add part1 of pset7

This commit is contained in:
juan 2022-11-28 16:32:09 +08:00
parent b02ecf51d0
commit d4d0020be9
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
2 changed files with 205 additions and 0 deletions

View file

@ -0,0 +1,131 @@
#include <stdio.h>
#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;
}

74
semester3/pset7/7-2-DFS.c Normal file
View file

@ -0,0 +1,74 @@
#include <stdio.h>
#include <stdlib.h>
#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;
}