Chapter 9
Graph Theory
1
Graphs
2
✤ A graph consists of nodes and edges
✤ The set of all nodes (or vertices) in a graph is V
✤ The set of all edges in a graph is E
✤ Each edge connects one or two vertices (called its’ endpoints)
✤ A graph is formally represented (V, E)
✤ We will (usually) stick to simple graphs
CA MD PA AK
NY MA VT
Simple Graphs
✤ No loops
✤ Endpoints of an edge are distinct
✤ No multiple edges
✤ between the same two vertices
3
Directed Graphs
✤ A directed edge, or arc connects two vertices, but has a start and end
✤ Formally an arc (u, v) starts at u and ends at v
✤ A graph with directed edges is a directed graph or digraph
4
5
Seven Bridges of Königsberg
Traveling Salesman Problem
6
CA MD PA AK
NY MA VT
Terminology
✤ Two vertices u and v in an undirected graph G are neighbors if there exists an edge e in G which connects u, and v
✤ Equivalently, u and v are said to be adjacent if there exists an edge which connects them
✤ The set of all neighbors of u (u’s neighborhood) is N(u)
✤ This is defined similarly for sets of vertices
7
Degree
✤ If an edge e has vertex u as one of its’ endpoints, e is incident with u
✤ The degree of a node u, deg(u), is the count of the number of edges incident with u
✤ A node with degree 0 is isolated
8
CA MD PA AK
NY MA VT
2 3 1 0
4 3 1
In/Out -Degree
✤ In directed graphs, separate degrees
✤ In-Degree: # of edges ending at udeg-(u)
✤ Out-Degree: # of edges starting at udeg+(u)
✤ Vertices with in-degree 0 are sources
✤ Vertices with out-degree 0 are sinks
9
CA MD PA AK
NY MA VT
1 2 2 1
1 2 1
2 1 0 1
4 1 0
deg−(v)v∈V∑ = deg+(v)
v∈V∑ = E
Representations
10
Graph ADT
11
✤ V = set of verticesE = set of edges
✤ Three operations
✤ getDegree(u)
✤ getAdjacent(u)
✤ isAdjacentTo(u, v)
Graph Representations
12
✤ Adjacency list
✤ For each vertex, list the adjacent vertices
✤ Good for sparse graphs with few edges
✤ Adjacency matrix
✤ 2D matrix of 1s and 0s
✤ 1 iff there is an edge from i to j
✤ Good for dense graphs with many edges
Graph Representations
13
V Adj.1 1,2,52 1,3,53 2,44 3,5,65 1,2,46 4
V 1 2 3 4 5 61 1 1 0 0 1 02 1 0 1 0 1 03 0 1 0 1 0 04 0 0 1 0 1 15 1 1 0 1 0 06 0 0 0 1 0 0
Graph Adjacency List Adjacency Matrix
Directed Graphs
14
7
6
5
4
3
1
2
V Term1 6,72 5,63 1,2,44 5,75 16 47
V 1 2 3 4 5 6 71 0 0 0 0 0 1 12 0 0 0 0 1 1 03 1 1 0 1 0 0 04 0 0 0 0 1 0 15 1 0 0 0 0 0 06 0 0 0 1 0 0 07 0 0 0 0 0 0 0
Graph Adjacency List Adjacency Matrix
Practice
15
e
c d
f
b
ga
Practice
16
b
c d
f
a
e
Performance of List vs Matrix
17
Space getDegree(u) isAdjacentTo(u,v) getAdjacent(u)
AdjacencyList O(V+E) O(D(u)) O(D(u)) O(D(u))
AdjacencyMatrix O(V2) O(V) O(1) O(V)
Paths
18
✤ A path (sometimes a walk) is a series of edges which starts at a vertex, travels from vertex to vertex along edges and ends at some vertex
✤ Formally a path p is a set of edges s.t. p.start = e0.start p.end = en.end en.end = en+1.start
✤ If p.start = p.end, then p is a circuit (cycle)
✤ If no edge is repeated, the path is simple
Cyclic vs Acyclic
✤ Directed graphs can be cyclic or acyclic
✤ Cyclic: contains a cycle
✤ Acyclic: does not contain any cycles
19
Cycle Practice
20
Cycle Practice
21
How many cycles are there?
22
Graph Search
23
Depth-First Search (DFS)
✤ Visit some start vertex
✤ Follow an edge to a vertex which hasn’t been explored
✤ Visit that vertex
✤ Follow an edge from that vertex to another unexplored vertex
✤ If there are no edges to choose from, backtrack to the previous vertex
24
a b c d
e f g h
i j k
l m
a b c d
e f g h
i j k
l m
a b c d
e f g h
i j k
l m
a b c d
e f g h
i j k
l m
a b c d
e f g h
i j k
l m
a b c d
e f g h
i j k
l m
DFS
25
a b c d
e f g h
i j k
l m
Order visited: a, b, c, g, h, d, k, j, f, e, i, m, l
A Recursive DFS Algorithm
procedure DFS(G: connected graph with vertices v1, v2, ..., vn)T := tree consisting only of the vertex v1fvisit(v1)
procedure visit(v: vertex of G)for each vertex w adjacent to v and not yet in T add vertex w and edge {v, w} to T visit(w)
26
Practice
27
b c d e
g h i j
l m n
a
f
k
Breadth-First Search
✤ Visit some start vertex
✤ Visit all neighbors of the start vertex
✤ Visit all those neighbors’ neighbors
✤ Repeat until all vertices visited
28
a b c d
e f g h
i j k
l m
a b c d
e f g h
i j k
l m
a b c d
e f g h
i j k
l m
a b c d
e f g h
i j k
l m
a b c d
e f g h
i j k
l m
a b c d
e f g h
i j k
l m
a b c d
e f g h
i j k
l m
BFS
29
a b c d
e f g h
i j k
l m
Order visited: a, b, e, c, f, i, g, j, m, h, k, l, d
A (non-recursive) BFS Algorithm
procedure BFS (G: connected graph with vertices v1, v2, ..., vn)T := tree consisting only of vertex v1L := empty listput v1 in the list L of unprocessed verticeswhile L is not empty remove the first vertex, v, from L for each neighbor w of v if w is not in L and not in T then add w to the end of the list L add w and edge {v, w} to T
30
Shortest-Path Problems
31
Shortest Path
32
✤ The length of a path is the number of edges in it
✤ Many problems try to find the shortest path between two vertices
✤ Flights with the fewest stopovers
✤ Driving directions with few instructions
✤ Can use DFS or BFS
Practice
33
Weighted Graphs
✤ Add a weight to every edge
✤ weights are normally “costs”
✤ Length of a path is the sum of the weights of each edge
34
LA
NYC
Atlanta
Miami
Chicago
Vegas
Boston
SF 2:20
2:00
2:10
3:50
1:40
1:50
2:104:05 0:
50
1:55
2:45
1:301:15
2:55
LA
NYC
Atlanta
Miami
Chicago
Vegas
Boston
SF $89
$89
$69
$129
$99
$59
$79$129 $3
9
$79
$99
$69$39
$99
LA
NYC
Atlanta
Miami
Chicago
Vegas
Boston
SF 957
834
908
2451
606
722
8602534 19
1
760
1090
595349
1855
Dijkstra’s Algorithm
✤ Finds shortest path between X and Z in O(n2) time
✤ Greedy algorithm:
✤ Start with C = {X}
✤ Search the neighbors of C to find next closest node to X not in C
✤ Add it to C
✤ Repeat until Z is the next closest node to X35
b d
c e
X Z
45
81
210
2
6
3
Dijkstra’s Algorithm
36
b d
c e
X Z
45
81
210
2
6
3
∞ ∞
∞
∞∞
{X}, 0
Dijkstra’s Algorithm
37
b d
c e
X Z
45
81
210
2
6
3
{X,b}, 4 ∞
∞
∞{X,c}, 2
{X}, 0
Dijkstra’s Algorithm
38
b d
c e
X Z
45
81
210
2
6
3
{X,c,b}, 3 {X,c,d}, 10
∞
{X,c,e}, 12{X,c}, 2
{X}, 0
Dijkstra’s Algorithm
39
b d
c e
X Z
45
81
210
2
6
3
{X,c,b}, 3 {X,c,b,d}, 8
∞
{X,c,e}, 12{X,c}, 2
{X}, 0
Dijkstra’s Algorithm
40
b d
c e
X Z
45
81
210
2
6
3
{X,c,b}, 3 {X,c,b,d}, 8
{X,c,b,d,Z}, 14
{X,c,b,d,e}, 10{X,c}, 2
{X}, 0
Dijkstra’s Algorithm
41
b d
c e
X Z
45
81
210
2
6
3
{X,c,b}, 3 {X,c,b,d}, 8
{X,c,b,d,e,Z}, 13
{X,c,b,d,e}, 10{X,c}, 2
{X}, 0
Dijkstra’s Algorithm
42
b d
c e
X Z
45
81
210
2
6
3
{X,c,b}, 3 {X,c,b,d}, 8
{X,c,b,d,e,Z}, 13
{X,c,b,d,e}, 10{X,c}, 2
{X}, 0
Practice
✤ Find the shortest path from g to fusing Djikstra’s Algorithm
43
Connectivity
44
Network Reliability
45
✤ What would happen if my router in NY went offline? If CA got knocked out?
✤ I often want there to always be a path available between all the nodes in my graph
CA MD PA AK
NY MA VT
Connectedness
46
✤ Two nodes are connected if there exists a path between them
✤ Otherwise they are disconnected
✤ If every pair of nodes in a graph is connected, then the whole graph is connected
✤ Otherwise it is disconnected
✤ If a graph is connected, then there exists a simple path between every pair of vertices in the graph
CA
MD PA
AK
NY
MA
VT
Connected vs Disconnected
47
4
2 1
3
5
4
2 1
3
5
Connected Components✤ A part of a graph which is
disconnected from all other parts is called a connected component
✤ Formally, a connected subgraph of G which is not a proper subgraph of any other connected subgraph of G is a connected component
✤ A connected graph has 1 connected component
✤ A disconnected graph has 2+48
4
2 1
3
5
Directed Graphs
49
✤ The underlying undirected graph of a digraph is the same graph, minus directions
✤ A digraph is weakly connected if the underlying undirected graph is connected
✤ i.e. if the digraph is “in one piece”
7
6
5
4
3
1
2
7
6
5
4
3
1
2
Strongly Connected
✤ A digraph G is strongly connected if for every pair of vertices a,b ∈ G, there exists a path from a to b and from b to a
✤ Note: this includes the pair a,a
50
a
e
b
d
c
a
e
b
d
c
Practice
51
f
c d
g
b
he
a
Strongly Connected Components
52
✤ Analogous to connected components
✤ Maximal subgraphs of G which are strongly connectedi.e. they are not contained within any other such strongly connected subgraphs
✤ We can compress a digraph into a directed acyclic graph by reducing it to a graph of its strongly connected components
✤ Treat all nodes in a SCC as interchangeable
✤ Model how to hop from one SCC to another
Strongly Connected Components
53
f
c d
g
b
he
a
f
c d
g
b
he
a
Euler and Hamilton Paths/Cycles
54
Euler Paths and Circuits
55
✤ Euler Path: path in G which uses every edge exactly once
✤ can visit a node more than once
✤ Euler Circuit: Euler path which is also a circuit
Practice
56
a b
d c
e
a b
d c
e
a b
d c e
Practice
57
a b
d c
a
g
c
d
f
e
b
a b
d c
Hamilton Paths and Circuits
✤ Hamilton Path: path visiting every node exactly once
✤ each edge can only be used once
✤ Hamilton Circuit: hamilton path which is also a circuit
58
Practice
59
a b
e c
d
a b
d c
a
b
d
c
e fg
Traveling Salesperson Problem
✤ Traveling Salesperson Problem (TSP)or Traveling Salesman Problem
✤ Find the shortest hamilton cycle
✤ i.e. visiting every node once and returning to the start
60
Grand Rapids
Saginaw
Detroit
Kalamazoo
Toledo
135
133
142
58
98137
113
56
147
167
Traveling Salesperson Problem
✤ This is an NP-hard problem
✤ There are (n - 1)!/2 Hamilton cycles
✤ O(n!) to compute exhaustively
✤ FedEx pays lots and lots of money for improvements on this problem
✤ Often okay to find an approximate solution
61
Route Total Distanc
eD - T - GR - S - K - D 610D - T - GR - K - S - D 516D - T - K - S - GR - D 588D - T - K - GR - S - D 458D - T - S - K - GR - D 540D - T - S - GR - K - D 504D - S - T - GR - K - D 598D - S - T - K - GR - D 576D - S - K - GR - T - D 682D - S - GR - T - K - D 646D - GR - S - T - K - D 670D - GR - T - S - K - D 728
Section 9.5
Spanning Trees
62
Spanning Tree
63
Etna Old Town
Herman
Hampden
Bangor
Orono Etna Old Town
Herman
Hampden
Bangor
Orono
Spanning Tree Tree w/ every
vertex of G
Finding Spanning Trees
✤ Trees are acyclic, graphs aren’t
✤ Remove edges to break up cycles
✤ Keep graph connected
64
a b c d
e f g
Practice
65
b c d e
g h i j
l m n
a
f
k
Minimum Spanning Trees
✤ Weighted graphs
✤ Find spanning tree with the smallest possible sum of edge weights
✤ Applications:
✤ Making connected graphs
66
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3
Prim’s Algorithm
procedure Prim (G)T := a minimum weight edgefor i := 1 to n - 2 e := an edge of minimum weight, incident to a vertex in T, not forming a cycle in T T := T with e addedreturn T
67
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3
Kruskal’s Algorithm
procedure Kruskal (G)T := an empty graphfor i := 1 to n - 1 e := any edge of minimum weight in G, not forming a cycle if added to T T := T with e addedreturn T
68
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3
a b c d
e
i j k l
f gh
2 3 1
3
5213
4 3
3
4 2
3
4
1
3