Recursive Algorithm for Geeratig Partitios of a Iteger Sug-Hyuk Cha Computer Sciece Departmet, Pace Uiversity 1 Pace Plaza, New York, NY 10038 USA scha@pace.edu Abstract. This article first reviews the problem of partitioig a positive iteger i geeral. Ad the recursive algorithms for, geeratig exactly k partitios of a positive umber are preseted. The most efficiet recursive algorithm s computatioal ruig time is Θ(k p() which is the lower boud of problem. 1 Prelimiary A positive iteger ca be represeted by sums of positive iteger terms. For example of = 5, there are 7 differet ways as listed i Table 1. Table 1. 5) ad 5, example 5 = 1 + 1 + 1 + 1 + 1 5,5) = {<1,1,1,1,1>} = 2 + 1 + 1 + 1 5,4) = {<2,1,1,1>} = 3 + 1 + 1 = 2 + 2 + 1 5,3) = {<2,2,1>, <3,1,1>} = 4 + 1 = 3 + 2 5,2) = {<3,2>, <4,1>} = 5 5,1) = {<5>} This classic problem of geeratig partitios of a positive iteger is formally defied i eq(1) ) { l l( l l( l( iteger{1,..., } l( l( j) if i j} Let l( deotes the i th elemet i the list l. ) is a set of ordered list l whose elemets l( s are positive itegers ad their sum is exactly. Ay permutatio of l is cosidered to be the same, e.g., (2 + 2 + 1) = (1 + 2 + 2) = (2 + 1 + 2). Hece, oly the sorted lists are icluded i the output set. A simple biary tree to geerate all partitios of all itegers is depicted i [1] ad Figure 1 (a). The algorithm is give below where +(l x, l y ) is cocateatig two lists ad l(1~ l -1) is a sub list of l startig from the fist elemet to l -1 th elemet, e.g., +(<3,3>,) = <3,3,1> = l x ad l x (1~2) = <3,3>. Algorithm 0: *) where 1. the root is 2. geerate childre by eqs (2) ad (3) for all odes, recursively. L ( l) ( l, 1 ) (2) ( l(1 ~ l 1), l( l ) 1 ) if l( l ) l( l 1 ) R ( l) (3) abort otherwise (1)
<1,1,1> <1,1> <2> <2,1> <3> 1) 2) 3) <1,1,1,1> <2,1,1> <2,2> <3,1> <4> <1,1,1,1,1> <2,1,1,1> <2,2,1> <3,1,1> <3,2> <4,1> <5> <1,1,1,1,1,1> <2,1,1,1,1> <2,2,1,1> <2,2,2> <3,1,1,1> <3,2,1> <3,3> <4,1,1> <4,2> <5,1> <6> (a) 4) 5) 6) <1,1,1,1,1,1> <2,1,1,1,1> <2,2,1,1> <3,1,1,1> <2,2,2> <3,2,1> <4,1,1> <3,3> <4,2> <5,1> 6,6) 6,5) 6,4) 6,3) 6,2) 7,4) 1 1 1 1 1 1 1 <4,1,1,1> 1 1 1 1 1 1 1 <3,2,1,1> 1 1 1 1 1 1 1 <2,2,2,1> <6> 6,1) (b) (c) Figure 1. (a) a biary tree for *), (b) a biary tree for 6), ad (c) 7,4) with bars. ) for ay ca be geerated together with all s for i < usig the Algorithm 0, yet aother more efficiet algorithm to geerate oly ) is depicted as a simple biary tree i [1] ad a example is show i Figure 1 (b). Let deote a set of ordered list l whose legth is exactly k, i.e., the positive iteger is expressed exactly k partitios as formally defied i eq(4) ad some examples are give i Table 2. k { l l(1),, l( l( l( iteger{1,..., } l( l( j) if i j} (4) i1 k1 ) (5) Notice that k th level i the biary tree i Figure 1 (b) cotais ad thus ca be computed usig the respective algorithm, yet oe must geerate 1)~k1) i order to geerate. Aother way to thik of the problem is usig k 1 bars to separate items ito k partitios. Table 2. Examples of 15,2) 9,3) 9,4) 9,5) 8,3) 8,4) 8,5) 7,3) 7,4) <8,7> <9,6> <10,5> <11,4> <12,3> <13,2> <14,1> <3,3,3> <4,3,2> <4,4,1> <5,2,2> <5,3,1> <6,2,1> <7,1,1> <3,2,2,2> <3,3,2,1> <4,2,2,1> <4,3,1,1> <5,2,1,1> <6,1,1,1> <2,2,2,2,1> <3,2,2,1,1> <3,3,1,1,1> <4,2,1,1,1> <5,1,1,1,1> <3,3,2> <4,2,2> <4,3,1> <5,2,1> <6,1,1> <2,2,2,2> <3,2,2,1> <3,3,1,1> <4,2,1,1> <5,1,1,1> <2,2,2,1,1> <3,2,1,1,1> <4,1,1,1,1> <3,2,2> <3,3,1> <4,2,1> <5,1,1> <2,2,2,1> <3,2,1,1> <4,1,1,1>
Geeratig is ot oly a importat umber theory problem, but also plays a key role i umerous applicatio problems. Albeit a efficiet was developed i 1779 ad described i [1], here recursive algorithms are preseted ot oly to provide a recursive defiitio of the problem, but also to reaso its efficiecy. 2 Desigig Recursive Algorithms Before embarkig o the algorithm, it is ecessary to defie some cocateate fuctios. The fuctio +(x,l), takes a iteger x ad L, a set of ordered lists of itegers as iputs as defied i eq(6). A ew set of lists are produced by isertig x i the head of each list i L as exemplified i eq(7). Isertig a elemet i the begiig of a list takes a costat computatioal time [2]. ( x, L) { x, l(1),, l( l ) l L} (6) 4,6,1,1 6,1,1 4,5,2,1 5,2,1 4,4,3,1 (4, 4,3,1 ) 4,4,2,2 4,2,2 4,3,3,2 3,3,2 Some of the lists i the output set i eq(6) may ot be ordered lists aymore. Hece, let s defie a ew fuctio (x,l) which excludes such cases as defied i eq(8). ( x, L) { x, l(1),, l( l ) l L l(1) x} (8) (7) 6,1,1 4,4,3,1 5,2,1 4,4,2,2 (4, 4,3,1 ) 4,3,3,2 4,2,2 3,3,2 (9) 3,1,1 (1, ) (10) 2,2,1 The output of may be a empty set as i eq(10). Note that (4,) returs <4>. Let (x,l) be idetical to (x,l) except that (4,) returs istead of <4>. ( x, L) { x, l(1),, l( l ) l L l(1) x} if L otherwise Usig the (x,l) fuctio = {(1, 1,k1), (2, 2,k1),, ( k1))} ad thus, a recursive ad algorithmic defiitio of ca be writte as i eq(12). Algorithm I: eq(12) (11) i1 ( i, i, k 1)) if k if k 1& k if k 1& k (12)
Oe should ot geerate the Fiboacci sequece umber by its recursive defiitio as it would take Θ(φ ). Similarly, the eq(12) is a soud recursive defiitio of but ufortuately Algorithm I makes too may uecessary recursive procedure calls as show i Figure 2 (a). Oe quick observatio from Figure 2 (a), k+1 upper boud ca be used to reduce the uecessary recursive calls. Theorem 1. i,k1) = if i > k+1. Proof. If i = k+1+ ε where ε 1, ( k+1+ ε), = k 1 ε,. Sice (k 1 ε) < (, i,k1) =. Istead of checkig all itegers from 1 to as i Algorithm I, oly from 1 to k+1 ca be checked. Usig the Theorem 1, Algorithm II is give i eq (13) Algorithm II: usig k+1 upper boud i eq(13) k1 i1 ( i, i, k 1)) if k 1 if k 1 (13) Notice that is used istead of sice the upper boud guaratees that ever returs a empty set because is always greater tha or equal to k. The recursive call diagram of Algorithm II for 6,4) is show i Figure 2 (b). Oce agai the eq(13) may be a better defiitio for tha the eq(4) which is a o-algorithmic defiitio yet Algorithm II still makes too may uecessary recursive calls. Aother upper boud ca be oticed from Figure 2(b); 3,2) i (1,3,2)) makes two recursive calls: (1,2,1)) ad (2,1,1)). Notice that (1, (2,1,1))) will retur always a empty set because the output list <,1,2, > is ot ordered. Theorem 2. (s, (j, j,) = if j > s Proof. Let j = s+ ε where ε 1. +(s, + (s+ ε, c+ ε, ) will retur < c, c+ ε, > which is ot a ordered list. Hece, (c, (c+ ε, c+ ε, ) = The value s i the paret level ca be aother upper boud for a give recursive procedure to avoid uecessary childre procedures. The value c must be passed to its childre recursive procedures as a extra parameter ad the recursive procedure must choose the miimum of two upper boud values. Hece, the followig Algorithm III has two parts: oe is the iitial call part i eq(14) ad the other is recursive procedure with a extra parameter part i eq(15). Algorithm III: usig the miimum of two upper bouds: eq(14) followed by eq(15) if k P ( (14) Q( k, k 1) otherwise if k 1 s Q( k, s) ( i, Q( i, k 1, mi( i, i k 2))) if k 1 (15) i1 Figure 2 (c) shows the recursive call diagram of Algorithm III for 6,4). It is much more efficiet tha Algorithm I ad II yet ca be improved further.
6,4) 6,4) 6,4) = Q(6,4,3) Q(6,4,3) (1, 5,3)) (1,4,2)) (1,3,1)) 3,1) = 3 <1,1,1,3> (2,2,1)) 2,1) = 2 <1,1,2,2 > (3,1,1)) 1,1) = 1 <1,1,3,1 > (4,0,1)) (2,3,2)) (1,2,1)) 2,1) = 2 <1,2,1,2> (2,1,1)) 1,1) = 1 <1,2,2,1> (3,0,1)) (3,2,2)) (1,1,1)) 1,1) = 1 <1,3,1,1> (4,1,2)) (2,0,1)) (5,0,2)) (2, 4,3)) (1,3,2)) (1,2,1)) (2,1,1)) 2,1) = 2 1,1) = 1 <2,1,1,2> <2,1,2,1> (3,0,1)) (2,2,2)) (1,1,1)) 1,1) = 1 <2,2,1,1> (3,1,2)) (2,0,1)) (4,0,2)) (3, 3,3)) (1,2,2)) (1,1,1)) 1,1) = 1 <3,1,1,1> (2,1,2)) (3,0,2)) (4, 2,3)) (5, 1,3)) (6, 0,3)) (a) Algorithm I (1,5,3)) (2,4,3)) (3,3,3)) (1,Q(5,3,1)) (2, Q(4,3,2)) (3, Q(3,3,1)) 6,4) = R(6,4,3) R(6,4,3) +(2,R(4,3,2)) +(3,R(3,3,1)) (1,4,2)) (2,3,2)) (3,2,2)) (1,3,1)) 3,1) = 3 (1,1,1,3) (2,2,1)) 2,1) = 2 (1,1,2,2) (3,1,1)) 1,1) = 1 (1,1,3,1) (1,2,1)) 2,1) = 2 (1,2,1,2) (2,1,1)) 1,1) = 1 (1,2,2,1) (1,1,1)) 1,1) = 1 (1,3,1,1) (1,3,2)) (1,2,1)) 2,1) = 2 (2,1,1)) 1,1) = 1 (2,2,2)) (1,1,1)) 1,1) = 1 (1,2,2)) (1,1,1)) 1,1) = 1 (b) Algorithm II (1, Q(4,2,1)) (1, Q(3,1,1)) Q(3,1,1) = 3 (1, Q(3,2,1)) (1, Q(2,1,1)) Q(2,1,1) = 2 (2, Q(2,2,1)) (1, Q(1,1,1)) Q(1,1,1) = 1 (1, Q(2,2,1)) (1, Q(1,1,1)) Q(1,1,1) = 1 (c) Algorithm III +(2,R(2,2,1)) +(1,R(1,1,1)) R(1,1,1) = 1 +(1,R(2,2,1)) +(1,R(1,1,1)) R(1,1,1) = 1 (d) Algorithm IV Figure 2. recursive procedure call diagrams for 6,4). (2,1,1,2) (2,1,2,1) (2,2,1,1) (3,1,1,1) (1,1,1,3) (2,1,1,2) (2,2,1,1) (3,1,1,1) (2,2,1,1) (3,1,1,1)
So far, oly the upper boud values have bee utilized i Algorithm II ad III, a lower boud, ceil(/ ca be observed from Figure 2. I order to cover with k partitios, the first partitio s size must be at least ceil(/. Theorem 3. ( i, i, k 1)) if i k Proof. Suppose the first partitio s size is i < ceil(/. The the remaiig partitios size must be less tha or equal to i. If all partitios sizes are equal to i, the total size is the maximum ad it is k i. But k i < because ceil(/. i i i r k The followig Algorithm IV utilizes both upper boud ad lower boud. Algorithm IV: Algorithm III + lower boud: eq(16) followed by eq(17) if k P ( (16) R( k, k 1) otherwise if k 1 ( s R k, s) ( i, R( i, k 1, mi( i, i k 2))) if 1 k (17) i k Figure 2(d) ad Figure 3 show the recursive procedure call diagram of Algorithm IV. Note that +(x,l) fuctio is used istead because the lower boud guaratees that all l(1) s are less tha or equal to x. R(12,4,9) 3 R(9,3,3) <3,3,3> 3 R(6,2,3) <3,3> 3 R(3,1,3) <3> <3,3,3,3> <4,3,3,2> <4,4,2,2> <4,4,3,1> <5,3,2,2> <5,3,3,1> <5,4,2,1> <5,5,1,1> <6,2,2,2> <6,3,2,1> <6,4,1,1> <7,2,2,1> <7,3,1,1> <8,2,1,1> <9,1,1,1> 4 R(8,3,4) <3,3,2> 3 R(5,2,3) <4,2,2> <4,3,1> 4 R(4,2,2) 5 R(7,3,5) 6 R(6,3,4) <3,2,2> <3,3,1> <4,2,1> <5,1,1> <2,2,2> <3,2,1> <4,1,1> 4 R(3,2,2) 5 R(2,2,3) 3 R(2,1,2) 2 R(2,1,2) 3 R(1,1,1) 3 R(4,2,3) <2,2> 2 R(2,1,2) <3,1> 3 R(2,1,2) 3 R(3,2,2) 4 R(2,2,1) <3,2> <2,2> <3,1> <2,1> <1,1> 2 R(1,1,1) 1 R(1,1,1) 2 R(4,2,2) <2,2> 2 R(2,1,2) <2,1> <1,1> 2 R(1,1,1) 1 R(1,1,1) <2> <2> <2> <2> 7 R(5,3,3) <2,2,1> <3,1,1> 2 R(3,2,2) <2,1> 2 R(1,1,1) 3 R(2,2,1) <1,1> 1 R(1,1,1) 8 R(4,3,2) <2,1,1> 2 R(2,2,1) <1,1> 1 R(1,1,1) 9 R(3,3,1) <1,1,1> 1 R(2,2,1) <1,1> 1 R(1,1,1) Figure 3. The recursive procedure call diagram of Algorithm IV for 12,4).
p( 3 Computatioal Complexity p() is widely kow as the iteger partitio fuctio which is a way of writig as a sum of positive itegers [3,4]. I other words, p() is the cardiality of ); p() = ). Let p( =. Fidig p() or p( such as p(27,8) = 352, p(31,5) = 427, ad some examples i Table 3 is a importat problem ad has bee widely studied i depth. It ca be computed much faster without computig [3,4]. We will ot cosider the algorithm for geeratig p() here but use it to state the computatioal complexity of Algorithm IV. p( ) p( (18) k1 Table 3. Cardiality p( \k 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 p() 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 1 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 5 5 1 2 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 7 6 1 3 3 2 1 1 0 0 0 0 0 0 0 0 0 0 0 11 7 1 3 4 3 2 1 1 0 0 0 0 0 0 0 0 0 0 15 8 1 4 5 5 3 2 1 1 0 0 0 0 0 0 0 0 0 22 9 1 4 7 6 5 3 2 1 1 0 0 0 0 0 0 0 0 30 10 1 5 8 9 7 5 3 2 1 1 0 0 0 0 0 0 0 42 11 1 5 10 11 10 7 5 3 2 1 1 0 0 0 0 0 0 56 12 1 6 12 15 13 11 7 5 3 2 1 1 0 0 0 0 0 77 13 1 6 14 18 18 14 11 7 5 3 2 1 1 0 0 0 0 101 14 1 7 16 23 23 20 15 11 7 5 3 2 1 1 0 0 0 135 15 1 7 19 27 30 26 21 15 11 7 5 3 2 1 1 0 0 176 16 1 8 21 34 37 35 28 22 15 11 7 5 3 2 1 1 0 231 17 1 8 24 39 47 44 38 29 22 15 11 7 5 3 2 1 1 297 The output set size of is k p( ad hece the computatioal ruig time complexity of ay algorithm is Ω(k p() which is the lower boud. Note that Algorithm IV makes exactly p( umber of cocateate fuctios for each level. There are k levels ad thus Algorithm IV takes Θ(k p(). All Algorithms I, II, ad III takes Ω(k p(). It should be oted that p( grows very fast as depicted i Figure 4. Geeratig ) takes expoetial, O( p()) by the eq(19), a.k.a. the Hardy ad Ramauja asymptotic formula [3,4]. 2 3 1 p( ) e (19) 4 3 10000 8000 k = 2 k = 3 k = 4 k = 5 6000 4000 2000 0 0 10 20 30 40 50 60 70 80 90 100 Figure 4. p(2), p(3), p(4), p(5) plots
4 Discussio Recursive thikig ofte sheds some light o how to defie ad solve a difficult problem. This paper preseted four recursive algorithms for. Naïve recursive algorithms are itroduced to devise a excellet recursive algorithm. It is ofte a good strategy ad tactic to desig a algorithm for a difficult problem. is about distributig ulabeled tasks to exactly k ulabeled processors. No idle processor is allowed. If processors are labeled, the order matters, e.g., <2,2,1> <2,1,2> <1,2,2>. Let O( be a way of distributig ulabeled tasks to exactly k labeled processors. To geerate O(, Algorithm II ca be used by replacig with + fuctio. Algorithm V: k1 O( ( i, O( i, k 1)) i1 if k 1 if k 1 Refereces 1. Doald E. Kuth, The Art of Computer Programmig, Volume 4, Fascicle 3: Geeratig All Combiatios ad Partitios, 2005 2. Thomas H. Corme Charles E. Leiserso Roald L. Rivest ad Clifford Stei Itroductio to Algorithms, 2 d ed., MIT Press, 2001 3. George E. Adrews ad Kimmo Eriksso Iteger Partitios, Cambridge Uiversity Press 2004 4. George E. Adrews, The Theory of Partitios, Cambridge Uiversity Press 1998