



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Assignment; Class: Fundamental Algorithms; Subject: Computer Science; University: Wellesley College; Term: Fall 2001;
Typology: Assignments
1 / 7
This page cannot be seen from the preview
Don't miss anything!
CS231 Algorithms Handout # 11 Prof. Lyn Turbak October 5, 2001 Wellesley College
Reading: Handout 10 (Induction, Loop Invariants, and List Sorting).
Required Problems: You should write up and turn in the solutions to the problems listed below: The points awarded per problem are given in brackets.
Problem 1 [25]: 2D-Searching Call a 2-dimensional array of integers 2d-sorted if each row is sorted from low to high value and each column is also sorted from low to high value. Here is an example of a 2d-sorted 8 × 8 array named B:
2 4 7 11 12 13 19 23
11
3 8 14 16 17 24 25 27 6 10 19 27 31 35 37 42 9 13 26 30 32 40 43 48 15 29 34 41 42 45 50 14 17 33 37 44 49 51 61 18 22 35 38 46 53 57 63 21 25 39 42 47 54 60 64
The notation B[i, j] refers to the element in the ith row and jth column of B, where row and column indices start at 1. For example, B[3, 2] is 10. In this problem, we will consider various algorithms for determining whether a 2d-sorted n × n array contains a given number k. The algorithms will be expressed as a function 2D-Search that takes a 2d-sorted n × n array and a number k as arguments and returns a boolean that is true if k is in the array and is false otherwise.
a. [4] Below is a straightforward iterative algorithm for searching a 2d-sorted n × n array for k.
2D-Search-a (A, k) for i ← 1 to n B number of rows for j ← 1 to n B number of columns if A[i, j] = k then return true B return exits the function immediately return false
Give the worst-case running time of this algorithm as a function of n. Justify your answer.
b. [4] Suppose there is a function Binary-Search(A, i, k) that perforrms binary search on the ith row of A to determine if k is in the row; it returns true if k is in the row and false otherwise. Below is an algorithm that uses Binary-Search to search for k in a 2d-sorted n × n array.
2D-Search-b (A, k) for i ← 1 to n B number of rows if Binary-Search(A, i, k) then return true return false
Give the worst-case running time of this algorithm as a function of n. Justify your answer.
c. [5] Another way to search for k in a 2d-sorted n × n array is to use divide and conquer to generalize Binary-Search to work on a 2d-sorted array A. Here’s the idea: suppose that p is the value in the array at A[n/ 2 , n/2]. (See the following picture. For simplicity, assume that n is a power of 2 throughout this part.)
n/
n/2 p
W X
Y Z
Then if k = p, the search terminates with true; if k < p, the search continues in the three square quadrants W , X, and Y ; and if k > p, the search continues in the three square quadrants X, Y , and Z.
Give the worst-case running time of this algorithm as a function of n. Use the recursion tree method of solving recurrence equations to justify your answer.
d. [10] Develop an a Θ(n) worst-case running time algorithm that implements 2D-Search on a 2d-sorted n × n array. Give pseudocode for your algorithm, and justify why it is Θ(n).
Hint: Any number k can be used to divide a 2d-sorted array into two parts: one consisting of the elements ≤ k, and the other consisting of the elements > k. For example, the following figure shows a thick line separating the two parts for the sample array B and k = 20.
2 4 7 11 12 13 19 23
11
3 8 14 16 17 24 25 27 6 10 19 27 31 35 37 42 9 13 26 30 32 40 43 48 15 29 34 41 42 45 50 14 17 33 37 44 49 51 61 18 22 35 38 46 53 57 63 21 25 39 42 47 54 60 64
e. [2] Rank the algorithms from parts a through d by running time. An algorithm with running time f should appear above an algorithm with running time g if f = ω(g), and should appear on the same line if f = Θ(g).
Write a formal proof that split2 satisfies the specification of a splitting function. Your proof should be based on the method of loop invariants. Recall that this requires the following steps:
Problem 4 [25]: Merge Sort Proofs Recall the definition of the Haskell merge sorting function msort presented in class:
msort [] = [] msort [x] = [x] msort xs = merge (msort ys, msort zs) where (ys,zs) = split xs
merge (ps, []) = ps merge ([], qs) = qs merge (p : ps, q : qs) | p <= q = p : (merge (ps, q : qs)) | otherwise = q : (merge (p : ps, qs))
The definition of the split function is not given. You may assume that it is any correct imple- mentation of the split specification given in the previous problem.
a. [20] In this part, you are to write a complete proof that msort is a correct sorting algorithm. That is, you must show that msort satisfies the following sort specification:
Let qs be the result of sort(ps). Then: (sort1) sorted (qs). I.e., if qs = [q 1 , q 2 ,... , qk], then xi ≤ xi+1 for all i ∈ [1 .. k − 1]. (sort2) elts(qs) =bag elts(ps)
To complete this proof, you must do the following:
b. [5]
The definition of msort contains the line:
msort [x] = [x]
Would the definition of msort still be a correct sorting algorithm if this line were deleted from the definition? If yes, explain what would need to be changed in the above proof. If no, explain what goes wrong with the above proof.
Problem Set Header Page Please make this the first page of your hardcopy submission.
In the Time column, please estimate the time you spend on the parts of this problem set. Please try to be as accurate as possible; this information will help me design future problem sets. I will fill out the Score column when grading your problem set.