3.2. Convert Algorithm
We show our algorithm to construct the DenseZDD $DZ$ from a given ZDD G in detail. The pseudocode of our algorithm is given in Algorithm 6. First, we describe how to build the zeroedge tree from G.
The zeroedge tree consists of all 0edges of the ZDD, with their directions being reversed. For a nonterminal node v, we say that v is a ${0}^{\mathrm{r}}$child of $\mathit{zero}(v)$. To make a zeroedge tree, we prepare a list ${\mathit{revzero}}_{v}$ for each node v, which stores ${0}^{\mathrm{r}}$children of the node v. For all nonterminal nodes v, we visit v by a depthfirst traversal of the ZDD and add v to ${\mathit{revzero}}_{\mathit{zero}(v)}$. This is done in $\mathcal{O}(m)$ time and $\mathcal{O}(m)$ space because each node is visited at most twice and the total size of ${\mathit{revzero}}_{v}$ for all v is the same as the number of nonterminal nodes.
Let T be the zeroedge tree before introducing dummy nodes. Let us introduce an order of the elements in ${\mathit{revzero}}_{v}$ for each v in T so that the $\mathit{getnode}$ operation, described later, can be executed efficiently. Note that the preorder ranks of nodes in T are determined by the order of children of every node in T.
Here, we observe the following fact. Consider a node
v in
T, and suppose that
v has
${0}^{\mathrm{r}}$children
${v}_{1},\dots ,{v}_{k}$, which are ordered as
${v}_{1}<\cdots <{v}_{k}$. Let
$\mathit{stsize}(v)$ be the subtree size of a node
v in
T. Then, if the preorder rank of
v is
p, that of
${v}_{i}$ is
$p+{\sum}_{j=1}^{i1}\mathit{stsize}({v}_{j})+1$ for
$i=1,\dots ,k$. Note that, even if the order of
${v}_{1},\dots ,{v}_{i1}$ is not determined (it is only determined that
${v}_{i}$ is located in the
ith position), the preorder rank of
${v}_{i}$,
$p+{\sum}_{j=1}^{i1}\mathit{stsize}({v}_{j})+1$, can be computed (see
Figure 8).
Now, we introduce an order of the elements in ${\mathit{revzero}}_{v}$ for each node v. First, for each node v, we order the elements in ${\mathit{revzero}}_{v}$ in the descending order of their indices. If the indices of two nodes are the same, we temporarily and arbitrarily determine their order (we will change the order later). Then, we do the following procedure for $i=1,\dots ,n$. In the ith procedure, suppose that the preorder ranks of all the nodes with index smaller than i have already been determined. We consider a node v. Let $\{{v}_{1},\dots ,{v}_{p}\}$, $\{{v}_{p+1},\dots ,{v}_{p+q}\}$ and $\{{v}_{p+q+1},\dots ,{v}_{p+q+r}\}$ be the sets of nodes with index larger than i, equals to i, and smaller than i in ${\mathit{revzero}}_{v}$, respectively. By our assumption, ${v}_{p+q+1},\dots ,{v}_{p+q+r}$ have already been ordered and we now determine the order of ${v}_{p+1},\dots ,{v}_{p+q}$. We let the order of ${v}_{p+1},\dots ,{v}_{p+q}$ be the descending order of the preorder ranks of their onechildren. Note that the preorder ranks of the onechildren of ${v}_{p+1},\dots ,{v}_{p+q}$ have already been determined. Thus, since the positions of ${v}_{p+1},\dots ,{v}_{p+q}$ in ${\mathit{revzero}}_{v}$ are determined, the above observation implies that the preorder ranks of ${v}_{p+1},\dots ,{v}_{p+q}$ are also determined. We do it for all nodes v. After the procedure is finished, the preorder ranks of all the nodes (that is, the order of the children of nodes in T) are determined. As a result, the ${0}^{\mathrm{r}}$children ${v}^{\prime}$ of each node are sorted in the lexicographical order of $\langle \mathit{index}({v}^{\prime}),\mathit{prank}(\mathit{one}({v}^{\prime}))\rangle $. The pseudocode is given in Algorithm 1.
Algorithm 1 Compute_Preorder: Algorithm that computes the preorder rank $\mathit{prank}(v)$ of each node v. Sets of nodes are implemented by arrays or lists in this code. 
 1:
${L}_{0}\leftarrow \left\{\langle \left\{\mathbf{0}\right\},[0,\mathit{stsize}(\mathbf{0})1]\rangle \right\}$  2:
${L}_{1},\dots ,{L}_{n}$ are lists that are empty initially.  3:
for$i=0,\dots ,n$do ▹${L}_{i}$ includes sets of nodes that have the same index  4:
for each $\langle A,[l,r]\rangle \in {L}_{i}$ in arbitrary order do ▹A is a set of nodes that have the same index and 0child ▹ Note that $\mathit{prank}$ of nodes with indices less than i are already computed  5:
for each $v\in A$ in descending order of $\mathit{prank}(one(v))$ do  6:
$\mathit{prank}(v)\leftarrow l$;  7:
$l\leftarrow l+1$;  8:
for each $j\in \left\{\phantom{\rule{0.222222em}{0ex}}j\phantom{\rule{0.166667em}{0ex}}\right\phantom{\rule{0.166667em}{0ex}}w\in \mathit{revzero}(v),j=\mathit{index}(w)\phantom{\rule{0.222222em}{0ex}}\}$ in descending order do  9:
$B\leftarrow \left\{\phantom{\rule{0.222222em}{0ex}}w\phantom{\rule{0.166667em}{0ex}}\right\phantom{\rule{0.166667em}{0ex}}w\in \mathit{revzero}(v),\mathit{index}(w)=j\phantom{\rule{0.222222em}{0ex}}\}$;  10:
$r\leftarrow l+\mathsf{sum}\{\phantom{\rule{0.222222em}{0ex}}\mathit{stsize}(\mathit{w})\phantom{\rule{0.166667em}{0ex}}\phantom{\rule{0.166667em}{0ex}}w\in B\phantom{\rule{0.222222em}{0ex}}\}$;  11:
append $\langle B,[l,r]\rangle $ to ${L}_{j}$; ▹ That is, the $\mathit{prank}$ of descendants of nodes in B are in $[l,r]$.  12:
$l\leftarrow r+1$;  13:
end for  14:
end for  15:
end for  16:
end for  17:
return;

To compute $\mathit{prank}$ efficiently, we construct the temporary BP for the zeroedge tree. Using the BP, we can compute the size of each subtree rooted by v in T in constant time and compact space. Since we can compute the size of the subtrees in T, we can know the ranges of real preorder ranks that are assigned to the subtrees by bottomup processing. The whole tree, the subtree rooted by the 0terminal node, is assigned the range of preorder rank $[0,m]$. Let w be a node rooting a subtree that is assigned a range of real preorder ranks $[l,l+\mathit{stsize}(w)1]$ and assume that the $\mathit{revzero}(\mathit{w})$ is sorted. Then, the real preorder rank of w is l and the subtree rooted by ${v}_{j}\in \mathit{revzero}(w)$ is assigned the range $[{l}_{j},{l}_{j+1}1]$, where ${l}_{j}={l}_{j1}+\mathit{stsize}({v}_{j})$ and ${l}_{1}=l+1$.
The DenseZDD for the given ZDD G is composed of these three data structures. We traverse T in depthfirst search (DFS) order based on assigned real preorder ranks and construct the BP representation U, the dummy node vector M, and the onechild array I. The BP and dummy node vector are constructed as if dummy nodes exist. Finally, we obtain the DenseZDD $DZ(G)=\langle U,M,I\rangle $. The pseudocode is given in Algorithms 2 and 3.
Algorithm 2 Convert_ZDD_BitVectors ($v,paren,dummy,onechild$): Algorithm for obtaining the BP representation of the zeroedge tree, the dummy node vector, and the onechild array. 
Input: ZDD node v, list of parentheses $paren$, list of bits $dummy$, list of integers $onechild$  1:
$i=\mathit{index}(v)$;  2:
for each$w\in \mathit{revzero}(v)$ in ascending order of $\mathit{prank}(w)$ do  3:
while $i+1\ne \mathit{index}(w)$ do  4:
if $i+1\le \mathit{index}(w)$ then  5:
append ‘(’ to $paren$, and ‘0’ to $dummy$;  6:
$i\leftarrow i+1$;  7:
else  8:
append ‘)’ to $paren$, and ‘0’ to $dummy$;  9:
$i\leftarrow i1$;  10:
end if  11:
end while  12:
append ‘(’ to $paren$, and ‘1’ to $dummy$;  13:
append $\mathit{prank}(\mathit{one}(w))\xb7({1}^{\mathit{empflag}(w)})$ to $onechild$;  14:
Convert_ZDD_BitVectors $(w,paren,dummy,onechild)$;  15:
append ‘)’ to $paren$, and ‘0’ to $dummy$;  16:
end for  17:
while$i>\mathit{index}(v)$do  18:
append ‘)’ to $paren$, and ‘0’ to $dummy$;  19:
$i\leftarrow i1$;  20:
end while  21:
return;

Algorithm 3 Construct_DenseZDD (W: a set of root nodes of ZDD): Algorithm for constructing the DenseZDD from a source ZDD. 
Output: DenseZDD $DZ$  1:
for each$v\in W$ compute $\mathit{revzero}$ fields for all descendants of v;  2:
compute $\mathit{stsize}$ field for all ${0}^{\mathrm{r}}$decsendants;  3:
Compute_Preorder;  4:
create empty lists $paren,dummy,onechild$;  5:
append ‘(’ to $paren$, and ‘0’ to $dummy$;  6:
Convert_ZDD_BitVectors($\mathbf{0}$, paren, dummy, onechild);  7:
append ‘)’ to $paren$, and ‘0’ to $dummy$;  8:
make BP U from $paren$;  9:
make FID M from $dummy$;  10:
make compressed representation I of $onechild$;  11:
return$DZ\leftarrow \langle U,M,I\rangle $;
