Download presentation
Presentation is loading. Please wait.
Published byNelson Butler Modified over 9 years ago
1
15-211 Fundamental Structures of Computer Science April 29, 2003
2
Announcements zHW6 due on Thursday at 11:59pm zFinal exam review on Sunday y4:30-6:00pm in WeH 7500 zOthello tournament on May 7 y4:30-6:00pm in WeH 7500 zFinal exam on May 8 y8:30-11:30am in UC McConomy zSpecial problem session ytomorrow’s recitation!
3
Randomized Algorithms
4
Quicksort, revisited
5
Quicksort idea zChoose a pivot.
6
Quicksort idea zChoose a pivot. zRearrange so that pivot is in the “right” spot.
7
Quicksort idea zChoose a pivot. zRearrange so that pivot is in the “right” spot. zRecurse on each half and conquer!
8
Quicksort algorithm 10547131730222519517134730222105 19 51730222105 1347 105222
9
Quicksort algorithm 10547131730222519 517134730222105 19 51730222105 1347 105222
10
Performance of quicksort zIn the worst case, quicksort runs in O(N 2 ) time. yThis happens when the input is sorted (or “mostly” sorted). zHowever, the average-case running time is O(Nlog N). yHeight of the quicksort tree is expected to be O(log N).
11
Worst-case analysis z“Quicksort has a worst-case running time of O(N 2 ).” zThis means that there is at least one input of size N that will require O(N 2 ) operations.
12
Average-case analysis z“Quicksort has an average-case running time of O(Nlog N).” zThis means that if we run quicksort on all inputs of size N, then on average O(Nlog N) operations will be required for each run.
13
Average case vs. worst case zSo, is quicksort a good algorithm or not? yIn other words, in the real world, do we get the worst-case or the average-case performance? zUnfortunately, in practice, mostly sorted inputs often occur much more often than is statistically expected.
14
Randomized Algorithms Read Chapter 9
15
Randomized quicksort zAs a slight variation of quicksort, let’s arrange for the pivot element to be chosen at random. zThen a sorted input will not necessarily exhibit the O(N 2 ) worst- case behavior. zIndeed, there are no longer any “bad inputs”; there are only “unlucky” choices of pivots.
16
Randomized algorithms zAlgorithms that make use of randomness are called randomized algorithms.
17
Analysis of randomized qsort zConsider taking a single, specific input of size N. yMaybe even a sorted input. zIf we repeatedly run our randomized quicksort on this input, we would expect to get different running times for many of the runs. zBut what would be the average running time?
18
Analysis of randomized qsort zConsider the quicksort tree: 10547131730222519 517134730222105 19 51730222105 1347 105222
19
Analysis of randomized qsort zThe time spent at each level of the tree is O(N). zSo, on average, how many levels? yThat is, what is the expected height of the tree? yIf on average there are O(log N) levels, then randomized quicksort is O(Nlog N) on average.
20
Expected height of qsort tree zAssume that pivot is chosen randomly. zWhen is a pivot “good”? When is it “bad”? 51317193047105222 Probability of a good pivot is 0.5. After good pivot, each child is at most 3/4 size of parent.
21
Expected height of qsort tree zSo, if we descend k levels in the tree, each time being lucky enough to pick a “good” pivot, the maximum size of the k th child is: yN(3/4)(3/4) … (3/4) (k times) y= N(3/4) k zBut on average, only half of the pivots will be good, so yN(3/4) k/2 = 1 yK= 2log 4/3 N = O(log N)
22
Randomized quicksort zSo, for any particular input, we expect randomized quicksort to run in O(Nlog N) time. zThis is referred to as the expected- case running time.
23
Expected running time zWorst-case: yThe time required for the “pathological” input. zAverage-case: yThe time required, averaged over all inputs. zExpected-case: yThe time required, averaged over infinitely many runs on any input.
24
Implementing Randomness
25
Choosing a pivot at random zGiven an array of N elements, we want to pick one of the elements at random. zOne way is to flip a coin multiple times. zAnother is to generate a random number between 0 and N-1. zHow to do these things?
26
Random sequences zNote that generally we need sequences of random choices. zThus, approaches such as reading the system clock or other “background noise” from nature aren’t practical. ySee http://www.fourmilab.ch/hotbits/
27
Random numbers in Java zThe class java.util.Random provides methods for generating sequences of random bits and numbers. java.util.Random() xcreates a random number generator yjava.util.nextBoolean() xreturns the next random bit java.util.nextInt(n) xreturns the next random number
28
Pseudorandom numbers zActually, java.util.Random does not generate true random numbers, but pseudorandom numbers. zPseudorandom numbers appear to be random and have many of the properties of random numbers. yBut they generally the sequences have a (large) period.
29
Linear congruential Method zA well known algorithm for generating random numbers – by D. Lehmer (1951) zTo generate a sequence of pseudorandom numbers x 1, x 2, … yx i+1 = (A x i )mod M yx 0 is the seed value, and should be chosen so that 1x 0 M. A is some constant value yThis will generate a sequence of numbers with a maximum period of M-1. yIf M= 2 31 -1 and A = 48,271, then we get a period of 2 31 -2.
30
Overflow zThe LCG is not entirely practical, because computing Ax i can result in very large numbers that overflow. yJava integers are represented in 32 bits. xRange of -2 31 …2 31 -1. yIf a computation results in a value that is out of range, then overflow occurs. yOverflow affects the randomness of the LCG sequence. yRead page 328 of the text to see how to deal with overflow
31
Other Randomized Algorithms
32
Randomized Primality testing zThere are no known polynomial-time algorithms for testing whether a large integer is prime. zFermat’s Little Theorem yIf p is prime and 0<A<P, ythen A p-1 = 1(mod P) yConverse is not true zHowever, there are polynomial-time randomized algorithms that work with very high probability. yAlgorithm might incorrectly say “prime”. zThe chance of an incorrect answer can be made smaller than the chance of a hardware failure.
33
Thursday zWe will revisit all of the topics we have discussed in the course. zGo to Recitation tomorrow
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.