BoGE Algo TD4 Solution
BoGE Algo TD4 Solution
1 BFS
In this part, we will focus on the following graph:
e t
a c
g b d
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).
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 ).
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.
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