HINT: Try to follow conventions described in class. Display a matrix where the ith row is the value of d[1..n] in stage i. When a node first enters the set S, write an underscore (or, circle) that value. You need not copy values to the next row if they are unchanged.
NOTE: when performing Link(x,y), you are allowed to make x or y the new root (in the absence of the rank heuristic).
Furthermore, the two edges exiting from the − and ÷ nodes are distinguished (i.e., labeled as ``Left'' or ``Right'' edges) while the two edges exiting from + or * nodes are indistinguishable. This is because x−y is different from y−x, and x÷y is different from y÷x, while x+y=y+x and x*y = y*x. Also, this is actually a multigraph because it is possible that there there are two edges from a node d to another node a (e.g., see the graph G′ in (b). Also, the Left/Right order of edges are implicit in our figures.
We define two nodes to be if they are both internal nodes with the same operator label, and their ``corresponding children'' are identical or (recursively) equivalent. This is a recursive definition. By ``corresponding children'' we mean that the order (left or right) of the children should be taken into account in case of − and ÷, but ignored for + and * nodes. For instance, the nodes a and c in (a) are equivalent. Recursively, this makes e and f equivalent.
Our goal is to construct a G′ from
G in which each equivalence class of nodes in G
are replaced by a single new node. For instance
with G in (a) as input, the reduced graph
is G′ in (b).
(a) Describe an efficient algorithm to compute the
height of each node of G. Height is defined
as the length of longest path to a sink. E.g.,
height of node e in (a) is 3
(there are paths of lengths 2 and 3 from e to sinks).
(b) Briefly argue why your algorithm
is correct and analyze its running time.
(c) Design an efficient algorithm to compute the
reduced DAG for any input G.
HINTS:
To ``merge'' equivalent nodes, use Union-Find.
Note that you only need to check for equivalence among
nodes of the same height, and this must be done
in a bottom-up manner (i.e., smaller height nodes
are merged first). To avoid quadratic behavior, try sorting.
(d) Analyze the running time of your algorithm.