From 007076ed2be0e2fa03b94d0711e54c7f562d87cc Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 7 Sep 2020 18:49:24 -0600 Subject: feat: record edges in graph --- src/03/graph.c | 4 ++++ src/03/graph.h | 4 ++++ src/03/graph_test.c | 13 +++++++++++++ 3 files changed, 21 insertions(+) (limited to 'src/03') diff --git a/src/03/graph.c b/src/03/graph.c index 0467e8b..1e67202 100644 --- a/src/03/graph.c +++ b/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; +} diff --git a/src/03/graph.h b/src/03/graph.h index d295490..95c96d8 100644 --- a/src/03/graph.h +++ b/src/03/graph.h @@ -1,10 +1,14 @@ +#include + 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); diff --git a/src/03/graph_test.c b/src/03/graph_test.c index 27af514..fd025a5 100644 --- a/src/03/graph_test.c +++ 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; } -- cgit v1.2.3