Professional Documents
Culture Documents
NU-Lec 19 Graphs Search
NU-Lec 19 Graphs Search
• BFS
• Find the shortest path from a vertex s to a vertex v
• Find the length of such a path
• Find if a strongly connected directed graph contains cycles
• Construct a BSF tree/forest from a graph
• DFS
• Find a path from a vertex s to a vertex v.
• Find the length of such a path.
• Construct a DSF tree/forest from a graph.
• Visit all vertices and edges
3
Breadth First Search
When search is limited to essentially two operations:
4
Breadth First Search
5
Breadth First Search
2 4 8
s 5 7
3 6 9
6
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: s
Top of queue
Finished
7
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: s 2
Top of queue
Finished
8
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: s 2 3
Top of queue
Finished
9
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 2 3 5
Top of queue
Finished
10
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 2 3 5
Top of queue
Finished
11
Breadth First Search
2 4 8
5 already discovered:
0 s 5 7
don't enqueue
3 6 9
Undiscovered
Discovered Queue: 2 3 5 4
Top of queue
Finished
12
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 2 3 5 4
Top of queue
Finished
13
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 3 5 4
Top of queue
Finished
14
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 3 5 4
Top of queue
Finished
15
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 3 5 4 6
Top of queue
Finished
16
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 5 4 6
Top of queue
Finished
17
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 5 4 6
Top of queue
Finished
18
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 4 6
Top of queue
Finished
19
Breadth First Search
3
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 4 6
Top of queue
Finished
20
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 4 6 8
Top of queue
Finished
21
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 6 8
Top of queue
Finished
22
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 6 8 7
Top of queue
Finished
23
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 6 8 7 9
Top of queue
Finished
24
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 8 7 9
Top of queue
Finished
25
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 7 9
Top of queue
Finished
26
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 7 9
Top of queue
Finished
27
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 7 9
Top of queue
Finished
28
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 7 9
Top of queue
Finished
29
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 9
Top of queue
Finished
30
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 9
Top of queue
Finished
31
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: 9
Top of queue
Finished
32
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue:
Top of queue
Finished
33
Breadth First Search
2 4 8
0 s 5 7
3 6 9
Level Graph
34
Depth First Search
Depth-first search: Strategy
Go as deep as can visiting un-visited nodes
Choose any un-visited vertex when you have a choice
If stuck at a dead-end, backtrack as little as Possible
B C G
Adjacency Lists
D E
A: F C B G
B: A
C: A
F D: F E
E: G F D
F: A E D:
G: E A:
H: I:
I: H:
38
Undirected Depth First Search
A
H I
B C G
D E
F newly
discovered F
Undiscovered
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 39
Undirected Depth First Search
A
H I
A already B C G
marked
D E
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 40
Undirected Depth First Search
A
H I
B C G
D E E newly
discovered
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 41
Undirected Depth First Search
A
H I
B C G G newly
discovered
D E
visit(E)
F (E, G) (E, F) (E, D)
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 42
Undirected Depth First Search
A
H I
E already B C G
marked
visit(G)
D E (G, E) (G, A)
visit(E)
F (E, G) (E, F) (E, D)
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 43
Undirected Depth First Search
A
H I
A already B C G
marked
visit(G)
D E (G, E) (G, A)
visit(E)
F (E, G) (E, F) (E, D)
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 44
Undirected Depth First Search
A
H I
Finished G
B C G
visit(G)
D E (G, E) (G, A)
visit(E)
F (E, G) (E, F) (E, D)
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 45
Undirected Depth First Search
A
H I
F already B C G
marked
D E
visit(E)
F (E, G) (E, F) (E, D)
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 46
Undirected Depth First Search
A
H I
B C G
D newly
D E
discovered
visit(E)
F (E, G) (E, F) (E, D)
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 47
Undirected Depth First Search
A
H I
F already B C G
marked
visit(D)
D E (D, F) (D, E)
visit(E)
F (E, G) (E, F) (E, D)
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 48
Undirected Depth First Search
A
H I
E already B C G
marked
visit(D)
D E (D, F) (D, E)
visit(E)
F (E, G) (E, F) (E, D)
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 49
Undirected Depth First Search
A
H I
Finished D
B C G
visit(D)
D E (D, F) (D, E)
visit(E)
F (E, G) (E, F) (E, D)
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 50
Undirected Depth First Search
A
H I
B C G
Finished E
D E
visit(E)
F (E, G) (E, F) (E, D)
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 51
Undirected Depth First Search
A
H I
D already B C G
marked
D E
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 52
Undirected Depth First Search
A
H I
B C G
D E
Finished F
F
visit(F)
Undiscovered (F, A) (F, E) (F, D)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 53
Undirected Depth First Search
A
H I
C newly
B C G
discovered
D E
Undiscovered
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 54
Undirected Depth First Search
A
H I
A already B C G
marked
D E
visit(C)
Undiscovered (C, A)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 55
Undirected Depth First Search
A
H I
B C G
D E
Finished C
F
visit(C)
Undiscovered (C, A)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 56
Undirected Depth First Search
A
H I
B newly
B C G
discovered
D E
Undiscovered
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 57
Undirected Depth First Search
A
H I
A already B C G
marked
D E
visit(B)
Undiscovered (B, A)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 58
Undirected Depth First Search
A
H I
B C G
D E
Finished B
F
visit(B)
Undiscovered (B, A)
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 59
Undirected Depth First Search
A
H I
G already B C G
finished
D E
Undiscovered
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 60
Undirected Depth First Search
A
H I
B C G
D E
Finished A
Undiscovered
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)
Finished
Stack 61
Undirected Depth First Search
A
H I
B C G
D E
Undiscovered
Marked
Active
Finished
Stack 62
Undirected Depth First Search
A
H I I newly
discovered
B C G
D E
Undiscovered
Marked visit(H)
Active (H, I)
Finished
Stack 63
Undirected Depth First Search
A
H I
H already B C G
marked
D E
visit(I)
Undiscovered (I, H)
Marked visit(H)
Active (H, I)
Finished
Stack 64
Undirected Depth First Search
A
H I
B C G
D E
Finished I
visit(I)
Undiscovered (I, H)
Marked visit(H)
Active (H, I)
Finished
Stack 65
Undirected Depth First Search
A
H I
B C G
D E
F
FinishedH
Undiscovered
Marked visit(H)
Active (H, I)
Finished
Stack 66
Undirected Depth First Search
A
H I
B C G
D E
Undiscovered
Marked
Active
Finished
Stack 67
Differences between DFS and BFS
68
Differences between DFS and BFS
69
Differences between DFS and BFS
70
References