BFS/DFS lecture

advertisement
GRAPH TRAVERSAL ALGORITHMS
*DEPTH-FIRST-SEARCH(DFS)
*BREADTH-FIRST-SEARCH(BFS)
CODY FORREST
DEPTH & BREADTH
Depth
Breadth
|
|
A
A
B
C
D
F
E
G
B
H
E
F
G
C
D
H
I
WHAT CAN YOU DO WITH(DFS||BFS)?
*Searching trees and graphs for a goal.
*Calculating shortest paths from *s node to *g node (BFS).
*Finding all nodes reachable from *s node(BFS/DFS).
*Path-Finding(DFS).
*Maze Generation(DFS).
*Simple Cycle Detections(DFS).
*Topological Sorting(DFS).
*Checking for Bipartite Graphs(BFS).
DEPTH-FIRST-SEARCH(DFS)
*Searches depth of a given path until its exhausted.
*Utilizes a stack data structure.
*Backtracks to next node on stack and checks nodes available.
*Simple Recursive Implementation.
*Less overhead in comparison to BFS(In most cases).
*High depth graphs or trees can be inefficient to search.
BREADTH-FIRST-SEARCH(BFS)
*Searches all neighbors of a node before increasing search depth.
*Utilizes a queue data structure.
*Searches node at head of the queue and checks for nodes available.
*Memory Intensive for trees or graphs with high branching factors.
TRI-COLORING
Node Types:
*White – Unvisited Nodes.
*Gray – Visited but not finalized nodes.
*Black – Finalized Nodes.
Generalizations:
*All nodes are initialized as White nodes.
*Nodes still on the search stack/queue must be gray nodes.
*Nodes in the search path but not in the search stack/queue must be black nodes.
*Any white nodes left after DFS/BFS finishes are considered unreachable from the *S node.
SIMPLE DFS AND BFS EXAMPLES
G1
G2
DFSVisited[] = { A , B , F , C , D, E }
BFSVisited[] = { A , B , C , F , D, E }
A
A
C
B
F
D
B
E
F
C
D
E
To see who’s paying attention,
the first person to raise their
hand gets removed from the name
pool.

LESS SIMPLE EXAMPLES
G1
A
G2
C
Search Path of DFS(G1, B)
A
C
&
B
E
H
Search Path of BFS(G2, H)
B
D
E
D
H
F
G
F
G
TOPOLOGICAL SORTING
WITH DFS
*Requires a Directed-Acyclic-Graph(DAG).
*Directed edges correspond to constraints.
*Vertices correspond to tasks.
*Provided the graph is a DAG at least one topological order always exists.
*Graphs with few constraints have many possible orderings.
TARJAN’S ALGORITHM
*Randomly pick a starting node.
*DFS the node until you hit a dead end.
*While backtracking, add the finalized nodes you came from to a stack.
*Repeat steps until all nodes have been marked.
*Stack represents the topological ordering.
MORE EXAMPLES 
A Topological Order Starting With DFS(G3,A)
G3
TopStack
TopStack
DFSStack
F
J
A
G
H
D
B
J
F
I
E
C
A
C
E
G
B
D
H
I
G
B
D
H
AA
CC
FF
JJ
EE
II
BIPARTITE GRAPH TESTING WITH BFS
Generalizations For Coloring:
*Graphs Nodes are colored Red or Blue.
*The *S Node is always red.
*All neighbors of red nodes must be blue nodes(Vice-versa).
*An edge always connects a red node to a blue node(Vice-versa).
EXAMPLES
G1
BFSCOL(G1,A)
A
D
G
G2
BFSCOL(G2,G)
B
C
E
F
H
QUE
QUE
*A
*A
*D
*E
*E
*G
*G
*B
*F
*H
E
G
A
F
B
D
C
THE GOOD | THE BAD | THE BETTER(MOSTLY)
DFS Good
|
*Low Memory Usage
*Many Applications(Maze Generation,
Topological Sorting, Spanning Trees)
BFS Good
|
*Finds shortest solution available.
*Guaranteed solution(if it exists).
*Simple implementation for state
searching(AI).
DFS Bad
|
*Recursive Solution adds overhead.
*Can get stuck on one path.
BFS Bad
|
*High Memory Usage
The Better…->
ITERATIVE DEEPENING DFS
*DFS with increasing depths.
*Mimic’s BFS traversal pattern.
*Space requirements of DFS.
*Requires duplicated work.
EXAMPLE
G1
A
B
C
D
F
E
G
H
Depth 0
Visited[] = {A}
Depth 1
Visited[] = {A,B,F}
Depth 2
Visited[] = {A,B,C,E,F,G,H}
Depth 3
Visited[] = {A,B,C,D,E,F,G,H}
EXTRA: MATRIX -> GRAPH
&
MATRIX - > GRID - > GRAPH
(I)
0
1
2
3
4
(J)
01234
{{0,9,0,0,3}
{0,0,2,4,7}
{0,0,0,0,0}
{0,0,0,0,0}
{0,0,0,0,0}}
0
9
2
E = (I -> J)
3
2
1
4
7
4
3
4
{{#,3,G}
{1,2,4} ->
{S,#,#}}
#
3
G
1
2
4
S
#
#
G
1
->
2
S
3
SOURCES
•
•
•
•
•
•
https://www.cs.usfca.edu/~galles/visualization/java/download.html
https://www.ics.uci.edu/~eppstein/161/960215.html
http://www.inf.ed.ac.uk/teaching/courses/cs2/LectureNotes/CS2Bh/ADS/ads10.pdf
https://www.cs.usfca.edu/~galles/visualization/TopoSortDFS.html
https://www8.cs.umu.se/kurser/TDBAfl/VT06/algorithms/BOOK/BOOK4/NODE160.HTM
https://www.google.com/search?q=grid&biw=1760&bih=888&tbm=isch&source=lnms&sa=
X&ved=0ahUKEwjBtqrcw8HJAhWM5iYKHYd8DdgQ_AUIBigB#imgrc=toBWsRVF1psi7M%3A
• https://en.wikipedia.org/wiki/Topological_sorting#CITEREFCormenLeisersonRivestStein2001
Download