Best Algorithms Design Assignment Help
Best Algorithms Design Assignment Help
(c) Run both BFS and DFS on the graph from part (b), starting from node A.
While performing each search, visit the outgoing neighbors of a vertex in
alphabetical order. For each search, draw the resulting tree and list vertices
in the order in which they were first visited.
Rubric:
• 2 points per tree
• 1 point per order
• Partial credit may be awarded
(d) It is possible to remove a single edge from the graph in (b) to
make it a DAG. State every edge with this property, and for
each, state a topological sort order of the resulting DAG.
Solution: There is only one cycle in the graph between vertices
D and F, so we can remove
either the edge (D, F) or (F, D) to make the graph acyclic.
Removing edge (D, F) results in a DAG with a unique
topological ordering (A, B, C, F, D, E). Removing (F, D) results
in a DAG with two topological orderings: specifically (A, B, C,
D, F, E) and (A, B, D, C, F, E).
Rubric:
• 1 point for the two edges
• 1 point for each topological order
Programminghomeworkhelp.com
Problem 5-3. Short-Circuitry
Star-crossed androids Romie-0 and Julie-8 are planning an engagement
party and want to invite all their robotic friends. Unfortunately, many
pairs of their friends can’t be in the same room without short-circuiting.
Romie-0 has an idea to throw two parties instead: inviting some friends to
the first party, and the rest to the second. Ideally, everyone would get
invited to a party, but nobody would go to the same party as someone that
would make them short-circuit. Julie-8 points out to Romie-0 that this
might not be possible, for example if they have three friends who all
make each other short-circuit. Given the n short-circuiting pairs of
friends, describe an O(n)-time algorithm to determine whether Romie-0
and Julie-8 can invite all of their friends to two peaceful parties, without
anyone short-circuiting.
Solution: Construct a graph G where each vertex is a friend who might
short-circuit, with an undirected edge between a pair of friends if they
make each other short-circuit. Note that we can ignore any friend not
participating in short-circuiting because they have no restrictions on
which party they can attend. We can assign the friends to two peaceful
parties if and only if there is a coloring of the vertices of G with two
colors such that no edge connects vertices of the same color, i.e., the
graph must be bipartite. To determine whether the graph is bipartite, we
will either find a valid two-coloring, or find an odd-length cycle in the
graph (if there is an odd-length cycle in the graph, there certainly cannot
be a valid two-coloring of the cycle or the graph).
To find a possible two-coloring of G, our approach will be to find a
spanning forest F of G, two-color it, and then check to see whether any
edge in G violates the coloring. If there is an edge {u, v} such that both
vertices have the same color, then the path between u and v in F has even
length (since it has a valid two-coloring), so adding {u, v} would be an
odd-length cycle.
Running either Full-BFS or Full-DFS on G can construct a spanning
forest F . We choose to run Full-BFS, marking each vertex with its
shortest path distance to the arbitrarily chosen source within its
connected component. To two-color F , assign one color to all vertices
with even distance to their source, and second
color to all vertices with odd distance to their source (since shortest
paths alternate distance parity along the path). Lastly, check whether
every edge in the graph has endpoints assigned opposite colors. If not,
return that two parties is impossible. Otherwise the coloring confirms
the graph is bipartite, so return that two parties is possible.
G has size O(n) since it has n edges and at most 2n vertices, and can
be constructed in as much time. Running Full-BFS on G takes time
O(n) (linear in the size of G), and then for each of the n edges we
perform a constant-time check. So this algorithm runs in O(n) time.
Rubric:
• 2 points for graph construction
• 3 points for description of a correct algorithm
• 1 points for correctness argument for a correct algorithm
• 1 points for running time argument for a correct algorithm
• 3 points if the algorithm is efficient, i.e., O(n)
• Partial credit may be awarded
Programminghomeworkhelp.com
Problem 5-4. Ancient Avenue
The ancient kingdom of Pesomotamia was a fertile land near the
Euphris and Tigrates rivers. Newly- discovered clay tablets show a map
of the kingdom on an n × n square grid, with each square either:
• part of a river, labeled as ’euphris’ or ’tigrates’; or
. not part of a river, labeled with a string: the name of the farmer who
owned the square plot of land.
The tablets accompanying the map state that ancient merchants built a
trade route connecting the two rivers: a path along edges of the grid
from some grid intersection adjacent to a ’euphris’ square to a grid
intersection adjacent to a ’tigrates’ square. The tablets also state
that the route:
• did not use any edge between two squares owned by the same
farmer (so as not to trample crops); and
• was the shortest such path between the rivers (assume a shortest
such path is unique).
Given the labeled map, describe an O(n 2 )-time algorithm to determine
path of the ancient trade route.
Solution: Let M be the n × n labeled map, where M [r][c] is the label
of the grid square in row r and column c. Construct graph G with (n +
1)2 vertices (r, c) for all r, c ∈ {0, . . . , n}, and an undirected edge
between:
• vertex (r, c − 1) and vertex (r, c) for r ∈ {0, . . . , n} and c ∈
{1, . . . , n}, except when:
– r ∈ { 1, . . . , n − 1} (not a boundary edge),
– M [r][c] is not ’euphris’ or ’tigrates’ (is owned by a
farmer), and
– M [r ][c − 1] = M [r ][c] (owned by the same farmer); and
• vertex (r − 1, c) and vertex (r, c) for c ∈ {0, . . . , n} and r ∈ {1, . . . , n},
except when:
– c ∈ { 1, . . . , n − 1} (not a boundary edge),
– M [r][c] is not ’euphris’ or ’tigrates’ (is owned by a
farmer), and
– M [r − 1][c] = M [r][c] (owned by the same farmer).
This graph has the property that a path between any two vertices corresponds to
a route on the map that does not trample crops. It remains how to find the
shortest such route between any vertex adjacent to a ’euphris’ square to a
vertex adjacent to a ’tigrates’ square. For each ’euphris’ square M
[r][c],
mark vertices {(r, c), (r+1, c), (r, c+1), (r+1, c+1)} as ’euphris’ vertices
(possible starting intersection
of the route), and mark ’tigrates’ vertices similarly.
We could solve SSSP from each ’euphris’ vertex using BFS, but that
would take too much time (poten- tially Ω(n 2 ) ’euphris’ vertices, with
each BFS taking Ω(n 2 ) time.) So instead, add a supernode s to G with an
undirected edge from s to each ’euphris’ vertex in G, to construct graph
G0. Then any possible trade route will correspond to a path from s to a
’tigrates’ vertex in G0 (without the first edge), and the trade route will be
the shortest among them. So run BFS from s to every ’tigrates’ vertex,
and return the shortest path to the closest one by traversing parent pointers
back to the source.
Graph G0 has (n + 1)2 + 1 vertices and O(n 2 ) + O(n 2 ) edges, so can be
constructed in O(n 2 ) time, and running BFS once from s also takes O(n 2 )
time, so this algorithm runs in O(n 2 ) time in total.
Rubric:
• 3 points for graph construction
• 2 points for description of a correct algorithm
• 1 points for correctness argument for a correct algorithm
• 1 points for running time argument for a correct algorithm
• 3 points if the algorithm is efficient, i.e., O(n 2 )
• Partial credit may be awarded
Solution: While Liza traverses the Statum Center, which doors she can
enter depends on which key cards she has in her possession. It would be
useful to know which cards she has in her possession while traversing
the building. Since there are only 4 types of key card, there are at most 24
possible sets of key cards she could have at any given time. We will use
graph duplication to represent these 24 = O(1) possible states.
Let T = {SeeSail, TOPS, S3C, DPW}, let C(`, k t ) equal 1 if ` = ` t and k t
otherwise, and let P (`, p) equal 1 if location ` contains a pizza and p
otherwise. If Liza enters any location ` having at least p pizzas and k t
key cards of type t, she can leave with at least P (`, p) pizzas and C(t, `,
k t ) key cards of type t, for any t ∈ T . Construct a graph G = (V, E ) as
follows.
This graph exactly encodes all possible state transitions from any
location in the Statum Center while keeping track of the number of
pizzas and key cards that Liza can be holding. Thus a path from
vertex s = v(e, 0, 0, 0, 0, 0) to any vertex q ∈ Q = {v(e, 1, kSeeSail,
kTOPS, kS3C, kDPW ) | ∀kt ∈ {0, 1} ∀t ∈ T } represents a path that
enters and leaves the Statum Center at e, while also procuring a pizza.
Since Liza would like to minimize the number of doors she must cross,
running BFS in G from s to each q ∈ Q finds the minimum doors
crossed to each of them, so we can return a shortest path to any of them
by traversing parent pointers back to the source.
Graph G has |L| · 25 = O(|L|) vertices and at most |D| · 25 = O(|
D|) edges, so can be constructed in
O(|L| + |D|) time, and running BFS once from s also takes time linear in
the size of the graph, so this algorithm runs in O (|L | + |D |) time.
Rubric:
• 5 points for graph construction
• 2 points for description of a correct algorithm
• 2 points for correctness argument for a correct algorithm
• 2 points for running time argument for a correct algorithm
• 4 points if the algorithm is efficient, i.e., O(|L| + |D|)
• Partial credit may be awarded
right
down right up —
− −→ — −
→ → →
Programminghomeworkhelp.com
up left
−→ −→ −down
−→
→
Programminghomeworkhelp.com
• Partial credit may be awarded
(b) If Tilt board configuration B can reach another Tilt board configuration
B 0 by a single move, we call B0 a successor of B , and B a predecessor
of B 0 . Argue that a board configuration B may have Ω(n s )
predecessors, but has at most r = O(1) successors.
Solution: A Tilt board configuration B has at most r = 4 successors,
since there are only 4 possible Tilt moves from the configuration, and
each move results in at most a single new configuration. However, B
may have Ω(n s ) predecessors: consider a board with no obstacles but s
≤ n sliders, and let B be a configuration with at most one slider in each
column, with each slider at the bottom of its column. This board is
reachable via a ’down’ move from any configuration having s
sliders in the same columns. Each slider could be in any of the n
squares in its column in a ’down’ predecessor of B , so there are at
least Ω(n s ) predecessors of B .
Rubric:
• 1 point for a correct argument for successors
• 2 points for a correct argument for predecessors
• Partial credit may be awarded
(c) [10 points] Given a 6.006 Tilt puzzle (B , t), describe an algorithm to
return a move sequence that solves the puzzle in the fewest moves, or
return that no such move sequence exists. If the puzzle’s shortest
solution uses k moves (k = ∞ if the puzzle is not solvable), your
algorithm should run in O(n 2 min{r k , C(n, b, s)}) time.
Solution: Consider the abstract graph G defined by the C (n, b, s)
configurations possibly reachable from B . Our approach will be to
run breadth-first search on G from the input board configuration B ,
but only construct other reachable configurations as they are reached
(and not try to construct the entire graph from the beginning).
Programminghomeworkhelp.com
While processing configura- tion B0 during the search, compute the
adjacent successor configurations from B0 in O(n 2 ) time (using the
move function provided), and return a shortest path to the first B ∗
found where B ∗ [y t ][x t ] =’o’. By definition of shortest paths, B ∗
is reachable in k moves, so since each configuration has at most r
= 4 successors, at most P i=0 k r i < r k + 1 = O(r k ) configurations
are explored during this search. If B is not solvable, every non-
solved configuration may need to searched, which is at most C(n,
b, s) configurations. Thus since it takes O(n 2 ) time to pro- cess
each configuration, this search takes at most O(n 2 min{r k , C(n, b,
s)}) time to complete.
Rubric:
• 2 points for graph construction
• 2 points for description of a correct algorithm
• 1 points for correctness argument for a correct algorithm
• 3 points for running time argument for a correct algorithm
• 2 points if the algorithm is efficient, i.e., O(n 4 )
• Partial credit may be awarded
Programminghomeworkhelp.com
(d) Write a Python function solve tilt(B, t) that implements your
algorithm from part (d) using the template code provided. You can
download the code template and some test cases from the website.
Solution:
Programminghomeworkhelp.com
14 for d in (’up’, ’down’, ’left’, ’right’):
15 B2 = move(B1, d)
16 if B2 not in P:
17 P[B2] = (B1, d)
18 L.append(B2)
19 if B2[yt][xt] == ’o’:
20 while P[B2]:
21 B2, d = P[B2]
22 M.append(d)
23 Ma.reverse()
24 return M
25 levels.append(L)
26 M = None
27 return M