Content uploaded by Mohammadreza Razzazi

Author content

All content in this area was uploaded by Mohammadreza Razzazi on Sep 01, 2015

Content may be subject to copyright.

A Linear Time Approximation Algorithm for Ruler

Folding Problem

Ali Nourollah

(Software Systems R&D Laboratory

Department of Computer Engineering & IT

Amirkabir University of Technology, Tehran, Iran

nourollah@aut.ac.ir)

Mohammadreza Razzazi

(Software Systems R&D Laboratory

Department of Computer Engineering & IT

Amirkabir University of Technology, Tehran, Iran

Institute for Studies in Theoretical Physics and Mathematics (I.P.M.)

razzazi@aut.ac.ir)

Abstract: Achainorn-link is a sequence of nlinks whose lengths are ﬁxed and are

joined together from their endpoints, free to turn about their endpoints, which act as

joints. “Ruler Folding Problem”, which is NP-Complete is to ﬁnd the minimum length

of the folded chain. The best linear approximation algorithm for it were proposed by

Hopcroft et al. Their algorithm folds any open chain in the interval whose length is less

than 2m1,wherem1is the length of the longest link in the chain. We propose a linear

time approximation algorithm using O(1) additional space. Our algorithm has lower

upper bound for the length of the folded chain which is max{2m1−m1−m2

2k−2,k−1

i=0

m1

2i},

where m1and m2are the lengths of the two distinct maximum length links in the chain

respectively, and kis the number of links whose lengths are m1in the chain. Hence it

is the best known approximation algorithm for “Ruler Folding Problem”.

Key Words: Ruler Folding Problem, Carpenter’s Ruler, Approximation Algorithms

Category: F.2, G.2.1

1 Introduction

We consider a sequence of closed straight line segments [A0,A

1], [A1,A

2], ...,

[An−1,A

n]ofﬁxedlengthsl1,l

2,...,l

n, respectively, imagining that these line

segments are mechanical objects such as rods, and their endpoints are joints

about which these rods are free to turn. The aim is to ﬁnd the minimum length

of folded chain in which each joint is to be completely straight, or completely

folded. This problem has been known as “Ruler Folding Problem”

“Ruler Folding Problem” was stated by Hopcroft et al. for the ﬁrst time and

has been shown to be NP-Complete by a reduction from PARTITION problem

[Hopcroft et al. 1985]. They developed an O(nm2

1) pseudo polynomial algorithm

for optimal folding of an n-link open chain in one dimensional space where m1

Journal of Universal Computer Science, vol. 14, no. 4 (2008), 566-574

submitted: 5/3/07, accepted: 25/2/08, appeared: 28/2/08 © J.UCS

is the length of the longest link [Hopcroft et al. 1985, Whitesides2001]. Hopcroft

et al. proposed a linear time approximation algorithm for the “Ruler Folding

Problem” with the upper bound of 2m1for the length of a folded chain, where

m1is the length of the longest link of the chain. They showed that this upper

bound is tight using an example [Hopcroft et al. 1985]. Recently, Calinescu and

Dumitrescu improved the previous result and provided a fully polynomial-time -

approximation scheme for ruler folding problem[Calinescu and Dumitrescu 2005].

Total running time of their algorithm was O(n4(1/)3log m1) and it required

O(n4(1/)3log m1) additional space. They used tuples to show the intervals and

by decreasing the length of the intervals improved the approximation ratio.

Nourollah and Razzazi introduced the ruler folding problem in d-dimensional

space. They proposed a dynamic programming approach to fold a given chain

whose links have integer lengths in a minimum length in O(nL) time and space.

Furthermore, they showed that by generalizing the algorithm it can be used in

d-dimensional space for orthogonal ruler folding problem such that it requires

O(2dndLd)timeusingO(2dndLd) space [Nourollah and Razzazi 2007]. Other

works on linkages are given in[Biedl et al.2002, Biedl et al.2005, Kantabutra1997,

Lenhart and Whitesides1995, O’Rourke1998, Whitesides 1992].

In this paper we present a linear time approximation algorithm for the “Ruler

Folding Problem” which improves the bound already obtained by Hopcroft et al.

Preliminaries are stated in section 2, our algorithms and the proof of correctness

is presented in section 3, and the conclusion is stated in section 4.

2 Preliminaries

Alinkage is a planar straight line graph G=(V,E) and a mapping l:E−→ R+

of edges to positive real lengths. Each vertex of a linkage is called a joint or an

articulation point, each straight line edge eof a linkage, which has a speciﬁed

ﬁxed length l(e) is called a bar or a link. A linkage whose underlying graph

is a single path is called polygon al arc,open chain or a ruler, a linkage whose

underlying graph is a single cycle is called polygonal cycle,closed chain or a

polygon and a linkage whose underlying graph is a single tree is called polygon al

tree or tree linkage.Ann-link polygonal arc is a sequence of nlinks of arbitrary

ﬁnite lengths moving in Euclidean plane. Theses links are joined together end-

to-end by freely rotating joints. The joints are denoted by A0,A

1,...,A

n.The

link between Ai−1and Ai,1≤i≤n, is called li. The length of a link lis shown

by |l|.

Assume L=(l1,...,l

n)isann-link open chain with at least two distinct

links. Let m1and m2be the lengths of the ﬁrst and the second longest links of

the chain. m2 may not exist if all links have the same length. Furthermore, let

kbe the number of links whose lengths are equal to m1. The formal deﬁnitions

567

Nourollah A., Razzazi M.: A Linear Time Approximation Algorithm ...

of m1,m2and kare as follows:

m1=max

1≤i≤n{|li|},(1)

m2=max

1≤i≤n{|li|;|li|<m

1},(2)

and

k=

1≤i≤n

|li|=m1

1(3)

3 The Approximation Algorithm

Hopcroft et al. [Hopcroft et al. 1985] developed a linear time approximation al-

gorithm for ruler folding problem which we call H Algorithm. This algorithm

takes an n-link open chain as input and folds it within the interval [0,2m1],

where m1is the length of the longest link in the chain. A short description of H

Algorithm is as follows. Using xaxis, place joint A0on the origin and then for

each link li,1≤i≤n, if folding lito the left direction results in placing Aion

a negative axis then fold lito the right, otherwise fold lito the left.

Theorem 1. Any n-link open chain can be folded in less than 2m1length in

O(n)time, where m1is the length of the longest link[Hopcroft et al. 1985].

We use a modiﬁed version of H Algorithm which is given as follows. Suppose we

want to fold a sub-chain (lr,...,l

s)intheinterval[a, a +2m1]onthexaxis.

For each link li,jointsAi−1and Aiare called left-joint and right-joint of the

link, respectively. Assume direction of folding during the algorithm is given. If

direction is left to right, Ar−1,As,lr,andlsare called ﬁrst-joint,last-joint,ﬁrst-

link,andlast-link, respectively and H Algorithm folds lrtoward ls. If direction

is right to left, As,Ar−1,ls,andlrare called ﬁrst-joint,last-joint,ﬁrst-link,

and last-link, respectively and H Algorithm folds lstoward lr. Position of ﬁrst-

joint of the sub-chain is given by FirstJoint ∈[a, a +2m1]andH Algorithm

takes it as input value. Figures (1) and (2) show an open chain and its sub-

chain in both cases. Position of last-joint of the given sub-chain is denoted by

LastJoint parameter and it is computed by H Algorithm. Five parameters a

(start point of the interval [a, a +2m1]), FirstLink (index of the ﬁrst link in

the given sub-chain), LastLink (index of the last link in the given sub-chain)

,FirstJoint (position of ﬁrst-joint of the given sub-chain), and Direction are

the input values of H Algorithm.

Output parameters of H Algorithm are an array F=(fr,...,f

s)andLastJoint,

where fi=+1or−1, r≤i≤s.Notethatr=Min{F irstLink, LastLink }and

s=Max{F ir stLink, LastLink}.Foreachi(r≤i≤s), if fi=+1,jointAi

has to be placed on the right side of joint Ai−1,andiffi=−1, joint Aiwould

568 Nourollah A., Razzazi M.: A Linear Time Approximation Algorithm ...

ﬁrst-joint last-joint

ﬁrst-link last-link

A1

A0Ar−1

Ar

Ar+1

As−1

As

An−1

An

Figure 1: Open chain folding such that direction =→(+1)(left to right)

A1

A0Ar−1

Ar

Ar+1

As−1

As

An−1

An

last-joint ﬁrst-joint

last-link ﬁrst-link

Figure 2: Open chain folding such that direction =←(−1)(right to left)

be placed on the left side of joint Ai−1. When the algorithm ends, parameter

LastJoint shows the position of last-joint of the given sub-chain. In this algo-

rithm, initially each link is placed toward the left boundary of the interval. The

algorithm proceeds by changing the direction of positioning the link on the x

axis whenever the end point of a link gets out of the interval. Independent of

the folding introduced in the algorithm, the given sub chain starting from lrand

ending by ls, the result of folding for both cases are the same.

Figure (3) shows the pseudo code of H Algorithm. Function DecideAndFold

decides to fold link liassuming the previous link related to the direction of

folding has been folded. If FirstJointIndex denotes the index of the ﬁrst-joint,

AFirstJointIndex =ﬁrst-joint, position of joint AFirstJointIndex+Direction∗iis ﬁxed

when the step ith,(1 ≤i≤|LastLink −FirstLink|+ 1), is taken. Variable

CurrentPos shows the position of joint AFirstJointIndex+Direction∗iafter ith

step on the xaxis. Based on the theorem (1) and using H algorithm,LastJoint

is always within the interval [a, a +2m1].

Parameters a,iand LastJoint are the input parameters of function Decide-

AndFold,CurrentPos and fi(ith element of array F) are its outputs. Note that

by this algorithm the folded sub-chain (lr,...,l

s) are completely laid within the

interval [a, a +2m1]. This is an important fact which is used in the development

of our algorithm.

569

Nourollah A., Razzazi M.: A Linear Time Approximation Algorithm ...

Algorithm H(a, F irstLink, LastLink, F irstJ oint, Direction, F, LastJ oint)

// Folds the given sub-chain (lMin{F irstLink,LastLink},...,

//lMax{F irstLink,LastLink}) within the interval [a, a +2m1] assuming

//that ﬁrst-joint is placed at point FirstJoint.

Input:ais the start point of the interval [a, a +2m1]intowhichthe

sub-chain must be folded.

FirstLink is index of the ﬁrst-link in the given sub-chain.

LastLink is index of the last-link in the given sub-chain.

FirstJoint is the position of ﬁrst-joint of the given sub-chain

(FirstJoint ∈[a, a +2m1]).

Output:ArrayF=(fMin{FirstLink,LastLink},...,f

Max{F irstLink,LastLink})

of size |LastLink −FirstLink|+1 such that fi=+1,iflihas been

folded to the right and fi=−1, if lihas been folded to the left.

LastJoint shows the position of last-joint of the given sub-chain

after the sub-chain is folded.

Begin

CurrentPos ←FirstJoint

//If FirstJointIndex denotes the index of the ﬁrst-joint,

//CurrentPos shows the position of joint AFirstJointIndex+Direction∗i

//after ith step(1 ≤i≤|LastLink −FirstLink|+1).

For i←FirstLink To LastLink Step Direction Do

CurrentPos ←DecideAndF ol d(a, i, CurrentP os, Direction)

End For

LastJoint ←CurrentPos

End of Algorithm

Function DecideAndF ol d(a, i, C urrentP os, Direction)

Begin

//place Aion the left side of Ai−1(if Direction =+1)

// or Ai−1ontheleftsideofAi(if Direction =−1)with

//distance lifrom it.

fi←−Direction

If CurrentPos −|li|<a Then

//place Aion the right side of Ai−1(if Direction =+1)

// or Ai−1on the right side of Ai(if Direction =−1)with

//distance lifrom it.

fi←Direction

End If

Return CurrentPos +Direction ∗fi∗|li|

End of Function

Figure 3: Modiﬁed H Algorithm

570 Nourollah A., Razzazi M.: A Linear Time Approximation Algorithm ...

To fold an n-link open chain we must place joint A0on point zero and call

H(0,1,n,0,+1,F,LastJoint). It is clear to see that the time complexity of H

algorithm is O(n)usingO(1) space. H algorithm will be used by our algorithm to

achieve an improved approximation algorithm for ruler folding problem. Based

on the H algorithm, we can develop an improved approximation algorithm whose

time complexity is O(n)usingO(1) space but its upper bound for the length of

the folded chain is less than that of the H algorithm.

Using Theorem (2), we propose a new algorithm to fold any n-link open chain

in an interval which is smaller than 2m1.

Theorem 2. Let L=(l1,l

2,...,l

n)be an n-link open chain, m1and m2be

lengths of the ﬁrst two maximum length links in L,andkbe the number of links

in Lwhose lengths are m1. There is an algorithm that can fold Lin such a way

that its folded length is less than or equal to

max{2m1−m1−m2

2k−2,

k−1

i=0

m1

2i}

Proof. The proof is by induction on k.Ifk= 1 then there is one link whose

length is m1. It is positioned in the interval [0,m

1] and the left part and right

part of it can be folded in the interval [0,2m2](using theorem 1), thus it is easy

to see that the total length of the folded chain is max{2m2,m

1}. Assume the

theorem holds for all open chains which have klinks whose lengths are m1.Let

Lbe an open chain which has k+1 links whose lengths are m1and moving from

l1toward lnlet ljbe (k+ 1)th link whose length is m1.Lcan be seen as three

distinct parts, L1=(l1,l

2,...,l

j−1), L2=(lj), and L3=(lj+1,...,l

n). L1has

klinks whose lengths are m1, and therefore, by the inductive hypothesis, it can

be folded in such a way that its folded length is less than or equal to

max{2m1−m1−m2

2k−2,

k−1

i=0

m1

2i}

L2has one link ljwhose length is m1, and therefore, we fold ljaround Aj−1

to the best direction such that the total length of folded chain (l1,l

2,...,l

j)is

minimum value. In the worst case, Aj−1is positioned in the middle of the folded

chain L1. Hence, folding ljresults in half length of the folded chain L1plus to

m1. Therefore we get

m1+1

2max{2m1−m1−m2

2k−2,

k−1

i=0

m1

2i}

=max{2m1−m1−m2

2k−2,

k−1

i=0

m1

2i}=max{2m1−m1−m2

2k−1,

k

i=0

m1

2i}

571

Nourollah A., Razzazi M.: A Linear Time Approximation Algorithm ...

Because lengths of lj+1 ,...,l

nare less than or equal to m2,wecanfoldL3at

the end of the others such that the current length does not exceed from

max{2m1−m1−m2

2k−1,

k

i=0

m1

2i}

Theorem (2) yields a recursive algorithm to fold an n-link open chain which is

shown in ﬁgure (4).

Analysis. It is easy to see that Rec Folding Algorithm processes one link at

atimethusitrequiresO(n) time and since it is called ktimes recursively, it

requires O(k) additional space for its stack but by rewriting the algorithm into

a nonrecursive algorithm its additional space reduces to O(1). Note that at the

ﬁrst glance, it seems, ﬁnding the last link with size m1takes O(n)timeineach

recursive call but totally the amortized time of these searches takes O(n)and

does not aﬀect the time complexity of the algorithm.

Clearly, the limit of

max{2m1−m1−m2

2k−2,

k−1

i=0

m1

2i}

is 2m1as kapproaches +∞and therefore our algorithm achieve a better upper

bound for folding a chain than H Algorithm.Inpracticekis normally a small

integer.

4Conclusion

The best previously known polynomial time approximation algorithm for the

ruler folding problem was developed by Hopcroft and et al.[Hopcroft et al. 1985].

They achieved upper bound of 2m1for the length of the folded chain, where m1

is the length of the longest link of the chain. In this paper, we developed a linear

time approximation algorithm for ruler folding problem which its result is lower

than the previous results. In our approach, the bound is given in terms of the

number of the links having the length of the largest link. Our algorithm requires

O(n)timeusingO(1) additional space.

Ruler folding problem has many applications including robot motions and

protein folding in biology science. The introduced algorithms are useful in robot

motion planning problems in which robot arms are modeled by linkages.

Acknowledgements

This research was in part supported by a grant from Institute for Studies in

Theoretical Physics and Mathematics(I.P.M.)

572 Nourollah A., Razzazi M.: A Linear Time Approximation Algorithm ...

Algorithm RecF olding(L, r, s, m1,k,j,F,a,b,LastJoint);

Input:anopenchainL=(lr,...,l

s)whosejointsareAr−1,...,A

s.

m1is the length of the longest link in L.

kis the number of links whose lengths are equal to m1.

jis the largest index of a link in Lwith length equal to m1

(j=max{i:|li|=m1}).

Output: Array F=(fr,...,f

s)ofsizes−r+1where if fi=1,

lihas been folded to the right direction and if fi=−1,

lihas been folded to the left direction, for each i.

[a, b] is the interval which Lis folded into it.

LastJoint is the position of Asafter folding.

Begin

If k=1Then

fj←+1 // Place ljin the interval [0,m

1]

//Fold (lr,...,l

j−1) in the interval [0,2m2] starting from

//point zero according to H algorithm.

H(0,j−1,r,0,−1,F,LastJoint)

// Fold (lj+1 ,...,l

s) in the interval [0,2m2] starting from

// point m1according to H algorithm.

H(0,j+1,s,m

1,+1,F,LastJoint)

[a, b]←[0,max{2m2,m

1}]

Else

//Call algorithm RecFolding recursively

//to fold open chain (lr,...,l

j−1).

tempj ←the index of (k−1)th link whose length is m1

RecF olding(L, r, j −1,m

1,k−1,tempj,F,a,b,d)

If b−min{a, d −|lj|} <max{b, d +|lj|} − aThen

Fj←−1 //Place Ajto the left of Aj−1

[a, b]←[min{a, d −|lj|},b]

Else

Fj←+1 //Place Ajto the right of Aj−1

[a, b]←[a, max{b, d +|lj|}]

End If

//Fold (lj+1,...,l

s)intheinterval[a, b] according to H algorithm.

H(a, j +1,s,d,+1,F,LastJoint)

End If

End of Algorithm.

Figure 4: RecFolding Algorithm

573

Nourollah A., Razzazi M.: A Linear Time Approximation Algorithm ...

References

[Biedl et al.2002] Biedl, T., Demaine, E., Demaine, M., Lazard, S., Lubiw, A.,

O’Rourke, J., Robbins, S., Streinu, I., Toussaint, G., Whitesides, S.: “A Note on

Reconﬁguring Tree Linkages: Trees can Lock”, Discrete Applied Mathematics, 117

(2002), 293-297.

[Biedl et al.2005] Biedl, T., Lubiw, A., Sun, J.: “When Can a Net Fold to a Polyhe-

dron?”, Computational Geometry: Theory and Applications, Volume 31 , Issue 3

(June 2005), 207 - 218.

[Calinescu and Dumitrescu 2005] Calinescu, G., Dumitrescu, A.: “The carpenter’s

ruler folding problem”, in Combinatorial and Computational Geometry, Jacob

Goodman, J´anos Pach and Emo Welzl (editors), Mathematical Sciences Research

Institute Publications, Cambridge University Press,(2005), 155-166.

[Hopcroft et al. 1985] Hopcroft, J., Joseph, D., Whitesides, S.: “On the movement of

robot arms in 2-dimensional bounded regions”, SIAM Journal on Computing, Vol.

14, No. 2,(May 1985), 315-333.

[Kantabutra1997] Kantabutra, V.: “Reaching a point with an unanchored robot arm

in a square”, International journal of Computational Geometry & Applications.,Vol.

7, No. 6, (1997), 539-549.

[Lenhart and Whitesides1995] Lenhart, W., Whitesides, S.: “Reconﬁguring closed

polygonal chains in Euclidean d-space”, Discrete and Computational Geometry,

Vol. 13, (1995), 123-140.

[Nourollah and Razzazi 2007] Nourollah, A., Razzazi, M., “A New Dynamic Program-

ming Algorithm for Orthogonal Ruler Folding Problem in d-Dimensional Space”,

ICCSA (1), Lecture Notes in Computer Science, Vol. 4705 , Springer, (2007), ISBN

978-3-540-74468-9, 15-25.

[O’Rourke1998] O’Rourke, J.: “Folding and unfolding in computational geometry”,

Proc. Japan Conf. Discrete Computational Geometry, (Dec 1998), LNCS vol. 1763,

(1999), 258-266.

[Whitesides 1992] Whitesides, S.: “Algorithmic issues in the geometry of planar linkage

movement”, Australian Computer Journal, Special Issue on Algorithms, vol. 24, No.

2, (May 1992), 42-50.

[Whitesides2001] Whitesides, S.: “Chain Reconﬁguration. The INs and Outs, Ups and

Downs of Moving Polygons and Polygonal Linkages”, Peter Eades, Tadao Takaoka

(Eds.): Algorithms and Computation, 12th International Symposium, ISAAC 2001,

Christchurch, New Zealand, (Dec 2001), Proceedings. Lecture Notes in Computer

Science vol. 2223, Springer 2001, ISBN 3-540-42985-9, 1-13.

574 Nourollah A., Razzazi M.: A Linear Time Approximation Algorithm ...