This is the end of the preview.
Sign up
to
access the rest of the document.

Unformatted text preview: Problem '3'
The Colonel Motors Corporation of Frankfort, Ketcltuclqtr has produced a new
line of vehicles which require chicken droppings for fuel. Because of this
unusual fuel requirement, there are only certain fueling stations in the country
1where the vehicles can he reﬁlled. Thus, to get from one place to another1 an
owner mustplaua route that ensmeathat hecan get reﬁlls along the was.
The Colonel Motors Corporation has hired Professor Sanders of the IIlentucl-tj.r
Institute of Technology as a consultant to propane an on-llne routeﬁnding
service. Tb use the computerised service, an owner will enter the driving
range of his vehicle [the distance the vehicle can go on a single ﬁll-up}, the
“distance to empty“ {the distance the vehicle can go with the fuel that’s new in
the tank}. his initial location {source}, and his desired destination. The service
will either respond with a shortest route from the source to the destination
such that the owner never runs out of fuel, or it will deem that no such route
exists. Model the professor's problem in terms of a weighted, directed graph
in which streets are edges, intersections are verrtices, and some intersections
have fueling stations, and design an eﬂicient algorithm to solve it. Assume
that a driver‘s source and destination are also at intersections. [For partial
credit. dmign an algorithm to ﬁnd any feasible path from the source to the
destination.)
Answer: _ __ _
Let G 2 (V, E} he the graph representing the basic map, in which
. F = {shunw} g V are the vertiocs that have fueling stations. The
number of fueling stations is f.
. Theweight ofedge {all} is thedistance hetweenfroru oto baloug the
oorrinponding street.
- Edgm are directed. to allow for one-way streets. We want to ﬁnd RDUTEf-G', ants, cite) — the shortest path in G tom the
sourceseﬂexatothedmtinationvertexdsuds that 1' no“leg“ fromsdirectlytodorfrontstoafuelingstationislongerthan
thegivendte ' no “leg” from a fueling station [to another fueling station or to d) is longer
than the given range r. Here’s the menu structure of the algorithm.
1. Construct a. graph G" I “F. E‘} in which . V" = F U {and} (Les. the vertioes are the fueling stations. the source,
end the destination}. Note that IV'I = GU}. ' I..etﬁ[n,u}hetheahortestdistaneefromnEV'terV' inG.
E" - {[uw} auchthat E{u,tv} 5 dte ifn=a, or ﬂow} 5r ifu its}
min. ﬂl = Elms] [mptthetwecanomitedgeatoaandedgeefr'omd,ifdeaired}. Note
that|E*|=D{.f=}. 2. Find the shortest pathp' from s to d in Cr". That path gives us the overall
route we went, and its weight is the weight of the shortest path from s to d in G, because each edge in 0" corresponds to a shortest path in G.
(Note: lftheedge [and] isinE‘, wetanskipthissearchandtaltethst edge
as the shortest path. Since all mtines in G‘ are also in G, the shortest
path from a to d in G: represented by the edge (e, d] in G‘, alreadyf goes
through whatever other vertieos of G" it needs to.) 3. Construct the full path in G for which 1:" is the skeleton. That is, replace
each edge in p' by the corresponding path in G. [Those paths in G are
already known, since we hound them in order to set up the edge weights in
o'.) This path is the desired route. Since all edge weights are nonnegstive, we can use Dijkstra‘s algorithm for all
the shortest-path searches {in both G and iii").
The running time of this algorithm is: . OH} to set up V‘ ' UU-[E+V1gV}} to set up E”. Pb: each ofthe DU] 1recrtices in V‘ {except
d]. run Dijkstra's algorithm to ﬁnd shortest paths from that vertex in G.
then select the paths with short enough distances from that source to each
vertex in P”. - o{E* + V‘lg 1”]: om + fig!) = on“) is ﬁnd shortest paths from s
in G“ using Dijkstra‘s algorithm. . GU - V) to replace each of the DUI“: = OH} edges of p' with an DIN]-
leugth path from G. ”Total: outs + Vlg‘lr")} = one + IVls V} The above solution earned full credit. The following improvement would earn bonus points. We can improve this time by preoompnting things that don"t depend on an
individual driver's query. In partimilar, as long as the basic map 1'3 doesn't
change, the shortest paths among fueling stations can’t change, so we can
precompute the edges of G“ that go between mti-ces in F. This is GU . {3+
VlgVJ-l work [I shortest—path oomputstions in G}. Then to construct E"
for a driver’s query, we onlyr need to do the shortest-paths search in G-twice
{for paths ﬁom s and for paths to d — the latter can he done by reversing
all edges and ionising for paths from d}. For edges between fueling stations
we just select the preeomputed edges that have distances 5 r. The work to
set up 5" is thus redqu from G[f+{E+V1gV)} to DIE +Vlg‘i’] and the
totolﬁme is rodumd to UffV-l-Ed-Vlglr"). Problem 8 Praia-mar Babylonia Wants to construct the tallest tower possible out of build-
ing blocks. She has 11 types of blades, and an unlimited supply of blocks of
each type. Each type-i block is a rectangular solid with linear dimensions
{rhythm}. A block can be oriented so that any two of its three dimensions
determ'me the dimensions of a base and the other dimension is the height. In
building a. toner, one block may be placed on top of another block as long as
the two dimensions of the base of the upper biotic: are each strictly smaller
than the corresponding base dimensions of the lower block. (Thus, for exam-
ple, blocks oriented to have equal-sized bases cannot he stacked.) Design an
efﬁcient algorithm to determine the tallest tower that the professor can build.
{For partial credit, assume that the blocks cannot be reoriented.) Answer: First of all, note that although there’s an unlimited supply of bloclrs, at
most 3 blocks of each type — one with 2:,- as the height, one with yr as the
height, and one with 2,, as the height —— can appear in the tower, because a
surface can only be placed on a strictly larger surface, so a block can‘t be used
twice with the same base surface. So we can transform our problem to one
with 311 blocks. {in fact, only 2 blocks of each type can possibly be used in
the tower, but we don‘t know in advance which 2. This observation reoeived
a small bonus.) “Richard Anderson has shown that if there is only 1 bloclr
of each type, the problem is NP-complete.}} For each block type r', we have
3 blocks: one with height I; and base dimensions as and pi, one with height
p.- and base dimensions at; and 2;, one with height 1,: and base dimensions y;
and 2;. Let’s call the dimensions of a block height, Whit [the smaller base
dimension], and leap-ti: {the larger base dimension}. Then block at can be
placed on block b if and only if lengtﬂo] «c imtgthEbI and widthﬁr] <2 width[b]. {Notes The above analysis turns the problem into one in which blocks can‘t
he reoriented. Thus the partial-credit solution unsold assume that reformula—
tion of the problem without deriving it from the more general problem, and
start from here.) Here are two ways of solving this restated problem, one using graphs and
one using dynamic programming, and both running in Gina} time. Either
ofthasealgorithmsisworthfullcredit. Weelsoshowacleverwaytouse a balanced data structure to rednee the time for the dynamic programming
algorithm to 013:. lg n}. ' Deﬁneagrathinwhich '* Each of the 3:1 hln-clr's is a vertex. There‘s also a vertex t representing
the table. ﬁThereisanedge{u,a}ifblockacanbeplanednntopofblockmend
anedgetu,t}foreverybiorkta 9' The weight. ofadge {tn 1:] is the height of block it (its, the amount added
to the tower height il'we actually plane a on n}. Note that I" G has 9(a) vertiees and Gina} edges. h“ Setting up far" takes GER”) time. D- G is a dag. Directed heeause we deﬁned it that was: soyelie because a
bios]: can be placed only on a smaller hloek. P The hloelrsonany patheaubestaeked tomalteatower. e The weight of any path in G is the height of a tower built from the
hloelrs on the path. In terms of G, the tallest tower that can he built consists of the blocks
along a longest-weight path in 53'. We will redeﬁne G slightly so that we
can use Dm-Saoerssr-Parﬂs to ﬁnd the answer. 5' Change the sign of the weights so that they are all negative. A. longest
pathintheoriginalGisashortest pathinthenew G. 9' Weneedasouroeeertexto ﬁndpaths from, soaddasonrceswitha
Iii-weight edge {3,tr] to every 1: that has iii—degree I]. {You could add
a D-weight edge {3.1:} to every s, but an edge {as} into a tr that has
another edge into it can't he part of the solution, since it‘s always better
to get to 't! through some edge with non—ﬂ weight than to go directly
from s.) Now the sequence of hloeks on a shortest path from s to t found by DAG—
SHoHTEST-PATHS in G {ones the tallest tower we can build. Dae—SnoerssT—Parss takes time 9W + E} = mini}. so the whole algo-
rithm runs in 011112) time. Dynamic programming First sort the hiodts in reverse order by width. In the sorted order, widtMBi] s”. uadthIBj} only if t 2::- j. so a hloek can he placed only on blocks that ap-
pear earlier is the list. Let H [i] he the height of the tallest tower that can he built with hioek B,-
on top. That tower mnsists of Bi on top of the tallest tower that can he
built with some E,- ou top that can support 3;. which is some 153- with
j s: t', 11211thle :- lengtMBg], and urtdtMBy] 3::- Width-[35}. [The width
must be checked because some 3: may have the same width as Er.) Deﬁne
the set bigger; = {j : j e.“ i and lsngt’MBj] 3:- i'eugthmg] and tﬂidthEBj] 3.:- widthwi]:
Then
H[ﬂ] = n H' = h' B; H'
{=1 . eel Miami [1]
SineeeathvaluedependsonhronearlierHtalues,weoanoomputeata— hieHﬁomleﬂtoright. Theheightofthetallesttoweristhenlgg He]. To ﬁndthehlodts inatallest tower,wecenlneeptrackofnrhich blushes-e placedonwhich. Thetis, whenthe niestjisdioseznteplace-iInn1 keeps. pointer hum i to j". The chain of pointers starting at the i for which H[:'] is mendinum chews the blocks that made: up that H [fl-height tower. The run time is P 0(nlgn} to sort the D[n) blocks '- O{n3] to compute the C(11) entries in H if the max is found by scanning
H {because computing H [i] must seen 13 entries, so the entries scanned
for all 1' form an arithmetic series} it 0(a) to ﬁnd the maximum height in H end trace the chain of hie-cits. for e. total of 01:11.2} Subsequent tn the quiz, a student in the class [Lisa Zheng}de1dsed e. way to
use an segmented red-black tree to hold the H values so that the maximum
that is needed to compute H [i] can be feund in curd}r Uflg f} time. This
reduces the time to compute H. and hence the total time. to Clﬁtlgn}. It When H [i]: is about to be computed. the tree holds the H values for
all blocks with width 2:- nridth[B,-]. As H is computed, each time a new
width is encountered, all H values for bloclm With the previous Width
are inserted into the tree. 5 The key of the node fer block j is lengtMHj}. “ Nodes are augmented with ﬁelds H to hold H [3] and mech to hold
the maximum H in the subtree meted at the node. Since H newt
changes and mes—H can be computed from the node‘s H and its chil- dren’s mum-H, the information can he maintained during insation in
Oﬁlg n) time.
b The machine: needed in the computation of H {1] is the maximum H in the tree for nodes with key greater than fengtMHi]. We leave es an
exercise for the render how to deduce this value from the tree in Oﬁgn] time. ...
View
Full Document