0% found this document useful (0 votes)
4 views7 pages

Midsols

The document contains a series of algorithm-related problems and their solutions, including true/false questions about Dijkstra's algorithm, polynomial multiplication, and minimum spanning trees. It also discusses algorithms for finding the longest path in a directed acyclic graph, maximizing puppy-child pairings, and minimizing the difference in a one-to-one mapping between two sets of integers. Additionally, it outlines an efficient method to determine if a specific edge is part of any minimum spanning tree in a weighted undirected graph.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Midsols

The document contains a series of algorithm-related problems and their solutions, including true/false questions about Dijkstra's algorithm, polynomial multiplication, and minimum spanning trees. It also discusses algorithms for finding the longest path in a directed acyclic graph, maximizing puppy-child pairings, and minimizing the difference in a one-to-one mapping between two sets of integers. Additionally, it outlines an efficient method to determine if a specific edge is part of any minimum spanning tree in a weighted undirected graph.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Midterm Solution Sketches

CIS 5020 – Algorithms


1. True or false? Justify your answers.

(a) Let G be a (positive) weighted, directed, graph on n nodes, and let s be a vertex in G. Dijkstra’s
algorithm builds a tree T of shortest paths from s to all vertices in G. Now suppose we increase
the weight of every edge by 10. Then T is still a shortest path tree from s.
Answer: False. Simple counter-example: Suppose there is a path of length 2 from s to b via
a, where both edges (s, a) and (a, b) have weight 1. There is also a direct edge from s to b with
weight 5. Clearly the path of length 2 is the shorter path in the original graph. But adding 10
to the weight of each edge makes this path have weight 22, while the direct edge has weight 15.
(b) From the value representations of two degree d polynomials, A(x) and B(x) at the points
{1, 2, . . . , d + 1}, we can find the coefficient representation of A(x)B(x), the product of the two
polynomials. Answer: False. The product has degree 2d and its values have to be known at
2d + 1 points to be able to find its coefficient representation.
(c) The solution to the recurrence

T (n) ≤ max T (i) + T (n − i) + n


i∈[1..n−1]
T (1) = 1

is T (n) = O(n log n).


Answer: False. The maximum is achieved for i = 1 and it is easily seen to be n+(n−1)+· · ·+1,
which is ω(n log n).
(d) If a weighted graph G = (V, E) has two different minimum spanning trees, then there must be
a cut (S, V − S) in G where the lightest weight edge is not unique.
Answer: True. If not, Prim’s algorithm would always pick a unique edge and arrive at a unique
MST.

2
2. Let G be a weighted, directed, acyclic graph. Given a vertex s, design an efficient algorithm to find
the longest path from s to each vertex in G. Argue that your algorithm is correct and analyze its
running time.

Answer: We first perform a topological sort on the graph. For each vertex v, any path from s can
only come from the vertex whose rank is higher than v. And we have the fact that the prefix of the
longest path is also the longest path. This problem has a dynamic programming problem structure.
The top level question is ”which is the second last vertex on the longest path from s to a vertex v?”
We can find the longest path from s from the highest rank to the lowest rank. Let d(v) be the weight
of the longest path from s to v. Define N (v) = {u|(u, v) ∈ E} and w(u, v) be the weight of edge
(u, v), The transfer function is
d(v) = max {d(u) + w(u, v)}
u∈N (v)

The base case for s is d(s) = 0, the base case for a vertex v which is not s and has no in-degree is
d(v) = −∞.
The time complexity is O(V + E) because we will visit each vertex and each edge only once. If we
want to construct the longest path, we can remember the previous vertex on the longest path when
calculating d(v). Then construct the longest path tree by this information.

Some wrong algorithms There are several solutions are ”modified Dijkstra”, Common modifica-
tions were to change the sign on all edge weights or modify the RELAX step. These algorithms are
not correct because Dijkstra algorithm won’t work if there exists negative weighted edges. Another
is to change the weight of each edge by subtract from the biggest edge. This approach is not correct
because the weight of a path may affected by the length of the path. For example, there are two
paths and the first path has edge weights 1, 5 and the second is 1, 2, 4. Then after we modify the
weights, the first path will become 4, 0 and the second will become 4, 3, 1. The second path always
has larger weight.
There are also several solutions are ”modified DFS with changing parent”, here is a counter example:

3
Figure 1: A counter example for ”modified DFS”: we will visit edge (E, F ) before edge (C, E)

4
3. We are given an array of size n that has the following specifications:

• Each element in the array is either a puppy or a child


• Each child can pet only one puppy
• A child cannot pet a puppy who is more than k units away from the child. In other words, if
position i has a child, that child can pet a puppy in position j only if |j − i| ≤ k.

Design an algorithm to find the maximum number of puppies that can be petted. Argue that your
algorithm is correct, and analyze its running time.
Answer:
The greedy algorithm is as follows: Initially all puppies and children are unmatched. Find the
leftmost puppy. If there is an unmatched child within k of this puppy, match the leftmost such child
to the puppy and mark both as matched. Repeat with each subsequent puppy.
This algorithm can be implemented in O(n) time by using two pointers to scan the array once —
one pointing to the ‘current puppy’ and one pointing to the ‘current child’ that can still be matched.
We prove correctness in the usual way we do for greedy algorithms.
First we prove a generic property we can assume about the optimal solution. Any solution is a set
of matched pairs of puppies and children. We say that such a solution is crossing if there exist two
puppies pi and pj with i < j, that are matched respectively to two children ck and c` , such that
k > `.
Then if a solution is crossing, we can uncross it without reducing the number of matched puppies.
The proof is a simple case analysis and is omitted. Note that the greedy solution is non-crossing.
We now prove the greedy choice property:
Let (p, c) be the first pair chosen in the greedy solution G. Suppose there is an optimal solution O
that does not have this pair. If O matches only one of p or c, i.e., it does not have two matched pairs
(p, c0 ) and (p0 , c), then we can replace the one pair in O involving p or c by the pair (p, c) and not
reduce the number of matched pairs.
So assume that O, in fact matches both p and c in pairs (p, c0 ) and (p0 , c). Clearly, by the way the
greedy algorithm works, these two pairs are crossing, since p comes before p0 and c0 comes after c.
As we argued earlier, if we uncross these pairs, we get (p, c) as one of the pairs.
So in all cases, there is an optimal solution that includes the pair (p, c), which establishes the greedy
choice property.
Having chosen the pair (p, c) we need to solve the recursive subproblem. It is clear that the number
of pairs matched in the original problem will be one more than the number matched in this recursive
problem. Thus, we should find a way to match the greatest number of pairs in the recursive problem,
which means we should solve it optimally. Thus the optimal substructure property also holds and
the greedy algorithm is correct.

5
4. Let S and T be finite sets of non-negative integers. Let |S| = m and |T | = n. Assume n ≥ m. Design
a dynamic programming algorithm to find a one-to-one function f mapping S to T that minimizes
P
i∈S |f (i) − i|.
[Hint: Say that a function f has a crossing if there are values s1 < s2 in S and t1 > t2 in T such
that f (s1 ) = t1 and f (s2 ) = t2 . Argue that you can always find an optimal f that does not have
crossings. This will help in the design of the dynamic programming algorithm.]

Answer: By sorting these two sets, assume S = {s1 , s2 , . . . , sm } and T = {t1 , t2 , . . . , tn } where
s1 < s2 < · · · < sm and t1 < t2 < · · · < tn . We first prove the hint. Consider the optimal solution
with the least number of crossings. If there are crossings in the solution. We can find an si such that
f (si ) > f (si+1 ). If [si , si+1 ] and [f (si+1 ), f (si )] don’t overlap with each other, the objective function
won’t change if we swap f (si ) and f (si+1 ). If they overlap with each other, the objective function
will reduce by 2 times the overlaping size if we swap them. So if we swap the crossing, we can reduce
the number of corssings by 1 without enlarge the objective function, which is a contridiction.
With the hint, we can only consider the function without crossing. Suppose d(i, j) is the optimal
objective function if we only consider the first i numbers in S and the first j elements in t. Since f
is a one to one function, i should be less or equal to j. For convenience, we say d(i, j) = ∞ if i > j.

A correct but inefficient algorithm. Consider the top level question is: which element in T
should si matched to? Since there are no crossing, f (si ) ≥ ti . So

d(i, j) = max {d(i − 1, k − 1) + |si − tk |}


i≤k≤j

The base case d(0, j) = 0 for any 1 ≤ j ≤ n. There are m ∗ n subproblems and we need to check at
most n − m probabilities when we solve each subproblem, so the time complexity is O(mn2 ).

A better algorithm Consider the top level question is: should f (si ) = tj ? In the subproblem
d(i, j), if f (si ) = tj , then we need to match the first i − 1 elments in S to the first j − 1 elements in
T . If f (si ) < tj , then we need to match the first i elements in S to the first j − 1 elements in T . So

d(i, j) = max{d(i − 1, j − 1) + |si − tj |, d(i, j − 1)}

The base case is d(0, 0) = 0. There are m ∗ n subproblems and we only need to check two choices
when we solve each subproblem, so the time complexity is O(mn).
By the way S = {3, 6, 201, 204} and T = {1, 4, 100, 203, 206} is a counter example for most of the
incorrect algorithms I’ve seen in your solutions.

6
5. We will use our standard notation where n denotes the number of vertices and m denotes the number
of edges in a graph. Given a weighted undirected graph G = (V, E) and an edge e in G, design an
O(m + n) algorithm to determine if there is some minimum spanning tree of G that contains e.
Provide a proof of correctness and an analysis of the running time. You can use statements proved
in class or in the homework without providing their proof. (A solution that simply runs an MST
finding algorithm like Kruskal’s or Prim’s will receive very little credit.)
Answer:
Delete all edges whose weight is equal to or greater than the weight of e = (u, v), including e itself.
If u and v are in the same connected component, then e is not in any MST. If they are in different
connected components, there is an MST that contains e. (Prove using Kruskal.) Run time: Just one
pass to delete the heavy edges and then dfs to find connected components.

You might also like