|
CS 3343/3341 Analysis of Algorithms Spring 2012 Recitation 6 Recurrences, etc. Complete Answers |
| Recurrences, solution by Master Method, Examples | |||||||
|---|---|---|---|---|---|---|---|
| Recurrence | a | b | logb(a) | nlogb(a) | f(n) | Case | Behavior |
| T(n) = 3 T(n / 2) + n2 | 3 | 2 | log2(3) = 1.585 | n1.585 | n2 | 3 | Θ(n2) |
| T(n) = 2 T(n / 2) + n2 | 2 | 2 | log2(2) = 1 | n1 | n2 | 3 | Θ(n2) |
| T(n) = 64 T(n / 4) + √n | 64 | 4 | log4(64) = 3 | n3 | n0.5 | 1 | Θ(n3) |
| T(n) = 9 T(n / 3) + n2 log(n) | 9 | 3 | log3(9) = 2 | n2 | n2 log(n) | none | Master fails |
| T(n) = 32 T(n / 2) + n2 log(n) | 32 | 2 | log2(32) = 5 | n5 | n2 log(n) | 1 | Θ(n5) |
| T(n) = 125 T(n / 5) + 1 | 125 | 5 | log5(125) = 3 | n3 | n0 = 1 | 1 | Θ(n3) |
| T(n) = 16 T(n / 4) + n4 | 16 | 4 | log4(16) = 2 | n2 | n4 | 3 | Θ(n4) |
| T(n) = 2 T(n / 4) + √n | 2 | 4 | log4(2) = 1/2 | n1/2 = √n | √n | 2 | Θ(√n log(n)) |
| T(n) = 2 T(n / 4) + √n log(n) | 2 | 4 | log4(2) = 1/2 | n1/2 = √n | √n log(n) | none | Master fails |
main: heapsize:12, heap: 15 13 9 5 12 8 7 4 0 6 2 1 maxHeapify: heapsize:11, heap: 1 13 9 5 12 8 7 4 0 6 2 maxHeapify: heapsize:11, heap: 13 1 9 5 12 8 7 4 0 6 2 maxHeapify: heapsize:11, heap: 13 12 9 5 1 8 7 4 0 6 2 maxHeapify: heapsize:11, heap: 13 12 9 5 6 8 7 4 0 1 2 Max of heap: 15 main: heapsize:11, heap: 13 12 9 5 6 8 7 4 0 1 2 |
main: heapsize:12, heap: 15 13 9 5 12 8 7 4 0 6 2 1 maxHeapInsert: heapsize:12, heap: 15 13 9 5 12 8 7 4 0 6 2 1 heapIncreaseKey: heapsize:13, heap: 15 13 9 5 12 8 7 4 0 6 2 1 -2000000000 heapIncreaseKey: heapsize:13, heap: 15 13 9 5 12 8 7 4 0 6 2 1 10 heapIncreaseKey: heapsize:13, heap: 15 13 9 5 12 10 7 4 0 6 2 1 8 heapIncreaseKey: heapsize:13, heap: 15 13 10 5 12 9 7 4 0 6 2 1 8 main: heapsize:13, heap: 15 13 10 5 12 9 7 4 0 6 2 1 8 |
// PQueueInt.java: priority queue of integers
public class PQueueInt {
private int[] A;
private int heapsize;
public PQueueInt(int[] Ap, int hs) {
A = Ap;
heapsize = hs;
}
public int qSize() { return heapsize; }
public int heapMaximum() {
if (heapsize < 1) {
System.out.println("Empty Queue");
System.exit(0);
}
return A[1];
}
public int heapExtractMax() {
if (heapsize < 1) {
System.out.println("Heap Underflow");
System.exit(0);
}
int max = A[1];
A[1] = A[heapsize];
heapsize--;
maxHeapify(1);
return max;
}
private int parent(int i) {return i/2;}
private int left (int i) {return 2*i;}
private int right (int i) {return 2*i + 1;}
private void exchange(int i, int largest) {
int temp = A[i];
A[i] = A[largest];
A[largest] = temp;
}
private void maxHeapify(int i) {
printheap("maxHeapify");
int l = left(i);
int r = right(i);
int largest;
if (l <= heapsize && A[l] > A[i])
largest = l;
else largest = i;
if (r <= heapsize && A[r] > A[largest])
largest = r;
if (largest != i) {
exchange(i, largest);
maxHeapify(largest);
}
}
| public void maxHeapInsert(int key) {
if (heapsize >= A.length - 1) {
System.out.println("Heap Overflow");
System.exit(1);
}
printheap("maxHeapInsert");
heapsize++;
A[heapsize] = -2000000000;
heapIncreaseKey(heapsize, key);
}
void heapIncreaseKey(int i, int key) {
if (key < A[i]) {
System.out.println("New key < Current");
System.exit(2);
}
printheap("heapIncreaseKey");
A[i] = key;
printheap("heapIncreaseKey");
while (i > 1 && A[parent(i)] < A[i]) {
exchange(i, parent(i));
i = parent(i);
printheap("heapIncreaseKey");
}
}
public void printheap(String id) {
System.out.print(id + ": heapsize:" +
heapsize + ", heap: ");
for (int i = 1; i <= heapsize; i++) {
System.out.print(A[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] A =
{0,15,13,9,5,12,8,7,4,0,6,2,1,-1,-1,-1};
int heapsize = 12;
PQueueInt pq = new PQueueInt(A, heapsize);
pq.printheap("main");
// do one of the other of the two below:
System.out.println("Max of heap: " +
pq.heapExtractMax()); // Problem 2
// pq.maxHeapInsert(10); // Problem 3
pq.printheap("main");
}
}
|
