A Parallel Dynamic Programming Algorithm for Unranking t–ary Trees

A Parallel Dynamic Programming Algorithm
for Unranking t–ary Trees
Zbigniew Kokosiński
Cracow University of Technology, Faculty of Electrical & Computer Eng.,
ul. Warszawska 24, 31-155 Kraków, Poland; [email protected]
Abstract. In this paper an O(n) parallel algorithm is presented for fast
unranking t–ary trees with n internal nodes in Zaks’ representation. A
sequential O(nt) algorithm is derived on the basis of dynamic programming paradigm. In the parallel version of the algorithm processing is
performed in a dedicated parallel architecture containing certain systolic
and associative features. At first a coefficient table is created by systolic computations. Then, n subsequent elements of a tree codeword is
computed in O(1) time through associative search operations.
1
Introduction
Many different representations and sequential generation algorithms were invented and used for binary and t–ary trees, f.i. bitstrings, x–sequences, y–
sequences, w–sequences, z–sequences etc. [3,4,7,12,14,15,17,19,20]. Two parallel
generation algorithms in a linear array model and in an associative model were
proposed providing parallelization of computations on the object level [2,11].
Sequential ranking and unranking algorithms were developed for binary trees
[7] and t–ary trees [4,15,17,20]. Ranking and unranking of combinatorial configurations is applied in adaptive and random generation algorithms, genetic
algorithms etc. [1,18].
In the present paper we propose a new parallel algorithm for unranking t–
ary trees developed on the basis of a dynamic programming technique. Dynamic
programming was successfully employed in many application areas including unranking combinations [9], partitions [8] and some other combinatorial objects [5,
6]. Although unranking problems are inherently sequential, a portion of computations can be parallelized. However, many sequential unranking algorithms are
not suitable for parallelization. Till now, parallel dynamic programming algorithms were proposed for unranking combinations [10].
2
Representations of t–ary Trees
Let < Ai >i∈I denote an indexed family of sets Ai = A, where: A ={1, ...,
m}, I ={1, ... , n}, 1 ≤ m,n. Any mapping f which ”chooses” one element
from each set A1 , ..., An is called a choice function of the family < Ai >i∈I [13].
R. Wyrzykowski et al. (Eds.): PPAM 2003, LNCS 3019, pp. 255–260, 2004.
c Springer-Verlag Berlin Heidelberg 2004
256
Z. Kokosiński
With additional restrictions we can model by choice functions various classes of
combinatorial objects [5,8].
If a suplementary condition: ai < aj , for i < j, and i, j ∈ I, is satisfied
then any choice function κ =< ai >i∈I , that belongs to the indexed family
< Ai >i∈I , is called increasing choice function of this family (k–sequence). If
n≤m, then all increasing choice functions κ =< ai >i∈I are representations of
all n–subsets (combinations) of m–element set A. In conventional representation
of combinations with repetitions we deal in fact with indexed sets Ki = {i, ... ,
m-n+1} ⊂ Ai .
Below we define choice functions ζ and χ corresponding to the notions of
z–sequences and x–sequences [12,19].
If suplementary conditions: 1. m=(n-1)t+1; 2. ai < aj , for i < j and i, j
∈ I; and 3. ai ∈ {1, ... , (i-1)t+1}, for i ∈ I; are satisfied then any choice
function ζ =< ai >i∈I , that belongs to the indexed family < Ai >i∈I , is called
increasing choice function with restricted growth of this family (z–sequence [19]).
In the above mappings we deal in fact with indexed sets Ti = {1, ... , (i-1)t+1}
⊂ Ai .
If Ai = {0,1} and I ={1, ... , tn} then any choice function χ =< ai >i∈I ,
that belongs to the indexed family < Ai >i∈I , is called binary choice function
of this family (x–sequence [19]). All binary choice functions, with the number
of a1 + ... + ai ≥ i/t, for 1 ≤ i ≤ tn, are bitstring representations of all t–
ary trees of the set A. A simple transformation converts choice functions ζ into
corresponding choice functions χ.
The number of all t–ary trees with n internal nodes is denoted by B(n, t) =
(tn)!/n!(tn − n + 1)!. The number of binary trees B(n, 2) is known as Catalan
number C(n).
For given n and t, the number of all choice functions ζ is a fraction Cnnt /(ntn+1)Cnnt−t+1 of the set of all choice functions κ and a fraction Ctnt /nCtnt−n+1
of the set of all choice functions κ with a1 = 1.
Let us introduce now the concept of Ruskey numbers [15]. The number
of different (n,t)–trees (i.e. trees with n internal nodes) less then or equal to
ζ =< z1 , . . . , zn−i+1 , zn−i+2 , . . . , zn >∈< Ti >i∈I with fixed < z1 , . . . , zn−i+1 >
(in increasing lexicographic order) is called Ruskey number:
1. Rnt (1, j) = j,
for 1 ≤ j ≤ n(t − 1) − 1 ;
2. Rnt (i, (i − 1)(t − 1) + 1) = B(n, t),
for 1 ≤ i ≤ n ;
3. Rnt (i, j) = Rnt (i, j −1)+Rnt (i−1, j),
for 1 ≤ i ≤ n, and (i − 1)(t − 1) + 1 ≤ j ≤ n(t − 1) − 1 .
The above recursive formula describes construction of Ruskey tables for different values n,t. The table RT containing a part of Ruskey table, for n=5 and
t=3, is shown in Table 1. Ruskey numbers R53 (i, j) are stored in corresponding
elements RT[i, j] of the table RT, while all remaining cells are filled with zeros.
A Parallel Dynamic Programming Algorithm for Unranking t–ary Trees
257
Table 1. Construction of the table RT for (n,3)–trees, n ≤ 5, B(5,3)=273.
3
i/j
1
2
3
4
5
6
7
8
9
1
2
3
4
5
1
0
0
0
0
2
0
0
0
0
3
3
0
0
0
4
7
0
0
0
5
12
12
0
0
6
18
30
0
0
7
25
55
55
0
8
33
88
143
0
9
42
130
273
273
Sequential Dynamic Programming Algorithm
In this section we assume t–ary trees to be represented by increasing choice functions with restricted growth (z–sequences). In the algorithm UNRANKTREE
presented in Fig.1 a table RT is used, which includes a part of Ruskey table.
Input : n — number of internal nodes, t — degree of tree nodes, Index — rank
of the choice function ζ representing t–ary tree (1 ≤ Index ≤ B(n,t)), table RT
with elements RT[i, j] containing Ruskey numbers Rnt (i, j).
Output: table T with choice function ζ.
Method: computations proceed with tree ranks in decreasing lexicographic order. In step 4 the maximum elements RT[i,m] satisfying inequality are selected
in each row. The next value T[n—i+1] is computed in step 4.1.2. After O(nt)
iterations we obtain the required t–ary tree ζ.
1.
2.
3.
4.
Index’:=B(n,t)—Index + 1;
for I:=1 to n do T[i]:=n;
i:=n; j:=(n(t—1)—1; m:=j;
while (Index’ > 0) do
4.1. if RT[i,m] ≤ Index’
then
4.1.1. Index’:=Index’—RT[i,m];
4.1.2. T[n—i+1]:=T[n—i+1]+n(t—1)—1—m;
4.1.3. m:=m+1;
4.1.4. i:=i—1;
else
4.1.5. m:=m—1;
4.1.6. if RT[i,m]=0 then i:=i—1;
5. return T.
Fig. 1. Algorithm UNRANKTREE.
258
Z. Kokosiński
Each coefficient Rnt (i, j) is mapped to the cell RT[i, j]. In the dynamic programming approach we assume that the table RT with Ruskey numbers is precomputed.
Theorem 1. Algorithm UNRANKTREE is correct ind its asymptotic computational complexity is O(nt).
Proof. The set of all B(n,t) trees can be shown in the form of a rooted ordered
tree of height n (see Fig.2). There are n(t-1)-1 nodes with depth n. Each node
with depth i, 0≤i≤n-1, has it − k + 1 descendants, where k is an integer label
for edge connecting given node with its ancestor (for root that has no ancestor
we assume k = 0), and edges connecting given node with its descendants are
labeled by k+1, k+2, . . ., it + 1, respectively. In this way all nodes with depth
i as well as all paths are ordered in the tree. Traversing the tree in preorder
and listing all paths from the root to subsequent leaves — by sequences of
edge labels — is equivalent to generation (enumeration) of all B(n,t) trees in
increasing lexicographic order. Let us assign to all such paths their ranks in
decreasing lexicographic order. Unranking the object with rank Index in the tree
is equivalent to finding in the tree the path with rank Index’=B(n,t)—Index—1,
0≤Index, Index’≤B(n,t)—1. Every node of the tree with depth i has an integer
label equal to the sum of all leaves of ordered subtrees rooted i in this node and
all its siblings with depth i following it. Each node label is a Ruskey coefficient.
We determine the path with rank Index’ by determining a proper subtrees on
the consequtive levels starting from the root. Rooted subtrees on ith level are
viewed in the decreasing order of their size (size means in this case the number
depth
(level)
0
1
2
3
path rank
Index’
---4---
---7--- (1)
---6--- (2)
(3) ---5--- (3)
0
1
2
---3---
---7-----6-----5--(7) ---4---
(1)
(2)
(4)
(4)
3
4
5
6
---7-----6-----5-----4--ROOT ---1--- (12) ---2--- (12) ---3---
(1)
(2)
(3)
(4)
(5)
7
8
9
10
11
Fig. 2. Rooted ordered tree of all B(3,3) trees, where —x— denotes edge label and
(x) denotes node label.
A Parallel Dynamic Programming Algorithm for Unranking t–ary Trees
259
of subtree leaves). In order to do this the current Index’ of the choice function is
compared with node labels Rnt (i, j) and taken from the cell RT[i, j]. In each level i
no more then (n—i+1)(t—1)—1 comparisons are made and before the next step
rank Index’ is modified (step 4.1.1 of the algorithm). Step 4 with complexity
O(1) is repeated O(nt) times. Condition RT[i,j]≤Index’ is satisfied n times in
each step 4, and next item of the required object is obtained. Hence, the total
complexity of the algorithm is O(nt).
4
Parallel Dynamic Programming Algorithm
In the algorithm UNRANKTREE two computational processes can be parallelized: 1) creation of the coefficient table RT, and 2) searching in the coefficient
table RT.
Let us notice that elements in ith row of RT form a sequence which is increasing with column index j. This property is essential for speeding up the search
in RT rows. For given pair {n,t} generation of RT requires O(nt) steps. Generation of the table RT from recursive formulas presented in section 2 may be
parallelized through systolic computations.
A simple parallel unranking algorithm for t–ary trees implementing associative search operations (no greater then and maximum value) may be sketched as
shown in Fig 3.
Input : identical as in algorithm UNRANKTREE.
Output: identical as in algorithm UNRANKTREE.
Method: computations proceed with tree ranks in decreasing lexicographic order. In order to determine table T an associative search is used. In each step 2.1
all elements RT[i,m] satisfying given inequality are selected. Then the element
with maximum m coordinate is selected and the index value is updated. Next
value T[n—i+1] is obtained in step 2.4. After n iterations we obtain the required
function ζ in the table T.
1.
2.
3.
4.
Index’:=B(n,t)—Index + 1;
for i:=1 to n do T[i]:=n;
j:=n(t—1)—1;
for i=n downto 1 do;
4.1. search in parallel for all m ≤ j : RT[i,m] ≤ Index’;
4.2. select maximum m ;
4.3. Index’:=Index’—RT[i,m];
4.4. T[n—i+1]:=T[n—i+1]+n(t—1)—1—m;
5. return T.
Fig. 3. Algorithm UNRANKTREE–PAR.
260
Z. Kokosiński
Theorem 2. Algorithm UNRANKTREE–PAR is correct ind its asymptotic
computational complexity is O(n).
Proof. The unranking algorithm is a variant of algorithm UNRANKTREE. Correctness of the method results from the Proof of Theorem 1.
References
1. Akl, S.G.: Parallel computation: models and methods. Prentice Hall (1997) 475–
509
2. Akl, S.G., Stojmenović, I.: Generating t–ary trees in parallel. Nordic J. of Computing 3 (1996) 63–71
3. Er, M.C.: Lexicographic listing and ranking t–ary trees. The Computer Journal 30
(1987) 559–572
4. Er, M.C.: Efficient generation of k–ary trees in natural order. The Computer Journal 35 (1992) 306–308
5. Kapralski, A.: New methods for the generation of permutations, combinations and
other combinatorial objects in parallel. Journal of Parallel and Distributed Computing 17 (1993) 315–326
6. Kapralski, A.: Modelling arbitrary sets of combinatorial objects and their sequential and parallel generation. Studia Informatica 21 (2000)
7. Knott, G.D.: A numbering system for binary trees. Comm. ACM 20 (1977) 113–
115
8. Kokosiński Z.: Circuits generating combinatorial objects for sequential and parallel
computer systems. Monografia 160, Politechnika Krakowska, Kraków (1993) [in
Polish]
9. Kokosiński Z.: Algorithms for unranking combinations and their applications. Proc.
Int. Conf. PDCS’95, Washington D.C., USA (1995) 216–224
10. Kokosiński Z.: Unranking combinations in parallel. Proc. Int. Conf. PDPTA’96,
Sunnyvale, CA, USA, Vol.I (1996) 79–82
11. Kokosiński Z.: On parallel generation of t–ary trees in an associative model. PPAM
2001, Lecture Notes in Computer Science 2328 (2002) 228–235
12. Makinen, E.: A survey of binary tree codings. The Computer Journal 34 (1991)
438–443
13. Mirsky, L.: Transversal theory. Academic Press (1971)
14. Roelants van Baronaigien, D., Ruskey, F.: Generating t–ary trees in a–order. Information Processing Letters 27 (1988) 205–213
15. Ruskey, F.: Generating t–ary trees lexicographically, SIAM Journal of Computing
7 (1978) 424–439
16. Stojmenović, I.: On random and adaptive parallel generation of combinatorial objects. Int. Journal of Computer Mathematics 42 (1992) 125–135
17. Trojanowski, A.E.: Ranking and listing algorithms for k–ary trees. SIAM Journal
of Computing 7 (1978) 492–509
18. Üçoluk G.: A method for chromosome handling of r–permutations of n–element
set in genetic algorithms, Proc. ICEC’97, Indianapolis, USA (1997) 55–58.
19. Zaks, S.: Lexicographic generation of ordered trees. Theoretical Computer Science
10 (1980) 63–82
20. Zaks, S.: Generating and ranking t–ary trees. Information Processing Letters 14
(1982) 44–48