CSE 40872 Lecture 013

Topic:Graphs (Overview, Traversal)
Author: Peter Bui <pbui@cse.nd.edu>
Date: September 30, 2009

Graphs

Defined by a set of vertices or nodes and edges which connect one vertex to another:

G = (V, E)
http://upload.wikimedia.org/wikipedia/commons/thumb/5/5b/6n-graf.svg/333px-6n-graf.svg.png

Terminology

Undirected vs. Directed

If undirected, then edge (x, y) also implies (y, x).

Acyclic vs. Cyclic

If acyclic, then no cycles or loops. Trees are acyclic, undirected graphs.

Adjacency Matrix

NxN matrix that records the edges.

  0 1 2 3 4
0[0 1 1 1 0]
1[1 0 1 0 0]
2[1 1 0 1 0]
3[1 0 1 0 0]
4[0 0 0 0 0]

Good for checking if edge(x,y) is in the graph, but can take up a lot of space.

AM Implementation

typedef struct {
    size_t nedges;
    size_t nvertices;
    bool   edges[][];
} Graph;

static void create_graph(Graph &g, size_t nedges, size_t nvertices) {
    g.nedges    = nedges;
    g.nvertices = nvertices;
    g.edges     = new bool*[nvertices];
    for (size_t v = 0; v < nvertices; v++)
        g.edges[v] = new bool[nvertices];
}

Adjacency List

Each vertex contains a collection of connected vertices

0 [1 2 3]
1 [0 2]
2 [0 1 3]
3 [0 2]
4 []

Space efficient, but less efficient for checking if edge is in graph.

AL Implementation

typedef struct {
    size_t nedges;
    size_t nvertices;
    vector < vector<int> > edges;
} Graph;

static void create_graph(Graph &g, size_t nedges, size_t nvertices) {
    g.nedges    = nedges;
    g.nvertices = nvertices;
    g.edges.resize(nvertices);
}

You could use the same struct for either representation, it's just how you use it.