Commit e7fc8f9
Changed files (2)
src
03
src/03/05/README.md
@@ -543,9 +543,15 @@ a higher priority concern than time.
To traverse every edge in both directions we can use an adjacency matrix
and iterate through every cell in the matrix. If the cell contains a 1 to
-indicate a connection than we know that we can traverse from the edge at
+indicate an edge than we know that we can traverse from the edge at
that row and column. Both directions will be represented in different cells
in the matrix.
When we visit each cell in the matrix we can flip the 1 to a 0 to ensure that
we do not revisit a visited edge.
+
+1. Start at any vertex
+2. Iterate through list of edges.
+3. If the vertex on the other end of the edge has not been visited yet then visit it and loop until all edges are exhausted for the vertex.
+4. Backtrack to previous vertex
+5. Visit any edge where you can backtrack safely.
src/03/graph_test.c
@@ -100,33 +100,41 @@ void traverse(int vertex) {
printf("->(%c)", labels[vertex]);
}
}
+ for (int edge = 0; edge < 16; ++edge) {
+ if (graph[vertex][edge] > 0 && graph[edge][vertex] > 0) {
+ graph[vertex][edge] = 0;
+ traverse(edge);
+ graph[edge][vertex] = 0;
+ printf("->(%c)", labels[vertex]);
+ }
+ }
}
-void inspect_graph() {
+void graph_inspect(int n) {
printf("\n");
printf("| ");
- for (int i = 0; i < 16; ++i)
+ for (int i = 0; i < n; ++i)
printf("|%c", labels[i]);
printf("|\n");
- for (int i = 0; i < 16; ++i) {
+ for (int i = 0; i < n; ++i) {
printf("|%c|", labels[i]);
- for (int j = 0; j < 16; ++j)
+ for (int j = 0; j < n; ++j)
printf("%d|", graph[i][j]);
printf("\n");
}
}
Ensure(every_edge_is_traversed_in_both_directions_at_least_once) {
+ int n = 16;
+
traverse(0);
- inspect_graph();
+ graph_inspect(n);
- for (int i = 0; i < 16; ++i) {
- for (int j = 0; j < 16; ++j) {
+ for (int i = 0; i < n; ++i)
+ for (int j = 0; j < n; ++j)
assert_that(graph[i][j], is_equal_to(0));
- }
- }
}
TestSuite *graph_tests() {