Design and Analysis of Algorithms: CSE 5311 Lecture 18 Graph Algorithm
Design and Analysis of Algorithms: CSE 5311 Lecture 18 Graph Algorithm
CSE 5311
Lecture 18 Graph Algorithm
• Graph G = (V, E)
– V = set of vertices
– E = set of edges ⊆ (V×V)
• Types of graphs
– Undirected: edge (u, v) = (v, u); for all v, (v, v) ∉ E (No self loops.)
– Directed: (u, v) is edge from u to v, denoted as u → v. Self loops are
allowed.
– Weighted: each edge has an associated weight, given by a weight
function w : E → R.
– Dense: |E| ≈ |V|2.
– Sparse: |E| << |V|2.
• |E| = O(|V|2)
b a c
c d c d a b
d a c
– Adjacency Matrix.
1 2 1 2 3 4
a b
1 0 1 1 1
2 1 0 1 0
c d 3 1 1 0 1
3 4 4 1 0 1 0
Dept. CSE, UT Arlington CSE5311 Design and Analysis of Algorithms 4
Adjacency Lists
b c
d
If weighted, store weights
c d c
also in adjacency lists.
d
a b a b d c
b a c
c d c d a b
d a c
Dept. CSE, UT Arlington CSE5311 Design and Analysis of Algorithms 5
Storage Requirement
• Pros
– Space-efficient, when a graph is sparse.
– Can be modified to support many graph variants.
• Cons
– Determining if an edge (u,v) ∈G is not efficient.
Have to search in u’s adjacency list. Θ(degree(u)) time.
Θ(V) in the worst case.
• Space: Θ(V2).
– Not memory efficient for large graphs.
• Time: to list all vertices adjacent to u: Θ(V).
• Time: to determine if (u, v) ∈ E: Θ(1).
• Can store weights instead of bits for weighted graph.
• Searching a graph:
– Systematically follow the edges of a graph
to visit the vertices of the graph.
• Used to discover the structure of a graph.
• Standard graph-searching algorithms.
– Breadth-first Search (BFS).
– Depth-first Search (DFS).
2 3 3
2
2 3
1 S
S 2 S
1 1
2
2 3 3
r s t u
∞ 0 ∞ ∞
∞ ∞ ∞ ∞
v w x y
Q: s
0
r s t u
1 0 ∞ ∞
∞ 1 ∞ ∞
v w x y
Q: w r
1 1
r s t u
1 0 2 ∞
∞ 1 2 ∞
v w x y
Q: r t x
1 2 2
r s t u
1 0 2 ∞
2 1 2 ∞
v w x y
Q: t x v
2 2 2
r s t u
1 0 2 3
2 1 2 ∞
v w x y
Q: x v u
2 2 3
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
2 3 3
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
3 3
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
3
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: ∅
r s t u
1 0 2 3
2 1 2 3
v w x y
BF Tree
DFS(G) DFS-Visit(u)
1. for each vertex u ∈ V[G] 1. color[u] ← GRAY ∇ White vertex u
2. do color[u] ← white has been discovered
3. π[u] ← NIL 2. time ← time + 1
4. time ← 0 3. d[u] ← time
5. for each vertex u ∈ V[G] 4. for each v ∈ Adj[u]
6. do if color[u] = white 5. do if color[v] = WHITE
7. then DFS-Visit(u) 6. then π[v] ← u
7. DFS-Visit(v)
8. color[u] ← BLACK ∇ Blacken u;;
Uses a global timestamp time. it is finished.
9. f[u] ← time ← time + 1
Example: animation.
Dept. CSE, UT Arlington CSE5311 Design and Analysis of Algorithms 29
Example (DFS)
(Courtesy of Prof. Jim Anderson)
u v w
1/
x y z
u v w
1/ 2/
x y z
u v w
1/ 2/
3/
x y z
u v w
1/ 2/
4/ 3/
x y z
u v w
1/ 2/
B
4/ 3/
x y z
u v w
1/ 2/
B
4/5 3/
x y z
u v w
1/ 2/
B
4/5 3/6
x y z
u v w
1/ 2/7
B
4/5 3/6
x y z
u v w
1/ 2/7
F B
4/5 3/6
x y z
u v w
1/8 2/7
F B
4/5 3/6
x y z
u v w
1/8 2/7 9/
F B
4/5 3/6
x y z
u v w
1/8 2/7 9/
F B C
4/5 3/6
x y z
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/12
F B C
• Loops on lines 1-2 & 5-7 take Θ(V) time, excluding time
to execute DFS-Visit.
Theorem 22.7
For all u, v, exactly one of the following holds:
1. d[u] < f [u] < d[v] < f [v] or d[v] < f [v] < d[u] < f [u] and neither u nor v
is a descendant of the other.
2. d[u] < d[v] < f [v] < f [u] and v is a descendant of u.
3. d[v] < d[u] < f [u] < f [v] and u is a descendant of v.
y z s t
3/6 2/9 1/10 11/16
B F C B
(s (z (y (x x) y) (w w) z) s) (t (v v) (u u) t)
Definition:
Forest: An acyclic graph G that may be disconnected.
Theorem 22.9
v is a descendant of u if and only if at time d[u], there is a path
u v consisting of only white vertices. (Except for u, which
was just colored gray.)
Theorem:
In DFS of an undirected graph, we get only tree and back edges. No
forward or cross edges.
• Edge type for edge (u, v) can be identified when it is first explored by
DFS.
• Identification is based on the color of v.
– White – tree edge.
– Gray – back edge.
– Black – forward or cross edge.
pants sweater
skates mask
blocker
Dept. CSE, UT Arlington CSE5311 Design and Analysis of Algorithms 54
Characterizing a DAG
Lemma 22.11
A directed graph G is acyclic iff a DFS of G yields no back edges.
Proof:
• ⇒: Show that back edge ⇒ cycle.
– Suppose there is a back edge (u, v). Then v is ancestor of u in
depth-first forest.
– Therefore, there is a path v u, so v u v is a cycle.
T T T
v u
B
Lemma 22.11
A directed graph G is acyclic iff a DFS of G yields no back edges.
Proof (Contd.):
• ⇐ : Show that a cycle implies a back edge.
– c : cycle in G, v : first vertex discovered in c, (u, v) : preceding edge
in c.
– At time d[v], vertices of c form a white path v u. Why?
– By white-path theorem, u is a descendent of v in depth-first forest.
– Therefore, (u, v) is a back edge. T T T
v u
B