Author: Hayden Smith 2021
Why?
What?
A graph G = (V, E) is a general data structure that consists of:
They may contain cycles, and there is no implicit order of items
Graphs are everywhere:
We can turn a map of Australia into a graph quite easily.
Distance | Adelaide | Brisbane | Canberra | Darwin | Melbourne | Perth | Sydney |
---|---|---|---|---|---|---|---|
Adelaide | - | 2055 | 1390 | 3051 | 732 | 2716 | 1605 |
Brisbane | 2055 | - | 1291 | 3429 | 1671 | 4771 | 982 |
Canberra | 1390 | 1291 | - | 4441 | 658 | 4106 | 309 |
Darwin | 3051 | 3429 | 4441 | - | 3783 | 4049 | 4411 |
Melbourne | 732 | 1671 | 658 | 3783 | - | 3448 | 873 |
Perth | 2716 | 4771 | 4106 | 4049 | 3448 | - | 3972 |
Sydney | 1605 | 982 | 309 | 4411 | 873 | 3972 | - |
This allows us to ask questions like:
We will look at these, but in later lectures:
Directed Graph
Weighted Graph
Core operations:
We will assume simplicity that vertexes are ints.
#include <stdbool.h>
typedef struct GraphRep *Graph;
typedef int Vertex;
typedef struct Edge {
Vertex v;
Vertex w;
} Edge;
Graph GraphNew(int);
void GraphEdgeInsert(Graph, Edge);
void GraphEdgeRemove(Graph, Edge);
bool GraphAdjacent(Graph, Vertex, Vertex);
void GraphShow(Graph);
void GraphDestroy(Graph);
Graph.h