6
Information Processing Letters 82 (2002) 137–142 The Traveling Salesman Problem for lines in the plane Håkan Jonsson 1 Department of Computer Science and Electrical Engineering, Luleå University of Technology, SE-971 87 Luleå, Sweden Received 1 December 2000; received in revised form 30 June 2001 Communicated by S. Albers Abstract We present an algorithm that computes the shortest route that intersects n lines in the plane in time polynomial in n; the time complexity is dominated by the time it takes to compute a shortest watchman route in a simple polygon of size n. We also present a simple O(n) time algorithm for computing a route that intersects the lines but which is guaranteed to have a length at most 2 times longer than the shortest route. 2002 Elsevier Science B.V. All rights reserved. Keywords: Computational geometry; TSP; Watchman problem; Shortest path 1. Introduction Path planning is one of the central problem areas in computational geometry and robotics. One partic- ular kind of path planning problems which has been studied involves ideal (point-sized and arbitrary turn- ing) robots required to touch or visit given sets of ob- jects. In their most general form, we call the solutions to these problems visiting paths. Maybe the most clas- sical example of a shortest visiting path is the route — i.e., the closed path — asked for in the Traveling Salesman Problem (TSP) [6]. Although this famous problem, in which the solution is the shortest visit- ing route for a given set of points in the plane, is NP- complete [10], recent studies have revealed that many variations of the problem can be solved in polynomial time [5]. Moreover, for some of the hard variants, for instance the TSP with Neighborhoods [9] which is NP- hard, there are efficient algorithms that compute prov- ably good approximations to the exact solutions. E-mail address: [email protected] (H. Jonsson). 1 Supported in part by NUTEK under contract No. P10552-1. In this paper we introduce and study the Traveling Salesman Problem for lines, a variation of the TSP in which the shortest visiting route for a set of n arbitrar- ily slopped lines in the plane is asked for. In Fig. 1, an example of a shortest visiting route for 8 lines is shown. In Section 2 we show how an exact solution can be computed in polynomial time by reducing the problem to the problem of computing a shortest watch- man route in a simple polygon [4]. The reduction is based on the construction of a special simple polygon of size O(n) defined by the lines and built in O(n log n) time. In Section 3 we then present a simple and ro- bust O(n) time algorithm for computing a route that intersects the lines but which is guaranteed to have a length at most 2 times longer than the shortest route. Fig. 2 shows the smallest perimeter axis-parallel rec- tangle that intersects the lines in Fig. 1. 2. Computing an exact solution In this first part of the paper, we study the follow- ing problem: “Given a ( finite) set of lines in the plane, 0020-0190/02/$ – see front matter 2002 Elsevier Science B.V. All rights reserved. PII:S0020-0190(01)00259-9

The Traveling Salesman Problem for lines in the plane

Embed Size (px)

Citation preview

Information Processing Letters 82 (2002) 137–142

The Traveling Salesman Problem for lines in the plane

Håkan Jonsson1

Department of Computer Science and Electrical Engineering, Luleå University of Technology, SE-971 87 Luleå, Sweden

Received 1 December 2000; received in revised form 30 June 2001Communicated by S. Albers

Abstract

We present an algorithm that computes the shortest route that intersectsn lines in the plane in time polynomial inn; thetime complexity is dominated by the time it takes to compute a shortest watchman route in a simple polygon of sizen. We alsopresent a simple O(n) time algorithm for computing a route that intersects the lines but which is guaranteed to have a length atmost

√2 times longer than the shortest route. 2002 Elsevier Science B.V. All rights reserved.

Keywords:Computational geometry; TSP; Watchman problem; Shortest path

1. Introduction

Path planning is one of the central problem areasin computational geometry and robotics. One partic-ular kind of path planning problems which has beenstudied involves ideal (point-sized and arbitrary turn-ing) robots required to touch or visit given sets of ob-jects. In their most general form, we call the solutionsto these problemsvisiting paths. Maybe the most clas-sical example of a shortest visiting path is theroute— i.e., the closed path — asked for in theTravelingSalesman Problem(TSP) [6]. Although this famousproblem, in which the solution is the shortest visit-ing route for a given set of points in the plane, is NP-complete [10], recent studies have revealed that manyvariations of the problem can be solved in polynomialtime [5]. Moreover, for some of the hard variants, forinstance theTSP with Neighborhoods[9] which is NP-hard, there are efficient algorithms that compute prov-ably good approximations to the exact solutions.

E-mail address:[email protected] (H. Jonsson).1 Supported in part by NUTEK under contract No. P10552-1.

In this paper we introduce and study theTravelingSalesman Problem for lines, a variation of the TSP inwhich the shortest visiting route for a set ofn arbitrar-ily slopped lines in the plane is asked for. In Fig. 1,an example of a shortest visiting route for 8 lines isshown. In Section 2 we show how an exact solutioncan be computed in polynomial time by reducing theproblem to the problem of computing a shortest watch-man route in a simple polygon [4]. The reduction isbased on the construction of a special simple polygonof size O(n) defined by the lines and built in O(n logn)

time. In Section 3 we then present a simple and ro-bust O(n) time algorithm for computing a route thatintersects the lines but which is guaranteed to have alength at most

√2 times longer than the shortest route.

Fig. 2 shows the smallest perimeter axis-parallel rec-tangle that intersects the lines in Fig. 1.

2. Computing an exact solution

In this first part of the paper, we study the follow-ing problem: “Given a( finite) set of lines in the plane,

0020-0190/02/$ – see front matter 2002 Elsevier Science B.V. All rights reserved.PII: S0020-0190(01)00259-9

138 H. Jonsson / Information Processing Letters 82 (2002) 137–142

Fig. 1. A shortest visiting route.

Fig. 2. The smallest perimeter axis-parallel rectangle that intersectsall 8 lines in Fig. 1.

compute the shortest route that visits them”. Through-out this paper, the given lines are referred to astargetlines. Furthermore,L denotes the set of target lineswhile n denotes the size ofL. The word “visits” issynonymous with “intersects” but its use is reservedfor intersections between paths and target lines.

To solve the problem given above we make use of anobservation that relates it to the problem of computinga shortest watchman route in a simple polygon. Inthe latter problem, a closed path in a simple polygonsuch that each point in the polygon is visible fromsome point on the path is asked for. Here, two pointsin a simple polygon arevisible to each other if theline segment between the points does not intersect theexterior of the polygon. This problem can be solvedin linear time [7] if the polygon isstar-shaped, thatis if it contains a point from which all other points inthe polygon are visible. Then this point is a shortest(degenerate) watchman route. In polygons which arenot star-shaped a shortest watchman route can becomputed in O(n5) time.2

2 Recently, a flaw in an algorithm for computing afixedshortestwatchman route was corrected [12]. Since this algorithm is used as asubroutine in the algorithm by Carlsson et al. [3], the complexity ofthe latter algorithm increased from the originally stated O(n4) by a

It is known that each non-star-shaped polygoncontains a finite set of line segments that it is necessaryand sufficient for a route to visit in order to be awatchman route [3]. The observation we use is thatthe shortest watchman route is a shortest visitingroute for these line segments in the polygon. Supposethat the shortest watchman route does not touchthe boundary of the polygon. Then it is in fact ashortest visiting route not only for the line segmentsthemselves (disregarding the polygon) but also for thelines in the plane that contain the line segments.

In the remainder of this section, we define a simplepolygonP in terms of the target lines inL such thata shortest watchman route inP is a shortest visitingroute for the target lines. We then show howP , andconsequently a shortest visiting route for the targetlines, can be computed in time polynomial inn.

2.1. Shortest visiting routes for target lines

We will use the symbolVopt to denote an arbitraryroute among the potentially infinitely many shortestvisiting routes for the setL. The fact thatVopt is theshortest possible route has several implications. As forits general shape, the parts between consecutive pointsof visit (vertices) on target lines are line segments(edges), since they are shortest paths in the plane.Moreover, since a non-convex route in the plane isstrictly longer than the boundary of its convex hull,and the convex hull of a route intersects any target linethe route intersects, we conclude thatVopt is convex.This implies thatVopt visits each target line in oneof three ways. Itcrosses over, or makes acrossingcontactwith, a target line if it not only shares at leastone point with the line but also contains points in eachof the two half-planes separated by the line (Fig. 3(a)).If Vopt does not cross over a target line, there is,sinceVopt is convex, exactly one connected subset ofthe target line whereVopt visits it. If the subset is asingle point, this point is apoint of reflectionand thecontact is referred to as areflection(Fig. 3(a)). If itis a line segment, we have atangential contactwhichcan be thought of as a degenerate reflection (Fig. 3(b)).The convexity ofVopt also implies thatVopt reflectsat most once on each target line. Such a reflection

factor O(n2). This result was very recently improved to O(n5) timeby Tan [11].

H. Jonsson / Information Processing Letters 82 (2002) 137–142 139

Fig. 3. (a) A crossing contact with� and a reflection on�′ . (b) A tan-gential contact with�.

occurs at acrossing, i.e., a point where two target linesintersect, or infragment, which is a maximal subsetof a target line that does not contain a crossing. Letthe angle of reflectionbe the angle formed at a pointof reflection by either the incoming or the outgoingedge ofVopt and the target line. Then, for reflectionsin fragments, the local optimality ofVopt is given byHeron’s reflection principle:

Lemma 2.1 (The reflection principle).The angles ofreflection are equal at a point of reflection that lies ina fragment.

A reflection where the two angles of reflection areequal isperfect. SinceVopt is the shortest possibleroute, we have:

Lemma 2.2. Let vi denote a vertex ofVopt and as-sume without loss of generality thatVopt is located sothat it makes a perfect reflection on the horizontal linethroughvi . Thenvi lies either in a horizontal targetline � (on whichVopt makes a perfect reflection) or ata crossing between two target lines with negative andpositive slope, respectively.

Proof. The lemma follows from the fact that if neither� nor the two target lines with negative and positiveslope, respectively, are among the target lines onwhich Vopt reflects atvi , Vopt can be shortened bymovingvi along a target line. ✷

To bound the region in the plane whereVopt lies interms of the target lines inL, consider the convex hullC (see Fig. 4) of all crossings.3 SinceC is convex,

3 If there are no crossings,Vopt is merely a line segmentperpendicular to the target lines.

Fig. 4. (a) The convex hullC. (b) The polygonF (shaded andcontaining the even darker shaded hullC).

Fig. 5.Vopt is not always contained inC.

each of its tangents partitions the plane into two half-planes of which theactivehalf-plane totally containsC. For setsL containing no parallel target lines, it canbe shown thatVopt lies inC. However, in the presenceof parallel target lines, the convex hullC does notalways containVopt (Fig. 5). Consider the intersectionF of the active half-planes of all tangents ofC that arenormals to target lines. ForF we have:

Lemma 2.3. Vopt is contained inF .

Proof. Assume, in order to get a contradiction, thatVopt is not totally contained in the active half-plane ofthe tangentt of C that is a normal to a target line�.Let t ′ denote the normal to� that is a tangent toVoptand does not lie in the active half-plane oft . Beinga tangent,t ′ intersectsVopt at a vertexvi that, sinceVopt is the shortest possible, lies in a target line�i . Tosimplify the presentation, we assume without loss ofgenerality that� is vertical, thatvi lies to the right of�, and thatVopt lies belowt ′ (Fig. 6).

The vertexvi belongs to no other target line than�i since it lies in the exterior ofC where there are nocrossings. Lemma 2.1 then implies that the reflectionat vi must be perfect, which in turn implies that�i isnot parallel to� sincevi would then not be a topmostvertex ofVopt. Rather,�i has a positive slope less thaninfinity since it must intersect� in C and belowt .

140 H. Jonsson / Information Processing Letters 82 (2002) 137–142

Fig. 6. Illustration used in Lemma 2.3.

Moreover, the angle of reflection atvi can not beexactlyπ/2 sinceVopt would in that case not intersect�. We conclude that the angle is less thanπ/2, fromwhich follows thatVopt has at least three vertices.Then, by Lemma 2.2, there is a target line through thevertex ofVopt that occurs immediately beforevi in thecounter-clockwise direction and intersects�i abovet ′.However, then this crossing lies in the exterior ofC incontradiction to the fact theC contains all crossings.From this the lemma follows. ✷2.2. The polygonP

P can be described as a convex polygon that is bigenough to containF and to whose boundary sawtooth-like notches have been added wherever target linesintersect. An example is shown in Fig. 7 and thetechnical details are as follows.

Let {f0, f1, . . . , f2n−1} be the set of end points ofthe intersections betweenF and the target lines, wherethe indices denote their counter-clockwise orderingalong the boundary ofF , and letδ > 0 be a constantsmaller than the length of the shortest of these inter-sections. Also, letωi denote the index of the targetline that contains the intersection for whichfi is anend point. Then, the polygonP is the region boundedby the concatenation

⋃4n−1i=0 [pi,p(i+1)] of line seg-

ments wherep0 = p4n and pi is defined as followsfor 0 � i � 4n − 1:• If i is even,pi is the point on�ω�i/2� at distanceδ/2

from f�i/2� in the exterior ofF ;• if i is odd, pi is instead the point on�ω�i/2� at

distanceδ from f�i/2� in the exterior ofF .Note that there are twice as many vertices in theboundary ofP as there are end pointsfi , and that thepoint at distanceδ from f�i/2� in the exterior ofF and

Fig. 7. The polygonsP andF (shaded).

on �ω�i/2� is not necessarily unique ifδ is larger thanthe length of the shortest line segment.

The boundary ofP zig-zags around the interior ofP . Every other vertex is reflex, i.e., the angle formedby the edges joined at the vertex is greater thanπ

in P , while the others are convex (i.e., not reflex).For any eveni, we call the sub-chain[vi, vi+1] ∪[vi+1, vi+2] in the boundary ofP asawtooth. We havethe following relationship:

Lemma 2.4. A shortest watchman route inP is ashortest visiting route forL in the plane.

Proof. Let Ci denote the chord inP that is formedas the intersection betweenP and�i . We first provethat it is necessary and sufficient for a route to visit allchordsCi to be a watchman route.

Since any chordCi partitionsP into two subpoly-gons, each of which is not totally visible from the inte-rior of the other, it is necessary for a route to intersectall chordsCi in order to be a watchman route. Next,let W denote a route that intersects the two chordsCi

andCj that extend intoP from the reflex vertices thatbounds a sawtooth. We then claim that the sawtooth isvisible fromW . Assume that this is not the case, i.e.,that there is a pointp in the sawtooth that is not visi-ble fromW . ShouldCi andCj be parallel, the chordthroughp parallel toCi would separateCi and Cj

from each other and be intersected byW , which wouldimply thatp is visible fromW in contradiction to ourassumption. If, on the other hand,Ci andCj intersectin P , W has to intersect the chord fromp through thepoint of intersection, sop would be visible also in thiscase. From this our claim follows. Moreover, if the en-tire boundary of a simple polygon is visible from aroute, the route is a watchman route [3]. Hence, it is

H. Jonsson / Information Processing Letters 82 (2002) 137–142 141

sufficient for a route to intersect all chordsCi to be awatchman route.

The necessity and sufficiency requirements implythat a watchman route inP is a visiting route forL in the plane and vice versa as long as the visitingroute is contained in the interior ofP . Hence, sinceby Lemma 2.3 all shortest visiting routes forL arecontained inF , which is a subset ofP , any shortestwatchman route inP is a shortest visiting routefor L. ✷2.3. ComputingVopt

The algorithm that computesVopt takes as input theset L and computesP as a first step. The convexhull C can be computed in O(n logn) time [1]. Thisinvolves sorting the target lines by slope as the primarykey and vertical position as the second key, whichcauses parallel target lines to end up next to each otherin the resulting ordering.

Since the target lines diverge in the exterior ofC, they intersect the boundary ofC in slope order.Given a target line, the tangent ofC that boundsone of the active half-planes of the target line can becomputed in O(logn) time by binary search along theboundary ofC. This tangent passes through a vertexof C. Moreover, sinceC is convex, the tangent ofCthat is a normal to the next target line in the sortedorder also passes through this vertex or some vertexfurther away along the boundary ofC. Hence, oncethe first tangent has been computed, the vertices wherethe remaining tangents intersect the boundary ofC

follow after each other in sorted slope order alongthe boundary. Therefore, only a single scan in onedirection around the boundary ofC is needed in orderto compute them all. It follows that the intersectionF

of their active half-planes can therefore be computedin O(n) time [2]. Note that the order in which thetarget lines intersect the boundary ofC andF is thesame since both polygons are convex,C ⊂ F , andC

contains all crossings. Hence, the verticespi definingthe polygonP can be computed in O(n) time as well.

From the description given above, it follows thatP

can be constructed in O(n logn) time and thatP hasO(n) vertices. By Lemma 2.4, a shortest watchmanroute inP is a shortest visiting route forL. Then, sincea shortest watchman route in a simple polygon can becomputed in O(n5) time [11], we have:

Theorem 1. A shortest visiting route forn lines in theplane can be computed inO(n5) time.

3. A linear time approximation

In this section we give a linear-time algorithm,based on linear programming, that computes theshort-est perimeter axis-parallel rectangleRopt that inter-sects all target lines inL.

Lemma 3.1. Ropt is a visiting route forL at most√

2times as long asVopt.

Proof. Let thebounding boxof a route be the axis-parallel rectangle with shortest perimeter that containsthe route. First, we claim that the bounding boxof the (convex) routeVopt is at most

√2 times

longer than the route itself. Because of the minimalityof bounding boxes, each of the four edges of thebounding box ofVopt touchesVopt in at least onepoint. Consider the shortest route that visits these fourpoints of intersection. This route visits the points inorder along the boundary of the bounding box, sincea shortest route does not cross over itself, and istherefore a convex quadrilateral. Then, the boundaryof the bounding box is at most

√2 times longer than

the shortest route since the sum of the lengths ofthe catheti is at most

√2 times the length of the

hypotenuse in a right-angled triangle. Hence, sinceVopt is at least as long as the shortest route, the claimfollows. Second, the minimality ofRopt implies thatthe length of the boundary ofRopt is at most as largeas the length of the boundary of the bounding box ofVopt. Then the lemma follows sinceRopt intersects alltarget lines. ✷

In the linear program, we use the length of theperimeter ofRopt as the object function. In otherwords, we seek the coordinatesx−, x+, y−, andy+ of the vertices(x−, y−), (x−, y+), (x+, y−) and(x+, y+) of Ropt, where x− < x+, and y− < y+,which minimizes 2(x+ − x−) + 2(y+ − y−). Shouldthe input contain vertical target lines, we eliminatethese by first finding the smallest slopeα1 and thesecond smallest slopeα2 among the slopes of thetarget lines. Then, since any slopeα betweenα1andα2 is different from the slope of any target line,

142 H. Jonsson / Information Processing Letters 82 (2002) 137–142

rotating the axis so thatα becomes the vertical slopeensures that no target line is vertical. LetL− andL+be the set of target lines with negative and positiveslope, respectively. Then, since each edge ofRopt isparallel to one of the axes, we have the followinglemma:

Lemma 3.2. The target lines inL− andL+ intersectthe diagonals[(x−, y−), (x+, y+)] and [(x−, y+),

(x+, y−)] of Ropt, respectively.

Lemma 3.2 gives us the linear constraints for ourprogram. Altogether, the problem of computingRoptcan now be formulated as follows: “Minimize2(x+ −x−) + 2(y+ − y−) subject to: by+ + ax− � −c and−by− − ax+ � −c for all ax + by + c = 0 ∈ L+,−by−−ax− � c andby++ax+ � c for all ax+by+c = 0 ∈ L−, x+ − x− � 0, and y+ − y− � 0”. Sincethe program is formulated using 2n+2 constraints and4 variablesx−, x+, y−, andy+, and the solution to alinear program in a fixed number of variables can befound in time linear in the number of constraints [8],by Lemma 3.1, we get:

Theorem 2. A visiting route forn lines in the plane,such that the length of the route is at most

√2 times

longer than a shortest visiting route for the lines, canbe computed inO(n) time.

4. Conclusions and discussion

It is unclear whether or not the problem of finding ashortest route that intersectsn given lines in the planecan be solved in less than O(n5) time. Although alower bound for this problem is also a lower bound forthe problem of computing a shortest watchman route,no explicit (non-trivial) lower bounds are known foreither problem.

If the lines are replaced with line segments theproblem turns into the TSP with Neighborhoods whichis NP-hard. Should the lines instead be replaced withrays we get a variant of the problem that is open andlies somewhere in between the problems concerninglines and line segments. Note that in this case thereduction to the problem of computing a shortestwatchman route used in Section 2 no longer workssince rays that have their starting points inF do not

yield useful cuts for definingP . However, a√

2-approximation can still be obtained in O(n) timeby using the linear program presented in Section 3but with a linear number of additional constraints toensure that the starting points of the rays lie inRopt.

Finally, we point out that the shortest visiting (non-closed)path for n lines can also be approximated byour algorithms. The route we get if we walk twicealong a shortest visiting path forL (back and forth) is avisiting route forL twice as long as the path. Since thisvisiting route is not shorter than theshortestvisitingroute, it follows that a shortest visiting route forL isat most twice as long as a shortest visiting path forL.Hence, by Theorem 2, we conclude that a visiting pathfor n lines in the plane, such that the length of the pathis at most 2

√2 times longer than a shortest visiting

path for the lines, can be computed in O(n) time.

References

[1] M.J. Atallah, Computing the convex hull of line intersections,J. Algorithms 7 (1986) 285–288.

[2] S. Carlsson, H. Jonsson, B.J. Nilsson, Approximating theshortest watchman route in a simple polygon, TechnicalReport, Dept. Computer Science, Lund University, 1997, LU-CS-TR:97-190, LUNFD6/(NFCS-3122)/l-27/(1997).

[3] S. Carlsson, H. Jonsson, B.J. Nilsson, Finding the short-est watchman route in a simple polygon, Discrete Comput.Geom. 22 (3) (1999) 377–402.

[4] W.-P. Chin, S. Ntafos, Shortest watchman routes in simplepolygons, Discrete Comput. Geom. 6 (1) (1991) 9–31.

[5] H. Jonsson, The Euclidean traveling salesman problem withneighborhoods and a connecting fence, PhD Thesis, LuleåUniversity of Technology, 2000.

[6] E.L. Lawler, J.K. Lenstra, A.H.G. Rinnooy Kan, D.B. Shmoys(Eds.), The Traveling Salesman Problem, Wiley, New York,1985.

[7] D.T. Lee, F.P. Preparata, An optimal algorithm for finding thekernel of a polygon, J. ACM 26 (1979) 415–421.

[8] N. Megiddo, Linear programming in linear time when thedimension is fixed, J. ACM 31 (1) (1984) 114–127.

[9] J.S.B. Mitchell, Shortest paths and networks, in: J.E. Good-man, J. O’Rourke (Eds.), Handbook of Discrete and Computa-tional Geometry, CRC Press LLC, 1997, pp. 445–466.

[10] C.H. Papadimitriou, The Euclidean traveling salesman prob-lem is NP-complete, Theoret. Comput. Sci. 4 (1977) 237–244.

[11] X. Tan, Shortest zookeeper’s routes in simple polygons,Inform. Process. Lett. 77 (1) (2001) 23–26.

[12] X.H. Tan, T. Hirata, Y. Inagaki, Corrigendum to ‘An incremen-tal algorithm for constructing shortest watchman routes’, Man-uscript (submitted to Internat. J. Comput. Geom. Appl.), TokaiUniversity, Japan, 1998.