Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Problem Set Three - Fundamental Algorithms | CS 231, Assignments of Algorithms and Programming

Material Type: Assignment; Class: Fundamental Algorithms; Subject: Computer Science; University: Wellesley College; Term: Fall 2001;

Typology: Assignments

Pre 2010

Uploaded on 08/16/2009

koofers-user-4vk
koofers-user-4vk 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS231 Algorithms Handout # 11
Prof. Lyn Turbak October 5, 2001
Wellesley College
Problem Set 3
Due: Monday, October 15
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 10192731353742
9 13263032404348
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×narray and a number kas 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×narray for k.
2D-Search-a (A, k)
for i1to nBnumber of rows
for j1to nBnumber of columns
if A[i, j] = k then return true Breturn 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 p erforrms binary search on
the ith row of Ato determine if kisintherow;itreturnstrueifkis in the row and false otherwise.
Below is an algorithm that uses Binary-Search to search for kin a 2d-sorted n×narray.
1
pf3
pf4
pf5

Partial preview of the text

Download Problem Set Three - Fundamental Algorithms | CS 231 and more Assignments Algorithms and Programming in PDF only on Docsity!

CS231 Algorithms Handout # 11 Prof. Lyn Turbak October 5, 2001 Wellesley College

Problem Set 3

Due: Monday, October 15

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:

  1. Carefully state your loop invariants. These should express relationships between the param- eters xsi (= xi : xs’i in the general case), ysi, and zsi of the ith invocation of splitTail (i.e., splitTail xsi ysi zsi).
  2. Prove that the loop invariants hold the first time the loop is entered (i.e., splitTail ws [] [] ).
  3. Prove that if the loop invariants are true at the beginning of an iteration (i.e., when a tail- recursive function is entered) they are true at the beginning of the next iteration (i.e., when the function is invoked tail-recursively in the body).
  4. Prove that that the loop terminates (i.e., the base case of the tail recursive function is eventually reached).
  5. Prove that the desired properties hold when the terminating state is reached.

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:

  1. Write a mathematical specification for the merging function that is sufficient for proving (below in part (3)) that msort is a correct sorting algorithm. What the merging function does in general is rather complex; your specification need only focus on the case where the two list arguments of the merging function are sorted.
  1. Prove that merge satisfies your specification for the merging function from part (1). This proof should be by induction. Carefully justify any use of the induction hypothesis. You should consider the “size” of a pairs of lists (ps, qs) to be the sum of the lengths of ps and qs.
  2. Prove that msort satisfies the sort specification. This proof should be by induction. Care- fully justify any use of the induction hypothesis. You may assume that merge satisfies your merging function specification from part (1)

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.

CS231 Problem Set 3

Due Monday, October 15

Name:

Date & Time Submitted:

Collaborators (anyone you worked with on the problem set):

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.

Part Time Score

General Reading

Problem 1 [25]

Problem 2 [20]

Problem 3 [30]

Problem 4 [25]

Extra Credit 1 [10]

Extra Credit 2 [20]

Total