Graph Theory
Graph Theory
Introduction to Algorithms
A graph G = (V, E)
V = set of vertices
E = set of edges
In an undirected graph:
edge(u, v) = edge(v, u)
In a directed graph:
edge(u,v) goes from vertex u to vertex v, notated u →
v
Chapter 22: Elementary Graph edge(u, v) is not the same as edge(v, u)
Algorithms
2
d e d e
V = {A, B, C, D} V = {A, B, C, D}
E = {(A,B), (A,C), (A,D), (C,B)} E = {(A,B), (A,C), (A,D), (C,B), Vertex a is adjacent to c and Vertex c is adjacent to a, but
(B,A), (C,A), (D,A), (B,C)} vertex c is adjacent to a vertex a is NOT adjacent to c
3 4
Graph Terminology Graph Terminology
Loops
A Path in a graph from u to v is a sequence of edges
b
between vertices
i w0, w1, …, wk, such h (wi, wi+1) ∈ E,
h that If the graph contains an edge (v, v) from a vertex to itself,
itself
then the path v, v is sometimes referred to as a loop.
u = w0 and v = wk, for 0 ≤ i < k a b
The length of the path is k, the number of edges on
the path c
d e
a b a b
d e d e A simple path is a path such that all vertices are distinct, except that the first
and last could be the same.
abedce is a path. acde is a path.
cdeb is a path.
path abec
b isi NOT a path.
th a b
bca is NOT a path.
c abedc is a simple path.
cdec is a simple path.
d e abedce is NOT a simple path.
5 6
7 8
Graph
p Terminology
gy Graph Terminology
If each edge in the graph carries a value, then the graph is
called weighted graph.
Complete Undirected Graph
A weighted graph is a graph G = (V, E, W), where each has all possible edges
edge, e ∈ E is assigned a real valued weight, W(e).
A complete graph is a graph with an edge between every
pair of vertices.
A graph ll d complete
h iis called l t graphh if every vertex
t isi
adjacent to every other vertex.
10
Graph
p Terminology
gy Graph Terminology
connected graph: any two tree - connected graph without cycles
vertices are connected by
some path
An undirected graph is
forest - collection of trees
connected if, for every
pair of vertices u and v
p
there is a path from u to
v.
12
Graph Terminology Graph
p Terminology
gy
End vertices (or endpoints) of an edge a
U and V are the endpoints
p of a
Self-loop
j is a self-loop
13 14
Graph
p Terminology
gy Graph
p Terminology
gy
Path Cycle
sequence of alternating circular sequence of
vertices and edges alternating vertices and edges
begins with a vertex
ends with a vertex
V Simple cycle V
a b cycle such that all its vertices
a b
Simple path P1 and edges are distinct
path such that all its vertices U d X Z U d X Z
and edges are distinct. P2 h Examples C2 h
Examples c e
e C1
C1 = (V, X, Y, W, U, V) is a c
P1 = (V,
(V XX, Z) is
i a simple
i l path.th W g simple cycle W g
P2 = (U, W, X, Y, W, V) is a
C2 = (U, W, X, Y, W, V, U) is a
path that is not simple. f cycle that is not simple f
Y Y
15 16
In-Degree of a Vertex Out-Degree of a Vertex
in degree is number of incoming edges
in-degree out degree is number of outbound edges
out-degree
indegree(2) = 1, indegree(8) = 0 outdegree(2) = 1, outdegree(8) = 2
2 2
3 3
8 8
1 10 1 10
4 4
5 5
9 9
11 11
6 6
7 7
17 18
Applications:
pp Communication Driving Distance/Time Map
Network vertex
e e = ccity,
y,
vertex = city, edge = communication link
edge weight = distance/time
2
3 2
4 3
8
1 8 8
10
1 6 10
4 2 4 5
5 4 4 3
9 5
11 9
11
5 6
6
7 6 7
7
19 20
Street Map
p Computer Networks
Some streets are one way
A bidirectional link represented
p by
y 2 directed Electronic
ect o c circuits
c cu ts cslab1a cslab1b
Paul
6 David
7
21 22
27 28
Breadth-First Search Breadth-First Search
Prior to the first iteration in line 10, the only green vertex,
Lines 1 1-4
4 paint every vertex white, set d[u] to be and the only vertex in Q Q, is the source vertex (s)
(s).
infinity for each vertex (u), and set p[u] the
parent of every vertex to be NIL. Line 11 determines the green vertex (u) at the head of the
queue Q and removes it from Q.
q
Line 5 paints the source vertex (s) green green.
Line 6 initializes d[s] to 0. The for loop of lines 12-17 considers each vertex (v) in the
adjacency list of (u).
Line 7 sets the parent of the source to be NIL.
If (v) is white, then it has not yet been discovered, and the
Lines 8-9 initialize Q to the queue containing just algorithm discovers it by executing lines 14-17.
the vertex (s).
It is first greened
greened, and its distance d[v] is set to d[u]+1
d[u]+1.
Th while
The hil loop
l off lines
li 10 18 iterates
10-18 it t as longl as
there remain green vertices, which are Then, u is recorded as its parent.
discovered vertices that have not y yet had their Finally,
Finally it is placed at the tail of the queue QQ.
adjacency
dj lilists
t ffully
ll examined.
i d
This while loop maintains the test in line 10, the queue When all the vertices on (u’s) adjacency list have been
Q consists of the set of the green vertices. examined, u is blackened in line 18.
29 30
∞ ∞ ∞ ∞ ∞ 0 ∞ ∞
∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
v w x y v w x y
Q: s
31 32
Breadth-First Search: Example Breadth-First Search: Example
r s t u r s t u
1 0 ∞ ∞ 1 0 2 ∞
∞ 1 ∞ ∞ ∞ 1 2 ∞
v w x y v w x y
Q: w r Q: r t x
33 34
1 0 2 ∞ 1 0 2 3
2 1 2 ∞ 2 1 2 ∞
v w x y v w x y
Q: t x v Q: x v u
35 36
Breadth-First Search: Example Breadth-First Search: Example
r s t u r s t u
1 0 2 3 1 0 2 3
2 1 2 3 2 1 2 3
v w x y v w x y
Q: v u y Q: u y
37 38
1 0 2 3 1 0 2 3
2 1 2 3 2 1 2 3
v w x y v w x y
Q: y Q: Ø
39 40
Depth-First Search Depth-First Search
Depth-first
ep s search
sea c iss another
a o e sstrategy
a egy for
o Initialize
color all vertices white
exploring a graph
Visit each and every white vertex using DFS-
Explore “deeper”
deeper in the graph whenever possible
Vi i
Visit
Each call to DFS-Visit(u) roots a new tree of
Edges are explored
Ed l d outt off th
the mostt recently
tl th d
the depth-first
th fi t forest
f t u
t att vertex
discovered vertex v that still has unexplored A vertex is white if it is undiscovered
edges
A vertex is green if it has been discovered but
not all of its edges have been discovered
When allll off v’s
Wh ’ edges
d h
have b
been explored,
l d A vertex is black after all of its adjacent vertices
backtrack to the vertex from which v was have been discovered (the adj. list was
discovered examined
i d completely)
l t l )
41 42
| |
| | |
43 44
DFS Example DFS Example
source source
vertex vertex
d f d f
1 | | | 1 | | |
2 | | 2 | |
| | | 3 | | |
45 46
2 | | 2 | |
3 | 4 | | 3 | 4 5 | |
47 48
DFS Example DFS Example
source source
vertex vertex
d f d f
1 | | | 1 | | |
2 | | 2 | 7 |
3 | 4 5 | 6 | 3 | 4 5 | 6 |
49 50
2 | 7 | 2 | 7 9 |
3 | 4 5 | 6 | 3 | 4 5 | 6 |
51 52
DFS Example DFS Example
source source
vertex vertex
d f d f
1 | 8 | | 1 | 8 |11 |
2 | 7 9 |10 2 | 7 9 |10
3 | 4 5 | 6 | 3 | 4 5 | 6 |
53 54
2 | 7 9 |10 2 | 7 9 |10
3 | 4 5 | 6 | 3 | 4 5 | 6 |
55 56
DFS Example DFS Example
source source
vertex vertex
d f d f
1 |12 8 |11 13| 1 |12 8 |11 13|
2 | 7 9 |10 2 | 7 9 |10
3 | 4 5 | 6 14| 3 | 4 5 | 6 14|15
57 58
2 | 7 9 |10 u v w u v w u v w
1/ 2/ 1/ 2/ 1/ 2/
B B
4/ 3/ 4/ 3/ 4/5 3/
3 | 4 5 | 6 14|15 x y z x y z x y z
59 60
DFS Example DFS Example
u v w u v w u v w u v w u v w u v w
1/ 2/ 1/ 2/7 1/ 2/7 1/8 2/7 9/ 1/8 2/7 9/ 1/8 2/7 9/
B C B C B C
B B F B F F F
4/5 3/6 4/5 3/6 4/5 3/6 4/5 3/6 10/ 4/5 3/6 10/ B 4/5 3/6 10/11 B
x y z x y z x y z x y z x y z x y z
u v w
u v w u v w u v w
1/8 2/7 9/12
1/8 2/7 1/8 2/7 9/ 1/8 2/7 9/ B C
B B B C F
F F F
4/5 3/6 10/11 B
4/5 3/6 4/5 3/6 4/5 3/6
x y z
x y z x y z x y z
61 62