Reasoning about Algorithms October 13, 2015 Rice University COMP 382, Spring 2014

advertisement

Reasoning about Algorithms

Rice University

Swarat Chaudhuri

Homework 3 – Due Thursday, October 15, 2015

Please refer to the course website for the full homework policy and options.

Reminders

• Your solutions are due in class. Please also submit a soft copy on the dropbox on Owlspace.

Late homework will not be accepted.

• Collaboration is permitted, but you must write the solutions by yourself without assistance , and be ready to explain them orally to a member of the course staff if asked. You must also identify your collaborators.

Getting solutions from outside sources such as the Web or students not enrolled in the class is strictly forbidden.

• To facilitate grading, please write down your solution to each problem on a separate sheet of paper. Make sure to include all identifying information and your collaborators on each sheet.

• When in doubt, email the TAs or the instructor.

• For problems that require you to provide an algorithm, you must give a precise description of the algorithm, together with an informal proof of correctness and an analysis of its running time.

For some of the algorithms (we specify which ones), you also need to present an implementation in your favorite programming language. You may use algorithms from class as subroutines. You may also use any facts that we proved in class.

1. Assume you have functions f and g such that f ( n ) is O ( g ( n )). Using the definition of big-O complexity, determine whether ( f ( n )) 2 is O (( g ( n )) 2 ). Please give a formal proof if the answer is

“yes”, and a counterexample if the answer is “no”.

2.

(Equivalence testing) You have a collection of n bank cards suspected of being used in fraud.

Each card corresponds to a unique account in the bank. Each account can have many bank cards corresponding to it, and two cards are equivalent if they correspond to the same account.

It is hard to read the account number off a card directly, but you have access to an “equivalence tester” that takes two bank cards and determines whether they are equivalent. So, the only feasible operation you can do with the cards are to take two cards and plug them into the equivalence tester.

The question is: among the set of n cards, is there a set of more than n/ 2 that are all equivalent to each other? Give an algorithm that decides this question using O ( n log n ) invocations of the equivalence tester.

3. Suppose you are given an array point of n points, and that your goal is to identify the shortest distance between any two pairs of points.

Consider the algorithm given in Figure 1. Show that this algorithm has a complexity of Θ(

n

2

), assuming that the function distance executes in constant time.

1

October 13, 2015

COMP 382, Spring 2014

Homework 3

1 minDistance := distance(point[1], point[2]);

2 for i := 1 to n {

3 for j := i + 1 to n {

4 thisDistance := distance(point[i], point[j]);

7

8

5

6

9 }

} if (thisDistance < minDistance) {

} minDistance := thisDistance;

Figure 1: Shortest distance

9

10

11

12

13

14

15

16

17

18

19

20 }

1 procedure Cruel (A[1 . . .

n]) {

2

3 if (n > 1) {

Cruel (A[1 . . .

n/2]);

4

5

Cruel (A[n/2 + 1 . . .

n]);

Unusual (A[1 . . .

n]);

6

7 }

}

8 procedure Unusual (A[1 . . .

n]) { if (n == 2) {

} else {

} if (A[1] > A[2]) swap (A[1], A[2]) for i := 1 to n/4 swap (A[i + n/4], A[i + n/2]);

Unusual (A[1 . . .

n/2]);

Unusual (A[n/2 + 1 . . .

n]);

Unusual (A[n/4 + 1 . . .

3n/4]);

Figure 2: Cruel and unusual sort

4. Solve the following recurrences. You can use the master’s theorem, if applicable:

(a) A ( n ) = 2 A ( n/ 2) + n log n

(b) B ( n ) =

2 n B (

2 n ) +

√ n

(c) C ( n ) = 4 C ( n/ 2) + n

2

5.

(Cruel and unusual sort) Consider the algorithm Cruel

in Figure 2. You should assume

that the size of the input array A is always a power of 2.

(a) Prove that the procedure Cruel sorts the input array A .

(b) Prove that Cruel would not sort correctly if we removed the for-loop from Unusual .

(c) Estimate the running times of Cruel and Unusual .

6.

(Sort detective) Your job in this problem is to take several unknown algorithms, and figure out what they are by experimenting with their computational properties.

2

The MysterySort program (JAR file downloadable from the course website) reads a sequence of numbers from a file and sorts them, printing the total amount of time consumed during the sorting process. It allows you to choose between five sorting algorithms, labeled A through E, corresponding to the following, though not necessarily in that order.

• Insertion sort

• Merge sort

• Selection sort

• Quicksort, with the center of the array as the pivot

• Quicksort, with random pivot

Several of these algorithms would be familiar to you. If not, you can find descriptions of the algorithms online, for example in Wikipedia.

Your job as sort detective is to figure out which algorithm corresponds to each letter.

To start the program, you would run the following command, which asks the machine to run algorithm E on the numbers found in the file named numbers.txt

. (This should work under

Windows, MacOS, or Linux.) prompt% java -jar MysterySort.jar E numbers.txt

2.389ms

E 10000 419 trials

The output of MysterySort , illustrated above, includes four columns. The first column is the important one: It reports the number of milliseconds needed to sort the array. The next two columns simply describe the parameters, including the sort identifier (E) and the size of array sorted. The fourth column mentions the number of trials; because of computers measure short times very poorly, the program repeatedly sorts the array until at least one second has passed, and then it reports the average time per sort. This last number is an artifact of the measurement technique and should be ignored.

In your experiment, you will, of course, write the code needed to generate the input file ( numbers.txt

in the above). The format of the file is numbers followed by newlines—e.g.,

8334

9290

1469

8787

7513

69

5065

8210 is an acceptable file.

You don’t need to submit the code used in your experiments; what you need to submit is a writeup. The writeup should demonstrate that you understand the differences in the sorting algorithms and can identify how those differences are reflected when observing the algorithms at work. Therefore, in addition to identifying the algorithm, your submission must include an explanation of the evidence you used to make your decision. If your experiments are well chosen, this writeup can be quite succinct.

3

Download