03 Breadth First Search
03 Breadth First Search
Graph Traversal
Breadth First Search
v
Find a sequence of vertices
0
v0, v1, …, vk such that
• v0 is source
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
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 …
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 = []
• Overall, O(n+m)
Complexity of Breadth First Search
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?
start
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?
• Do a BFS starting at w
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
G
Breadth First Search Application:
Bipartite Testing
LEA RL Y
C !
R TI TE
BIPA
Breadth First Search Application:
Bipartite Testing (Another Example)