summaryrefslogtreecommitdiff
path: root/src/03/graph.c
blob: 808043bbd25501e550e82e5d3e0fa7e6779eb7a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "graph.h"
#include <stdio.h>
#include <stdlib.h>

/**
 * Creates a new Vertex for use in a Graph.
 *
 * @param label The label to attach to the node.
 * @return Returns a new vertex
 */
Vertex *vertex_initialize(char label) {
  Vertex *item = malloc(sizeof(Vertex));
  item->label = label;
  return item;
};

/**
 * Initializes a new Graph
 *
 * @return Returns a new instance of a Graph.
 */
Graph *graph_initialize(void) {
  Graph *item = malloc(sizeof(Graph));
  for (int i = 0; i < 128; ++i)
    item->vertices[i] = NULL;
  return item;
}

/**
 * Inserts a new vertex into a graph with the provided label.
 *
 * @param graph The graph to insert a new vertex into
 * @param label The label to apply to the new vertex.
 * @return Returns the new vertex
 */
Vertex *graph_add_vertex(Graph *graph, char label) {
  Vertex *item = vertex_initialize(label);
  graph->vertices[(int)label] = item;
  return item;
}

/**
 * Updates a adjacency matrix to indicate that an edge exists
 * between two vertexes.
 *
 * @param graph The graph to modify.
 * @param a The vertex that points to vertex b.
 * @param b The vertex that vertex a points to.
 */
void graph_add_edge(Graph *graph, Vertex *a, Vertex *b) {
  graph->edges[a->label][b->label] = true;
}

/**
 * Returns true or false to specify if vertex `a`
 * in a graph is connected to vertex `b` in the same
 * graph.
 *
 * @param graph The graph to investigate
 * @param a The starting vertext to check
 * @param b The vertex that vertex a might be pointing at.
 * @return Returns true if an edge exists between the two vertexes otherwise
 * false.
 */
bool graph_has_edge(Graph *graph, Vertex *a, Vertex *b) {
  return graph->edges[a->label][b->label];
}