Docsity

Algorithm Design - Homework 1 Solution | ECS 122A, Assignments of Computer Science

Toggle sidebar
bg1
Homework 1 Solutions ECS 122A, Spring 2009
Problem 1. CLRS 1.2-2
Consider the following table:
n InsertionSort : 8n2M ergeSort : 64nlg n
2 32 128
4 128 512
8 512 1,536
16 2,048 4,096
32 8,192 10,240
40 12,800 13,624
41 13,448 14,058
42 14,112 14,495
43 14,792 14,933
44 15,488 15,374
64 32,768 24,576
128 131,072 57,344
512 2,097,152 294,912
Therefore, it becomes clear that ∀n > 43, Merge Sort is a more efficient algorithm than Insertion
Sort. Notice that 8n2= 64nlog nis a transcendental equation, and is therefore only solvable by
numerical approximation methods.
Problem 2. CLRS 2.2-2
Selection-Search(A,v)
1for j←1to length[A]−1
2do smallestElementIndex ←j
3for i←j+ 1 to length[A]
4do if A[i]< A[smallestElementIndex]
5then smallestElementIndex ←i
6Swap-Array-Element(j, smallestE lementIndex)
7
Invariant: At the start of each iteration of the f or loop, A[1, ..., j −1] is sorted.
The algorithm only needs to run for the first n−1 elements, because the algorithm consistently
walks the entire array, looking for the smallest element. Therefore, the largest element bubbles
to the A[n] position. Therefore, when the algorithm terminates, the A[n] element is already the
largest.
Best Case: For Selection-Search, the best case running time occurs when the input is already
sorted. Then line 4 is never true and line 5 is never executed. In the following equation, cirefers
1
pf3

Partial preview of the text

Download Algorithm Design - Homework 1 Solution | ECS 122A and more Assignments Computer Science in PDF only on Docsity!

Homework 1 Solutions ECS 122A, Spring 2009

Problem 1. CLRS 1.2-

Consider the following table:

n InsertionSort : 8n^2 M ergeSort : 64n lg n 2 32 128 4 128 512 8 512 1 , 536 16 2 , 048 4 , 096 32 8 , 192 10 , 240 40 12 , 800 13 , 624 41 13 , 448 14 , 058 42 14 , 112 14 , 495 43 14 , 792 14 , 933 44 15 , 488 15 , 374 64 32 , 768 24 , 576 128 131 , 072 57 , 344 512 2 , 097 , 152 294 , 912

Therefore, it becomes clear that ∀n > 43, Merge Sort is a more efficient algorithm than Insertion Sort. Notice that 8n^2 = 64n log n is a transcendental equation, and is therefore only solvable by numerical approximation methods.

Problem 2. CLRS 2.2-

Selection-Search(A,v) 1 for j ← 1 to length[A] − 1 2 do smallestElementIndex ← j 3 for i ← j + 1 to length[A] 4 do if A[i] < A[smallestElementIndex] 5 then smallestElementIndex ← i 6 Swap-Array-Element(j, smallestElementIndex) 7

Invariant: At the start of each iteration of the f or loop, A[1, ..., j − 1] is sorted.

The algorithm only needs to run for the first n − 1 elements, because the algorithm consistently walks the entire array, looking for the smallest element. Therefore, the largest element bubbles to the A[n] position. Therefore, when the algorithm terminates, the A[n] element is already the largest. Best Case: For Selection-Search, the best case running time occurs when the input is already sorted. Then line 4 is never true and line 5 is never executed. In the following equation, ci refers

to the cost of executing line i.

T (n) = c 1 (n) + c 2 (n − 1) + c 3

∑^ n

j=

j + c 4

n∑− 1

j=

j + c 6 (n − 1)

= c 1 (n) + c 2 (n − 1) + c 3 (n(n + 1)/2) + c 4 ((n(n + 1)/2) − n) + c 6 (n − 1) = αn^2 + βn + γ

where α, β, γ are constants, independent of n. Worse Case: The worst case running time is only slightly different from the best case. The worst case occurs when the input for Selection-Search is NOT in sorted order. Therfore line 4 is always true and line 5 is always executed. The running time T (n) differs from above by only one term, and is still quadratic.

T (n) = c 1 (n) + c 2 (n − 1) + c 3

∑^ n

j=

j + c 4

n∑− 1

j=

j + c 5

n∑− 1

j=

j + c 6 (n − 1)

= T (n) = c 1 (n) + c 2 (n − 1) + c 3 (n(n + 1)/2) + c 4 ((n(n + 1)/2) − n) + c 5 ((n(n + 1)/2) − n) + c 6 (n − 1) = α 1 n^2 + β 1 n + γ 1

where α 1 , β 1 , γ 1 are constants, independent of n. There, both the best case and the worst case running time are O(n^2 ).

Problem 3. CLRS 2.3-

Proof by Induction: Assume that ∀n, ∃k : n = 2k Base Case: Let k = 1. Therefore n = 2^1 = 2 and T (n) = T (2) = 2 = 2 lg 2 = 2. Inductive Hypothesis: Assume for n = 2k, T (n = 2k) = n lg n = 2k^ lg 2k. Inductive Step: show that for n = 2k+1, T (n = 2k+1) = 2k+1^ lg 2k+1:

T (2k+1) = 2 T (2k+1/2) + 2k+ = 2 T (2k) + 2k+ = 2(2k^ lg 2k) + 2k+ = 2 k+1^ lg 2k^ + 2k+ = 2 k+1(lg 2k^ + 1) = 2 k+1(lg 2k^ + lg 2) = 2 k+1(lg 2k2) = 2 k+1(lg 2k+1).

Problem 4

a. First, note that 2n − 2 = 2(n − 1). The simplest algorithm starts with the first element as the minumum and then walks the array, comparing each element with the current minimum and updating if necessary. If the length of the array is n, this takes (n − 1) comparisons because the first element is never compared with itself. To find the maximum, the same algorithm is applied, only checking for the maximum instead of the minimum element. Therefore, the total number of comparisons is (n − 1) + (n − 1) = 2(n − 1) = 2n − 2.

b. Since the length of our array is a power of two, consider the following divide and conquer algorithm: