Sie sind auf Seite 1von 7

Problem Set 8 Solutions

Problem 8-1. No left turns Youve 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. n addition, to avoid car accidents and otherwise attracting attention, you arent going to make any !-turns either. You want to reach your chop-shop as #uickly as possible, and since you can drive down straight streets very #uickly, your primary goal is to minimi$e the number of %right& turns you make. 'ut if two paths have the same number of right turns, then you want to minimi$e the straight-line distance.
"

(ortunately, you have a map of the city, so you can plan out a route that uses no left turns and no !-turns. )ven better, the map comes in electronic form as an m n grid of cells, each marked as either empty %navigable& or blocked %unnavigable&. *t each cell you visit, you can continue in the direction you were going, or turn right. +wo examples of no-leftturn maps are shown in (igure ".

Figure 1, +wo no-left-turn maps. (ind a path from s to t with as few right turns as possible, and no left-or !-turns. You can play with these maps using a -ava applet on http://www. clickmazes.com/noleft/ixnoleft.htm. (a) .ive an efficient algorithm to find a no-left-turn path through an m n map usingthe minimum number of right turns, or report that no such path exists. *mong all pathswiththeminimumnumberofrightturns,youralgorithmshouldreturnthepathminimi$ingt hestraight-linedistance%i.e.,thenumberofstraightsteps&.%Hint:(orintuition,solvethelefthandmapin(igure".&Solution:+hebasicideainsolvingthisproblemistocreateagraphrepresen tationoftheproblemandthenuseashortestpathsalgorithmtofindtheoptimalno-leftturnpath.)achedge(u,v)inthegraphhastwoweights,w1(u,v)andw2(u,v)./esetw1(u,v)

=1iftheedgerepresentsarightturnandw2(u,v)=1iftheedgerepresentsastraightlinepathofdi stance".

0newaytoturnthisproblemintoagraphGisshownin(igure1.0urmapofcellsisconvertedintoa weighted,directedgraph.(oreachofthemncellsofthemap,wecreate2nodes. fthecarentersapa rticularcellfromthecellbelowitonthemapitwouldenterthe3south4nodeassociatedwiththatpar ticularcell.*ftercreatingthenodes,weassigndirectededgestothem.)dgesthatcorrespondtoari ghthandturnhaveanedgeweightw1(u,v)=1andw2(u,v)= andedgesthatcorrespondtodriv ingstraighthaveaweightw1(u,v)= andw2(u,v)=1.)dgesthatwouldrepresentlefthandtur nsoruturnsarenotincludedinthegraph.*nedgewhichgoesfromoneorientationtoanother%exa mplesouthtowest&representsarightturn. twilltakeus!(mn)timetocreatethisgraphrepresent ation.

Now that we have our graph representation we need to determine a path that minimi$es right-hand turns, and then minimi$es the straight-line distance. nstead of having a single real-number weight on every edge, however, the weight of an edge w(u, v) is now a pair of numbers, (w1(u, v), w2(u,

v)). /e compare two weights by looking at the first weight, and then breaking ties using the second weight. n other words, two weights w =(w1, w2) and w=(w , w ) satisfy w"w exactly when either 1, or w1 = w and w2 " w
1 2 # # #1

w1 " w .*lternatively, we can get by in this problem with a single edge weight on every edge,a right turn is given some weight w $ %&% %i.e., 'mn ( 1&, and straight-line edges aregiven weight ". +hen, the shortest path in the graph will always contain the minimum number of right turns, because making a right turn is always more expensive thanmaking the maximum possible number of straight-line movements. )ven though these path and edge weights w are pairs instead of real numbers, we can use 6ijkstras algorithm because the edge weights are still 3positive.4 n other words, given a path p from s to u with weight w(p), the weight of a path pformed by adding an edge (u, v) to p is never smaller, i.e., w(p ) w(p) ( w(u, v). 7ince 6ijkstras algorithm runs in () l* ) ( &) time, this will give us a running time of !(mn l* mn).
# 12 5

f we dont care about breaking ties by straight-line distance, and we just want a path with the minimum number of right turns, then it is possible to optimi$e the priority #ueue for 6ijkstras algorithm to support priority #ueue operations in !(1) time. +his reduces the runtime of the algorithm to !(mn). 0ne simple approach for doing this is to notice that the path lengths are always integers, and the maximum path weight is 'mn %plus nodes at +) +hus, it is possible to maintain the priority #ueue as !(mn) linked lists, one for each possible path weight. n fact, we actually need only two linked lists if we use a breadth-first search-like algorithm. +he only edge weights are for straight-line travel, and 1 for right turns. New nodes that we discover through a weight-8 edge are inserted into one list, while nodes that are discovered through a weight-" edge are inserted into the other. /e leave the precise statement of this algorithm and the proof of correctness as an exercise for the reader. /e conjecture that there exists an !(mn) algorithm to solve this problem when we do break ties by straight-line distance. f you have an !(mn) solution with a complete correctness proof, please let us know9 (b) *fter mapping the route produced by your algorithm from part %a&, you reali$e that you may lead the police right to the chop-shop if they are following you. You decide that the best strategy to avoid pursuit is to make two right turns in #uick succession,

i.e., in adjacent s#uares, the :8-degree rotations intervened by only one unit of straight travel. 7o now you want to find a route that makes at least two consecutive right turns, while still making no left turns, making as few right turns as possible, and among all such paths, minimi$ing the straight-line distance. .ive an efficient algorithm to find such a path through the grid, or report that no such path exists. GSolution: ;reate a new graph G that consists of two copies G = () , & ) and 1 =()1, &1)of the graph G from the previous problem. nsert an edge from u , ) to v1 , )1 if you can get from u to v using two consecutive right turns in G. +his edge has weight w1(u , v1)=2and w2(u , v1)= . +hen, find a shortest path in this new graph G from s to t1.
# #

7ince the source only exists in G and the sink only exists in G1, you are guaranteed to make two consecutive right turns at least once. G has twice as many nodes as the original graph G %i.e., -mn&. /e double the number of edges, and then add at most -mn edges representing two consecutive right turns %for every node in the original graph, we can get to at most one other node making two consecutive right turns&. +herefore, asymptotically, searching G using 6ijkstras algorithm still takes !(mn l*mn)time.
# #

Problem 8-2. Video Game Design <rofessor ;loud has been consulting in the design of the most anticipated game of the year, Takehome Fantasy XII. 0ne of the levels in the game is a ma$e that players must navigate through multiple rooms from an entrance to an exit. )ach room can be empty, contain a monster, or contain a life potion. *s the player wanders through the ma$e, points are added or subtracted from her life points .. 6rinking a life potion increases ., but battling a monster decreases .. f . drops to or below, the player dies. *s shown in (igure 5, the ma$e can be represented as a digraph G = (), &), where vertices correspond to rooms and edges correspond to %one-way& corridors running from room to room. * vertex-weight function represents the room contents,

f f(v)= , the room is empty. f f(v)$ , the room contains a life potion. )very time the player enters the room, her life points . increase by f(v). f f(v) " , the room contains a monster. )very time the player enters the room, her life points . drop by %f(v)%, killing her if . becomes nonpositive.

+he entrance to the ma$e is a designated room s , ) , and the exit is another room t , ) . *ssume that a path exists from s to every vertex v , ) , and that a path exists from every vertex v , ) to t. +he player starts at the entrance s with . = . $ life points. (or simplicity, assume that the entrance is empty, f(s)= .

<rofessor ;loud has designed a program to put monsters and life potions randomly into the ma$e, but some ma$es may be impossible to safely navigate from entrance to exit unless the player enters with a sufficient number . $ of life points. * path from s to t is safe if the player stays alive along the way, i.e., her life points never become nonpositive. 6efine a ma$e to be /-admissible if a safe path through the ma$e exists when the player begins with . = / life points. =elp the professor by designing an efficient algorithm to determine the minimum value / for which a given ma$e is /-admissible, or determine that no such / exists. " (a) (ind a safe path in the ma$e in (igure 5. 1 (b) (ormulate the problem as an e#uivalent problem where the weights are on the edges,and prove e#uivalence. Solution: f the entrance node has a weight, we create a new entrance with weight and add an edge from the new entrance to the original entrance. Now move the weights from the nodes to the edges in the following manner. f node v has weight f (v), then for all (u, v) , &, we set w(u, v)= f (v). +he e#uivalent problem is to find a path through this edge weighted graph,

so that the weight of every subpath is positive. (c) *ssume for this problem part that there are no cycles whose traversal gives a net increase in life points. .iven /, how would you check whether the ma$e is /admissible> Solution: /e use a modified version of 'ellman-(ord algorithm. .iven an /, for every node u we find the maximum %positive& number of points 01u2 the player can have when she reaches u. f 01t2 is positive, then the graph is /-admissible. (or each vertex u , ), we maintain p1u2 which is a lower bound on 01u2. /e initiali$e all the p1u2s to 3+, except the entrance, which is initiali$ed to /. *s we run the 'ellman-(ord *lgorithm and relax edges, the value of p1u2 increases until it converges to 01u2 %if there are no positive weight cycles&. +he important point to note is that reaching a node with negative points is as good as not reaching it at all. +hus, we modify p1u2 only if it becomes positive, otherwise p1u2 remains 3 . /e change the relaxation routine to incorporate this as follows.

*fter all the edges have been relaxed ) times, if there are no positive weight cycles, all p1u2s will have converged to the corresponding 01u2s %the maximmum number of points you can have on reaching vertex u&. f 01t2 is positive at this point, then the player can reach there with positive life points and thus the graph is /-admissible. (d) Now assume that there can be cycles in the graph whose traversal gives a net increasein life points. .iven /, how would you check whether the ma$e is /admissible> Solution: f p1t2 is not positive after relaxing all the edges ) 31 times, we relax all the edges one more time %just like 'ellman-(ord&. f p1u2 of any node changes, we have found a positive weight cycle which is reachable from s starting with / points. +hus the player can go around the cycle enough times to collect all the necessary points to reach t and thus the graph is /-admissible. f we dont find a reachable positive weight cycle and p1t2 is 3+, then the graph is not / admissible. +he correctness of the algorithm follows from the correctness of 'ellman-(ord, and the running time is !()&).

(e) =ow can you find the minimum / for which the ma$e is /-admissible> Solution: .iven the above sub-routine, we now find the minimum /. /e first check if the graph is 1-admissible. f it is, we return 1 as the answer. f it is not, then we check if it is 2admissible and then '-admissible and so on. +hus on the ith step we check if the graph is 2 -admissible. )ventually, we find k such that the graph is not 2 4a5missi6le,
i31 k31

but it is 2 -admissible. +hus the minimum value of / lies between these two values. +hen we binary search between / = 2 and /= 2 to find the right value of /. *nalysis, +he number of iterations is k( !(l* /)= !(l* /), since
k k31 k

+hus you have to run 'ellman-(ord !(l* /) times, and the total running time is !()&l* /).

Das könnte Ihnen auch gefallen