Download Problem 1 - Suraj @ LUMS

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Large numbers wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Algorithm wikipedia , lookup

Addition wikipedia , lookup

Algorithm characterizations wikipedia , lookup

Halting problem wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
Page 1 of 4
Problem 1:
Suppose that you want to sort n numbers, each of which is either 0 or 1. Write names of two
asymptotically efficient algorithms/techniques for this problem with running time.
[4]
Call partition procedure used in quick sort once or use counting sort
O(n) in each case.
Problem 2:
Insertion sort can be expressed as a recursive procedure as follows. In order to sort A[1 … n],
we recursively sort A[1 … n -1] and then insert A[n] into the sorted array A[1 …n - 1].
Write a recurrence for the running time of this recursive version of insertion sort and solve it
using substitution.
[2+3]
Assume T(n) be the time to sort A[1…n].
Then by using the recursive version
T(1)=1
T(n)=T(n-1) + c(n-1)
T(n)=T(n-2) + c(n-2)+c(n-1) and so on
T(n)=O(n2)
Solution of the recurrence relation is O(n2).
Problem 3:
Assume that we are given as input n pairs of items, where the first item is a number and the
second item is one of three colors (red, blue, or yellow). Further, assume that the items are sorted
by number. Give an O(n) algorithm to sort the items by color (all reds before all blues before all
yellows) such that the numbers for identical colors stay sorted.
For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red),
(1,blue), (4,blue), (6,yellow).
[4]
First scan the pairs and write those pairs with red color then scan the pairs and write pairs
with blue color and then with yellow.
Or any other correct O(n) algorithm.
Problem 4:
Take as input a sequence of 2n real numbers. Design an O(nln(n)) algorithm that partitions the
numbers into n pairs, with the property that the partition minimizes the maximum sum of a pair.
For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)),
((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8).
Thus the third partition has 10 as its maximum sum, which is the minimum over the three
partitions.[Hint: Sorting the numbers may help in this problem]
[7]
Sort the numbers using any O(nln(n)) algorithm
Page 2 of 4
Consider
first pair consisting of first and last numbers
second pair consisting of second and second last numbers and so on.
Or any valid O(nln(n)) algorithm
Problem 5:
What is the maximum and minimum number of times that the largest element could be moved
during the execution of Quicksort? Explain your answer with an example.
To answer these questions consider Partition procedure given in your book.
[5]
Max : n -1
Min: 0
[5,4,3,2,1]
[1,2,3,4,5]
Problem 6:
Consider a set S of n  2 distinct numbers given in unsorted order. Give an algorithm to
determine two distinct numbers x and y in the set S that satisfy a stated condition.
In as few words as possible, describe your algorithm and justify its running time.
To keep your answers brief, use algorithms from the book as subroutines.
a) In O(n) time, determine x, y in S such that |x - y|  |w - z| for all w, z in S.
b) In O(nlg(n)) time, determine x, y in S such that x  y and |x - y|  |w - z| for all w, z
in S such that w  z.
[3+4]
a) x, y are the two farthest apart. Find min, max, each in O(n) time.
b) x, y are the two closest together. First sort with O(nlgn) algo, then scan the sorted
numbers for the two adjacent numbers with the minimum difference in O(n).
Page 3 of 4
Problem 7:
The mode of a set of numbers is the number that occurs most frequently in the set. The set
(4,6,2,4,3,1) has a mode of 4.
a) Give an efficient and correct algorithm to compute the mode of a set of n numbers.
b) Suppose we know that there is an (unknown) element that occurs n/2+1 times in the
set. Give a worst-case linear-time algorithm to find the mode.
[4+6]
Soln:
a)
Sort the numbers using O(nlgn) algorithm.
Scan the sorted array and track the most frequent occurring number.
b)
Divide recursive the set by doing n / 2 pairwise comparisions and only keeping one
representative of the pairs which are equal.
For example (1,1,1,1,2,2) Divide the set in 3 pairs as follows:
1,1
equal take 1
1,1
equal take 1
2,2
equal take 2
Now divide (1,1,2) into the following pairs
1,1 take 1
2
discard
1 is the answer.
Consider another permutation of the above problem: (1,2,1,1,2,1)
1,2
1,1
take 1
2,1
1 is the answer.
1,1
2,2
1,1
Consider another permutation of the above problem: (1,1,2,2,1,1)
take 1
take 2
take 1
Now divide (1,2,1) into the following pairs
discard
1,2
1
1 is the answer.
Page 4 of 4
Problem 8:
Consider the code for BuildHeap, which operates on a heap stored in an array A[1…n]:
BuildHeap(A)
1 heap-size[A] ← length[A]
2 for i ←⌊length[A]/2⌋downto 1
3
doHEAPIFY(A, i)
a) Show how this procedure can be implemented as a recursive divide-and-conquer procedure
BuldHeap(A, i), where A[i] is the root of the subheap to be build. To build the entire heap,
we would call BuildHeap(A, 1).
b) Give a recurrence that describe the worst-case running time of your procedure.
c) Solve the recurrence using the master theorem given in chapter 3 in your book.
[4+2+2]
Soln:
BuildHeap(A, i)
If i<= length [A]/2
Then BuildHeap(A, 2i)
BuildHeap(A, 2i + 1)
Heapify(A,i)
T(n) = 2 T(n/2) + O(lgn)
T(n) = theta(n) case 1 of master theorem