0% found this document useful (0 votes)
4 views4 pages

BoGE Algo TD4 Solution

This document covers the Breadth-First Search (BFS) algorithm for exploring graphs, detailing its implementation, data structures used, and time complexity. It explains how to modify BFS to identify connected components within a graph and presents pseudocode for both BFS and algorithms to find all connected components. The document emphasizes the importance of using appropriate data structures like queues and hash tables for efficient graph traversal.

Uploaded by

ritaberrada06
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)
4 views4 pages

BoGE Algo TD4 Solution

This document covers the Breadth-First Search (BFS) algorithm for exploring graphs, detailing its implementation, data structures used, and time complexity. It explains how to modify BFS to identify connected components within a graph and presents pseudocode for both BFS and algorithms to find all connected components. The document emphasizes the importance of using appropriate data structures like queues and hash tables for efficient graph traversal.

Uploaded by

ritaberrada06
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/ 4

Algorithms

Seminar 4 – Graph Search

This seminar aims at discovering another algorithm to explore a graph: Breadth-First


Search (BFS), and see an application of graph search algorithms.

1 BFS
In this part, we will focus on the following graph:

e t

a c

g b d

and try to go from vertex s to vertex t.


For BFS, vertices are visited by order of proximity with the starting point (e.g.,
the number of edges between them). Once all the vertices at a given level of proximity
have been visited, we move to the first vertex in the next level and start again.
1. Following the instructions given above, draw the path followed by the BFS
algorithm. Write the order in which the vertices are visited.

Answer
Assuming an alphabetical order, the following sequence corresponds to
BFS: s, a, b, c, d, e, f, t.

2. What data structure will be used to store the vertices left to visit?

Answer
Contrary to DFS, a queue is used to store the vertices to visit. The
queue garantees that the vertices are going to be visited in the right
order, i.e., increasing distance from the source vertex (where the distance

1
is the number of arcs here).

3. Write the pseudocode for the BFS algorithm.


Hint: It resembles the iterative version of DFS a lot.

Answer

Algorithm 1: BFS(G, s, t)
1 Q = QUEUE()
2 ENQUEUE(Q, s)
3 V isited = {s}
4 while Q.size > 0 do
5 u = DEQUEUE(Q)
6 if u == t then
7 return true
8 for v ∈ G.Adj[u] do
9 if v ∈
/ V isited then
10 V isited.add(v)
11 ENQUEUE(Q,v)
12 return false

4. Which data structure should be used for the set of visited vertices?

Answer
Since we need good search performance, it is best to use a hash table (i.e.,
a dictionary in python), which yields a time complexity of O(1) when it
comes to searching for an element.

5. What are the ways of implementing G.Adj[u]? What is the search time for
each?

Answer
Either an adjacency list or adjacency matrix. If an adjacency list is
used, the time complexity of the search is O(deg(u)). Otherwise, with an
adjacency matrix, it is O(|V |).

6. What is the worst-case time complexity for BFS? What becomes of this com-
plexity when the graph is densely connected?

Answer
To compute the complexity, we just need to know how many times each
loop is executed. Whether it is a queue or a stack, the worst-case scenario
has next take every vertex in the graph. Hence, its time complexity is
O(|V |) overall. When an adjacency matrix is used, the time complexity
is therefore O(|V |2 ).

P the for loop


With an adjacency list, on the other hand, we can notice that
will execute O(deg(u) times for each u, which gives O u∈V deg(u) =


2
O(2|E|) = O(|E|) after the while loop finishes. Therefore, in that case,
the worst-case time complexity is only O(|V | + |E|).
Finally, when the graph is densely connected |E| → |V |2 , thus the com-
plexity is O(|E|).

2 Connected components
When a graph G is not connected, each subgraph G0 ⊂ G that is connected is called a
connected component of G. In the example below, G has three connected components:

i
f

e g h
b

d
c

The problem of identifying one connected component could be written like this:
Input: A graph G = (V, E), a vertex s ∈ V
Question: Find the largest possible subgraph G0 ⊂ G containing s.

7. What should be modified in BFS to solve this problem?

Answer
We need to remove the test (if u == t) so that the search goes on until
all vertices have been reached in the component. Instead of returning a
boolean, we can return the set of visited vertices.

Algorithm 2: BFS-SINGLE-CC(G, s)
1 Q = QUEUE()
2 ENQUEUE(Q, s)
3 V isited = {s}
4 while Q.size > 0 do
5 u = DEQUEUE(Q)
6 for v ∈ G.Adj[u] do
7 if v ∈
/ V isited then
8 V isited.add(v)
9 ENQUEUE(Q,v)
10 return Visited

8. Based on the previous question, write an algorithm to find all the connected
components in a graph.

3
Answer
We simply need to apply the previous algorithm to each vertex in
the graph (we also associate each connected component to a different
number):

Algorithm 3: BFS-ALL-CC(G, s)
1 Components = ∅
2 for v ∈ G.V do
3 Components[v] = −1
4 i = 0
5 for v ∈ G.V do
6 if Components[v] == −1 then
7 C = BFS-SINGLE-CC(G, v)
8 for u ∈ C do
9 Components[u] = i
10 i=i+1
11 return Components

You might also like