Chapter 15: Graphs

Data Structures and Algorithms with C
KönigsbergDefinitionsMatrix and listsAdjacency multilistDFSBFSComponents
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Define

Use G = (V, E), directed/undirected graphs, adjacency, subgraphs, paths, degree and connectivity.

Prove

Justify that the degree sum = 2e (Theorem 1) and that connectivity needs e ≥ n − 1.

Implement

Build adjacency matrices, adjacency lists, sequential arrays, orthogonal lists and adjacency multilists in C.

Traverse

Code and trace depth-first (DFS) and breadth-first (BFS) search; analyze them as O(n + e).

Apply

Find connected components, cycles and bipartiteness with DFS/BFS.

Choose

Pick the right representation from the operations the problem needs: “data structure first, then code”.

Introduction

The problem: the 7 Bridges of Königsberg

C (north bank)B (south bank)A (island)D7 bridges

On the Pregel river, two islands (A, D) and two banks (B, C) are joined by 7 bridges. Can you cross every bridge exactly once and return to the start?

C (north bank)B (south bank)A (island)D7 bridgesCADB

Euler (1736) made the land masses vertices and the bridges edges: what matters is not the shape but the connections (notation, not drawing).

C (north bank)B (south bank)A (island)D7 bridgesCADBd=3d=5d=3d=3

Such a walk exists only if every vertex has even degree. Here all degrees are odd (5, 3, 3, 3) → no such walk.

This is regarded as the birth of graph theory. Euler (pronounced Oiler) stated the result as a theorem; its proof is left to Graph Theory courses (4th year: “Graph Theory and Combinatorics”).

Definitions and Terminology

Definition: G = (V, E)

A graph G consists of two finite sets: a nonempty set V of vertices (nodes) and a possibly empty set E of edges (arcs). Undirected: (v0, v1) ≡ (v1, v0); directed (digraph): <v0, v1> ≠ <v1, v0> — <tail, head>: the arrow leaves the tail and points into the head.

0123G1: connected (K4)0123456G2: a tree, no cycles012G3: directed02134567G4: two components
GraphV(G)E(G)
G1{0, 1, 2, 3}{(0,1), (0,2), (0,3), (1,2), (1,3), (2,3)}
G2{0, …, 6}{(0,1), (0,2), (1,3), (1,4), (2,5), (2,6)}
G3{0, 1, 2}{<0,1>, <1,0>, <1,2>} — both are listed in a digraph

E is a subset of the Cartesian product V × V (it need not contain every pair).

Definitions and Terminology

Where Is the Graph Data Structure Used?

Engineering

Analyzing electrical circuits, project planning (activity networks), identifying chemical compounds.

Paths and networks

Shortest paths, routing, many problems on computer networks.

Trees and ordering

Trees are graphs; topological sorting, finite automata.

Flow

Flow in transport networks; max flow: moving as much oil/water as possible from source to sink.

Spanning trees

Minimum spanning tree: connecting villages with the least cable.

Connectivity

Is the graph connected, how many components, is a network broken?

Instructor: “Where aren't they used! Graphs have a very wide range of uses.”

Definitions and Terminology

Terminology: Adjacent Vertices, Subgraphs

Adjacent

If (v0, v1) is an edge of an undirected graph, v0 and v1 are adjacent, and the edge is incident on both.

Subgraph

If G′ is a subgraph of G, then V(G′) ⊆ V(G) and E(G′) ⊆ E(G). A single vertex is also a subgraph.

0123G100121230123Some of the possible subgraphs of G1012G301012Subgraphs of G3

Definitions and Terminology

Path, Connectivity and Degree

  • Path: a sequence of vertices from vp to vq such that (vp, vi1), (vi1, vi2), …, (vin, vq) are edges.
  • If there is a path from v0 to v1, they are connected; if every distinct pair (vi, vj) is joined by a path, G is connected.
  • Degree: the number of edges incident on the vertex. In G2 d(0) = 2, in G1 d(1) = 3, in G2 d(4) = 1.
  • In a digraph, degree = in-degree + out-degree. In G3 d(0) = 1 + 1 = 2; d(1) = 1 + 2 = 3; d(2) = 1 + 0 = 1.
012in: 1out: 1

Vertex 0 in G3: d = 1 + 1 = 2

“In G3, d = 1 + 1 = 2” refers to vertex 0: a degree is always stated for a particular vertex.

Definitions and Terminology

Theorem 1: The Sum of Degrees = 2e

In a graph G with n vertices and e edges, 2e = d0 + d1 + … + dn−1. Proof: each edge adds exactly 2 to the degree sum of its two endpoints. It holds for simple and non-simple (multi-edge) graphs. Simple graph: at most one edge between two vertices, no self-loops.

0123d=1d=1d=0d=0edges processed: e = 1Σ d = 1 + 1 + 0 + 0 = 22e = 2

Edge 1 added: each endpoint's degree rose by 1 → the sum rose by 2.

0123d=2d=1d=1d=0edges processed: e = 2Σ d = 2 + 1 + 1 + 0 = 42e = 4

Edge 2 added: each endpoint's degree rose by 1 → the sum rose by 2.

0123d=3d=1d=1d=1edges processed: e = 3Σ d = 3 + 1 + 1 + 1 = 62e = 6

Edge 3 added: each endpoint's degree rose by 1 → the sum rose by 2.

0123d=3d=2d=2d=1edges processed: e = 4Σ d = 3 + 2 + 2 + 1 = 82e = 8

Edge 4 added: each endpoint's degree rose by 1 → the sum rose by 2.

0123d=3d=3d=2d=2edges processed: e = 5Σ d = 3 + 3 + 2 + 2 = 102e = 10

Edge 5 added: each endpoint's degree rose by 1 → the sum rose by 2.

0123d=3d=3d=3d=3edges processed: e = 6Σ d = 3 + 3 + 3 + 3 = 122e = 12

Edge 6 added: each endpoint's degree rose by 1 → the sum rose by 2.

Instructor: “‘Read the book that will earn you money!’ says the teacher; ‘No, I won't buy it!’ comes the reply. The habit of reading professional books must be built at this age.”

Representations

How Do We Represent a Graph in Memory?

1Adjacency matrix
2Adjacency lists (+ inverse)
3Sequential array representation
4Orthogonal lists (directed)
5Adjacency multilists
+Weighted representation
Instructor: “First we explain the concept, then its implementation on the computer… Our aim here is not to teach discrete mathematics. It is to teach ‘how do we represent a graph in memory?’”

Representations

1. Adjacency Matrix

For G with n vertices, an n × n matrix: a[i][j] = 1 if (i, j) ∈ E. For an undirected graph the matrix is symmetric; to save space only the lower or upper triangle may be stored — memory is still O(n²).

G1 — (0,2) ∈ E(G1): a[0][2] = a[2][0] = 1
0123
00111
11011
21101
31110
G3 — <0,1> and <1,2> marked; not symmetric
012
0010
1101
2000
Finding a degree

Count the 1s in the row (or column): O(n). In a digraph the row gives out-degree, the column in-degree; together O(2n). In G3 out-degree d(2) = 0, in-degree d(2) = 1.

Counting edges

Count all the 1s and divide by 2: O(n²). By symmetry half the matrix is enough; still O(n²/2) = O(n²).

Representations

G4's Adjacency Matrix — Checking Symmetry

G4; red frame: cells often gotten wrong
01234567
001100000
110010000
210010000
301100000
400000100
500001010
600000101
700000010
02134567

G4: two components

Common mistake: writing row 1 as 1 0 1 0 … and row 7 as … 0 0 1. The matrix of an undirected graph must be symmetric; these rows break symmetry. Correct: row 1 1 0 0 1 0 0 0 0 (neighbors 0, 3), row 7 0 0 0 0 0 0 1 0 (neighbor 6).

The number of components can also be found from the matrix: each DFS scans neighbors in O(n), so O(n²) in total.

Adjacency matrix · Run the code step by step

Code Trace: degree(a, 3, 1)

012001011012000a[][] (G3)012

degree(a, 3, 1): finding the degree of vertex 1 in G3.

012001011012000a[][] (G3)012

The counters are set to zero.

012001011012000a[][] (G3)012

Row 1, column 0: a[1][0] = 1 → out = 1.

012001011012000a[][] (G3)012

Row 1, column 1: a[1][1] = 0 → out = 1.

012001011012000a[][] (G3)012

Row 1, column 2: a[1][2] = 1 → out = 2.

012001011012000a[][] (G3)012

Column 1, row 0: a[0][1] = 1 → in = 1.

012001011012000a[][] (G3)012

Column 1, row 1: a[1][1] = 0 → in = 1.

012001011012000a[][] (G3)012

Column 1, row 2: a[2][1] = 0 → in = 1.

012001011012000a[][] (G3)012

return 2 + 1 = 3. Both loops run n times → O(2n) = O(n).

int degree(int a[][MAX_V], int n, int v)
{
    int j, out = 0, in = 0;
    for (j = 0; j < n; j++)
        out += a[v][j];      /* row    */
    for (j = 0; j < n; j++)
        in += a[j][v];      /* column */
    return out + in;
}

Representations

2. Adjacency Lists

Instead of each matrix row, a linked list: graph[i] points to the list of i's neighbors. Drawback: an undirected graph uses 2 nodes per edge.

#define MAX_V 50   /* max number of vertices */
typedef struct node *node_ptr;
typedef struct node {
    int vertex;         /* the neighbor's number  */
    node_ptr link;      /* next neighbor in list   */
} node;                 /* don't forget ';' and the name */
node_ptr graph[MAX_V];   /* one list per vertex */
0123102320133012

G1 represented with adjacency lists

Number of edges

Count all the nodes and divide by 2 (Theorem 1): O(n + e). In graph theory, complexity is usually stated in terms of e.

Degree

In an undirected graph, counting the nodes in list i is enough: O(di).

Representations

Adjacency Lists: Undirected and Directed Examples

0121032033124554665776G4's adjacency lists011022G3 (outgoing edges)011021G3's inverse (incoming edges)012
Undirected (G4): the benefit of sorted lists

If the lists are sorted, in each list we can count the nodes whose number is below the head's (e.g. (1,0)) and skip the rest; we visit half the vertices, but it is still O(n + e).

Directed (G3)

<0,1> ≠ <1,0> → every edge must be counted. Out-degree is easy from the lists; in-degree requires scanning every list. The fix: also keep the inverse (transpose) lists, built in a single pass.

Representations

A Sparse Graph: 11 Vertices, 2 Edges

056914710238G5

Lists: graph[1] → 4, graph[4] → 1, graph[7] → 8, graph[8] → 7. The other 7 heads are NULL.

Total: 11 heads + 2e = 4 nodes. Matrix: 11 × 11 = 121 cells.

11 vertices, 2 edges, 9 components. The matrix wastes 117 of its 121 cells; the lists hold only 11 heads + 4 nodes. Consider lists for sparse graphs and a matrix for dense ones — but always decide by the operations to be performed.

Adjacency lists · Run the code step by step

Code Trace: adding edges to the structure

012012edges added so far are bold

Edge (0,1) → first add_edge(0, 1): memory for a new node is allocated (NULL check!).

0112012edges added so far are bold

p->vertex = 1, p->link = NULL, graph[0] = p: the node went to the front of the list.

0112012edges added so far are bold

Edge (0,1) → then add_edge(1, 0): memory for a new node is allocated (NULL check!).

01102012edges added so far are bold

p->vertex = 0, p->link = NULL, graph[1] = p: the node went to the front of the list.

01102012edges added so far are bold

Edge (0,2) → first add_edge(0, 2): memory for a new node is allocated (NULL check!).

021102012edges added so far are bold

p->vertex = 2, p->link = &node(1), graph[0] = p: the node went to the front of the list.

021102012edges added so far are bold

Edge (0,2) → then add_edge(2, 0): memory for a new node is allocated (NULL check!).

0211020012edges added so far are bold

p->vertex = 0, p->link = NULL, graph[2] = p: the node went to the front of the list.

0211020012edges added so far are bold

Edge (1,2) → first add_edge(1, 2): memory for a new node is allocated (NULL check!).

02112020012edges added so far are bold

p->vertex = 2, p->link = &node(0), graph[1] = p: the node went to the front of the list.

02112020012edges added so far are bold

Edge (1,2) → then add_edge(2, 1): memory for a new node is allocated (NULL check!).

021120210012edges added so far are bold

p->vertex = 1, p->link = &node(0), graph[2] = p: the node went to the front of the list.

void add_edge(int u, int v)   /* u -> v */
{
    node_ptr p = malloc(sizeof(node));
    if (p == NULL) exit(1);       /* no memory */
    p->vertex = v;
    p->link = graph[u];   /* at front: O(1) */
    graph[u] = p;
}

Representations

Who Builds the Graph?

Instructor: “Edges come in, and they must be added to the structure. I looked and drew it; a program can't just look and draw! The algorithm has to do the work.”
Insert at the front

Each edge is placed in O(1); the list ends up in the reverse of arrival order. Total build time O(n + e).

Insert at the end

Keeps the order, but each time you must walk to the NULL (O(d)); or keep a last pointer for each list.

Sorted insertion

Keeping each list sorted by vertex number: edge counting and traversal order become predictable; insertion is O(d).

Freeing

At the end of the program, every graph[i] list must be walked and freed.

Inverse adjacency lists · Run the code step by step

Code Trace: transpose()

adj[] (G3)011022rev[] (transpose)012

First the rev[] heads are set to NULL.

adj[] (G3)011022rev[] (transpose)0102

Edge <0,1>: 0 is added to list rev[1] (<1,0>).

adj[] (G3)011022rev[] (transpose)01102

Edge <1,0>: 1 is added to list rev[0] (<0,1>).

adj[] (G3)011022rev[] (transpose)011021

Edge <1,2>: 1 is added to list rev[2] (<2,1>).

adj[] (G3)011022rev[] (transpose)011021

adj[2] is empty: no edges leave 2.

adj[] (G3)011022rev[] (transpose)011021

Each node was looked at once: the transpose was built in O(n + e). Now in-degree is also found in O(d).

void transpose(node_ptr adj[], node_ptr rev[],
               int n)
{
    int i;
    node_ptr p;
    for (i = 0; i < n; i++) rev[i] = NULL;
    for (i = 0; i < n; i++)
        for (p = adj[i]; p; p = p->link)
            insert(rev, p->vertex, i); /* <j,i> */
}

Representations

3. Sequential Representation with an Array

Undirected, n vertices, e edges: a single array int vertex[n + 2*e + 1]. The first n + 1 cells are starting indices; the neighbors of i are in vertex[vertex[i]] … vertex[vertex[i+1] − 1].

0911121331541751862072282391102110123130143151162175184196205217226n + 1 = 9 heads (vertex[n] = n + 2e + 1 = 23)2e = 14 neighbor entries

The array for G4. Each edge is still stored twice, but there are no pointers.

0911121331541751862072282391102110123130143151162175184196205217226n + 1 = 9 heads (vertex[n] = n + 2e + 1 = 23)2e = 14 neighbor entriesneighbors of 0: vertex[0] = 9 … vertex[1] = 11 (excl.) → 1, 2

Ex. 1: from [0] = 9 (incl.) to [1] = 11 (excl.): indices 9 and 10 → neighbors 1, 2.

0911121331541751862072282391102110123130143151162175184196205217226n + 1 = 9 heads (vertex[n] = n + 2e + 1 = 23)2e = 14 neighbor entriesneighbors of 1: vertex[1] = 11 … vertex[2] = 13 (excl.) → 0, 3

Ex. 2: [1] = 11, [2] = 13 → indices 11, 12 → neighbors 0, 3.

0911121331541751862072282391102110123130143151162175184196205217226n + 1 = 9 heads (vertex[n] = n + 2e + 1 = 23)2e = 14 neighbor entriesneighbors of 7: vertex[7] = 22 … vertex[8] = 23 (excl.) → 6

Ex. 3: [7] = 22, [8] = 23 → only index 22 → a single neighbor, 6.

Instructor: “Storing is one issue; the efficiency of the algorithms that will run on that data is another… This structure is not efficient for many insertions and deletions. The computers in a network, say, are fixed; if the problem is to store them, this structure may suit.”

Sequential representation · Run the code step by step

Code Trace: print_adj(vertex, 5)

0911121331541751862072282391102110123130143151162175184196205217226n + 1 = 9 heads (vertex[n] = n + 2e + 1 = 23)2e = 14 neighbor entries

print_adj(vertex, 5): the neighbors of vertex 5.

0911121331541751862072282391102110123130143151162175184196205217226k = 18

k = 18 < vertex[6] = 20 → vertex[18] = 4 is printed.

0911121331541751862072282391102110123130143151162175184196205217226k = 19

k = 19 < vertex[6] = 20 → vertex[19] = 6 is printed.

0911121331541751862072282391102110123130143151162175184196205217226n + 1 = 9 heads (vertex[n] = n + 2e + 1 = 23)2e = 14 neighbor entriesneighbors of 5: vertex[5] = 18 … vertex[6] = 20 (excl.) → 4, 6

k = 20 = vertex[6] → the loop ends. Neighbor count = vertex[6] − vertex[5] = the degree: O(d).

void print_adj(const int vertex[], int i)
{
    int k;
    for (k = vertex[i]; k < vertex[i + 1]; k++)
        printf("%d ", vertex[k]);
    printf("\n");
}

Representations

4. Orthogonal Representation of Digraphs

001122001122row headscolumn heads (same nodes, drawn twice)011012tail | head | column link | row linkdark cell = NULL

G3: each edge node sits in one row list (out) and one column list (in) at the same time

Two links

Column link, for the head: where we come into this vertex from. Row link, for the tail: where we go from this vertex. One node per edge is enough.

Like a sparse matrix

It resembles the linked sparse matrix structure; it can be made circular, doubly linked, with head nodes. “Using 2e nodes bothered us, so we found this one.”

Instructor: “Never trust the first solution that comes to mind, or its code. If the underlying data structure were something else, could I solve it more efficiently?”

Representations

5. Adjacency Multilists

typedef struct edge *edge_ptr;
typedef struct edge {
    short marked;        /* processed? */
    int vertex1, vertex2;  /* ends   */
    edge_ptr list1;    /* next for vertex1 */
    edge_ptr list2;    /* next for vertex2 */
} edge;
edge_ptr graph[MAX_V];

The edge is the basic unit: there is one node for (vi, vj), but it is on both vi's and vj's lists. marked stops a processed edge from being handled again. To carry data on a vertex, add a field such as char data.

Nodevertex1vertex2list1list2
N001N1N3
N102N2N3
N203NULLN4
N312N4N5
N413NULLN5
N523NULLNULL

Heads for G1: graph[0] = N0, graph[1] = N0, graph[2] = N1, graph[3] = N2. A vertex with no edges has a NULL head.

Adjacency multilist · Run the code step by step

Code Trace: walking over the neighbors of 2

01··N0N1N302··N1N2N303··N2∅N412··N3N4N513··N4∅N523··N5∅∅fields: vertex1 | vertex2 | list1 | list2 (∅ = NULL)p

p = graph[2] = N1: the first edge of 2.

01··N0N1N302··N1N2N303··N2∅N412··N3N4N513··N4∅N523··N5∅∅fields: vertex1 | vertex2 | list1 | list2 (∅ = NULL)p

N1 = (0,2): v is vertex2 → print the other end, 0.

01··N0N1N302··N1N2N303··N2∅N412··N3N4N513··N4∅N523··N5∅∅fields: vertex1 | vertex2 | list1 | list2 (∅ = NULL)p

Follow the link: p = p->list2 = N3.

01··N0N1N302··N1N2N303··N2∅N412··N3N4N513··N4∅N523··N5∅∅fields: vertex1 | vertex2 | list1 | list2 (∅ = NULL)p

N3 = (1,2): v is vertex2 → print the other end, 1.

01··N0N1N302··N1N2N303··N2∅N412··N3N4N513··N4∅N523··N5∅∅fields: vertex1 | vertex2 | list1 | list2 (∅ = NULL)p

Follow the link: p = p->list2 = N5.

01··N0N1N302··N1N2N303··N2∅N412··N3N4N513··N4∅N523··N5∅∅fields: vertex1 | vertex2 | list1 | list2 (∅ = NULL)p

N5 = (2,3): v is vertex1 → print the other end, 3.

01··N0N1N302··N1N2N303··N2∅N412··N3N4N513··N4∅N523··N5∅∅fields: vertex1 | vertex2 | list1 | list2 (∅ = NULL)

Follow the link: p = p->list1 = NULL.

01··N0N1N302··N1N2N303··N2∅N412··N3N4N513··N4∅N523··N5∅∅fields: vertex1 | vertex2 | list1 | list2 (∅ = NULL)

NULL: no more matching nodes. The neighbors of 2 are 0, 1, 3 — each edge was stored only once.

void print_adj(int v)
{
    edge_ptr p = graph[v];
    while (p) {
        if (p->vertex1 == v) {      /* v: vertex1 */
            printf("%d ", p->vertex2);  p = p->list1;
        } else {                    /* v: vertex2 */
            printf("%d ", p->vertex1);  p = p->list2;
        }
    }
}

Representations

Adding an Edge to an Adjacency Multilist

  • Traversal: follow list1 if v is vertex1, list2 if v is vertex2. Stop at NULL. “Any more? Follow the link!”
  • Insertion: you could walk the list to NULL and append; but inserting at the front saves the walk: O(1).
  • An undirected graph needs one head array; a digraph keeps a tail array as well.
  • The gain: only e nodes instead of 2e.
void ml_add_edge(int u, int v)
{
    edge_ptr p = malloc(sizeof(edge));
    if (p == NULL) exit(1);
    p->marked = 0;
    p->vertex1 = u;  p->vertex2 = v;
    p->list1 = graph[u];  /* u's list */
    p->list2 = graph[v];  /* v's list */
    graph[u] = graph[v] = p;  /* front of both */
}
Instructor: “If the edges come in this order and you are told ‘place each in O(1)’, insert at the front of the list! I give no credit to anyone who draws it this way (appending to the end), because I can see they appended.”

Representations

Exercise: <0,1>, <0,3>, <1,2>, <2,0>, <3,2>

0132

For the digraph with this edge list, give the adjacency matrix, the adjacency lists (and their inverse) and the orthogonal lists.

0132
row = tail, column = head
0123
00101
10010
21000
30010

Matrix: a[i][j] = 1 if <i,j> exists. Row sums are out-degrees (2, 1, 1, 1), column sums in-degrees (1, 1, 2, 1).

013122032

outgoing (adjacency lists)

021021330

incoming (transpose of the lists)

Lists and inverse: 2e = 10 nodes in all.

Nodetailheadtail linkhead link
N101N2NULL
N203NULLNULL
N312NULLN5
N420NULLNULL
N532NULLNULL
Vertextail (out)head (in)
0N1N4
1N3N1
2N4N3
3N5N2

Orthogonal: only e = 5 nodes (N1…N5). Each node is on its tail's out-list and its head's in-list.

Representations

Takeaway: First Decide How the Data Is Stored

RepresentationMemory(u,v) present?u's neighborsInsert/delete
Adjacency matrixO(n²)O(1)O(n)O(1)
Adjacency listsO(n + 2e)O(du)O(du)front O(1) / search O(d)
Sequential arrayn + 2e + 1O(du)O(du)expensive (shifting)
Orthogonal (directed)O(n + e)O(d)in and out O(d)O(1) at front
MultilistO(n + e)O(du)O(du)O(1) at front
Instructor: “The first solution that comes to mind may not be a good one. Maybe an n³ algorithm can be brought down to n. That is where you must design the data structure well. Before writing any code, I think about how to store the data. Storing data is not that easy!”

Representations

Weighted Graph Representation

42581ABCD

weight = distance, cost, time…

cost matrix: ∞ when there is no edge (a large constant in C)
ABCD
A042∞
B4015
C2108
D∞580
#define INF 1000000   /* not INT_MAX: sums overflow */
int cost[MAX_V][MAX_V];

typedef struct wnode *wnode_ptr;
typedef struct wnode {
    int vertex;
    int weight;        /* cost of the edge */
    wnode_ptr link;
} wnode;
wnode_ptr wgraph[MAX_V];
  • In the matrix, a weight instead of 1, and ∞ when there is no edge.
  • In the lists, a weight field is added to the node.
  • Using INT_MAX for ∞ overflows in the sum d + w; choose a safe large constant.
  • MST and shortest-path algorithms use this representation.

Traversals

Basic Graph Operations: Traversal

  • Given an undirected G = (V, E) and a vertex v, visit every vertex reachable from v (visit = print its data field).
  • Depth-first search (DFS) resembles preorder traversal of a tree.
  • Breadth-first search (BFS) resembles level-order traversal of a tree.
  • Both use a graph stored as adjacency lists; the lists are sorted.
01234567

Sample graph

0121034205631741752762773456

graph[] adjacency lists

Traversals

Depth-First Search (DFS)

  • Visit v; pick an unvisited w from v's list and continue the search from w.
  • A stack is needed to remember our place in the list; when u has nowhere left to go, pop a vertex from the stack and continue.
  • A recursive routine makes this seemingly complex scheme easy: the stack is the call stack.
  • The global array visited[] must start out FALSE.
#include <stdio.h>
#include <stdbool.h>
#define TRUE  true
#define FALSE false
bool visited[MAX_V];   /* don't forget ';' */
void dfs(int v)   /* depth-first search */
{
    node_ptr w;
    visited[v] = TRUE;    /* visited */
    printf("%d ", v);
    for (w = graph[v]; w; w = w->link)
        if (!visited[w->vertex])
            dfs(w->vertex);
}

Depth-first search · Run the code step by step

Code Trace: dfs(0)

01234567visited[]0T1F2F3F4F5F6F7Fgraph[0]12

dfs(0) is called: 0 is marked and printed.

01234567visited[]0T1T2F3F4F5F6F7Fgraph[1]034

0 → 1: 1 is unvisited → dfs(1). 1 is marked and printed.

01234567visited[]0T1T2F3F4F5F6F7Fgraph[1]0w34

w → 0: visited[0] is TRUE; skip.

01234567visited[]0T1T2F3T4F5F6F7Fgraph[3]17

1 → 3: 3 is unvisited → dfs(3). 3 is marked and printed.

01234567visited[]0T1T2F3T4F5F6F7Fgraph[3]1w7

w → 1: visited[1] is TRUE; skip.

01234567visited[]0T1T2F3T4F5F6F7Tgraph[7]3456

3 → 7: 7 is unvisited → dfs(7). 7 is marked and printed.

01234567visited[]0T1T2F3T4F5F6F7Tgraph[7]3w456

w → 3: visited[3] is TRUE; skip.

01234567visited[]0T1T2F3T4T5F6F7Tgraph[4]17

7 → 4: 4 is unvisited → dfs(4). 4 is marked and printed.

01234567visited[]0T1T2F3T4T5F6F7Tgraph[4]1w7

w → 1: visited[1] is TRUE; skip.

01234567visited[]0T1T2F3T4T5F6F7Tgraph[4]17w

w → 7: visited[7] is TRUE; skip.

01234567visited[]0T1T2F3T4T5T6F7Tgraph[5]27

(dfs(4) is done.) 7 → 5: 5 is unvisited → dfs(5). 5 is marked and printed.

01234567visited[]0T1T2T3T4T5T6F7Tgraph[2]056

5 → 2: 2 is unvisited → dfs(2). 2 is marked and printed.

01234567visited[]0T1T2T3T4T5T6F7Tgraph[2]0w56

w → 0: visited[0] is TRUE; skip.

01234567visited[]0T1T2T3T4T5T6F7Tgraph[2]05w6

w → 5: visited[5] is TRUE; skip.

void dfs(int v)   /* depth-first search */
{
    node_ptr w;
    visited[v] = TRUE;    /* visited */
    printf("%d ", v);
    for (w = graph[v]; w; w = w->link)
        if (!visited[w->vertex])
            dfs(w->vertex);
}

Depth-first search · Run the code step by step

Code Trace: dfs(0) (cont.)

01234567visited[]0T1T2T3T4T5T6T7Tgraph[6]27

2 → 6: 6 is unvisited → dfs(6). 6 is marked and printed.

01234567visited[]0T1T2T3T4T5T6T7Tgraph[6]2w7

w → 2: visited[2] is TRUE; skip.

01234567visited[]0T1T2T3T4T5T6T7Tgraph[6]27w

w → 7: visited[7] is TRUE; skip.

01234567visited[]0T1T2T3T4T5T6T7Tgraph[5]27w

dfs(6), dfs(2) are done; resume where 5 left off. w → 7: visited[7] is TRUE; skip.

01234567visited[]0T1T2T3T4T5T6T7Tgraph[7]3456w

dfs(5) is done; resume where 7 left off. w → 6: visited[6] is TRUE; skip.

01234567visited[]0T1T2T3T4T5T6T7Tgraph[1]034w

dfs(7), dfs(3) are done; resume where 1 left off. w → 4: visited[4] is TRUE; skip.

01234567visited[]0T1T2T3T4T5T6T7Tgraph[0]12w

dfs(1) is done; resume where 0 left off. w → 2: visited[2] is TRUE; skip.

01234567visited[]0T1T2T3T4T5T6T7T

dfs(0) is done. Visit order 0 1 3 7 4 5 2 6; the orange edges form a tree.

void dfs(int v)   /* depth-first search */
{
    node_ptr w;
    visited[v] = TRUE;    /* visited */
    printf("%d ", v);
    for (w = graph[v]; w; w = w->link)
        if (!visited[w->vertex])
            dfs(w->vertex);
}

Traversals

What Does the DFS Result Depend On?

Start vertex

From 0: 0 1 3 7 4 5 2 6. The start need not be 0: from 6, 6 2 0 1 3 7 4 5; from 4, 4 1 0 2 5 7 3 6. Same vertices, different order.

Effect of list order

Had 0's list been {2, 1} instead of {1, 2}, the order would be 0 2 5 7 3 1 4 6. Whether lists were built by inserting at the front or end, or kept sorted, changes the traversal.

Analysis

dfs is called at most once per vertex; each list is scanned once: O(n + e). There are at most n(n − 1)/2 edges.

Data structure

With a matrix, finding v's neighbors would take O(n), O(n²) in total. With lists we traverse in linear time.

The traversal produced a tree (the depth-first spanning tree). Adding one more edge to it creates a cycle, and it is no longer a tree.

Traversals

Breadth-First Search (BFS)

  • Start at v and mark it; then visit all the vertices on v's list.
  • Then the unvisited neighbors of the first vertex on v's list, then those of the second…
  • A visited vertex goes into a queue (first in, first out); when a list is finished, a vertex is taken from the queue and its list is scanned.
  • The search ends when the queue is empty.
Instructor: “Just as depth-first search by its nature required a stack, this one requires a queue.”
void bfs(int v)   /* breadth-first search */
{
    node_ptr w;
    queue_ptr front = NULL, rear = NULL;
    printf("%d ", v);
    visited[v] = TRUE;
    addq(&front, &rear, v);
    while (front) {
        v = deleteq(&front, &rear);
        for (w = graph[v]; w; w = w->link)
            if (!visited[w->vertex]) {
                printf("%d ", w->vertex);
                addq(&front, &rear, w->vertex);
                visited[w->vertex] = TRUE;
            }
    }
}

Traversals

The Dynamic Queue Used by BFS

typedef struct queue *queue_ptr;
typedef struct queue {
    int vertex;
    queue_ptr link;
} queue;
void addq(queue_ptr *f,
          queue_ptr *r, int d)
{
    queue_ptr p = malloc(sizeof(queue));
    if (p == NULL) exit(1);
    p->vertex = d;  p->link = NULL;
    if (*f) (*r)->link = p;  else *f = p;
    *r = p;
}
int deleteq(queue_ptr *f,
             queue_ptr *r)
{
    queue_ptr p = *f;   /* f != NULL */
    int d = p->vertex;
    *f = p->link;
    if (*f == NULL) *r = NULL;
    free(p);
    return d;
}
A prototype such as int deleteq(queue_ptr *), taking only the front pointer, is not enough. When the queue empties, the rear pointer must become NULL too, so deleteq also gets r.

Breadth-first search · Run the code step by step

Code Trace: bfs(0)

01234567visited[]0T1F2F3F4F5F6F7Fqueue0▲ front▲ rear

0 is printed, marked and added to the queue.

01234567visited[]0T1F2F3F4F5F6F7Fqueueempty (front = rear = NULL)graph[0]12

The queue is not empty: the front item 0 is removed; now 0's list is scanned.

01234567visited[]0T1T2F3F4F5F6F7Fqueue1▲ front▲ reargraph[0]1w2

w → 1: unvisited → printed, added to the rear of the queue, marked.

01234567visited[]0T1T2T3F4F5F6F7Fqueue12▲ front▲ reargraph[0]12w

w → 2: unvisited → printed, added to the rear of the queue, marked.

01234567visited[]0T1T2T3F4F5F6F7Fqueue2▲ front▲ reargraph[1]034

The queue is not empty: the front item 1 is removed; now 1's list is scanned.

01234567visited[]0T1T2T3T4F5F6F7Fqueue23▲ front▲ reargraph[1]03w4

w → 3: unvisited → printed, added to the rear of the queue, marked. (0 was already marked.)

01234567visited[]0T1T2T3T4T5F6F7Fqueue234▲ front▲ reargraph[1]034w

w → 4: unvisited → printed, added to the rear of the queue, marked.

01234567visited[]0T1T2T3T4T5F6F7Fqueue34▲ front▲ reargraph[2]056

The queue is not empty: the front item 2 is removed; now 2's list is scanned.

01234567visited[]0T1T2T3T4T5T6F7Fqueue345▲ front▲ reargraph[2]05w6

w → 5: unvisited → printed, added to the rear of the queue, marked. (0 was already marked.)

01234567visited[]0T1T2T3T4T5T6T7Fqueue3456▲ front▲ reargraph[2]056w

w → 6: unvisited → printed, added to the rear of the queue, marked.

01234567visited[]0T1T2T3T4T5T6T7Fqueue456▲ front▲ reargraph[3]17

The queue is not empty: the front item 3 is removed; now 3's list is scanned.

01234567visited[]0T1T2T3T4T5T6T7Tqueue4567▲ front▲ reargraph[3]17w

w → 7: unvisited → printed, added to the rear of the queue, marked. (1 was already marked.)

front = rear = NULL;
printf("%d ", v);  visited[v] = TRUE;
addq(&front, &rear, v);
while (front) {
    v = deleteq(&front, &rear);
    for (w = graph[v]; w; w = w->link)
        if (!visited[w->vertex]) {
            printf("%d ", w->vertex);
            addq(&front, &rear, w->vertex);
            visited[w->vertex] = TRUE;
        }
}

Breadth-first search · Run the code step by step

Code Trace: bfs(0) (cont.)

01234567visited[]0T1T2T3T4T5T6T7Tqueue567▲ front▲ reargraph[4]17

The queue is not empty: the front item 4 is removed; now 4's list is scanned.

01234567visited[]0T1T2T3T4T5T6T7Tqueue67▲ front▲ reargraph[5]27

The queue is not empty: the front item 5 is removed; now 5's list is scanned.

01234567visited[]0T1T2T3T4T5T6T7Tqueue7▲ front▲ reargraph[6]27

The queue is not empty: the front item 6 is removed; now 6's list is scanned.

01234567visited[]0T1T2T3T4T5T6T7Tqueueempty (front = rear = NULL)graph[7]3456

The queue is not empty: the front item 7 is removed; now 7's list is scanned.

01234567visited[]0T1T2T3T4T5T6T7Tqueueempty (front = rear = NULL)

The queue is empty → the search ends. Order 0 1 2 3 4 5 6 7: level by level. The orange edges are the breadth-first spanning tree.

front = rear = NULL;
printf("%d ", v);  visited[v] = TRUE;
addq(&front, &rear, v);
while (front) {
    v = deleteq(&front, &rear);
    for (w = graph[v]; w; w = w->link)
        if (!visited[w->vertex]) {
            printf("%d ", w->vertex);
            addq(&front, &rear, w->vertex);
            visited[w->vertex] = TRUE;
        }
}

Traversals

BFS: Analysis and Beyond

Analysis

Each vertex enters the queue once → the while loop runs n times. The lists hold 2e nodes in all: d0 + … + dn−1 = 2e. Total O(n + e); with a matrix O(n²).

What does it give?

Visits in order of number of edges (level) from the start; the shortest-path tree of an unweighted graph.

Beyond

The ideas of depth-first/breadth-first search reappear in AI when a search space is traversed as a tree: minimax, B* search, branch and bound.

DFS or BFS?

DFS: stack/recursion, goes deep (components, cycles, topological thinking). BFS: queue, layer by layer (path with fewest edges).

Instructor: “In game problems too, if they say ‘the moves could be these,’ that means it expands breadth-first.”

Components and Beyond

Connected Components

void connected(int n)
{
    int i;
    for (i = 0; i < n; i++)
        if (!visited[i]) {   /* new */
            dfs(i);
            printf("\n");
        }
}

Is the undirected graph connected, and how many components does it have? Each DFS (or BFS) call marks one whole component; increment a counter on each new line and you get the number of components too.

0123456789

Output: 0 1 3 2 / 4 5 6 / 7 8 9

012345678910

Output: 0 / 1 2 4 3 / 5 6 7 8 / 9 10

The output of line 2 is 1 2 4 3; 5 belongs to the next component.

Connected components · Run the code step by step

Code Trace: connected(10)

0123456789visited[]0T1T2T3T4F5F6F7F8F9Fcomponents: 1

i = 0: unmarked → a new component; dfs(0) visits 0 1 3 2.

0123456789visited[]0T1T2T3T4F5F6F7F8F9Fcomponents: 1

dfs returned; go to a new line.

0123456789visited[]0T1T2T3T4F5F6F7F8F9Fcomponents: 1

i = 1: already marked (visited by the previous DFS) → the if is skipped, O(1).

0123456789visited[]0T1T2T3T4F5F6F7F8F9Fcomponents: 1

i = 2: already marked (visited by the previous DFS) → the if is skipped, O(1).

0123456789visited[]0T1T2T3T4F5F6F7F8F9Fcomponents: 1

i = 3: already marked (visited by the previous DFS) → the if is skipped, O(1).

0123456789visited[]0T1T2T3T4T5T6T7F8F9Fcomponents: 2

i = 4: unmarked → a new component; dfs(4) visits 4 5 6.

0123456789visited[]0T1T2T3T4T5T6T7F8F9Fcomponents: 2

dfs returned; go to a new line.

0123456789visited[]0T1T2T3T4T5T6T7F8F9Fcomponents: 2

i = 5: already marked (visited by the previous DFS) → the if is skipped, O(1).

0123456789visited[]0T1T2T3T4T5T6T7F8F9Fcomponents: 2

i = 6: already marked (visited by the previous DFS) → the if is skipped, O(1).

0123456789visited[]0T1T2T3T4T5T6T7T8T9Tcomponents: 3

i = 7: unmarked → a new component; dfs(7) visits 7 8 9.

0123456789visited[]0T1T2T3T4T5T6T7T8T9Tcomponents: 3

dfs returned; go to a new line.

0123456789visited[]0T1T2T3T4T5T6T7T8T9Tcomponents: 3

i = 8: already marked (visited by the previous DFS) → the if is skipped, O(1).

0123456789visited[]0T1T2T3T4T5T6T7T8T9Tcomponents: 3

i = 9: already marked (visited by the previous DFS) → the if is skipped, O(1).

0123456789visited[]0T1T2T3T4T5T6T7T8T9Tcomponents: 3

The loop is done: 3 components. Every vertex and every list was processed once in total.

void connected(int n)
{
    int i;
    for (i = 0; i < n; i++)
        if (!visited[i]) {   /* new */
            dfs(i);
            printf("\n");
        }
}

Components and Beyond

Analysis: n × O(n + e)?

The outer for runs n times, and the inner dfs() was O(n + e). Is the complexity their product, O(n²)?

Instructor: “Is it? Is it? Is it? Does it really run that long? No! … ‘I'll just multiply them all and state the result.’ Wrong!”
Why O(n + e)?

With a single component, the first iteration runs in O(n + e) and every visited[] becomes TRUE; for the other values of i the if is skipped. The components are disjoint, so the total work is O(n + e).

With a matrix?

Each dfs call searches for neighbors in O(n) → finding components is O(n²). “If that is written efficiently too…”

Components and Beyond

Theorem: A Connected Graph Has at Least n − 1 Edges

  • Theorem: a graph needs at least n − 1 edges to be connected.
  • It is one-way: n − 1 edges are necessary but not sufficient; a graph with n − 1 edges may or may not be connected. With fewer it is certainly not connected.
  • At most: if all vertices are adjacent to each other (the complete graph Kn), (n − 1) + (n − 2) + … + 1 = n(n − 1)/2. So e can be quadratic in n.
  • In a connected graph: n − 1 ≤ e ≤ n(n − 1)/2.
0123401234K5: 10 edgesn − 1 = 4 edges, connected
Instructor: “Tic-tac-toe… ‘How do I spoil my opponent's move?’ I expect these concepts will settle better once you read about it.”

Cycles with DFS · Run the code step by step

Code Trace: does an undirected graph have a cycle?

0123visited[]0T1F2F3F

has_cycle(0, -1): 0 is marked.

0123visited[]0T1T2F3F

has_cycle(1, 0): 1 is marked.

0123visited[]0T1T2F3F

w → 0: marked, but it is 1's parent; the edge we came in on is not a cycle.

0123visited[]0T1T2T3F

has_cycle(2, 1): 2 is marked.

0123visited[]0T1T2T3F

w → 1: marked, but it is 2's parent; the edge we came in on is not a cycle.

0123visited[]0T1T2T3T

has_cycle(3, 2): 3 is marked.

0123visited[]0T1T2T3T

w → 1: marked and not the parent (2) → a back edge (3,1): there is a cycle! TRUE is passed up.

bool has_cycle(int v, int parent)
{
    node_ptr w;
    visited[v] = TRUE;
    for (w = graph[v]; w; w = w->link)
        if (!visited[w->vertex]) {
            if (has_cycle(w->vertex, v)) return TRUE;
        } else if (w->vertex != parent)
            return TRUE;          /* back edge */
    return FALSE;
}

Components and Beyond

Cycle Detection: Undirected and Directed

Undirected

In DFS, a visited neighbor that is not the parent = a back edge = a cycle. O(n + e).

Directed

An edge to a vertex “currently on the stack” (gray) is a cycle; three colors: white/gray/black. A topological sort routine also catches cycles.

Why does it matter?

With prerequisite tasks, a cycle means the project never finishes; in an MST, a cycle means an extra edge.

With a matrix

The same DFS becomes O(n²) with a matrix.

Components and Beyond

Bipartite Graph Test

  • V can be split into two sets with every edge joining different sets: a bipartite graph (e.g. K3,3, students–courses).
  • Test: two-color with BFS, giving each neighbor the opposite color. If an edge joins two same-colored vertices, it is not bipartite.
  • Theorem: a graph is bipartite ⇔ it has no odd-length cycle.
  • Same cost as BFS: O(n + e); start it separately for each component.
0123456-cycle012triangle

BFS: 0 colored (color 0). Each neighbor gets the opposite color.

0123456-cycle012triangle

BFS: 1 colored (color 1). Each neighbor gets the opposite color.

0123456-cycle012triangle

BFS: 5 colored (color 1). Each neighbor gets the opposite color.

0123456-cycle012triangle

BFS: 2 colored (color 0). Each neighbor gets the opposite color.

0123456-cycle012triangle

BFS: 4 colored (color 0). Each neighbor gets the opposite color.

0123456-cycle012triangle

BFS: 3 colored (color 1). Each neighbor gets the opposite color.

0123456-cycle: bipartite ✓012triangle

Triangle: 0 colored.

0123456-cycle: bipartite ✓012triangle

Triangle: 1 colored.

0123456-cycle: bipartite ✓012triangle

Triangle: 2 colored.

0123456-cycle: bipartite ✓012triangle

Two adjacent vertices with the same color → not bipartite (there is an odd cycle).

Lab · C code step by step

Lab: DFS and BFS

Summary

Complexity Summary

OperationMatrixListsNote
MemoryO(n²)O(n + e)2e nodes in undirected lists
Degree (undirected)O(n)O(d)inverse lists for in-degree in digraphs
Number of edgesO(n²)O(n + e)Theorem 1: sum / 2
DFS / BFSO(n²)O(n + e)each vertex once
Connected componentsO(n²)O(n + e)not a product!
Cycles / bipartitenessO(n²)O(n + e)on top of DFS / BFS
Building transpose lists—O(n + e)in a single pass

n: number of vertices · e: number of edges · d: degree of a vertex · n − 1 ≤ e ≤ n(n − 1)/2 (connected simple graph)

Summary

Common Mistakes

1Assuming <v,u> exists in a digraph because <u,v> does; taking the matrix to be symmetric.
2In BFS, marking a vertex when it leaves the queue instead of when it enters → the same vertex is queued several times.
3Forgetting to reset visited[] to FALSE before each traversal (a global array!).
4Multiplying the outer for by DFS and calling components O(n(n + e)).
5Not checking the result of malloc when building lists; not freeing the lists at the end of the program.
6Appending each edge at the end and walking the list from the start each time; missing the O(1) insert at the front.

Summary

Exercises

  1. Is there a simple undirected graph with degree sequence (3, 3, 2, 2, 1)? How many edges does it have?
  2. In the sequential array representation of G4, which indices hold the neighbors of vertex 5?
  3. Run DFS and BFS on the 8-vertex sample graph starting at 7 (lists sorted).
  4. What happens if the line visited[w->vertex] = TRUE in the BFS code is moved after deleteq?
  5. Write a routine that counts the components of a graph given as an adjacency matrix; what is its complexity?
  6. To report in-degree in O(1) for a digraph, which structure would you keep, and how would you update it?
Answers / hints
  1. The sum is 11, which is odd → it cannot be 2e (Theorem 1). No such graph exists.
  2. vertex[5] = 18, vertex[6] = 20 → indices 18, 19: neighbors 4 and 6.
  3. DFS(7): 7 3 1 0 2 5 6 4 · BFS(7): 7 3 4 5 6 1 2 0.
  4. The same vertex can enter the queue more than once (e.g. 7, through both 3 and 4); output repeats and the work exceeds O(e).
  5. For each unmarked i, a DFS that scans matrix rows: O(n²).
  6. An in[] counter per vertex (or inverse lists), updated whenever an edge is added or removed.

Summary

Chapter Summary

Concepts

G = (V, E); directed/undirected, adjacent, subgraph, path, degree; Σd = 2e; connectivity needs e ≥ n − 1.

Representations

Matrix O(n²); lists O(n + e); sequential array; orthogonal (directed); multilist (one node per edge).

Traversals

DFS = recursion/stack; BFS = queue. Both O(n + e) with lists, O(n²) with a matrix.

Applications

Connected components in O(n + e); cycle detection; bipartite test; groundwork for spanning trees.

Method

First the problem, then how the data is stored; don't trust the first solution.

Summary

References

  • E. Horowitz, S. Sahni, S. Anderson-Freed, Fundamentals of Data Structures in C, 2nd ed., Chapter 6 (Graphs).
  • T. H. Cormen et al., Introduction to Algorithms, 4th ed., Chapter 20 (Elementary Graph Algorithms).
  • R. Sedgewick, Algorithms in C, Part 5: Graph Algorithms, 3rd ed.

The End

Chapter 15: Graphs
Next chapter: Chapter 16: Sets and Union-Find · Course page
1 / 1 Data Structures in C