GATE / CS ARCHIVE

GATE CSE 1999 / QUESTION 8

Optimal decision tree for three matrix minima

Algorithms · Searching, Sorting & Selection · Full GATE CSE 1999 paper

GATE 1999, Q8 5 marks · Descriptive
Report issue

Searching, Sorting & Selection

Question 8, GATE 1999: An n×n matrix A has every row and column in ascending order. Draw a decision tree finding its first, second, and third smallest entries using the minimum number of comparisons. Assume n≥2; count repeated values as separate entries.
Tap to enlarge
Shared context

An n×n matrix A has every row and column in ascending order. Draw a decision tree finding its first, second, and third smallest entries using the minimum number of comparisons. Assume n≥2; count repeated values as separate entries.

Answer & solution

Answer: Two comparisons suffice and are necessary in the worst case. (no official key was published)

The first entry is always A₁₁. Set b=A₁₂ and c=A₂₁. Every remaining entry is at least b or c, so the second is min(b,c).

If b≤c, the only candidates for third are c and d=A₁₃. Other entries in the first row are at least d; entries outside the first row are at least c (including A₂₂). If c<b, the analogous candidates are b and e=A₃₁. When n=2, the missing d or e can be treated as +∞, so the third is simply the other one of b,c and just one comparison suffices for that special size.

For n≥3 the full decision tree is:

                         compare b ≤ c?
                       / yes             \ no
              compare d ≤ c?           compare e ≤ b?
              / yes     \ no           / yes      \ no
          (A₁₁,b,d)  (A₁₁,b,c)    (A₁₁,c,e)   (A₁₁,c,b)

Each root-to-leaf path has two comparisons. The leaves give the first three entries in order. Ties can follow the ≤ branch: tied entries have equal values, so either identity order is valid.

Optimality for n≥3: all four strict order possibilities displayed at the leaves can occur in a row-and-column-sorted matrix. A single binary comparison has only two outcomes and cannot distinguish four possible ordered triples of positions; two comparisons are necessary in the worst case. For n=2 there are only the two orders of b,c and one comparison is both sufficient and necessary. For n=1 a third entry does not exist, so the task presupposes at least n=2.

Original paper, page 7