hw2sol - CPSC 629: Analysis of Algorithms, Fall 2003...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CPSC 629: Analysis of Algorithms, Fall 2003 Solutions to Homework 2 Solution to 1 (17.1-3) f ( n ) = n + b lg n c X i =1 (2 i- 1) = n- b lg n c + b lg n c X i =1 2 i < n- b lg n c + 2 n < 3 n. Hence, amortized cost is 3 per operation. Solution to 2 (17.2-2) Charge 3 for each operation. For operation i , if i is not a power of 2, the cost is paid by its charge with 2 extra credits. If i = 2 j for some j ∈ N , then the extra credits paid by operation 2 j- 1 + 1 ,..., 2 j- 1 is 2(2 j- 1- 1), which, together with the 3 credit of the current operation, is enough for operation i . Solution to 3 (17.3-2) Define the potential function Φ( D ) = 0, and Φ( D i ) = 2 i- 2 1+ b lg i c for i > 0. For operation 1, b c i = c i + Φ( D i )- Φ( D i- 1 ) = 1 + 2- 2 1+ b lg 1 c- 0 = 1 . For operation i ( i > 1), if i is not a power of 2, then b c i = c i + Φ( D i )- Φ( D i- 1 ) = 1 + 2 i- 2 1+ b lg i c- [2( i- 1)- 2 1+ b lg( i- 1) c ] = 3 . If i = 2 j for some j ∈ N , then b c i = c i + Φ( D i )- Φ( D i- 1 ) = i + 2 i- 2 1+ j- [2( i- 1)- 2 1+ j- 1 ] = i + 2 i- 2 i- 2 i + 2 + i = 2 . Thus, the amortized cost is 3 per operation. Solution to 4 (17.3-3) Let the potential function be Φ( D i ) = ∑ i k =1 lg k . Then the amortized cost for INSERT is b c i = c i + Φ( D i )- Φ( D i- 1 ) ≤ lg i + i X k =1 lg k- i- 1 X k =1 lg k = 2lg i ∈ O (lg n ) . The amortized cost for EXTRACT-MIN is b c i = c i + Φ( D i )- Φ( D i- 1 ) ≤ lg i + 1 + i- 1 X k =1 lg k- i X k =1 lg k = 1 ∈ O (1) . Solution to 5 (17.4-3) For a TABLE-DELETE operation without contraction, the change of potential value before and after the operation is 2 or -2. Therefore, the amortized cost is b c i = c i + Φ( D i )- Φ( D i- 1 ) ≤ 1 + 2 = 3 ∈ O (1) . 1 If a TABLE-DELETE operation triggers contraction, then b c i = c i + Φ( D i )- Φ( D i- 1 ) ≤ num [ T i ] + 1 + | 2 · num [ T i ]- size [ T i ] | - | 2 · num [ T i- 1 ]- size [ T i- 1 ] | = num [ T i ] + 1 + 0- | 2 · num [ T i ]- 3 · num [ T i ] | = 1 ∈ O (1) . Solution to 6 (17-2) 1. To perform a SEARCH on this data structure, we need to perform binary search in the sorted arrays. In worst case, we need to search all the arrays to find an element. Start from the largest array, do a binary search, if the element is not found, go to the next largest array, and so on, until the element is found or all arrays are searched. The worst case running time is ∑ k- 1 i =0 i · n i ∈ O (lg 2 n ). Remark: To search in decreasing order of array size gives better performances in some cases, but the improvement...
View Full Document

Page1 / 4

hw2sol - CPSC 629: Analysis of Algorithms, Fall 2003...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online