Commit 007076e

mo khan <mo.khan@gmail.com>
2020-09-08 00:49:24
feat: record edges in graph
1 parent 642c56d
Changed files (3)
src/03/graph.c
@@ -20,3 +20,7 @@ Vertex *graph_add_vertex(Graph *graph, char label) {
   graph->vertices[(int)label] = item;
   return item;
 }
+
+void graph_add_edge(Graph *graph, Vertex *a, Vertex *b) {
+  graph->edges[a->label][b->label] = true;
+}
src/03/graph.h
@@ -1,10 +1,14 @@
+#include <stdbool.h>
+
 typedef struct {
   char label;
 } Vertex;
 
 typedef struct {
   Vertex *vertices[128];
+  bool edges[128][128];
 } Graph;
 
 Graph *graph_initialize(void);
 Vertex *graph_add_vertex(Graph *graph, char label);
+void graph_add_edge(Graph *graph, Vertex *a, Vertex *b);
src/03/graph_test.c
@@ -33,6 +33,17 @@ Ensure(add_vertex_adds_max_number_of_verticies_to_graph) {
   }
 }
 
+Ensure(add_edge_connects_two_vertices) {
+  Graph *graph = graph_initialize();
+  Vertex *a = graph_add_vertex(graph, 'a');
+  Vertex *b = graph_add_vertex(graph, 'b');
+
+  graph_add_edge(graph, a, b);
+
+  assert_that(graph->edges[a->label][b->label], is_equal_to(true));
+  assert_that(graph->edges[b->label][a->label], is_equal_to(false));
+}
+
 TestSuite *graph_tests() {
   TestSuite *x = create_test_suite();
 
@@ -43,5 +54,7 @@ TestSuite *graph_tests() {
   add_test(x, add_vertex_returns_a_new_vertex);
   add_test(x, add_vertex_adds_max_number_of_verticies_to_graph);
 
+  add_test(x, add_edge_connects_two_vertices);
+
   return x;
 }