DOC PREVIEW
Penn CIT 594 - Randomized Algorithms

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Randomized AlgorithmsA short list of categoriesRandomized algorithmsPseudorandom numbersGenerating random numbersGetting (pseudo)random numbers in JavaGaussian distributionsShuffling an arrayChecking randomnessRandomly choosing N thingsStupidSortAnalyzing StupidSortZero-sum two-person gamesApproximating an optimal strategyExample of random playThe 0-1 knapsack problemImproving the knapsack solutionQueueing problemsConclusionsThe EndJan 14, 2019Randomized Algorithms2 Also known as Monte Carlo algorithms or stochastic methodsA short list of categoriesAlgorithm types we will consider include:Simple recursive algorithmsBacktracking algorithmsDivide and conquer algorithmsDynamic programming algorithmsGreedy algorithmsBranch and bound algorithmsBrute force algorithmsRandomized algorithms3Randomized algorithmsA randomized algorithm is just one that depends on random numbers for its operationThese are randomized algorithms:Using random numbers to help find a solution to a problemUsing random numbers to improve a solution to a problemThese are related topics:Getting or generating “random” numbersGenerating random data for testing (or other) purposes4Pseudorandom numbersThe computer is not capable of generating truly random numbersThe computer can only generate pseudorandom numbers--numbers that are generated by a formulaPseudorandom numbers look random, but are perfectly predictable if you know the formulaPseudorandom numbers are good enough for most purposes, but not all--for example, not for serious security applicationsDevices for generating truly random numbers do existThey are based on radioactive decay, or on lava lamps“Anyone who attempts to generate random numbers by deterministic means is, of course, living in a state of sin.” —John von Neumann5Generating random numbersPerhaps the best way of generating “random” numbers is by the linear congruential method:r = (a * r + b) % m;where a and b are large prime numbers, and m is 232 or 264The initial value of r is called the seedIf you start over with the same seed, you get the same sequence of “random” numbersOne advantage of the linear congruential method is that it will (eventually) cycle through all possible numbersAlmost any “improvement” on this method turns out to be worse“Home-grown” methods typically have much shorter cyclesThere are complex mathematical tests for “randomness” which you should know if you want to “roll your own”6Getting (pseudo)random numbers in Javaimport java.util.Random;new Random(long seed) // constructornew Random() // constructor, uses System.timeInMillis() as seedvoid setSeed(long seed)nextBoolean()nextFloat(), nextDouble() // 0.0 < return value < 1.0nextInt(), nextLong() // all 232 or 264 possibilitiesnextInt(int n) // 0 < return value < nnextGaussian() Returns a double, normally distributed with a mean of 0.0 and a standard deviation of 1.07Gaussian distributionsThe Gaussian distribution is a normal curve with mean (average) of 0.0 and standard deviation of 1.0It looks like this:Most real “natural” data—shoe sizes, quiz scores, amount of rainfall, length of life, etc.—fit a normal curveTo generate realistic data, the Gaussian may be what you want:r = desiredStandardDeviation * random.nextGaussian() + desiredMean;“Unnatural data,” such as income or taxes, may or may not be well described by a normal curve 33% 33%17% 17% -1.0 0.0 1.08Shuffling an arrayGood: static void shuffle(int[] array) { for (int i = array.length; i > 0; i--) { int j = random.nextInt(i); swap(array, i - 1, j); } } // all permutations are equally likelyBad: static void shuffle(int[] array) { for (int i = 0; i < array.length; i++) { int j = random.nextInt(array.length); swap(array, i, j); } } // all permutations are not equally likely9Checking randomnessWith a completely random shuffle, the probability that an element will end up in a given location is equal to the probability that it will end up in any other locationI claim that the second algorithm on the preceding slide is not completely randomI don’t know how to prove this, but I can demonstrate itDeclare an array counts of 20 locations, initially all zeroDo the following 10000 times:Fill an array of size 20 with the numbers 1, 2, 3, ..., 20Shuffle the arrayFind the location loc at which the 5 (or some other number) ends upAdd 1 to count s[loc]Print the counts array10Randomly choosing N thingsSome students had an assignment in which they had to choose exactly N distinct random locations in an arrayTheir algorithm was something like this:for i = 1 to n { choose a random location x if x was previously chosen, start over }Can you do better?11StupidSortHere’s a related technique:void stupidSort(int[] array) { while (!sorted(array)) { shuffle(array); }}This is included as an example of one of the worst algorithms knownYou should, however, be able to analyze (compute the running time of) this algorithm12Analyzing StupidSortvoid stupidSort(int[] array) { while (!sorted(array)) { shuffle(array); }}Let’s assume good implementations of the called methodsYou can check if an array is sorted in O(n) time (n = size of the array)The shuffle method given earlier takes O(n) time (check this)There are n! possible arrangements of the arrayIf we further assume all elements are unique, then there is only one correct arrangement--so the odds for each shuffle are 1 in n!If the odds of something are 1 in x, then on average we have to wait x trials for it to occur (I won’t prove this here)Hence, running time is O((test + shuffle)*n!) = O(2n * n!) = O((n + 1)!)13Zero-sum two-person gamesA zero-sum two person game is represented by an array such as the following:-28037-16-8235-612-9-8-1437RedBlueThe game is played as follows:Red chooses a row and, at the same time, Blue chooses a columnThe number where the row and column intersect is the amount of money that Blue pays to Red (negative means Red pays Blue)Repeat as often as desiredThe best strategy is to play unpredictably--that is, randomlyBut this does not mean “equal probabilities”--for example,


View Full Document

Penn CIT 594 - Randomized Algorithms

Documents in this Course
Trees

Trees

17 pages

Searching

Searching

24 pages

Pruning

Pruning

11 pages

Arrays

Arrays

17 pages

Stacks

Stacks

30 pages

Recursion

Recursion

25 pages

Hashing

Hashing

24 pages

Recursion

Recursion

24 pages

Graphs

Graphs

25 pages

Storage

Storage

37 pages

Trees

Trees

21 pages

Arrays

Arrays

24 pages

Hashing

Hashing

24 pages

Recursion

Recursion

25 pages

Graphs

Graphs

23 pages

Graphs

Graphs

25 pages

Stacks

Stacks

25 pages

Recursion

Recursion

25 pages

Quicksort

Quicksort

21 pages

Quicksort

Quicksort

21 pages

Graphs

Graphs

25 pages

Recursion

Recursion

25 pages

Searching

Searching

24 pages

Counting

Counting

20 pages

HTML

HTML

18 pages

Recursion

Recursion

24 pages

Pruning

Pruning

11 pages

Graphs

Graphs

25 pages

Load more
Download Randomized Algorithms
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Randomized Algorithms and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Randomized Algorithms 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?