Download - Advanced DFS & BFS HKOI Training 20041 Advanced D epth - F irst S earch and B readth- F irst S earch
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
HKOI Training 2004 1
AdvancedDepth-First Search and Breadth-First Search
HKOI Training 2004 2
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Overview
• Depth-first search (DFS)
• DFS Forest
• Breadth-first search (BFS)
• Some variants of DFS and BFS
• Graph modeling
HKOI Training 2004 3
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Prerequisites
• Elementary graph theory
• Implementations of DFS and BFS
HKOI Training 2004 4
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
What is a graph?
• A set of vertices and edges
vertex
edge
HKOI Training 2004 5
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Trees and related terms
root
siblings
descendents children
ancestors
parent
HKOI Training 2004 6
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
What is graph traversal?
• Given: a graph
• Goal: visit all (or some) vertices and edges of the graph using some strategy
• Two simple strategies:– Depth-first search– Breadth-first search
HKOI Training 2004 7
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Depth-first search (DFS)
• A graph searching method
• Algorithm:
at any time, go further (depth) if you can; otherwise, retreat
HKOI Training 2004 8
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
DFS (Pseudo code)
DFS (vertex u) {
mark u as visited
for each vertex v directly reachable from u
if v is unvisited
DFS (v)
}
• Initially all vertices are marked as unvisited
HKOI Training 2004 9
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS F
A
BC
D
E
DFS (Demonstration)
unvisited
visited
HKOI Training 2004 10
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
“Advanced” DFS
• Apart from just visiting the vertices, DFS can also provide us with valuable information
• DFS can be enhanced by introducing:– birth time and death time of a vertex
• birth time: when the vertex is first visited• death time: when we retreat from the vertex
– DFS tree– parent of a vertex (see next slide)
HKOI Training 2004 11
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
DFS tree / forest
• A rooted tree
• The root is the start vertex
• If v is first visited from u, then u is the parent of v in the DFS tree
HKOI Training 2004 12
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
A
F
B
C
D
E
GH
DFS forest (Demonstration)
unvisited
visited
visited (dead)
A B C D E F G H
birth
death
parent
A
B
C
F
E
D
G
1 2 3 13 10 4 14
12 9 8 16 11 5 15
H
6
7
- A B - A C D C
HKOI Training 2004 13
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Classification of edges
• Tree edge
• Forward edge
• Back edge
• Cross edge
• Question: which type of edges is always absent in an undirected graph?
A
B
C
F
E
D
G
H
HKOI Training 2004 14
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Determination of edge types
• How to determine the type of an arbitrary edge (u, v) after DFS?
• Tree edge– parent [v] = u
• Forward edge– not a tree edge; and– birth [v] > birth [u]; and– death [v] < death [u]
• How about back edge and cross edge?
HKOI Training 2004 15
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Applications of DFS Forests
• Topological sorting (Tsort)
• Strongly-connected components (SCC)
• Some more “advanced” algorithms
HKOI Training 2004 16
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Example: SCC
• A graph is strongly-connected if– for any pair of vertices u and v, one can
go from u to v and from v to u.
• Informally speaking, an SCC of a graph is a subset of vertices that– forms a strongly-connected subgraph– does not form a strongly-connected
subgraph with the addition of any new vertex
HKOI Training 2004 17
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
SCC (Illustration)
HKOI Training 2004 18
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
SCC (Algorithm)
• Compute the DFS forest of the graph G
• Reverse all edges in G to form G’
• Compute a DFS forest of G’, but always choose the vertex with the latest death time when choosing the root for a new tree
• The SCCs of G are the DFS trees in the DFS forest of G’
HKOI Training 2004 19
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
A
F
B
C
D
GH
SCC (Demonstration)
A
F
B
C
D
E
GH
A B C D E F G H
birth
death
parent
1 2 3 13 10 4 14
12 9 8 16 11 5 15
6
7
- A B - A C D C
D
G
A E B
F
C
H
HKOI Training 2004 20
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
SCC (Demonstration)
D
G
A E B
F
C
H
A
F
B
C
D
GH
E
HKOI Training 2004 21
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Breadth-first search (BFS)
• A graph searching method
• Instead of searching “deeply” along one path, BFS tries to search all paths at the same time
• Makes use of a data structure - queue
HKOI Training 2004 22
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
BFS (Pseudo code)
while queue not empty
dequeue the first vertex u from queue
for each vertex v directly reachable from u
if v is unvisited
enqueue v to queue
mark v as visited
• Initially all vertices except the start vertex are marked as unvisited and the queue contains the start vertex only
HKOI Training 2004 23
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
A
B
C
D
E
F
G
H
I
J
BFS (Demonstration)
unvisited
visited
visited (dequeued)
Queue: A B C F D E H G J I
HKOI Training 2004 24
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Applications of BFS
• Shortest paths finding
• Flood-fill (can also be handled by DFS)
HKOI Training 2004 25
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Comparisons of DFS and BFS
DFS BFS
Depth-first Breadth-first
Stack Queue
Does not guarantee shortest paths
Guarantees shortest paths
HKOI Training 2004 26
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Bidirectional search (BDS)
• Searches simultaneously from both the start vertex and goal vertex
• Commonly implemented as bidirectional BFS
start goal
HKOI Training 2004 27
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Iterative deepening search (IDS)
• Iteratively performs DFS with increasing depth bound
• Shortest paths are guaranteed
HKOI Training 2004 28
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
What is graph modeling?
• Conversion of a problem into a graph problem
• Sometimes a problem can be easily solved once its underlying graph model is recognized
• Graph modeling appears almost every year in NOI or IOI
HKOI Training 2004 29
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Basics of graph modeling
• A few steps:– identify the vertices and the edges– identify the objective of the problem– state the objective in graph terms– implementation:
• construct the graph from the input instance• run the suitable graph algorithms on the graph• convert the output to the required format
HKOI Training 2004 30
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Simple examples (1)
• Given a grid maze with obstacles, find a shortest path between two given points
start
goal
HKOI Training 2004 31
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Simple examples (2)
• A student has the phone numbers of some other students
• Suppose you know all pairs (A, B) such that A has B’s number
• Now you want to know Alan’s number, what is the minimum number of calls you need to make?
HKOI Training 2004 32
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Simple examples (2)
• Vertex: student
• Edge: whether A has B’s number
• Add an edge from A to B if A has B’s number
• Problem: find a shortest path from your vertex to Alan’s vertex
HKOI Training 2004 33
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Complex examples (1)
• Same settings as simple example 1
• You know a trick – walking through an obstacle! However, it can be used for only once
• What should a vertex represent?– your position only?– your position + whether you have used the
trick
HKOI Training 2004 34
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Complex examples (1)
• A vertex is in the form (position, used)
• The vertices are divided into two groups– trick used– trick not used
HKOI Training 2004 35
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Complex examples (1)
start
goal
unused
used
start goal
goal
HKOI Training 2004 36
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Complex examples (2)
• The famous 8-puzzle
• Given a state, find the moves that bring it to the goal state
1 2 3
4 5 6
7 8
HKOI Training 2004 37
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Complex examples (2)
• What does a vertex represent?– the position of the empty square?– the number of tiles that are in wrong
positions?– the state (the positions of the eight tiles)
• What are the edges?
• What is the equivalent graph problem?
HKOI Training 2004 38
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Complex examples (2)
1 2 34 5 67 8
1 2 34 5 67 8
1 2 34 57 8 6
1 2 34 67 5 8
1 2 34 5 6
7 8
1 24 5 37 8 6
1 2 34 57 8 6
HKOI Training 2004 39
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Complex examples (3)
• Theseus and Minotaur– http://www.logicmazes.com/theseus.html– Extract:
• Theseus must escape from a maze. There is also a mechanical Minotaur in the maze. For every turn that Theseus takes, the Minotaur takes two turns. The Minotaur follows this program for each of his two turns:
• First he tests if he can move horizontally and get closer to Theseus. If he can, he will move one square horizontally. If he can’t, he will test if he could move vertically and get closer to Theseus. If he can, he will move one square vertically. If he can’t move either horizontally or vertically, then he just skips that turn.
HKOI Training 2004 40
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Complex examples (3)
• What does a vertex represent?– Theseus’ position– Minotaur’s position– Both
• How long do you need to solve the last maze?
• How long does a well-written program take to solve it?
HKOI Training 2004 41
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Some more examples
• How can the followings be modeled?– Tilt maze (Single-goal mazes only)
• http://www.clickmazes.com/newtilt/ixtilt2d.htm
– Double title maze• http://www.clickmazes.com/newtilt/ixtilt.htm
– No-left-turn maze• http://www.clickmazes.com/noleft/ixnoleft.htm
– Same as complex example 1, but you can use the trick for k times
HKOI Training 2004 42
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Ad
van
ced
DF
S &
BF
SA
dva
nce
d D
FS
& B
FS
Competition problems
• HKOI2000 S – Wormhole Labyrinth• HKOI2001 S – A Node Too Far• HKOI2004 S – Teacher’s Problem *• TFT2001 – OIMan * • TFT2002 – Bomber Man *• NOI2001 – cung1 ming4 dik7 daa2 zi6 jyun4• IOI2000 – Walls *• IOI2002 – Troublesome Frog• IOI2003 – Amazing Robots *