Graphs

Sometimes it seems a graph can represent almost anything....

Definitions

A graph, or network is a data structure that can be used to represent general relationships among a set of elements. The elements are called vertices, nodes, or points; the relationships are called edges or arcs.

examplegraph.png

Rahter than define terms, we'll illustrate their use in the graph above:

There are a few terms that apply only to connected graphs. Consider the block in the graph above containing vertices $\{D, C, O, N\}$ and edges $\{q, p, h, i, t, j\}$:

Uses

Graphs can model, for example

Kinds of Graphs

Here are a few types of graphs. The types overlap, of course; you can have a simple, directed, weighted graph.

Exercise: Prove that in a simple graph with $n$ nodes and $e$ edges that $e < n(n-1)/2$.
Exercise: Prove that in a tree with $n$ nodes that the number of edges is $n-1$.
Exercise: Prove that a graph with $n$ nodes and less than $n-1$ edges is not connected.

Implementations

Adjacency Matrix

Rows and columns are indexed by nodes; cells contain the edges (or pointers to edges, really).

ABC DEFGH IJKLMNOP
A g un d
Bk
C j h
D j q
Eu l
F m
G m
H l fs,s e
I c
Jd e o
K a
L b
M
N h q p
O i,t p
P o r

Note that undirected edges appear twice. It is also possible to store only a triangular matrix.

Incidence List

For each node, store the list(s) of its outgoing edges, incoming edges, and incident undirected edges.

Adjacency List

For each node, store a list of the nodes that it is adjacent to.