master
 1#include "graph.h"
 2#include <stdio.h>
 3#include <stdlib.h>
 4
 5/**
 6 * Creates a new Vertex for use in a Graph.
 7 *
 8 * @param label The label to attach to the node.
 9 * @return Returns a new vertex
10 */
11Vertex *vertex_initialize(char label) {
12  Vertex *item = malloc(sizeof(Vertex));
13  item->label = label;
14  return item;
15};
16
17/**
18 * Initializes a new Graph
19 *
20 * @return Returns a new instance of a Graph.
21 */
22Graph *graph_initialize(void) {
23  Graph *item = malloc(sizeof(Graph));
24  for (int i = 0; i < 128; ++i)
25    item->vertices[i] = NULL;
26  return item;
27}
28
29/**
30 * Inserts a new vertex into a graph with the provided label.
31 *
32 * @param graph The graph to insert a new vertex into
33 * @param label The label to apply to the new vertex.
34 * @return Returns the new vertex
35 */
36Vertex *graph_add_vertex(Graph *graph, char label) {
37  Vertex *item = vertex_initialize(label);
38  graph->vertices[(int)label] = item;
39  return item;
40}
41
42/**
43 * Updates a adjacency matrix to indicate that an edge exists
44 * between two vertexes.
45 *
46 * @param graph The graph to modify.
47 * @param a The vertex that points to vertex b.
48 * @param b The vertex that vertex a points to.
49 */
50void graph_add_edge(Graph *graph, Vertex *a, Vertex *b) {
51  graph->edges[a->label][b->label] = true;
52}
53
54/**
55 * Returns true or false to specify if vertex `a`
56 * in a graph is connected to vertex `b` in the same
57 * graph.
58 *
59 * @param graph The graph to investigate
60 * @param a The starting vertext to check
61 * @param b The vertex that vertex a might be pointing at.
62 * @return Returns true if an edge exists between the two vertexes otherwise
63 * false.
64 */
65bool graph_has_edge(Graph *graph, Vertex *a, Vertex *b) {
66  return graph->edges[a->label][b->label];
67}