Lirong_TAN

advertisement
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 move ๐‘2 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: ๐‘๐‘˜ ≤ ๐‘ฅ < ๐‘๐‘˜+1
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 (๐‘›0 , ๐‘›1 , ๐‘›2 , … , ๐‘›๐‘˜−1 ) that satisfies
that:
๐‘ฅ = ๐‘›0 × ๐‘0 + ๐‘›1 × ๐‘1 + ๐‘›2 × ๐‘2 + โ‹ฏ + ๐‘›๐‘˜−1 × ๐‘๐‘˜−1
This is impossible for any optimal solution, which can be seen from the table
below.
k
Ck
0 ๐‘0
All optimal solutions must
satisfy
๐‘›0 ≤ ๐‘ − 1
Max value of coins 0,1,….,k-1 in any
OPT
-
1 ๐‘1
2 ๐‘2
… …
k ๐‘
๐‘›1 ≤ ๐‘ − 1
๐‘›2 ≤ ๐‘ − 1
…
(๐‘ − 1) × ๐‘0 = ๐‘ − 1
(๐‘ − 1) × ๐‘1 + (๐‘ − 1) = (๐‘ − 1)(๐‘0 + ๐‘1 )
…
๐‘˜−1
๐‘˜
๐‘›๐‘˜ ≤ ๐‘ − 1
∑
(๐‘ − 1) × ๐‘๐‘– = ๐‘๐‘˜ − 1
๐‘–=0
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, ๐‘‘(๐‘ข) = ∞ ๐‘“๐‘œ๐‘Ÿ ๐‘Ž๐‘™๐‘™ ๐‘œ๐‘กโ„Ž๐‘’๐‘Ÿ ๐‘ฃ๐‘’๐‘Ÿ๐‘ก๐‘’๐‘ฅ ๐‘ข
while (๐‘ƒ ≠ ∅) do
{
select the node ๐‘ฅ, which subjects to ๐‘ฅ ∈ ๐‘ƒ, and ๐‘ฅ has the smallest ๐‘‘(๐‘ฅ)
among all nodes in ๐‘ƒ
add ๐‘ฅ to S
delete ๐‘ฅ from P
for node ๐‘ฆ, ๐‘ฆ ∈ ๐‘ƒ ๐‘Ž๐‘›๐‘‘ ๐‘ฆ ๐‘–๐‘  ๐‘Ž ๐‘›๐‘’๐‘–๐‘”โ„Ž๐‘๐‘œ๐‘Ÿ ๐‘œ๐‘“ ๐‘ฅ, do
{
๐œ‹(๐‘ฆ) = ๐‘‘(๐‘ฅ) + ๐‘™(๐‘ฅ, ๐‘ฆ), ๐‘™(๐‘ฅ, ๐‘ฆ) is the weight of edge from ๐‘ฅ to ๐‘ฆ
If ๐œ‹(๐‘ฆ) < ๐‘‘(๐‘ฆ) do ๐‘‘(๐‘ฆ) = ๐œ‹(๐‘ฆ)
}
}
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 ๐‘ฆ on path ๐‘. Let ๐‘ฅ − ๐‘ฆ be the first edge in ๐‘ƒ that leaves ๐‘†,
and let ๐‘’ be the subpath to ๐‘ฅ. Due to nonnegative property of weights, we ignore
the weights from ๐‘ฆ to ๐‘ข, thus ๐‘™(๐‘) ≥ ๐‘™(๐‘′ ) + ๐‘™(๐‘ฅ, ๐‘ฆ)
Due to the hypothesis that ๐‘‘(๐‘ฅ) is the shortest path from ๐‘ฃ to ๐‘ฅ for all ๐‘ฅ in ๐‘†
๐‘™(๐‘) ≥ ๐‘™(๐‘′ ) + ๐‘™(๐‘ฅ, ๐‘ฆ) ≥ ๐‘‘(๐‘ฅ) + ๐‘™(๐‘ฅ, ๐‘ฆ)
From the definition of ๐œ‹(๐‘ฆ), we have
๐‘™(๐‘) ≥ ๐‘™(๐‘′ ) + ๐‘™(๐‘ฅ, ๐‘ฆ) ≥ ๐‘‘(๐‘ฅ) + ๐‘™(๐‘ฅ, ๐‘ฆ) ≥ ๐œ‹(๐‘ฆ)
๐‘ข is the next node to be added to ๐‘†, so ๐œ‹(๐‘ฆ) ≥ ๐œ‹(๐‘ฅ)
๐‘™(๐‘) ≥ ๐‘™(๐‘′ ) + ๐‘™(๐‘ฅ, ๐‘ฆ) ≥ ๐‘‘(๐‘ฅ) + ๐‘™(๐‘ฅ, ๐‘ฆ) ≥ ๐œ‹(๐‘ฆ) ≥ ๐œ‹(๐‘ข)
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 ๐‘š๐‘™๐‘œ๐‘”๐‘›, 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 ๐‘ฃ.
First of all, we should clarify that shortest-path spanning tree is defined as a tree,
in which the path weight from root node ๐‘ฃ to any other node is minimized.
In G, for an arbitrary vertex ๐‘ข, the path from ๐‘ฃ to ๐‘ข defined by ๐‘‡ is denoted as ๐‘,
any other path from ๐‘ฃ to ๐‘ข is denoted as ๐‘’. We have ๐‘™(๐‘′ ) ≥ ๐‘™(๐‘) for any ๐‘’.
In G*, all edges have been multiplied by a constant factor ๐‘. Hence, the path
weight between any two vertices is ๐‘ times of the weight in the original graph ๐บ.
Thus for G*, ๐‘™ ∗ (๐‘′ ) = ๐‘ × ๐‘™(๐‘′ ), ๐‘™ ∗ (๐‘) = ๐‘ × ๐‘™(๐‘).
So, ๐‘™ ∗ (๐‘′ ) ≥ ๐‘™ ∗ (๐‘) ๐‘–๐‘“ ๐‘ > 0, which means path ๐‘ still has the lowest weight in
graph G*. Since vertex ๐‘ข and path ๐‘’ are selected arbitrarily. We are safe to
conclude that T is still the shortest-path spanning tree in G* from ๐‘ฃ.
(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.
No, T is not necessarily the shortest-path spanning tree in G+ from ๐‘ฃ.
Similar to question (a), for an arbitrary vertex ๐‘ข, the path defined by ๐‘‡ is denoted
as ๐‘, and another arbitrary path is denoted as ๐‘’.
We have, ๐‘™(๐‘′ ) ≥ ๐‘™(๐‘)
In G+, ๐‘™ + (๐‘′ ) = ๐‘™(๐‘′ ) + ๐‘š × ๐‘‘, ๐‘™ + (๐‘) = ๐‘™(๐‘) + ๐‘› × ๐‘‘, where ๐‘š and ๐‘› are the
number of edges on path ๐‘’ and ๐‘, respectively.
Since the relationship between ๐‘š and ๐‘› is not determined (I mean ๐‘› could be
much larger than ๐‘š), the relationship between ๐‘™ + (๐‘′ ) and ๐‘™ + (๐‘) can not be
determined. It could be ๐‘™ + (๐‘′) < ๐‘™ + (๐‘).
Following is a counter example. The shortest-path spanning tree is shown in red.
We can see that the shortest-path spanning tree in two graphs are not the same.
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 ๐บ ≠ ๐‘‡.
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: (๐‘›1 , ๐‘›2 , … , ๐‘›๐‘˜ ).
๐‘›1 is connected to ๐‘›2 and ๐‘›๐‘˜ . Let ๐‘›๐‘– ∈ (๐‘›1 , ๐‘›2 , … , ๐‘›๐‘˜ ) is the first accessed node in
this cycle.
Breadth-first search: both ๐‘›๐‘–−1 and ๐‘›๐‘–+1 are one depth deeper than node ๐‘›๐‘– .
Depth-first search: at most one of the two neighboring nodes (๐‘›๐‘–−1 and ๐‘›๐‘–+1 ) will
be explored at the next level.
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.
Download