intro to computer algorithms lecture 8 phillip g. bradford computer science university of alabama

16
Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

Upload: sydney-cook

Post on 11-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

Intro to Computer Algorithms Lecture 8

Phillip G. BradfordComputer Science

University of Alabama

Page 2: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer 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

Page 3: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

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

Page 4: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

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])

Page 5: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

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

Page 6: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

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])

Page 7: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

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)

Page 8: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

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)

Page 9: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

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]

Page 10: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

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

Page 11: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

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?

Page 12: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

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.

Page 13: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

Full-History Recurrences

Giving T(n) = 2n-1 Check the base cases!

Page 14: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

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

Page 15: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

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!

Page 16: Intro to Computer Algorithms Lecture 8 Phillip G. Bradford Computer Science University of Alabama

Elementary Matrix Multiplication

Row & Column multiplication