Lecture 14: Graph Traversals - CS6507– Python Programming and

Lecture 14: Graph Traversals
CS6507– Python Programming and Data Science Applications
Summary
Breadth-first and depth-first search. Applications.
Dr Kieran T. Herley
Department of Computer Science
University College Cork
2015/16
KH (11/04/16)
Lecture 14: Graph Traversals
2015/16
1/1
Depth First Search–Pseduocode
KH (11/04/16)
Lecture 14: Graph Traversals
2015/16
2/1
DFS cont’d
Noteworthy features
Algorithm Explore(G, v):
Mark v visited
for each edge (v, w) touching v do
if w is not marked visited then
Explore(G, w)
Algorithm Explore(G, v ):
Mark v visited
for each edge (v, w) touching v do
if w is not marked visited then
Explore(G, w)
Algorithm DFS(G, v):
Mark every node unvisited
Explore(G, v)
Algorithm DFS(G, v):
Mark every node unvisited
Explore(G, v)
KH (11/04/16)
Recursion: Explore(G , v ) may
call Explore for v ’s neighbours
Node marking: nodes
visited/unvisited
Lecture 14: Graph Traversals
2015/16
3/1
KH (11/04/16)
Lecture 14: Graph Traversals
2015/16
4/1
DFS in action
Illustration 2 – execution trace
Explore(G , x)
Mark x visited
1
2
3
4
5
6
7
8
9
10
11
12
Start:
14
15
Is y visited? (No)
Explore(G, 1)
Explore(G , y )
→ Explore(G, 5)
→ Explore(G, 9)
y
· · · → Explore(G, 2)
13
For edge (x, y )
x
16
z
Algorithm Explore(G, v ):
Mark v visited
for each edge (v, w) touching v do
if w is not marked visited then
Explore(G, w)
→ Explore(G, 6)
Algorithm Explore(G, v ):
Mark v visited
for each edge (v, w) touching v do
if w is not marked visited then
Explore(G, w)
Mark y visited
For edge (y , x)
Is x visited? (Yes)
For edge (y , z)
Is z visited ? (No)
Explore(G , z)
(Backtrack to Explore(G, 3))
→ Explore(G, 7)
Mark z visited
For edge (z, x)
Is x visited? (Yes)
For edge (z, y )
Is y visited? (Yes)
Algorithm DFS(G, v):
Mark every node unvisited
Explore(G, v)
→ Explore(G, 10)
Unwind recursion
For edge (x, z)
Is z visited? (Yes)
All done!
KH (11/04/16)
Lecture 14: Graph Traversals
2015/16
5/1
Observations 1
KH (11/04/16)
2015/16
6/1
Observation 2
Algorithm Explore(G, v ):
Mark v visited
for each edge (v, w) touching v do
if w is not marked visited then
Explore(G, w)
Observation
No node is marked visited twice
Algorithm Explore(G, v ):
Mark v visited
for each edge (v, w) touching v do
if w is not marked visited then
Explore(G, w)
Why?
Node x marked visited only at
beginning of Explore(G, x)
Lecture 14: Graph Traversals
2015/16
Observation
Once a node is marked visited, so
too will all its neighbours
(eventually)
Why?
Explore(G, x) marks x visited
Explore(G, x) only called if x
is unmarked . . .
KH (11/04/16)
Lecture 14: Graph Traversals
Calls Explore(G, y) for each
unmarked neighbour y
7/1
KH (11/04/16)
Lecture 14: Graph Traversals
2015/16
8/1
Observation 3
Observation
Observation
Every node reachable from the
starting point is eventually visited
Algorithm Explore(G, v ):
Mark v visited
for each edge (v, w) touching v do
if w is not marked visited then
Explore(G, w)
Why?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
During traversal all graph edge categorized a either as
Tree edges (red) reflecting pattern of calls to Explore
Node y reachable from x
implies path between them
adjacent members of which
are neighbours
Result follows from previous
observation
Back edges (blue) reflecting
edges leading to nodes already
visited
a
KH (11/04/16)
Lecture 14: Graph Traversals
2015/16
9/1
Usefulness of DFS
Algorithm in dfs.c does not explicitly mark edges as tree/back but can be
KHto
(11/04/16)
Lecture 14: Graph Traversals
2015/16
modified
do so.
10 / 1
Path Discovery
Depth-first search forms basis for solving lots of problems:
Reachability can node y be
reached from node y ?
Objective Given x and y , find path from x to y
Observation Explore(G , x) visits y , if it is reachable from x
Connectedness can every node
be reached from every other?
Idea Build up path info. as Explore executes
Acyclicity does graph contain
any cycles?
Planarity can graph be drawn
without edges crossing (hard)
KH (11/04/16)
Lecture 14: Graph Traversals
2015/16
11 / 1
KH (11/04/16)
Lecture 14: Graph Traversals
2015/16
12 / 1
A Useful Observation
Path Discovery cont’d
1
2
3
4
5
6
7
8
Pattern of calls to
Explore(G , x) defines a tree
(red edges) rooted at x (here
1)
9
10
11
12
Tree “spans” (i.e. touches) all
nodes reachable from x
13
14
15
16
Note: to find x-to-y path
follow tree edges from y to x
KH (11/04/16)
Lecture 14: Graph Traversals
Previous Explore augmented
Note array parent []
Algorithm Explore(G, v ):
Mark v visited
for each edge (v, w) touching v do
if w is not marked visited then
parent [w] ←tree1v
Explore(G, w)
2015/16
13 / 1
2015/16
15 / 1
Notes and Acknowledgements
Reading
Code
Acknowledgements
Lecture 14: Graph Traversals
Explore(G , x) calls
Explore(G , y ) implies
x = parent[y]
Encodes a spanning tree
rooted at y
Back Material
KH (11/04/16)
Records recursive calling
pattern
KH (11/04/16)
Lecture 14: Graph Traversals
2015/16
14 / 1