1. Give and verify a linear time algorithm that takes two sequences of

1. Give and verify a linear time algorithm that takes two sequences of events
(say encoded as lists of binary integers), and determines whether the first
sequence of events is a subsequence of the second.
Problem formulation:
Two sequences: and , with length of and , respectively.
Test whether is a subsequence of.
Idea:
Use two pointers, 1 for sequence , 2 for sequence . Compare 1
with
2
:
If 1
2
,move both pointers forward, which is 1 1 1, 2 2 1
If 1
2
, only move2 forward, which is 2 2 1.
In this way, 1 traverses once, and 2 traverses once.
The time complexity is Algorithm:
Verification:
This is a greedy algorithm. We claim that greedy algorithm always finds the
feasible solution if there is any.
We prove this by contradiction. For the example below, greedy algorithm selects
a set of jobs from sequence B to match sequence A, which is (g1, g2,..., gr, g(r+1), …).
The solution is denoted as (s1, s2, …, sr, s(r+1) , …). g1=s1, g2=s2, …, gr=sr for largest
possible value of r. Since g(r+1) and s(r+1) match the same job. Why do not replace
s(r+1) with g(r+1)? After replacement, the solution is still feasible. We get, s(r+1) =g(r+1).
There is another job in common, which contradicts with the largest possible value
is r.
Inductively, we can always replace the choice of solution with the choice of
greedy algorithm. And such replacements will not jeopardize the feasibility of the
solution. In another word, greedy algorithm always returns a feasible solution if
there is any.
2. In lecture 3 we discussed the greedy cashier’s algorithm for making change of
x units using the smallest number of coins. The cashier’s algorithm gives the
customer one unit of the highest denomination coin of at most x units, say d
units. Now repeat to make change of the remaining x-d units. For each of the
following nation’s coinage, establish whether or not this greedy algorithm
always minimizes the number of coins returned in change. If so, prove it, if not
give a counter example.
(a) MiddleEarth coinage, which includes coins for 1, 4, 5, 10, and 20.
No.
X=8
Optimal solution: 8=4+4, two coins
Greedy Algorithm: 8=5+1+1+1, four coins
So, greedy algorithm does not always minimize the number of coins.
(b) English coinage before the decimalization, which consisted of half-crowns
(30 pence), orins (24 pence), shillings (12 pence), sixpence (6 pence), threepence
(3 pence), pennies (1 pence).
No.
X=48;
Optimal solution: 48=24+24, two coins
Greedy Algorithm: 48=30+12+6, three coins
So, greedy algorithm does not always minimize the number of coins returned.
(c) Martian coinage, where the available denominations are powers of some
integer p>1, i.e., 1, p, p^2, p^3,…,p^k.
Yes, greedy algorithm always minimizes the number of coins returned.
Let arbitrary satisfy: ≤ < For greedy algorithm, the solution contains coin k. Suppose greedy algorithm is
not an optimal solution. In another word, the optimal solution does not contain
coin k. Thus, there exists a set of coefficients , , , … , that satisfies
that:
= × + × + × + ⋯ + × This is impossible for any optimal solution, which can be seen from the table
below.
k
Ck
0 All optimal solutions must
satisfy
≤ − 1
Max value of coins 0,1,….,k-1 in any
OPT
-
1 2 … …
≤ − 1
≤ − 1
…
k ≤ − 1
− 1 × = − 1
− 1 × + − 1 = − 1 + …
$
%&
− 1 × % = − 1
Therefore, we refuse the hypothesis that the optimal solution does not contain
coin k.
Then, problem reduces to coin-changing − , which, by induction, is also
optimally solved by greedy algorithm.
3. The single-destination shortest path problem for a weighted directed graph is
to find the shortest path from every vertex to a specified vertex v. Give and
verify an efficient algorithm to solve the single-destination shortest paths
problem.
Idea:
First, reverse all edges; second, use the Dijkstra’s Algorithm to find the shortest
paths.
Algorithm:
Reverse the direction of all edges. Thus, the destination vertex ' becomes the
source.
Maintain a set of explored nodes ( for which we have determined the shortest
path distance ) from ' to vertex ). And a set of unexplored nodes *.
Initialize ( = ∅, ' = 0, ) = ∞./011/2ℎ40'4024)
while (* ≠ ∅) do
{
select the node, which subjects to ∈ *, and has the smallest among all nodes in *
add to S
delete from P
for node 6, 6 ∈ *678479ℎ:/0/., do
{
;6 1, 6, 1, 6 is the weight of edge from to 6
If ;6 < 6 do 6 = ;6
}
}
Verification:
Claim: ) is the shortest path from ' to )
We prove this by induction on|(|.
|(| = 1, ) is obviously the shortest path from ' to ), if ) is selected to be
added to ( next.
|(| > 1, let ) be the next node to be added to (. And we need to prove ) is
still the shortest path from ' to ).
There are two possibilities for the path from ' to ):
1, the path only contains the nodes in ( . In this situation, ) is the shortest
path since we update ) every time we add a new node to (
2, the path contains nodes in both ( and *, as shown in the figure below.
Suppose there is a node 6 on path . Let − 6 be the first edge in * that leaves (,
and let ’ be the subpath to . Due to nonnegative property of weights, we ignore
the weights from 6 to ), thus 1 ? 1 @ 1, 6
Due to the hypothesis that is the shortest path from ' to for all in (
1 ? 1@ 1 , 6 ? 1, 6
From the definition of ;6, we have
1 ? 1 @ 1 , 6 ? 1, 6 ? ;6
) is the next node to be added to (, so ;6 ? ;
1 ? 1 @ 1, 6 ? 1, 6 ? ;6 ? ;)
To sum up, any path from ' to ) will have a greater weight than )
Due to the reversibility of path, we conclude that ) is the shortest path from )
to'.
The time complexity for this algorithm is 1/9, where is the maximal value of
out-going degree among all vertices in the reversed graph, is the number of
vertices. (Implemented with binary heap)
4. Let G = (V,E) be an undirected weighted graph, and let T be the shortest-path
spanning tree rooted at a vertex v.
(a) Consider the graph G* obtained by modifying all the edge weights in G by
multiplying the weight by a constant factor c >0. Is T still the shortest-path
spanning tree in G* from v? Justify your answer.
Yes, T is still the shortest-path spanning tree in G* from v.
Shortest-path spanning tree is defined as a spanning tree whose sum of edge
weights is minimized. Furthermore, for any graph with K nodes, there will be K-1
edges in any spanning tree.
For graph G, T is defined as a set of edges 4 , 4 , … , 4 , and *A = ∑%& 4% is
minimized. For any other spanning trees, the sum of the edge weights is denoted
as C. According to the definition of shortest-path spanning tree, we have
*A ≤ C.
For G*, all the edge weights are multiplied by a constant factor . The sum of the
edge weights in T is × *A, and for all other spanning trees, the sum of the
weights is × C.
Since we have × *A ≤ × C, > 0, T is still the shortest-path spanning
tree for G*. Furthermore, all the edges have been multiplied with a constant ,
the shortest edge in G is still shortest in G*, thus T is still rooted at vertex ' in G*.
(b) Consider the graph G+ obtained by modifying all the edge weights in G by
adding to the weight by a constant d >0. Is T still the shortest-path spanning tree
in G+ from v? Justify your answer.
Yes, T is still the shortest-path spanning tree in G+ from '.
Similar to question (a), we have *A ≤ C for graph G.
For G+, all the edge weights are increased by a constant . The sum of the edge
weights in T is *A + × , and for all other spanning trees, the sum of the
weights is C + × .
Thus, *A + × ≤ C + × , > 0.
So, T is still the shortest-path spanning tree in G+. Since all edges have been
increased by a constant , the shortest edge in G is still shortest in G+, thus T is
still rooted at vertex ' in G+.
5. Suppose that you run both depth-first search and breadth-first search on a
connected graph G, and they both return the same tree T. Prove that G=T, i.e.,
there are no additional edges in the graph.
Suppose D A.
First of all, T will not have edges that are not in G, since T has the minimal number
of edges to construct a connected graph.
Second, assume there is an edge in G but not in T.
Then, there will be a cycle in G. Nodes in this cycle is denoted as: , , … , .
is connected to and . Let % ∈ , , … , is the first accessed node in
this cycle.
Breadth-first search: both %
and % are one depth deeper than node % .
Depth-first search: at most one of the two neighboring nodes (%
be explored at the next level.
and % ) will
This contradicts with “both depth-first search and breadth-first search return the
same tree”.
Therefore, there is no additional edges in G. Thus, G=T.