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 and 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?
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.
- Under load-store architecture with binary operations occurring only between registers, the Sethi-Ullman labeling for expression tree nodes is defined as:
- For leaf node stored in memory: (needs 1 register to load).
- For an interior binary node with children and :
- Compute labels from bottom up:
- Leaf nodes : each has label .
- Evaluate subtrees:
- Left
-node : children and both have label 1, so . - Inner
+node : children and both have label 1, so . - Right
-node : left child has label 1, right child has label 2. Since , .
- Root
+node: left child has label 2 and right child has label 2. Since both children have equal label 2, the root label is:
- Thus, evaluating one subtree requires 2 registers while holding its result in 1 register, and evaluating the other subtree simultaneously requires 2 registers, totaling registers minimum.
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.