Algorithm Homework Help
Algorithm Homework Help
Decision Problems
For each of the following questions, circle either T (True) or F (False),
and briefly justify your answer in the box provided (a single sentence or
picture should be sufficient). Each problem is worth 4 points: 2 points for
your answer and 2 points for your justification. If you leave both answer
and justification blank, you will receive 1 point.
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
(d) T F Given an array A containing n comparable items, sort A using
merge sort. While sorting, each item in A is compared with O(log n)
other items of A.
Solution: False. As a counter example, during the final merge
step between two sorted halves of the array, each of size Θ(n), a
single item from one array may get compared to all the items from
the other list.
(e) T F Given a binary min-heap storing n items with comparable
keys, one can build a Set AVL Tree containing the same items
using O(n) comparisons.
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
(g) T F Run Bellman-Ford on a weighted graph G = (V, E, w) from a vertex s
∈ V . If there is a witness v ∈ V , i.e., δ|V |(s, v) < δ|V |−1(s, v), then v is on
a negative- weight cycle of G.
Solution: False. A witness is only guaranteed to be reachable
from a negative-weight cycle; it may not actually be on a
negative-weight cycle.
(h) T F Floyd–Warshall and Johnson’s Algorithm solve all-pairs shortest paths
in the same asymptotic running time when applied to weighted complete graphs,
i.e., graphs where every vertex has an edge to every other vertex.
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
Problem 3. Sorting Sorts
(a)An integer array A is k-even-mixed if there are exactly k even
integers in A, and the odd integers in A appear in sorted order. Given
a k-even-mixed array A containing n distinct integers for k = dn/ lg
ne, describe an O(n)-time algorithm to sort A.
Solution: Scan through A and put all even integers in order into an
array A E and all odd integers in order into an array A O (where |A E | =
k and |AO| = n − k). A O is sorted by definition, and we can sort A E in
O(k log k) = O((n/ lg n) log(n/ lg n)) = O(n) time, e.g., via merge
sort. Then we can merge sorted A E and A O back into A in O(n) time
using the merge step of merge sort, using O(n) time in total.
Common Mistakes:
•Using insertion sort
•Splitting into a non-constant number of subarrays and trying to
merge
•Using binary search to insert evens into odds (but with linear
shifts)
•Using radix or counting sort
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
•Trying to write an incorrect dynamic program
•Trying to write an incorrect four-finger algorithm
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
Problem 7. On the Street
Friends Dal and Sean want to take a car trip across the country from Yew
Nork to Fan Sancrisco by driving between cities during the day, and
staying at a hotel in some city each night. There are n cities across the
country. For each city ci, Dal and Sean have compiled:
•the positive integer expense h(c i ) of staying at a hotel in city ci for
one night; and
•a list L i of the at most 10 other cities they could drive to in a single
day starting from city ci, along with the positive integer expense g(ci ,
c j ) required to drive directly from ci to c j for each c j ∈ L i .
Describe an O(nd)-time algorithm to determine whether it is possible for
Dal and Sean to drive from Yew Nork to Fan Sancrisco in at most d
days, spending at most b on expenses along the way.
Solution: Let C = {c 0 , . . . , cn−1 }, and let cs denote Yew Nork and let ct
denote Fan Sancrisco. Construct a graph G with:
•a vertex (c i , d0) for each city ci ∈ C and day d0 ∈ {0, . . . ,
d}, representing staying the night in city ci on the night
before day d0; and
•a directed weighted edge ((c i , d0), (c j , d0 + 1)) with weight
g(ci, c j ) + h(c j )
for each city ci ∈ C , cj ∈ L i and d0 ∈ {0, . . . , d − 1}.
Then the weight of any path in G from vertex (cs , 0) to any vertex (ct ,
d0) for d0 ∈ {0, . . . , d} corresponds to the expenses incurred along a
driving route from Yew Nork to Fan Sancrisco in at most d days
(assuming they stay the night upon reaching ct; other assumptions are
also okay). G is acyclic, since each edge always connects a vertex from
a smaller day to a larger day, so run DAG Relaxation to compute single-
source shortest paths from (cs , 0) in G. Then return whether δ((cs , 0),
(ct, d0)) ≤ b for any d0 ∈ {0, . . . d}. G has O(nd) vertices and O(nd)
edges (since)
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
|Li | ≤ 10 for all i ∈ {0, . . . , n − 1}), so DAG relaxation runs in O(nd)
time and checking all destination values takes O(d) time, leading to O(nd)
time in total.
Common Mistakes:
•Not considering paths on fewer than d days (or considering paths on
more than d days)
•Not enforcing staying at a hotel every night
•Not including hotel costs
0 S(u,v)∈E
vertices appearing in those edges, {u, v} (which contains
specifically V = vertices from
Vr and Vg ). Run breadth-first search from s in G0 to compute
unweighted distances. Then the r
minimum weight distance in G from s to any green vertex in V 0 ∩ Vg is
wr times the unweighted
distance computed. G0 has size O(|V |), so this step takes O(|V |) time.
Step 2: Now construct weighted graph G00 = (V 00, E00) composed of
vertex s with a new directed
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
edge to each green vertex in V 0 ∩ Vg weighted by its distance found in Step
1 (i.e., the minimum weight of any path from s to that vertex), along with
weighted edges E g and the vertices appearing in those edges. All the
weights in G00 are positive, so run Dijkstra from s in G00 to compute
minimum weight distances. Then the computed distance to any blue vertex
v in V 00 ∩ Vb is the minimum weight of any path from s to v in G that
traverses only red or green edges. G00 has size O(1 + |Vg | + |Eg |) = O(|V |
0.99
), so this step takes O(|V |0.99 log |V |0.99) = O(|V |) time.
Step 3: Now construct a weighted graph G000 = (V 000, E000) composed of
vertex s with a new directed edge to each blue vertex in V 00 ∩ Vb weighted
by its distance found in Step 2 (i.e., the minimum weight of any path from
s to that vertex), along with weighted edges E b and the vertices appearing
in those edges. Weights in G000 may be positive or negative, so run Bellman-
Ford from s in G000 to compute weighted minimum weight distances. Then
the computed distance to t is the minimum
weight of any path from s to t in G, as desired. G000 has size O(1 + |Vb| +
pp p
this
|E b |) step
= O(takes O(so |V | |V |) = O(|V |) time, leading to O(|V |) time in
|V |),
total.
Common Mistakes: Continued on S1.
Problem 9. Separated Subsets
For any set S of integers and for any positive integers m and k, an (m, k)-
separated subset of S
is any subset S0 ⊆ S such that S0 sums to m and every pair of distinct
integers a, b ∈ S0 satisfies
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
•Subproblem x(i, j) only depends on strictly larger i, so acyclic
4.Base
•x(n, 0) = 1, the empty subset can always be acheived
•x(n, j) = 0 for j > 0, empty sets cannot sum to a positive number
5.Original
•x(0, m), the number of (m, k)-separated subsets of A
6.Time
•# subproblems: (n + 1)(m + 1) = O(nm)
•Work per subproblem: O(n) to find f (i) by linear scan
•O(n m) time in total
2
•(Note that it is possible to compute f (i) in O(log n) time via binary
search, or in amor- tized O(1) time from f (i − 1), but these
optimizations are not necessariy for full points.)
Common Mistakes: Continued on S1.
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
•Additionally, every guest hates every other guest: for every two guests i,
j, Ted knows the
positive integer mutual hatred d(i, j) = d(j, i) between them.
Given Ted’s guest information, describe an O(n3 )-time algorithm to
determine a respectful seating order that minimizes the sum of mutual
hatred between pairs of guests seated next to each other. Significant
partial credit will be awarded to correct O(n4 )-time algorithms.
Solution:
1.Subproblems
•Sort the guests increasing by favor in O(n log n) time into F =
(f 0 , . . . , f 2 n−1 )
•Any partition of F into two length-n subsequences corresponds to
a respectful seating
•x(i, j L , j R , n L ): minimum total hatred of adjacent guests
possible by respectfully seating the n − i guests from suffix F
[i :] next to the Queen, with n L guests to the left and n R = (n
− i) − n L guests to the right, where guest j L < i has already been
seated n L + 1 places to the left, and guest j R < i has already
been seated n R + 1 places to the right.
•for i ∈ {0, . . . , 2n}, j L , j R ∈ {−1, . . . , 2n − 1} and n L ∈
{0, . . . , n}
where either j L = i − 1 or j R = i − 1
•Let d(−1, i) = d(i, −1) = 0 for all i ∈ {0, . . . , 2n − 1} (no
hatred at the end of table)
2.Relate
•Guess whether guest i is seated on the right or left
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
•Sitting next to j L costs hatred d(i, j L ); sitting next to j R
costs
•x(i,hatred
j , j d(i,
, n j)R ) d(i, j ) + x(i + 1, i, j , n − 1)
LRL L R L if n L > 0,
= min d(i, j R ) + x(i + 1, j L , i, n L ) if (n − i) −
nL > 0
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
Common Mistakes:
•Correctly finding shortest paths within each color, but not connecting
them properly
•Assuming an optimal path from s to t goes through the green vertex
closest to s
•Solving APSP on the red or green graph (inefficient)
•Trying to apply DAG relaxation to a possibly cyclic graph
•Assuming that if |V | = |E|, graph has at most one cycle (only true if
connected)
Common Mistakes:
•Maintaining a subset of used items (yields exponential state)
•Unconrstrained subproblem for relation that relies on a constraint
•Solving the decision problem rather than the counting problem (or max
instead of sum)
•Missing a base case
Common Mistakes:
•Not ensuring that n guests are seated to either side of Queen
•Not keeping track of guests on either end
•Defining Ω(n 3 ) subproblems whose parameters are not independent
•Maintaining a subset of used items (yields exponential state)
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers