0% found this document useful (0 votes)
12 views

Bfsdfs

The document describes the breadth-first search (BFS) algorithm for traversing graphs. BFS returns nodes level by level, starting with nodes closest to the root node. It uses a queue data structure, where the root node is first enqueued and then dequeued. When a node is dequeued, its neighbors are enqueued and added to the back of the queue. This process of dequeuing a node and enqueuing its neighbors continues until the queue is empty, providing a breadth-first traversal of the graph. Pseudocode and an example are provided to illustrate how BFS is implemented on a computer using a queue.

Uploaded by

Anas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Bfsdfs

The document describes the breadth-first search (BFS) algorithm for traversing graphs. BFS returns nodes level by level, starting with nodes closest to the root node. It uses a queue data structure, where the root node is first enqueued and then dequeued. When a node is dequeued, its neighbors are enqueued and added to the back of the queue. This process of dequeuing a node and enqueuing its neighbors continues until the queue is empty, providing a breadth-first traversal of the graph. Pseudocode and an example are provided to illustrate how BFS is implemented on a computer using a queue.

Uploaded by

Anas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Graph Traversal

© 2004 Goodrich, Tamassia Breadth-First Search 1


Can you name all the nodes?
B

E X K

P G A R

D T H M

© 2004 Goodrich, Tamassia Breadth-First Search 2


Breadth-First Search

© 2004 Goodrich, Tamassia Breadth-First Search 3


Depth-First Search

© 2004 Goodrich, Tamassia Breadth-First Search 4


Breadth-First Search
L0
A

L1
B C D

L2
E F

© 2004 Goodrich, Tamassia Breadth-First Search 5


Breadth-First Search
Breadth-first search (BFS) is a general technique for
traversing a graph.

A BFS traversal of a graph returns the nodes of the


graph level by level.
L0

L1

L2

L3

© 2004 Goodrich, Tamassia Breadth-First Search 6


What is a BFS traversal?
L0
A Let’s start at A

L1
B C D

L2 E F

© 2004 Goodrich, Tamassia Breadth-First Search 7


What is a BFS traversal?
L0
1

L1
2 3 4

L2 5 6

© 2004 Goodrich, Tamassia Breadth-First Search 8


What is a BFS traversal?
L0
A Let’s start at A

L1
B C D

L2 E F

ABCDEF Is that the only BFS?

© 2004 Goodrich, Tamassia Breadth-First Search 9


What is a BFS traversal?
L0
1

L1
4 3 2

L2 6 5

© 2004 Goodrich, Tamassia Breadth-First Search 10


Computer Algorithm
A computer does not see the BIG PICTURE
of a graph. It has a list of nodes in a graph
and a list of edges, and it knows which
nodes are connected by which edge.

How would a computer perform BFS?

© 2004 Goodrich, Tamassia Breadth-First Search 11


A
Adjacency Matrix
B C D

E F

A B C D E F
A 0 1 1 1 0 0
B 1 0 0 0 0 0
C 1 0 0 0 1 1
D 1 0 0 0 0 1
E 0 0 1 0 0 0
F 0 0 1 1 0 0

© 2004 Goodrich, Tamassia Breadth-First Search 12


Queue
A queue is a line.
If you’re the first to get in a bus line,
you’re the first to get on the bus.

First In, First Out

© 2004 Goodrich, Tamassia Breadth-First Search 13


Example
L0
A
Let’s start at A

B C D

E F Enqueue means to
put something at the
end of the line.
Enqueue the root A.
Queue: A

© 2004 Goodrich, Tamassia Breadth-First Search 14


Here’s Our Algorithm
Each time, we’re going to take out one
node (we’ll call it node X) from the
queue. We call the process dequeue.
Then, we’re going to add to the queue
all of the new neighbors of node X. We
call that process enqueue.

© 2004 Goodrich, Tamassia Breadth-First Search 15


First Step:

DEQUEUE!
(take a node out of the queue)

© 2004 Goodrich, Tamassia Breadth-First Search 16


Example
L0
A
A

B C D

E F

Queue: A
Dequeue A  Queue:

© 2004 Goodrich, Tamassia Breadth-First Search 17


Second Step:

ENQUEUE THE
NEW NEIGHBORS!
(put the nodes into the queue)

© 2004 Goodrich, Tamassia Breadth-First Search 18


Example
L0
A
A

B C D

E F

What are the new neighbors of A?


Queue:

© 2004 Goodrich, Tamassia Breadth-First Search 19


Example
L0
A
A

L1
B C D

E F

Enqueue new neighbors of root A.


Queue: B C D

© 2004 Goodrich, Tamassia Breadth-First Search 20


Example
L0
A
A
B
L1
B C D

E F

Queue: B C D
Dequeue B  Queue: C D

© 2004 Goodrich, Tamassia Breadth-First Search 21


Example
L0
A
A
B
L1
B C D

E F
There are none!
Enqueue new neighbors of B.
Queue: C D

© 2004 Goodrich, Tamassia Breadth-First Search 22


Example
L0
A
A
B
L1 C
B C D

E F

Queue: C D
Dequeue C  Queue: D

© 2004 Goodrich, Tamassia Breadth-First Search 23


Example
L0
A
A
B
L1 C
B C D

L2
E F

Enqueue new neighbors of C.


Queue: D E F

© 2004 Goodrich, Tamassia Breadth-First Search 24


Example
L0
A
A
B
L1 C
B C D
D

L2
E F

Queue: D E F
Dequeue D  Queue: E F

© 2004 Goodrich, Tamassia Breadth-First Search 25


Example
L0
A
A
B
L1 C
B C D
D

L2
E F
F is already visited!
Enqueue new neighbors of D.
Queue: E F

© 2004 Goodrich, Tamassia Breadth-First Search 26


Example
L0
A
A
B
L1 C
B C D
D
E
L2
E F

Queue: E F
Dequeue E  Queue: F

© 2004 Goodrich, Tamassia Breadth-First Search 27


Example
L0
A
A
B
L1 C
B C D
D
E
L2
E F
There are none!
Enqueue new neighbors of E.
Queue: F

© 2004 Goodrich, Tamassia Breadth-First Search 28


Example
L0
A
A
B
L1 C
B C D
D
E
L2 F
E F

Queue: F
Dequeue F  Queue:

© 2004 Goodrich, Tamassia Breadth-First Search 29


Example
L0
A
A
B
L1 C
B C D
D
E
L2 F
E F
There are none!
Enqueue new neighbors of F.
Queue:

© 2004 Goodrich, Tamassia Breadth-First Search 30


Example
L0
A
A
B
L1 C
B C D
D
E
L2 F
E F

Queue: The next step would be to dequeue.


But since the queue is empty, we’re done!

© 2004 Goodrich, Tamassia Breadth-First Search 31


Now you try it!
Enqueue root 1.

Dequeue ____. Queue:


Dequeue ____. Queue:
Dequeue ____. Queue:
Dequeue ____. Queue:
Dequeue ____. Queue:
Dequeue ____. Queue:
Dequeue ____. Queue:
Dequeue ____. Queue:
Dequeue ____. Queue:
Dequeue ____. Queue:
Dequeue ____. Queue:
Dequeue ____. Queue:

© 2004 Goodrich, Tamassia Breadth-First Search 32


Now you try it!
Enqueue root 1.

Dequeue __1_. Queue: 2 3 4


Dequeue __2_. Queue: 3 4 5 6
Dequeue __3_. Queue: 4 5 6
Dequeue __4_. Queue: 5 6 7 8
Dequeue __5_. Queue: 6 7 8 9 10
Dequeue __6_. Queue: 7 8 9 10
Dequeue __7_. Queue: 8 9 10 11 12
Dequeue __8_. Queue: 9 10 11 12
Dequeue __9_. Queue: 10 11 12
Dequeue _10_. Queue: 11 12
Dequeue _11_. Queue: 12
Dequeue _12_. Queue:

© 2004 Goodrich, Tamassia Breadth-First Search 33


Depth-First Search
A

B C D

E F

© 2004 Goodrich, Tamassia Depth-First Search 34


Depth-First Search
Depth-first search (DFS) is a another technique for
traversing a graph.

A DFS traversal of a graph returns the nodes of the


graph by traveling deep through one path until
hitting a dead end and then retracing the steps.

© 2004 Goodrich, Tamassia Depth-First Search 35


What is a DFS traversal?
A Let’s start at A

B C D

E F

© 2004 Goodrich, Tamassia Breadth-First Search 36


What is a DFS traversal?
1

2 3 6

4 5

© 2004 Goodrich, Tamassia Breadth-First Search 37


What is a DFS traversal?
A Let’s start at A

B C D

E F

ABCEFD Is that the only DFS?

© 2004 Goodrich, Tamassia Breadth-First Search 38


What is a DFS traversal?
1

6 4 2

5 3

© 2004 Goodrich, Tamassia Breadth-First Search 39


Computer Algorithm
Let’s look at how a computer would perform
DFS!

© 2004 Goodrich, Tamassia Breadth-First Search 40


Stack
A stack is a pile.
If you put books in a pile, the last book
will be on the top, and it will be the first
one to be retrieved.

Last In, First Out

© 2004 Goodrich, Tamassia Breadth-First Search 41


Example
A Stack:

B C D

A
E F

Let’s start at A!
Push the root A onto the stack.

© 2004 Goodrich, Tamassia Breadth-First Search 42


Our Algorithm is the SAME!
Except, this time, we’re using a stack!
Each time, we’re going pop a node
(we’ll call it node X) off the stack.
Then, we’re going to push all of the
new neighbors of node X onto the
stack.

© 2004 Goodrich, Tamassia Breadth-First Search 43


First Step:

POP A NODE
OFF THE STACK!

© 2004 Goodrich, Tamassia Breadth-First Search 44


Example
A
Stack:

B C D

A
E F

Pop a node off the stack.


A

© 2004 Goodrich, Tamassia Breadth-First Search 45


Second Step:

PUSH THE NEW


NEIGHBORS ONTO THE
STACK!

© 2004 Goodrich, Tamassia Breadth-First Search 46


Example
A
Stack:

B C D

E F

What are the new neighbors of A?

© 2004 Goodrich, Tamassia Breadth-First Search 47


Example
A
Stack:

B C D
D
C
B
E F

Push the new neighbors of root A onto the stack.


A

© 2004 Goodrich, Tamassia Breadth-First Search 48


Example
A
Stack:

B C D
D
C
B
E F

Pop a node off the stack.


AD

© 2004 Goodrich, Tamassia Breadth-First Search 49


Example
A
Stack:

B C D
F
C
B
E F

Push the new neighbors of D onto the stack.


AD

© 2004 Goodrich, Tamassia Breadth-First Search 50


Example
A
Stack:

B C D
F
C
B
E F

Pop a node off the stack.


ADF

© 2004 Goodrich, Tamassia Breadth-First Search 51


Example
A
Stack:

B C D
C
B
E F
C is already visited!

Push the new neighbors of F onto the stack.


ADF

© 2004 Goodrich, Tamassia Breadth-First Search 52


Example
A
Stack:

B C D
C
B
E F

Pop a node off the stack.


ADFC

© 2004 Goodrich, Tamassia Breadth-First Search 53


Example
A
Stack:

B C D
E
B
E F

Push the new neighbors of C onto the stack.


ADFC

© 2004 Goodrich, Tamassia Breadth-First Search 54


Example
A
Stack:

B C D
E
B
E F

Pop a node off the stack.


ADFCE

© 2004 Goodrich, Tamassia Breadth-First Search 55


Example
A
Stack:

B C D

B
E F
There are none!

Push the new neighbors of E onto the stack.


ADFCE

© 2004 Goodrich, Tamassia Breadth-First Search 56


Example
A
Stack:

B C D

B
E F

Pop a node off the stack.


ADFCEB

© 2004 Goodrich, Tamassia Breadth-First Search 57


Example
A
Stack:

B C D

E F
There are none!

Push the new neighbors of B onto the stack.


ADFCEB

© 2004 Goodrich, Tamassia Breadth-First Search 58


Example
A
Stack:

B C D

E F

The next step would be to pop a node off the stack.


ADFCEB But since the stack is empty, we’re done!

© 2004 Goodrich, Tamassia Breadth-First Search 59


Example
A

B C D
Did you find it strange that
although we pushed in the E F
neighbors from left to right (e.g. B,
C, D), the order of the search that ADFCEB
we got back starts on the right?

That’s because we used a stack! Last in first out,


remember?
If we want to search to end up A B C E F D, all we
have to do is to push the neighbors into the stack
from right to left (e.g. D, C, B).

© 2004 Goodrich, Tamassia Breadth-First Search 60


DFS and Maze Traversal
The DFS algorithm is
similar to a classic
strategy for exploring a
maze.
We go as far as possible
on one path until we
reach a dead end.
Then, we retrace our
steps and go
somewhere we haven’t
visited before until we
reach a dead end.

© 2004 Goodrich, Tamassia Depth-First Search 61


Now you try it!
Stack

Stack Stack Stack

Pop off ____. Pop off ____. Pop off ____.

Stack Stack Stack

Pop off ____. Pop off ____. Pop off ____.

© 2004 Goodrich, Tamassia Breadth-First Search 62


Now you try it!
Stack
From
previous slide!

Stack Stack Stack

Pop off ____. Pop off ____. Pop off ____.

Stack Stack Stack

Pop off ____. Pop off ____. Pop off ____.

© 2004 Goodrich, Tamassia Breadth-First Search 63


Now you try it!
Stack

1
4
Stack Stack 3 Stack 5
2 6 6
7 7 7
Pop off __1_. 8 Pop off __2_. 8 Pop off __3_. 8

Stack 5 Stack Stack


6 6
7 7 7
8 8
Pop off __4_. 8 Pop off __5_. Pop off __6_.

© 2004 Goodrich, Tamassia Breadth-First Search 64


Now you try it!
Stack
From
7
previous slide!
8

Stack Stack Stack


10
9 11
Pop off __7_. 8 Pop off __8_. 12 Pop off __9_. 12

Stack Stack Stack

11
Pop off _10_. 12 Pop off _11_. 12 Pop off _12_.

© 2004 Goodrich, Tamassia Breadth-First Search 65


BFS vs. DFS
L0
A A

L1
B C D B C D

L2
E F E F

BFS DFS
ABCDEF ADFCEB

© 2004 Goodrich, Tamassia Breadth-First Search 66


Applications
BFS
 Finding the shortest path
 Testing for bipartiteness
 Bipartite graphs are useful for modeling
matching problems

DFS
 Maze Problem

© 2004 Goodrich, Tamassia Breadth-First Search 67


Bipartite Graphs

© 2004 Goodrich, Tamassia Breadth-First Search 68

You might also like