ps8sol - Introduction to Algorithms Massachusetts Institute...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Algorithms November 22, 2005 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik D. Demaine and Charles E. Leiserson Handout 27 Problem Set 8 Solutions Problem 8-1. No left turns You’ve just stolen a brand-new Nexus Nano, but the owner has locked the car with The Spade , a mechanical lock on the steering wheel, which prevents you from making left turns. In addition, to avoid car accidents and otherwise attracting attention, you aren’t going to make any U-turns either. You want to reach your chop-shop 1 as quickly as possible, and since you can drive down straight streets very quickly, your primary goal is to minimize the number of (right) turns you make. But if two paths have the same number of right turns, then you want to minimize the straight-line distance. Fortunately, you have a map of the city, so you can plan out a route that uses no left turns and no U-turns. Even better, the map comes in electronic form as an m × n grid of cells, each marked as either empty (navigable) or blocked (unnavigable). At each cell you visit, you can continue in the direction you were going, or turn right. Two examples of no-left-turn maps are shown in Figure 1. s t s t Figure 1 : Two no-left-turn maps. Find a path from s to t with as few right turns as possible, and no left-or U-turns. You can play with these maps using a Java applet on http://www. clickmazes.com/noleft/ixnoleft.htm . (a) Give an efficient algorithm to find a no-left-turn path through an m × n map using the minimum number of right turns, or report that no such path exists. Among all 1 A garage that illegally disassembles cars in order to sell the parts.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Handout 27: Problem Set 8 Solutions pathswiththeminimumnumberofrightturns,youralgorithmshouldreturnthepath minimizingthestraight-linedistance(i.e.,thenumberofstraightsteps).( Hint: For intuition,solvetheleft-handmapinFigure1.) Solution: Thebasicideainsolvingthisproblemistocreateagraphrepresentation oftheproblemandthenuseashortestpathsalgorithmtofindtheoptimalno-left-turn path.Eachedge ( u, v ) inthegraphhastwoweights, w 1 ( u, v ) and w 2 ( u, v ) .Weset w 1 ( u, v ) = 1 iftheedgerepresentsarightturnand w 2 ( u, v ) = 1 iftheedgerepresents astraightlinepathofdistance1. .. 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 1 1 Figure2 :GraphRepresentation. Onewaytoturnthisproblemintoagraph G isshowninFigure2.Ourmapofcells isconvertedintoaweighted,directedgraph.Foreachofthe mn cellsofthemap,we create4nodes.Ifthecarentersaparticularcellfromthecellbelowitonthemap itwouldenterthe“south”nodeassociatedwiththatparticularcell. Aftercreating thenodes,weassigndirectededgestothem.Edgesthatcorrespondtoarighthand turnhaveanedgeweight w 1 ( u, v ) = 1 and w 2 ( u, v ) = 0 andedgesthatcorrespond todrivingstraighthaveaweight w 1 ( u, v ) = 0 and w 2 ( u, v ) = 1 .Edgesthatwould representlefthandturnsoruturnsarenotincludedinthegraph.Anedgewhichgoes fromoneorientationtoanother(examplesouthtowest)representsarightturn.Itwill takeus O ( mn ) timetocreatethisgraphrepresentation.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

ps8sol - Introduction to Algorithms Massachusetts Institute...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online