Social Network 1.0.0
The second major course output (MCO2) for CCDSALG.
Loading...
Searching...
No Matches
graph.h File Reference

The helper functions that are used for handling and interacting with graphs and their vertices. More...

#include <stdbool.h>
#include <stddef.h>

Go to the source code of this file.

Classes

struct  Graph
 A collection implementing the adjacency list data graph structure using arrays. More...
struct  GraphEdge
 A(n) (un)directed connection between two vertices in a graph. More...

Macros

#define MAX_VERTEX_LABEL_LENGTH   8
 The maximum number of characters a vertex’ label can have.
#define MAX_GRAPH_VERTEX_COUNT   20
 The maximum number of vertices a graph can contain.

Typedefs

typedef char Vertex[MAX_VERTEX_LABEL_LENGTH+1]
 A string-labeled vertex in a graph.
typedef struct Graph Graph
 A collection implementing the adjacency list data graph structure using arrays.
typedef struct GraphEdge GraphEdge
 A(n) (un)directed connection between two vertices in a graph.

Functions

void initialize_graph (Graph *const graph, const size_t vertex_count)
 Initializes a graph’s vertex adjacency arrays to their default values and sets its vertex count.
void clone_graph (const Graph *const graph, Graph *const new_graph)
 Clones the contents of a graph into another graph.
size_t get_vertex_index (const Graph *const graph, const Vertex vertex)
 Gets the index of the adjacency array of a vertex from a graph.
bool has_vertex (const Graph *const graph, const Vertex vertex)
 Checks if a graph contains a vertex.
void add_adjacency (Graph *const graph, const Vertex key_vertex, const Vertex adjacent_vertex)
 Adds an adjacency between a pair of vertices to a graph.
size_t get_adjacency_count (const Vertex adjacencies[MAX_GRAPH_VERTEX_COUNT])
 Gets the total number of adjacencies in a key led array of adjacencies.
bool has_adjacency (const Vertex adjacencies[MAX_GRAPH_VERTEX_COUNT], const Vertex adjacent_vertex)
 Checks if a key led array of adjacencies contains an adjacent vertex.
void sort_adjacencies (Graph *const graph)
 Sorts the adjacency arrays by their keys alphabetically, and sorts their adjacent vertices alphabetically.
void get_edges (const Graph *const graph, GraphEdge edges[], size_t *const edge_count)
 Gets the edges formed by connections between vertices contained in a graph.
bool has_edge (const GraphEdge edges[], const size_t edge_count, const Vertex source_vertex, const Vertex destination_vertex)
 Checks if a pair of vertices already has a corresponding edge in an array of edges.

Detailed Description

The helper functions that are used for handling and interacting with graphs and their vertices.

Author
Raphael Panaligan
Jek Degullado

Function Documentation

◆ add_adjacency()

void add_adjacency ( Graph *const graph,
const Vertex key_vertex,
const Vertex adjacent_vertex )

Adds an adjacency between a pair of vertices to a graph.

This adds the adjacent vertex to the array of the key vertex. If the array doesn’t exist, this creates the array before adding the adjacent vertex. If an adjacent vertex is not supplied, then this will still create the array for the key vertex if it doesn’t exist.

Parameters
[in,out]graphThe graph to add to.
[in]key_vertexThe key vertex of the adjacency.
[in]adjacent_vertexThe vertex adjacent to the key vertex.
Precondition
The graph’s vertex count has been checked against and the number of vertices will not exceed MAX_GRAPH_VERTEX_COUNT.

◆ clone_graph()

void clone_graph ( const Graph *const graph,
Graph *const new_graph )

Clones the contents of a graph into another graph.

Parameters
[in]graphThe graph to clone from.
[out]new_graphThe graph to clone to.

◆ get_adjacency_count()

size_t get_adjacency_count ( const Vertex adjacencies[MAX_GRAPH_VERTEX_COUNT])

Gets the total number of adjacencies in a key led array of adjacencies.

Parameters
[in]adjacenciesThe key led adjacencies to get from.
Returns
The total number of adjacencies in the key led array of adjacencies.

◆ get_edges()

void get_edges ( const Graph *const graph,
GraphEdge edges[],
size_t *const edge_count )

Gets the edges formed by connections between vertices contained in a graph.

Parameters
[in]graphThe graph to get from.
[out]edgesThe edges of the graph.
[out]edge_countThe number of edges the graph has.

◆ get_vertex_index()

size_t get_vertex_index ( const Graph *const graph,
const Vertex vertex )

Gets the index of the adjacency array of a vertex from a graph.

Parameters
[in]graphThe graph to get from.
[in]vertexThe vertex to get the index of.
Returns
The index of the vertex’ adjacency array, or MAX_GRAPH_VERTEX_COUNT if the graph doesn’t contain the vertex.

◆ has_adjacency()

bool has_adjacency ( const Vertex adjacencies[MAX_GRAPH_VERTEX_COUNT],
const Vertex adjacent_vertex )

Checks if a key led array of adjacencies contains an adjacent vertex.

Parameters
[in]adjacenciesThe key led adjacencies to check against.
[in]adjacent_vertexThe adjacent vertex to check for.
Returns
Whether the key led array of adjacencies contains the adjacent vertex.

◆ has_edge()

bool has_edge ( const GraphEdge edges[],
const size_t edge_count,
const Vertex source_vertex,
const Vertex destination_vertex )

Checks if a pair of vertices already has a corresponding edge in an array of edges.

Parameters
[in]edgesThe edges to check against.
[in]edge_countThe number of edges to check against.
[in]source_vertexThe first or source vertex to check for.
[in]destination_vertexThe second or destination vertex to check for.
Returns
Whether the pair of vertices has a corresponding edge in the array of edges.

◆ has_vertex()

bool has_vertex ( const Graph *const graph,
const Vertex vertex )

Checks if a graph contains a vertex.

Parameters
[in]graphThe graph to check.
[in]vertexThe vertex to check for.
Returns
Whether the graph contains the vertex.

◆ initialize_graph()

void initialize_graph ( Graph *const graph,
const size_t vertex_count )

Initializes a graph’s vertex adjacency arrays to their default values and sets its vertex count.

Parameters
[in,out]graphThe graph to initialize.
vertex_countThe number of vertices the graph should contain.
Precondition
The vertex_count doesn’t exceed MAX_GRAPH_VERTEX_COUNT.

◆ sort_adjacencies()

void sort_adjacencies ( Graph *const graph)

Sorts the adjacency arrays by their keys alphabetically, and sorts their adjacent vertices alphabetically.

Parameters
[in,out]graphThe graph to sort.