diff options
| author | mo khan <mo.khan@gmail.com> | 2020-09-26 17:33:30 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-09-26 17:33:30 -0600 |
| commit | cbc20066c9b321b4502cbda1e43812f78c9c2388 (patch) | |
| tree | 25a952b638cc768820eef0770dffd5fd9b8c50a8 /src/03/matrix.c | |
| parent | e7fc8f9de29fbcd28b32d0ad743de9af0751340a (diff) | |
Extract adjacency matrix into separate files
Diffstat (limited to 'src/03/matrix.c')
| -rw-r--r-- | src/03/matrix.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/03/matrix.c b/src/03/matrix.c new file mode 100644 index 0000000..752eabd --- /dev/null +++ b/src/03/matrix.c @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <stdlib.h> + +char labels[26] = { + 'a', 'b', 'c', 'd', + 'e', 'f', 'g', 'h', + 'i', 'j', 'k', 'l', + 'm', 'n', 'o', 'p', + 'q', 'r', 's', 't', + 'u', 'v', 'w', 'x', + 'y', 'z' +}; + +void matrix_traverse(int n, int graph[n][n], int visited[n], int vertex) { + printf("->(%c)", labels[vertex]); + visited[vertex] = 1; + + for (int edge = 0; edge < n; ++edge) { + if (!visited[edge] && graph[vertex][edge] > 0) { + graph[vertex][edge] = 0; + matrix_traverse(n, graph, visited, edge); + graph[edge][vertex] = 0; + printf("->(%c)", labels[vertex]); + } + } + for (int edge = 0; edge < n; ++edge) { + if (graph[vertex][edge] > 0 && graph[edge][vertex] > 0) { + graph[vertex][edge] = 0; + matrix_traverse(n, graph, visited, edge); + graph[edge][vertex] = 0; + printf("->(%c)", labels[vertex]); + } + } +} + +void matrix_inspect(int n, int graph[n][n]) { + printf("\n"); + + printf("| "); + for (int i = 0; i < n; ++i) + printf("|%c", labels[i]); + printf("|\n"); + + for (int i = 0; i < n; ++i) { + printf("|%c|", labels[i]); + for (int j = 0; j < n; ++j) + printf("%d|", graph[i][j]); + printf("\n"); + } +} |
