16
Graph Algorithms 1
Graph Searching
1
Graph Searching
Given: a graph G = (V, E), directed or
undirected
Goal: methodically explore every vertex and
every edge
Ultimately: build a tree on the graph
Pick a vertex as the root
Choose certain edges to produce a tree
Note: might also build a forest if graph is not
connected
2
Breadth-First Search
Will associate vertex “colors” to guide the
algorithm
White vertices have not been discovered
All vertices start out white
Grey vertices are discovered but not fully explored
They may be adjacent to white vertices
Black vertices are discovered and fully explored
They are adjacent only to black and gray vertices
Explore vertices by scanning adjacency list of
grey vertices
3
Breadth-First Search: Example
r
s
t
u
¥
¥
¥
¥
¥
¥
¥
¥
v
w
x
y
4
Breadth-First Search: Example
r
s
t
u
¥
0
¥
¥
¥
¥
¥
¥
v
w
x
y
Q: s
5
Breadth-First Search: Example
r
s
t
u
1
0
¥
¥
¥
1
¥
¥
v
w
x
y
Q: w
r
6
Breadth-First Search: Example
r
s
t
u
1
0
2
¥
¥
1
2
¥
v
w
x
y
Q: r
t
x
7
Breadth-First Search: Example
r
s
t
u
1
0
2
¥
2
1
2
¥
v
w
x
y
Q:
t
x
v
8
Breadth-First Search: Example
r
s
t
u
1
0
2
3
2
1
2
¥
v
w
x
y
Q: x
v
u
9
Breadth-First Search: Example
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q: v
u
y
10
Breadth-First Search: Example
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q: u
y
11
Breadth-First Search: Example
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q: y
12
Breadth-First Search: Example
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q: Ø
13
BFS: The Code Again
Overhead for initialization: O(V)
Each vertex is queued atmost once
and de-queued atmost once O(V)
Adjacency lists of the vertex is only
scanned when it is dequeued. The
sum of the length of all adjacency
list is O(E)
Total running time: O(V+E)
Total space used:
O(max(degree(v))) = O(E)
14
Breadth-First Search: Properties
BFS calculates the shortest-path distance to
the source node
Shortest-path distance d(s,v) = minimum number
of edges from s to v, or ¥ if v not reachable from s
Proof given in the book (p. 472-5)
BFS builds breadth-first tree, in which paths to
root represent shortest paths in G
Thus can use BFS to calculate shortest path from
one vertex to another in O(V+E) time
15
Depth-First Search
Explore “deeper” in the graph
whenever possible
Edges are explored out of the
most recently discovered vertex
v that still has unexplored
edges
16
Depth-First Search
Vertices initially colored white
Then colored gray when discovered
Then black when finished
17
Depth-First Search: The Code
So, running time of DFS = O(V+E)
18
Notes
v.d when v is first discovered and grayed ….
discovering time
v.f when the search finishes examining v’s adjacency list
(blacken v) …. Finishing time
These timestamps are integers between
1 and |2V|, since there is one discovery event and one finishing event
For each of the |v | vertices. For every vertex u u.d < u.f
u is white before time u.d, gray between time u.d and u.f and black
thereafter
19
DFS Example
source
vertex
20
DFS Example
source
vertex
d
f
1 |
|
|
|
|
|
|
21
|
DFS Example
source
vertex
d
f
1 |
|
2 |
|
|
|
|
22
|
DFS Example
source
vertex
d
f
1 |
|
2 |
|
|
3 |
|
23
|
DFS Example
source
vertex
d
f
1 |
|
2 |
|
|
3 | 4
|
24
|
DFS Example
source
vertex
d
f
1 |
|
2 |
|
|
3 | 4
5 |
25
|
DFS Example
source
vertex
d
f
1 |
|
2 |
|
|
3 | 4
5 | 6
26
|
DFS Example
source
vertex
d
f
1 |
8 |
2 | 7
|
|
3 | 4
5 | 6
27
|
DFS Example
source
vertex
d
f
1 |
8 |
2 | 7
|
|
3 | 4
5 | 6
28
|
DFS Example
source
vertex
d
f
1 |
8 |
2 | 7
|
9 |
3 | 4
5 | 6
What is the structure of the grey vertices?
What do they represent?
29
|
DFS Example
source
vertex
d
f
1 |
8 |
2 | 7
|
9 |10
3 | 4
5 | 6
30
|
DFS Example
source
vertex
d
f
1 |
8 |11
2 | 7
|
9 |10
3 | 4
5 | 6
31
|
DFS Example
source
vertex
d
f
1 |12
8 |11
2 | 7
|
9 |10
3 | 4
5 | 6
32
|
DFS Example
source
vertex
d
f
1 |12
8 |11
2 | 7
13|
9 |10
3 | 4
5 | 6
33
|
DFS Example
source
vertex
d
f
1 |12
8 |11
2 | 7
13|
9 |10
3 | 4
5 | 6
34
14|
DFS Example
source
vertex
d
f
1 |12
8 |11
2 | 7
13|
9 |10
3 | 4
5 | 6
35
14|15
DFS Example
source
vertex
d
f
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
5 | 6
36
14|15
DFS: Kinds of edges
DFS introduces an important distinction
among edges in the original graph:
Tree edge: are edges in the depth-first forest GΠ
edge (u,v) is a tree edge if v was first discovered by
exploring edge (u,v).
The tree edges form a spanning forest
37
DFS Example
source
vertex
d
f
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
5 | 6
Tree edges
38
14|15
DFS: Kinds of edges
DFS introduces an important distinction
among edges in the original graph:
Tree edge: encounter new (white) vertex
Back edge: from descendent to ancestor
Encounter a grey vertex (grey to grey)
39
DFS Example
source
vertex
d
f
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
5 | 6
Tree edges Back edges
40
14|15
DFS: Kinds of edges
DFS introduces an important distinction
among edges in the original graph:
Tree edge: encounter new (white) vertex
Back edge: from descendent to ancestor
Forward edge: from ancestor to descendent
Not a tree edge, though (connecting a vertex u to a
descendant v in a depth-first tree).
From grey node to black node
41
DFS Example
source
vertex
d
f
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
5 | 6
Tree edges Back edges Forward edges
42
14|15
DFS: Kinds of edges
DFS introduces an important distinction
among edges in the original graph:
Tree edge: encounter new (white) vertex
Back edge: from descendent to ancestor
Forward edge: from ancestor to descendent
Cross edge: between a tree or subtrees (are all other
edges). They can go between vertices in the same depthfirst tree as long as one vertex is not an ancestor of the
other.
From a grey node to a black node
43
DFS Example
source
vertex
d
f
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
5 | 6
14|15
Tree edges Back edges Forward edges Cross edges
44
DFS: Kinds of edges
DFS introduces an important distinction
among edges in the original graph:
Tree edge: encounter new (white) vertex
Back edge: from descendent to ancestor
Forward edge: from ancestor to descendent
Cross edge: between a tree or subtrees
Note: tree & back edges are important; most
algorithms don’t distinguish forward & cross
45
DFS And Graph Cycles
Thm: An undirected graph is acyclic iff a DFS
yields no back edges
Thus, can run DFS to find whether a graph has
a cycle
What will be the running time?
A: O(V+E)
46
Directed Acyclic Graphs
A directed acyclic graph or DAG is a directed
graph with no directed cycles:
directed graph G is acyclic iff a DFS of G yields no back edges:
47