Download as pdf or txt
Download as pdf or txt
You are on page 1of 71

Data Structures

Graphs – BFS & DFS


Graph Searching

Given: a graph G = (V, E), directed or undirected


Goal: methodically explore every vertex and every edge

Two main approaches:


Breadth-First Search
Depth-First Search
Some Applications of BFS and DFS

• 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:

• Visit and inspect a node of a graph

• Gain access to visit the nodes that neighbor the


currently visited node

4
Breadth First Search

The algorithm uses a queue data structure to store


intermediate results as it traverses the graph, as follows:
1. Enqueue the root node
2. Dequeue a node and examine it
1.If the element required is found in this node, quit the
search and return a result.
2. Otherwise enqueue any successors (the direct child
nodes) that have not yet been discovered.
3. If the queue is empty, every node on the graph has been
examined – quit the search and return "not found".
4. If the queue is not empty, repeat from Step 2.

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

Back up to where you could go to another unvisited vertex


Then continue to go on from that point
Eventually you’ll return to where you started
Depth-first searching

A depth-first search (DFS)


A explores a path all the way
to a leaf before
B C
backtracking and exploring
another path
For example, after searching
D E F G
A, then B, then D, the
search backtracks and tries
H I J K another path from B
Node are explored in the order
L M N O P Q ABDEHLMNIOPCF
GJKQ
N will be found before J
36
DFS Algorithm
create a stack S
mark v as visited and push v onto S
while S is non-empty
pop at the top u of S
if u has an (unvisited)neighbour w,
mark w and push it onto S
else
pop S
Undirected Depth First Search
A
H I

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

Depending on the data and what you are looking


for, either DFS or BFS could be advantageous.

Consider your family tree

If you are searching for some of your siblings


cousins then it would be safe to assume that person
would be on the bottom of the tree.

Which approach is better in this case?

68
Differences between DFS and BFS

Consider your family tree

If you are searching for some of your ancestors


then it would be safe to assume that person would
be closer to the top of the tree.

Which approach is better in this case?

In general, both approaches have the same time


complexity. In worst case, they need to visit all the
nodes.

69
Differences between DFS and BFS

In terms of memory – DFS is better than BFS.

DFS needs to store only the path from the root to


the leaf node, beside the siblings of each node on
the path, remember that BFS needs to store all
the explored nodes in memory.

What can BFS do and DFS can’t (efficently)?


Finding shortest paths (in unweighted graphs)

70
References

Nell Dale -- Chapter 9


http://hci.iwr.uni-heidelberg.de/MIP/Teaching/alda/handouts-20-mst.pdf
http://www.cs.uah.edu/~rcoleman/Common/CodeVault/Code/Code301_Graphs.html
http://en.wikipedia.org/wiki/Breadth-first_search

You might also like