Professional Documents
Culture Documents
Competitive Programming: Bfs and Dfs
Competitive Programming: Bfs and Dfs
Week 5 Assignment
1. Say while implementing DFS, you add a time variable T and initially set it to 0. T is incremented every
time a vertex in the graph is visited. For vertex v, let Pre(v) = T, right before we mark the vertex as
visited and increase T. Post(v) is T when all adjacent vertices of v are visited.
Consider two vertices A and B.
(a) If Post(A) < Pre(B), then A and B are in the same connected component.
A. True B. False C. Can’t say
Solution
It could be that both are on different sides of a tree or they could also be part of different
connected components.
(b) If Post(A) > Pre(B) and Post(A) < Post(B), then A and B are in the same connected component.
A. True B. False C. Can’t say
Solution
If Post(A) > Pre(B) and Post(A) < Post(B), then that means A is visited while visiting B’s
neighbours and doing DFS on them. So A and B should be in the same connected compo-
nent.
It could be a isolated vertex, or a vertex with degree 1 or also a vertex where all of its
neighbours are already visited.
Page 2
2. Suppose you are given a tree having N vertices numbered from 1 to N in the form of adjacency list
adj list. You can do the following one operation on this tree as many times as possible:
→ Delete all vertices which have either 0 or 1 neighbours.
You need to determine after how many such operations on the given tree, all of its vertices will be
deleted.
Note: Tree is a connected and undirected graph which do not have any cycles.
(a) For the below given tree, after how many operations all of its vertices will be deleted?
Solution
Page 3
distance := [INF, INF, INF, ..., INF]
distance[root] := 0
The given BFS function returns the maximum distance of any leaf from the given root. If
we choose the root such that the maximum distance between the root and any other vertex
is least, then this distance will be same as the minimum required operations to delete all
the elements. Because if we root the tree at some vertex such that it has minimum number
of different levels, the according to the given operation, in each operation we will delete
all vertices of the farthest level. So, number of minimum operations equals to number of
minimum levels.
(c) What is the time complexity and space complexity of the above given BFS function on tree with N
vertices?
A. Time Complexity := O(N 2 ), Space Complexity := O(N 2 )
B. Time Complexity := O(N ), Space Complexity := O(N )
C. Time Complexity := O(N ), Space Complexity := O(N 2 )
D. Time Complexity := O(N 2 ), Space Complexity := O(N )
Page 4
Solution
The time complexity of BFS is O(V + E) as during the visit of every vertex every edge is
visited exactly once. Since tree has N vertices and N − 1 edges, time complexity will be
O(N ). The maximum size the queue can reach is N and the distance array also uses O(N )
space, so the space complexity will be O(N ).
Page 5
3. Consider the problem Longest Trail
You are a map of islands, V and the distance between different pair of islands. Each island is uniquely
identified by a number between 1 and |V |. Each island is connected to another island either directly or
through a series of islands Ii , Ii+1 , ...., Ij . However, there is no cycle between any two islands. There
is only one boat available for traversing between the islands. Your trip to these islands only allows for
one exchange between any pair of islands: if you start from island Ii , and stop at Ij , you cannot use the
boat further, your trip ends. Additionally, you can only fill the fuel in the boat once for the whole trip.
Your task is to find the maximum amount of fuel that you should fill your boat with, so you can cover
the maximum possible distance in your whole trip. Assume the boat mileage to be 1 unit per litre.
For example:
Consider the input of form (u , v | dist(u, v)) u, v ∈ G
|V| = 3
1 , 2 | 3
2 , 3 | 4
The output should be : 7
(a) Answer either True or False: Consider the islands as vertices and add an edge between any pair
of islands that are connected directly. For any pair of islands that are not directly connected, there
are no edges. This graph is a tree.
Solution
The resulting graph is circuit-free (as no islands are connected by a circular path). Addi-
tionally, every island (or vertex) is connected with every other island (or vertex). Hence,
the resulting graph is one connected component. By definition, a connected and circuit-free
graph is a tree. Hence the statement is true.
Page 6
4. Consider the problem Get Target Fluid!
You are given two jars J1 and J2 with capacity J1cap, J2cap respectively (in litres). Your task is to
measure out exactly x liters of water using J1 and/or J2 . The following operations are allowed:
• Empty either J1 or J2
• Fill either of the jugs with water to its maximum capacity
• Shift water from one jug to another till water level in the first jug is either zero or the second jug is
full
You are allowed to do these operations any number of times as you wish. Note that if it is possible to
retrieve x capacity of water using these operations, we shall have x liters of water in either J1 , J2 or
both at the end of the iterations.
For example:
J1cap = 6 , J2cap = 2 , x = 5
Output: False
(a) What will Get Target Fluid output for the following input?
J1cap = 10078 , J2cap = 9899 , x = 45
A. True
B. False
Solution
Page 7
Among the following options, select the most appropriate for Exp1 and Exp2.
A. Exp1 => j1cap < j2cap and j1cap<t
B. Exp1 => max(j1cap,j2cap)>t and j1cap%j2cap==0
C. Exp1 => j1cap+j2cap<t
D. Exp2 => t%hcf(j1cap,j2cap)
E. Exp2 => t%lcm(j1cap,j2cap)
F. Exp2 => lcm(j1cap,j2cap)%t
G. Exp2 => hcf(j1cap,j2cap)%t
H. Exp2 => hcf(j1cap-t,j2cap-t)
I. Exp2 => t%max(j1cap,j2cap)
Solution
Food for thought: When we transfer/shift fluid from one jug to another, can we argue
that the net effect of transfer is always in terms of hcf of the two capacities?
Page 8