CSc 345: Homework Assignment 2

Transcription

CSc 345: Homework Assignment 2
CSc 345: Homework Assignment 2
Assigned: Tuesday, February 3 2015
Due: 2:00 PM, Tuesday, February 17 2015
Clear, neat and concise solutions are required in order to receive full credit so revise your work carefully before
submission, and consider how your work is presented. If you cannot solve a particular problem, state this clearly
in your write-up, and write down only what you know to be correct. For involved proofs, first outline the argument
and the delve into the details.
1. (10 pts) Show the operation of MergeSort and HeapSort on the following input: 6,4,7,2,9,1,3,5,8.
2. (10 pts)
(a) Let A be an array of n distinct integers. Suppose A has the following property: there exists an index
1 ≤ k ≤ n such that A[1], . . . , A[k] is an increasing sequence and A[k], . . . , A[n] is a decreasing sequence.
Design and analyze an efficient algorithm for finding k.
(b) You have two sorted arrays A, B. The size of the first array A is m and it contains exacty m numbers.
The second array B is of size m + n and it has only n numbers in the first n positions. Design and
analyze an algorithm to merge A and B into B such that B is sorted, under the constraint that you
have only constant additional memory (i.e., you can use only O(1) storage in the form of temporary
variables).
3. (10 pts) An n × n array A has the property that each row is sorted from left to right and and each column
is sorted from top to bottom. Consider the following algorithm for searching for a number x in A:
Algorithm 2D-Binary-Search(A, x)
1. if n = 1
2.
if A[1][1] = x
3.
then return TRUE
4.
else return FALSE
5. if A[n/2][n/2] = x
6.
then return TRUE
7. if A[n/2][n/2] < x
8.
then return 2D-Binary-Seach(A[1 . . . n/2][n/2 + 1 . . . , n], x)
∨ 2D-Binary-Seach(A[n/2 + 1 . . . , n][1 . . . n/2], x)
∨ 2D-Binary-Seach(A[n/2 + 1 . . . , n][n/2 + 1 . . . , n], x)
9.
else return 2D-Binary-Seach(A[1 . . . n/2][1 . . . n/2], x)
∨ 2D-Binary-Seach(A[1 . . . n/2][n/2 + 1 . . . , n], x)
∨ 2D-Binary-Seach(A[n/2 + 1 . . . , n][1 . . . n/2], x)
(a) Prove that the Algorithm 2D-Binary-Seach correctly determines whether A contains x.
(b) Analyze the running time of 2D-Binary-Search.
4. (10 pts) Consider the following algorithm for sorting an array A of n numbers:
Algorithm Three Part Sort(A)
1. n ← |A|
2. if n ≤ 4
3.
then Sort A using insertion sort
4.
else
5.
Three Part Sort(A[1 . . . d 2n
3 e]) // Sort first two-third of A
6.
Three Part Sort(A[b n3 c + 1 . . . n]) // Sort last two-third of A
7.
Three Part Sort(A[1 . . . d 2n
3 e]) // Sort first two-third of A
1
(a) Does the algorithm correctly sort any input array A? If yes, prove its correctness, if no, give an example
of A, where the algorithm fails.
(b) Analyze the running time of the algorithm.
5. (10 pts) Consider an array A with n numbers. An odd-pair in A is a pair of two indices (i, j), such that
1 ≤ i < j ≤ n and A[i] > A[j].
(a) What is the maximum number of odd-pairs in an array of size n? What is the property of the array
that achieves this maximum number?
(b) What is relationship between the running time of InesertionSort and the number of odd-pairs in the
input array.
(c) Design an algorithm that counts the number of odd-pairs in an arry in O(n log n) time.
6. (10 pts) Consider the following modification of MergeSort. Instead of dividing the input array A into two
parts, we divide A into k parts, each with size n/k. We then sort each of the k parts recursively and finally
merge these k sorted lists. Call this algorithm generalized merge sort.
(a) Design an algorithm to merge k sorted list with size n/k each, into one sorted list in O(n log k) time.
√
(b) For k = n, write the recurrence relation for the running time of generalized merge sort, using your
efficient algorithm for merging from part (a). Solve the recurrence to compute the Θ-bound for the
running time.
7. (10 pts) The following questions deal with the heap and priority queue data structures:
(a) Is a sorted array a MinHeap?
(b) Is an array representing a MaxHeap sorted in non-increasing order?
(c) How many leaves are there in a heap with n elements?
(d) Given that the height of a heap is the number of edges on the longest root-to-leaf path, what are the
smallest number of elements and the greatest number of elements in a heap of height h?
8. (10 pts)
(a) Design and analyze an efficient non-recursive procedure for Max Heapify.
(b) For some sorting algorithms there is a significant difference between the best-case running time and
the worst-case running time. What are the best-case and worst-case running times for HeapSort on a
list with n numers?
9. (10 pts) Consider the heap A = h18, 14, 10, 3, 9, 7, 4, 2, 0, 5, 3, 6i.
(a) Illustrate the operation of HEAP-EXTRACT-MAX on A.
(b) Illustrate the operation of MAX-INCREASE-KEY(A, 9, 17) on A.
10. (10 pts)
(a) Let A and B be two n-element sorted arrays. Design and analyze an O(log n) time algorithm to find
the median of A ∪ B.
(b) Assuming that you have an algorithm M to find the median of an (unsorted) array A with n numbers
in O(n) time, design a simple O(n log n)-time algorithm to sort A, using M as a subroutine.
Extra Credit: During a visit to Mt. Lemmon Skiway you noticed a long icicle on the ski lift. At the end
of the day the icicle had fallen down and broken into 3 pieces which formed a nice-looking isosceles triangle. On
your way down the Catalina highway, you tried to calculate the probability that the three pieces form a triangle
(not just isosceles, but any triangle). 45 minutes later you were still not sure about the right answer. So, what
is it?
2