Assignment

advertisement
Assignment
CS3381 Design and Analysis of Algorithms
Helena Wong, 2001
Submission date/time: Nov 28 (Wed) 6:35-6:40 pm during week 13 lecture.
Submissions before or after the above collection time will not be accepted.
Note that sample answers of this assignment will be discussed during week 13 lecture,
so we won’t have any extension of the deadline, ABSOLUTELY.
Grading Criteria:
All 7 questions in this assignment are compulsory for this course.
Any student who correctly answers all these questions will get a B grade or above.
Appropriate explanations, comments and illustrations are important, if you want to get a higher mark.
Note:
1. Printed submission is preferred (unless your hand writing is very neat).
2. This is not a teamwork assignment.
You are welcome to ask questions relating to this assignment on or before Nov 26.
You are encouraged to discuss among your classmates. But don’t plagiarize.
No programming is required for this assignment. Present your algorithms in pseudocode only.
Question 1:
The Tower of Hanoi problem:
After creating the world, God set on Earth 3 rods and m rings. The rings are all different in size. At the creation the rings
were threaded on the first rod in order of size, the largest at the bottom and the smallest at the top. God told a monastery to
transfer all the rings onto the second rod. The only operation permitted is to move a single ring from one rod to another in
such a way that no ring is ever placed on top of another smaller one. Below is the algorithm:
/* suppose the rods are labeled 1, 2, and 3 */
Hanoi (m, i, j) /* to move m rings from rod i to rod j */
If (m>0)
If (i=1 and j=2) or (i=2 and j=1)
Then intermediate_rod = 3
If (i=1 and j=3) or (i=3 and j=1)
Then intermediate_rod = 2
If (i=2 and j=3) or (i=3 and j=2)
Then intermediate_rod = 1
Hanoi(m-1,i,intermediate_rod)
Move top ring of rod i to rod j
Hanoi(m-1,intermediate_rod,j)
Try to analyze the running time of this algorithm by the recurrence tree method, substitution method, and master method.
(You may find question 4 of the Recurrences tutorial exercises useful).
Show how this algorithm follows the Divide-and-Conquer paradigm.
[For more details, you may visit: http://www.cut-the-knot.com/recurrence/hanoi.html for an animation and explanation]
Question 2:
Give asymptotic tight bounds for the followings using master method. Show your work.
a. T(n) = 9T(n/3) + 2n2 + n/3
b. T(n) = 9T(n/4) + n2
c. T(n) = 3T(n/2) + n
Question 3:
Refer to question 4 in the quiz.
Write a recursive QUICK_FINISH(course_code) algorithm in pseudocode to solve the problem. This algorithm should
return the optimal number of weeks to finish a course by a new student.
Write a dynamic programming algorithm for the problem.
Write a memoization algorithm for the problem.
Which algorithm is more efficient?
Assignment
CS3381 Design and Analysis of Algorithms
Helena Wong, 2001
Question 4:
A ski rental agency has m pairs of skis, where the height of the i th pair of skis is si. There are n skiers who wish to rent
skis, where the height of the ith skier is hi. Ideally, each skier should obtain a pair of skis whose height matches his own
height as closely as possible. Now, we want to assign skis to skiers so that the sum of the absolute differences of the
heights of each skier and his skis is minimized. Formulate a recursive function to solve this problem.
Question 5:
Is the following statement right or wrong? Prove it if it is correct (eg. by proving directly or proving by contradiction);
construct a ‘simple’ counter example if it is wrong. (Your answer should be brief.)
a.
In an undirected graph with positive edge weights, the shortest edge in the graph always belongs to any tree of
shortest paths, provided the edge weights are distinct.
b.
To find the longest path in a graph G with positive weights, we can change the weight of every edge e from w(e) to
k – w(e), where k is a value larger than any edge weight in G, and then find the shortest path in the resultant graph.
c.
If T is a tree of shortest paths from vertex s in a graph G, then T is also a tree of shortest paths from vertex s in a
graph G’ obtained by increasing the weight of every edge by same value c.
Question 6:
A bus company operates k bus routes serving n different bus stops. The i-th route, Pi, passes through mi stops and can be
represented as a sequence of stops, (vi,1 => vi,2 => vi,3 => …=> vi,Mi), where vi,1 and vi,Mi are the first and last stops
respectively. The return route (vi,Mi => vi,Mi-1 … vi,1) is considered a different route. The bus fare is ci dollars regardless
of where you get on and off along the i-th route. Given 2 bus stops, s and t, we want to find the cheapest cost to travel
from s to t. The problem can be specified as follows:
Input:
integer k > 0 specifying the number of routes,
integer n > 0 specifying the number of different bus stops,
array C[1..k] storing the cost of each route,
array R[1..k] of pointers where R[i] points to a linked list containing the sequence of stops for the i-th route,
integers s and t, specifying the start and destination stops.
Output
The cheapest cost to travel from stop s to t.
Design an efficient algorithm to solve the above problem. Present your algorithm in pseudocode. Analyze the running
time of your algorithm in terms of n and k. (Hint: Construct a weighted directed graph with n vertices.)
Question 7:
A circuit is a path that starts and ends at the same vertex in a graph. The Hamiltonian Circuit (HC) problem is defined as
follows.
Input:
an undirected graph G
Output: “yes” if there is a circuit that visits every vertex in G exactly once; and “no” otherwise.
The problem of Hamiltonian Path Between Two Points (HPBTP) is defined as follows.
Input:
an undirected graph G and two vertices, s and t, in G.
Output: “yes” if there is a path with end-points s and t that visits every vertex in G exactly once; and “no” otherwise.
Prove that HC is NP-complete assuming that HPBTP is NT-complete.
Hint: Imagine another graph G’, that is the mirror image of G. Similar to G, there are also 2 vertices: s’ and t’ in G’.
Connect s’ with s, and t’ with t. Observe what you get.
[This question relates to the topics of NP-completeness, that will be introduced during week 12 lecture.]
--- End of Assignment ---
Download