Question

Consider evaluating the following expression tree on a machine with load-store architecture in which memory can be accessed only through load and store instructions. The variables a,b,c,da, b, c, d and ee are initially stored in memory. The binary operators used in this expression tree can be evaluated by the machine only when the operands are in registers. The instructions produce result only in a register. If no intermediate results can be stored in memory, what is the minimum number of registers needed to evaluate this expression?

Expression tree with root plus, left child minus with operands a and b, and right child minus with operand e and right subtree plus of c and d
Choose one option
Official answer and solution
Official answer
D
From
Official GATE 2011 answer key

Solution

Apply the Sethi-Ullman algorithm to determine the minimum number of registers required for expression evaluation.

  1. Under load-store architecture with binary operations occurring only between registers, the Sethi-Ullman labeling L(n)L(n) for expression tree nodes is defined as:
  • For leaf node xx stored in memory: L(x)=1L(x) = 1 (needs 1 register to load).
  • For an interior binary node with children ll and rr:
L(n)={max⁡(L(l),L(r))if L(l)≠L(r)L(l)+1if L(l)=L(r)L(n) = \begin{cases} \max(L(l), L(r)) & \text{if } L(l) \ne L(r) \\ L(l) + 1 & \text{if } L(l) = L(r) \end{cases}
  1. Compute labels from bottom up:
  • Leaf nodes a,b,c,d,ea, b, c, d, e: each has label 11.
  1. Evaluate subtrees:
  • Left - node (a−b)(a - b): children aa and bb both have label 1, so L(−1)=1+1=2L(-_1) = 1 + 1 = 2.
  • Inner + node (c+d)(c + d): children cc and dd both have label 1, so L(+2)=1+1=2L(+_2) = 1 + 1 = 2.
  • Right - node (e−(c+d))(e - (c + d)): left child ee has label 1, right child (c+d)(c + d) has label 2. Since L(l)≠L(r)L(l) \ne L(r), L(−2)=max⁡(1,2)=2L(-_2) = \max(1, 2) = 2.
  1. Root + node: left child has label 2 and right child has label 2. Since both children have equal label 2, the root label is:
L(root)=2+1=3L(\text{root}) = 2 + 1 = 3
  1. Thus, evaluating one subtree requires 2 registers while holding its result in 1 register, and evaluating the other subtree simultaneously requires 2 registers, totaling 1+2=31 + 2 = 3 registers minimum.
Final answer

D

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

Source

Paper
GATE 2011 Computer Science and Information Technology (CS)
Question
36 of 65, printed as CS Q.36
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.