0% found this document useful (0 votes)
9 views73 pages

03 Breadth First Search

The document provides an overview of graph traversal, specifically focusing on the Breadth First Search (BFS) technique. It outlines the objectives and outcomes of a session on BFS, explaining its application in various areas, complexity analysis, and the process of traversing a graph level by level. Additionally, it includes examples and pseudocode to illustrate the BFS algorithm and its efficiency in exploring graphs.

Uploaded by

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

03 Breadth First Search

The document provides an overview of graph traversal, specifically focusing on the Breadth First Search (BFS) technique. It outlines the objectives and outcomes of a session on BFS, explaining its application in various areas, complexity analysis, and the process of traversing a graph level by level. Additionally, it includes examples and pseudocode to illustrate the BFS algorithm and its efficiency in exploring graphs.

Uploaded by

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

Amity School of Engineering and Technology

Graph Traversal
Breadth First Search

B.Tech (CSE), Semester 5,


Analysis and Design of Algorithms
Dr. Shuchi Mala
Objectives
This session will enable students to:

1. Understand the need and basics of graph traversal.

2. Traverse the graph using breadth first search technique.


Outcomes
After completing this session, students will be able to:

1. Apply breadth first search technique for various applications areas.

2. Analyze the complexity of breadth first search technique.


Graph Traversal

• Graph traversal refers to the process of visiting each vertex in a graph.

• Graph Traversal vs Graph Search


 "Traversal": visit every node in the graph.
 "Search": visit nodes until find what we are looking for. E.g.:
 A node labeled "New York".
 A node containing integer 2014

• Using graph traversal, we can:

 Find if there’s a connection from one node to another?


 Determine if we can start from our node and touch every other node?
 Find the shortest path between two nodes?
Finding a Route
• Graph traversal refers to the process of visiting each vertex in a graph.

v
Find a sequence of vertices
0
v0, v1, …, vk such that

• v0 is source

• Each (vi,vi+1) is an edge


v
in E v
1
3
• vk is target

v v
2 4

v
Finding a Route
• Graph traversal refers to the process of visiting each vertex in a graph.

v
Find a sequence of vertices
0
v0, v1, …, vk such that

• v0 is source

• Each (vi,vi+1) is an edge


v
in E v
1
3
• vk is target

v v
For small graphs, route can be found 4
2
visually. For large graphs, some
traversal algorithm is required. v
Graph Traversal: Breadth First Search
• Explore the graph level by level.
• First visit vertices one step away
• Then two steps away …

• Remember which vertices have been visited

• Also keep track of vertices visited, but whose neighbours


are yet to be explored

• Array visited[i] records whether i has been visited

• When a vertex is visited for the first time, add it to a queue

• Explore vertices in the order they reach the queue


Graph Traversal: Breadth First Search
• Exploring a vertex i:

for each edge (i,j) if


visited[j] == 0
visited[j] = 1 append j to
queue

• Initially, queue contains only source vertex


• At each stage, explore vertex at the head of the queue
• Stop when the queue becomes empty
Breadth First Search
1
2
1 3
4
3 5
Visite
6
2 5 d
7
4 8
6 7
9
10
Queu
8 9 e

1
Breadth First Search
1
2
1 3
4
3 5
Visite
6
2 5 d
7
4 8
6 7
9
10
Queu
8 9 e

1 head
Breadth First Search
1 1
2
1 3
4
3 5
Visite
6
2 5 d
7
4 8
6 7
9
10
Queu
8 9 e
1
1
Breadth First Search
1 1
2
1 3
4
3 5
Visite
6
2 5 d
7
4 8
6 7
9
10
Queu
8 9 e

1
Breadth First Search
1 1
2 1
1 3
4
3 5
Visite
6
2 5 d
7
4 8
6 7
9
10
Queu
8 9 e
2
1
Breadth First Search
1 1
2 1
1 3 1
4
3 5
Visite
6
2 5 d
7
4 8
6 7
9
10
Queu
8 9 e
2 3
1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5
Visite
6
2 5 d
7
4 8
6 7
9
10
Queu
8 9 e
2 3 4
1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5
Visite
6
2 5 d
7
4 8
6 7
9
10
Queu
8 9 e
3 4
110
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5
Visite
6
2 5 d
7
4 8
6 7
9
10
Queu
8 9 e
4
1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5
Visite
6
2 5 d
7
4 8
6 7
9
10
Queu
8 9 e

1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5 1
Visite
6
2 5 d
7
4 8
6 7
9
10
Queu
8 9 e
5
1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5 1
Visite
6
2 5 d
7
4 8 1
6 7
9
10
Queu
8 9 e
5 8
1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5 1
Visite
6
2 5 d
7
4 8 1
6 7
9
10
Queu
8 9 e
8
1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5 1
Visite
5
6 1
2 d
7
4 8 1
6 7
9
10
Queu
8 9 e
8 6
1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5 1
Visite
5
6 1
2 d
7 1
4 8 1
6 7
9
10
Queu
8 9 e
8 6 7
1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5 1
Visite
5
6 1
2 d
7 1
4 8 1
6 7
9
10
Queu
8 9 e
6 7
1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5 1
Visite
5
6 1
2 d
7 1
4 8 1
6 9 1
7
10
Queu
8 9 e
6 7 9
1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5 1
Visite
5
6 1
2 d
7 1
4 8 1
6 9 1
7
10
Queu
8 9 e
7 9
1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5 1
Visite
5
6 1
2 d
7 1
4 8 1
6 9 1
7
10
Queu
8 9 e
9
1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5 1
Visite
5
6 1
2 d
7 1
4 8 1
6 9 1
7
10
Queu
8 9 e

1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5 1
Visite
6 1
2 5 d
7 1
4 8 1
6 9 1
7
10 1
Queu
8 9 e
10
1
Breadth First Search
1 1
2 1
1 3 1
4 1
3 5 1
Visite
6 1
2 5 d
7 1
4 8 1
6 9 1
7
10 1
Queu
8 9 e

1
Breadth First Search
function BFS(i) // BFS starting from vertex i

//Initialization
for j = 1..n {visited[j] = 0}; Q = []

//Start the exploration at i


visited[i] = 1; append(Q,i)

//Explore each vertex in Q


while Q is not empty
j = extract_head(Q)
for each (j,k) in
E
if visited[k] == 0
visited[k] = 1;
append(Q,k)
Complexity of Breadth First Search

• Each vertex enters Q exactly once

• If graph is connected, loop to process Q iterated n times

• For each j extracted from Q, need to examine all


neighbours of j

• In adjacency matrix, scan row j: n entries

• Hence, overall O(n2)


Complexity of Breadth First Search
• Let m be the number of edges in E. What if m << n2?

• Adjacency list: scanning neighbours of j takes time


proportional to number of neighbours (degree of j)

• Across the loop, each edge (i, j) is scanned twice, once


when exploring i and again when exploring j

• Overall, exploring neighbours takes time O(m)

• Marking n vertices visited still takes O(n)

• Overall, O(n+m)
Complexity of Breadth First Search

• For graphs, O(m+n) is considered the best possible


Need to see each edge and vertex at least once

• O(m+n) is considered to be linear in the size of the graph


Breadth First Search: Another Example

Queue: a, g
d
a j
i
Visited: a,
h
e b

c
Breadth First Search: Another Example

Queue: b, d g
d
a j
i
Visited: a, b, d
h
e b

c
Breadth First Search: Another Example

Queue: d, c, e g
d
a j
i
Visited: a, b, d, c, e
h
e b

c
Breadth First Search: Another Example

g
Queue: c, e, f, g d
a j
i
Visited: a, b, d, c, e, f, g
h
e b

c
Breadth First Search: Another Example

g
Queue: e, f, g d
a j
i
Visited: a, b, d, c, e, f, g
h
e b

c
Breadth First Search: Another Example

g
Queue: f, g, h d
a j
i
Visited: a, b, d, c, e, f, g, h
h
e b

c
Breadth First Search: Another Example

Queue: g, h g
d
a j
i
Visited: a, b, d, c, e, f, g, h
h
e b

c
Breadth First Search: Another Example

g
Queue: h, i d
a j
i
Visited: a, b, d, c, e, f, g, h, i
h
e b

c
Breadth First Search: Another Example

g
Queue: i d
a j
i
Visited: a, b, d, c, e, f, g, h, i
h
e b

c
Breadth First Search: Another Example

g
Queue: d
a j
i
Visited: a, b, d, c, e, f, g, h, i
h
e b

c
Breadth First Search:
An Interesting Property
Note: We visited the nodes in “rings” – maintained a gradually growing
“frontier” of nodes.

g
d
a j
i
h
e b

c
Breadth First Search:
An Interesting Property
Note: We visited the nodes in “rings” – maintained a gradually growing
“frontier” of nodes.

g
d
a j
i
h
e b

c
Breadth First Search:
An Interesting Property
Note: We visited the nodes in “rings” – maintained a gradually growing
“frontier” of nodes.

g
d
a j
i
h
e b

c
Breadth First Search:
An Interesting Property
What does this look like for trees?
Breadth First Search:
An Interesting Property
What does this look like for trees?
Breadth First Search:
An Interesting Property
What does this look like for trees?
Breadth First Search:
An Interesting Property
What does this look like for trees?

The algorithm traverses the width, or “breadth” of the tree


Breadth First Search
BFS also finds all the nodes
reachable from the starting point

start

It is also a good way to find


all the connected
components.
Breadth First Search: Application

Shortest Path:
• How long is the shortest path between w and v?

v
Breadth First Search: Application

Shortest Path:
How long is the shortest path between w and v?

Not been there


yet
Can reach there
in zero steps

Can reach there


w in one step

Can reach there


in two steps
v
Can reach there
in three steps
It’s three!
Breadth First Search: Application
The distance between two vertices
is the length of the shortest path
between them.
To find the distance between w and all other vertices v:

• Do a BFS starting at w

• For all v in Li (the i’th level of the BFS tree)


• The shortest path between w and v has length i
• A shortest path between w and v is given by the path
in the BFS tree.

• If we never found v, the distance is infinite.


Breadth First Search: Application

• What did we just learn?

• The BFS tree is useful for computing distances


between pairs of vertices.
• We can find the shortest path between u and v in
time O(m).
• The BSF tree is also helpful for:
 Testing if a graph is bipartite or not.
Breadth First Search Application:
Bipartite Testing

• Bipartite means it looks like this: Can we color the vertices


so that there are no edges
between any same-
colored vertices

Example:
are students
are classes
if the student is
enrolled in the class
Breadth First Search Application:
Bipartite Testing
Is this graph bipartite?
Breadth First Search Application:
Bipartite Testing
Is this graph bipartite?
Breadth First Search Application:
Bipartite Testing
What about this one?
Breadth First Search Application:
Bipartite Testing

Solution using BFS


• Color the levels of the BFS tree in alternating
colors. A

• If you ever color a node so that you never color


two connected nodes the same, then it is B E
bipartite.

• Otherwise, it’s not. F


C D

G
Breadth First Search Application:
Bipartite Testing

Not been there


yet
Can reach there
in zero steps

Can reach there


in one step
start
Can reach there
in two steps

Can reach there


in three steps
Breadth First Search Application:
Bipartite Testing

Not been there


yet
Can reach there
in zero steps

Can reach there


in one step
start
Can reach there
in two steps

Can reach there


in three steps
Breadth First Search Application:
Bipartite Testing

Not been there


yet
Can reach there
in zero steps

Can reach there


in one step
start
Can reach there
in two steps

Can reach there


in three steps
Breadth First Search Application:
Bipartite Testing

Not been there


yet
Can reach there
in zero steps

Can reach there


in one step
start
Can reach there
in two steps

Can reach there


in three steps
Breadth First Search Application:
Bipartite Testing

Not been there


yet
Can reach there
in zero steps

Can reach there


in one step
start
Can reach there
in two steps

Can reach there


in three steps

LEA RL Y
C !
R TI TE
BIPA
Breadth First Search Application:
Bipartite Testing (Another Example)

Not been there


yet
Can reach there
in zero steps

Can reach there


in one step
start
Can reach there
in two steps

Can reach there


in three steps
Breadth First Search Application:
Bipartite Testing (Another Example)

Not been there


yet
Can reach there
in zero steps

Can reach there


in one step
start
Can reach there
in two steps

Can reach there


in three steps
Breadth First Search Application:
Bipartite Testing (Another Example)

Not been there


yet
Can reach there
in zero steps

Can reach there


in one step
start
Can reach there
in two steps

Can reach there


in three steps
Breadth First Search Application:
Bipartite Testing (Another Example)

Not been there


yet
Can reach there
in zero steps

Can reach there


in one step
start
Can reach there
in two steps

Can reach there


in three steps
Breadth First Search Application:
Bipartite Testing (Another Example)

Not been there


yet
Can reach there
in zero steps

Can reach there


in one step
start
Can reach there
in two steps
NOT !
RTI TE
BIPA
Can reach there
in three steps
Breadth First Search Application:
Bipartite Testing (Another Example)

BFS can be used to detect


bipartite-ness in time O(n + m).
Summary

• Graph Traversal- Need and Basics


• Breadth First Search Algorithm
• Complexity Analysis of Breadth First Search Algorithm
• Breadth First Search Algorithm: Applications

You might also like