Comp 251: Assignment 5

Report 0 Downloads 145 Views
Comp 251: Assignment 5 Instructor: Jérôme Waldispühl Due on April 11th at 11h59 • Your solution must be returned electronically on MyCourse. • The only format accepted for written answers are PDF or text files (e.g. .txt or .rtf). PDF files must open on SOCS computers. Any additional files (e.g. images) must be included in the PDF. • Do not submit compressed files (e.g. zip files). Upload instead a single PDF or text file individually. • To some extent, collaborations are allowed. These collaborations should not go as far as sharing code or giving away the answer. You must indicate on your assignments the names of the persons with who you collaborated or discussed your assignments (including members of the course staff). If you did not collaborate with anyone, you write “No collaborators” at the beginning of your document. If asked, you should be able to orally explain your solution to a member of the course staff. • Unless specified, all answers must be justified. • When applicable, your pseudo-code should be commented and indented. • The clarity and presentation of your answers is part of the grading. Be neat! • Violation of all rules above may result in penalties or even absence of grading (Please, refer to the course webpage for a full description of the policy). • Partial answers will receive credits. • The course staff will answer questions about the assignment during office hours or in the online forum at https://osqa.cs.mcgill.ca/. We urge you to ask your questions as early as possible. We cannot guarantee that questions asked less than 24h before the submission deadline will be answered in time. • Late submissions will be accepted until April 19th without penalty. 1. (30 points) Suppose we perform a sequence of n operations on a data structure in which the ith operation costs i if i is an exact power of 2, and 1 otherwise. Use aggregate analysis to determine the amortized cost per operation. 2. (30 points) Professor Toole proposes a new divide-and-conquer algorithm for computing minimum spanning trees, which goes as follows. Given a graph G = (V, E), partition the set V of vertices into two sets V1 and V2 such that |V1 | and |V2 | differ by at most 1. Let E1 be the set of edges that are incident only on vertices in V1 , and let E2 be the set of edges that are incident 1

only on vertices in V2 . Recursively solve a minimum-spanning-tree problem on each of the two subgraphs G1 = (V1 , E1 ) and G2 = (V2 , E2 ). Finally, select the minimum-weight edge in E that crosses the cut (V1 , V2 ), and use this edge to unite the resulting two minimum spanning trees into a single spanning tree. Either argue that the algorithm correctly computes a minimum spanning tree of G, or provide an example for which the algorithm fails. 3. (30 points) When RANDOMIZED-QUICKSORT runs, how many calls are made to the randomnumber generator RANDOM in the worst case? How about in the best case? Give your answer in terms of Θ-notation with a brief justification. 4. (10 points) End-of-class bonus!

COMP 251 - HW5

Page 2 of 2

Winter 2017

COMP 251 Winter 2017 Professor Jerome Waldispuhl

Assignment 5 – Solutions 1. In a sequence of n operations, there are exactly ⌊log2(n)⌋ + 1 exact powers of 2 (i.e.: 1, 2, 4, 8, …, 2⌊𝑙𝑜𝑔2 𝑛⌋ . The total cost is determined by the geometric sum 2 𝑛⌋ 𝑖 ∑⌊𝑙𝑜𝑔 2 = 2⌊𝑙𝑜𝑔2 𝑛⌋+1 – 1 = 2n – 1. All other operations have cost 1, and there 𝑖=0 are exactly n – ⌊log2(n)⌋ – 1 such operations. The total cost of all these operations is 2n – 1 + n – ⌊log2(n)⌋ – 1 = 3n – ⌊log2(n)⌋ – 2 = O(n)  O(1) amortized cost per operation. 2. This algorithm will not always work. Counterexample:

The above graph G = (V, E) has four vertices {v1, v2, v3, v4}, and is partitioned into subsets G1 with V1 = {v1, v2} and G2 with V2 = {v3, v4}. The MST of G1 has weight 5, the MST of G2 has weight 6, and the minimum weight edge crossing the cut (shown in dotted lines above) has weight 2. This algorithm gives a MST of weight 13 with edges {v1v2, v1v4, v3v4}. However, the actual MST of the graph above has weight 11 with edges {v1v2, v1v4, v2v3}. In conclusion, this example shows that the proposed algorithm fails to obtain a MST. Counterexample adapted from http://cs.jhu.edu/~cs363/fall2013/assign7_sln.pdf 3. RANDOM is called once each time RANDOMIZED-PARTITION is called  #calls of RANDOM-PARTITION = #calls to RANDOMIZED-QUICKSORT. The worst-case behavior occurs when the partitioning produces one subproblem of size n–1 and one of size 0 each time it is called.

1

The number of calls to RANDOMIZED-PARTITION is given by the recurrence T(n) = T(n – 1) + T(0) + Θ(1). Since RANDOMIZED-PARTITION is not called on a subproblem of size 0, one can set T(0) = 0  T(n) = T(n – 1) + Θ(1). Therefore, solving the recurrence gives T(n) = Θ(n) in the worst case. The best-case behavior occurs when the partitioning produces two 𝑛 subproblems of size ≤ 2.

The number of calls to RANDOMIZED-PARTITION is given by the recurrence 𝑛 T(n) = 2T(2) + Θ(1).

Applying Master Theorem to the above recurrence, we have a = 2, b = 2, logba = log22 = 1, f(n) = 1 From Case 1, f(n) = O(𝑛𝑙𝑜𝑔2 2−𝜖 ) = O(𝑛1−𝜖 ). One can obtain O(1) when ϵ = 1 – 0 = 1. Therefore, solving the recurrence gives T(n) = Θ(n) in the best case. Conclusion: Θ(n) calls are made to RANDOM in the best and worst cases. Solution adapted from: https://www.eecis.udel.edu/~saunders/courses/621/03f/modelE.pdf

2