Question

The given diagram shows the flowchart for a recursive function A(n)A(n). Assume that all statements, except for the recursive calls, have O(1)O(1) time complexity. If the worst case time complexity of this function is O(nα)O(n^\alpha), then the least possible value (accurate up to two decimal positions) of α\alpha is ________.

Flowchart for recursive function A(n). Starting at Start, control passes through an A(n/2) call to Decision Diamond 1. From Diamond 1, the left branch calls A(n/2) and returns (2 calls). The right branch passes through two sequential A(n/2) calls to Decision Diamond 2. From Diamond 2, the right branch returns (3 calls total), while the down branch calls A(n/2) to reach Decision Diamond 3. From Diamond 3, the right branch returns (4 calls total), while the left branch calls A(n/2) and returns (5 calls total). Worst-case execution path makes 5 recursive calls of size n/2.
Flowchart for Recursive Function A(n)
Official answer and solution
Accepted answers
2.2 to 2.4
From
Official GATE 2016 answer key

Solution

Trace all execution paths in the flowchart to find the worst-case number of recursive calls, then apply the Master Theorem.

Trace all possible paths from Start to Return in the flowchart:

  1. Path 1: Start →A(n/2)→\to A(n/2) \to Diamond 1 Left →A(n/2)→\to A(n/2) \to Return.
    Total recursive calls: 1+1=21 + 1 = 2 calls.
  1. Path 2: Start →A(n/2)→\to A(n/2) \to Diamond 1 Right →A(n/2)→A(n/2)→\to A(n/2) \to A(n/2) \to Diamond 2 Right →\to Return.
    Total recursive calls: 1+1+1=31 + 1 + 1 = 3 calls.
  1. Path 3: Start →A(n/2)→\to A(n/2) \to Diamond 1 Right →A(n/2)→A(n/2)→\to A(n/2) \to A(n/2) \to Diamond 2 Down →A(n/2)→\to A(n/2) \to Diamond 3 Right →\to Return.
    Total recursive calls: 1+1+1+1=41 + 1 + 1 + 1 = 4 calls.
  1. Path 4: Start →A(n/2)→\to A(n/2) \to Diamond 1 Right →A(n/2)→A(n/2)→\to A(n/2) \to A(n/2) \to Diamond 2 Down →A(n/2)→\to A(n/2) \to Diamond 3 Left →A(n/2)→\to A(n/2) \to Return.
    Total recursive calls: 1+1+1+1+1=51 + 1 + 1 + 1 + 1 = 5 calls.

In the worst case, the function executes Path 4, making 5 recursive calls of input size n/2n/2. All other statements take O(1)O(1) time, yielding the recurrence:

T(n)=5T(n/2)+Θ(1)T(n) = 5 T(n/2) + \Theta(1)

By the Master Theorem (Case 1) with a=5,b=2,f(n)=Θ(1)a = 5, b = 2, f(n) = \Theta(1):

nlog⁡ba=nlog⁡25n^{\log_b a} = n^{\log_2 5}
α=log⁡25=ln⁡5ln⁡2≈1.6094380.693147≈2.3219\alpha = \log_2 5 = \frac{\ln 5}{\ln 2} \approx \frac{1.609438}{0.693147} \approx 2.3219

Rounding to two decimal positions gives α=2.32\alpha = 2.32.

Final answer

2.32

Common trap

Missing the initial A(n/2)A(n/2) before Diamond 1 or miscounting the sequential calls along the right branch of Diamond 1.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2016 Computer Science and Information Technology (CS) Session 2
Question
49 of 65, printed as CS Q.39
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.