intro to computer algorithms lecture 8 phillip g. bradford computer science university of alabama
TRANSCRIPT
Intro to Computer Algorithms Lecture 8
Phillip G. BradfordComputer Science
University of Alabama
Announcements
Colloquia on Monday 22-Sept David M. Mount presents:
“Approximation Algorithms for Clustering”
At 11:00 in 108 Houser Advisory Board’s Industrial Talk
Series http://www.cs.ua.edu/9IndustrialSeries.shtm
Quick Sort
Well studied algorithm Empirically beats Merge-Sort
Partition Based Sort a[0], a[1], …, a[s], a[s+1], …, a[n-
1] Where a[i] < a[s] for all i: s>i>0 And a[j] > a[s] for all j: n-1>j>s
Quick Sort Input unsorted list a[0, …, n-1] Output sorted list a[0, …, n-1] l0; rn-1 QSort(a[l,…,r]) If l< r then
spartition(a[l, …, r]) QSort(a[l, …, s-1]) QSort(a[s+1, …, r])
Partition Given a[l,…,r]; pa[l]; il; jr+1; Repeat
Repeat ii+1 until a[i] > p Repeat jj-1 until a[j] < p Swap(a[i], a[j])
Until i>j; Swap(a[i], a[j]); swap(a[l], a[j]) Return j
Partition Convince me this works!
Start at position a[2], why? Why don’t we start with a[r+1] ??? The expression “until i>j” can
terminate If i=j or if i>j. Say i=j, the both a[i]>p and a[i]<p
Thus, a[i]=p. So, swap( a[l], a[j]) puts the pivot in the correct position
Say i>j, then p<a[j] and a[i]>p Must undo the last Swap(a[i],a[j])
Partition & Q-Sort
What does it cost? Fundamental operation: comparisons
O(r-l) = O(n) Worst case: Really n+1 if i & j cross
So Q-Sort Best-case T(n) = 2T(n/2) + n Master Theorem: T(n) = O(n log n)
Q-Sort Worst-Case
Unbalanced partition Why is this the worst?
T(n) = T(n-1) + n T(1) = 1
So, we know that T(n) = (n2)
Q-Sort Average-Case
Ta(n) = [(n+1) + Ta(s) + Ta(n-s-1)]/n
Where the index is s=0 to n-1. Base: Ta(0)=0 and Ta(1) = 0. The 1/n factor is based on the
assumption that the split can occur uniformly at any place in a[0,..,n-1]
Hints for the Q-Sort HW question
Important fact: Hn = 1+1/2+1/3+…+1/n Hn = ln n + What about Hn – 1 – ½ -1/3 ?
Ta(n) = n+1+(2/n)Ta(s), s=0 to n-1 Giving
nTa(n) = n(n+1)+ 2 Ta(s), s=0 to n-1
Hints for the Q-Sort HW question
Eventually getting something like Ta(n)/n < 2(1/n+1/(n-1)+ … + ¼) +
Ta(2)
What does this give?
Full-History Recurrences
Elimination of history technique T(n) = T(0) + … + T(n-1) with T(0) = 1
Consider, for n>2 T(n-1) = T(0) + … + T(n-2), with T(0)
= 1 Combining these
T(n) – T(n-1) = T(n-1) Giving T(n) = 2T(n-1), for n>2.
Full-History Recurrences
Giving T(n) = 2n-1 Check the base cases!
Integer and Matrix Multiplication Cost of multiplying two n-digit integers
O(n2) digit operations Can we do any better? 23 = 2*101 + 3*100 14 = 1*101 + 4*100 23*14 = (2*1)102 + (3*1+2*4)101 +
(3*4)100 Four multiplications Powers of 10 can be done via shifting
Integer and Matrix Multiplication
Note: (3*1)+(2*4) = (2+3)*(1+4) –(2*1)-(3*4)
Because: (a0+b0)*(a1+b1) = a0a1+a0b1+b0a1+b0b1 We want: a0b1+b0a1
Subtract off a0a1+ b0b1 We needed a0a1+ b0b1 anyway!
Elementary Matrix Multiplication
Row & Column multiplication