Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner.

Slides:



Advertisements
Similar presentations
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner.
Advertisements

Analysis of Algorithms
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
CPSC 411, Fall 2008: Set 10 1 CPSC 411 Design and Analysis of Algorithms Set 10: Randomized Algorithms Prof. Jennifer Welch Fall 2008.
Probability theory and average-case complexity. Review of probability theory.
Probabilistic Analysis and Randomized Algorithm. Worst case analysis Probabilistic analysis  Need the knowledge of the distribution of the inputs Indicator.
Chapter 5. Probabilistic Analysis and Randomized Algorithms
UMass Lowell Computer Science Analysis of Algorithms Spring, 2002 Chapter 5 Lecture Randomized Algorithms Sections 5.1 – 5.3 source: textbook.
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different approaches –Probabilistic analysis of a deterministic algorithm –Randomized.
1 Hiring Problem and Generating Random Permutations Andreas Klappenecker Partially based on slides by Prof. Welch.
Hash Tables How well do hash tables support dynamic set operations? Implementations –Direct address –Hash functions Collision resolution methods –Universal.
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different but similar analyses –Probabilistic analysis of a deterministic algorithm.
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
Probabilistic Analysis and Randomized Algorithms
CS421 - Course Information Website Syllabus Schedule The Book:
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
How should a computer shuffle?. Intro - 2 Comp 122, Goal  Input: Given n items to shuffle (cards, …)  Output: Return some list of exactly those n items;
25 June 2015Comp 122, Spring 2004 Asymptotic Notation, Review of Functions & Summations.
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Qsort - 1 Lin / Devi Comp 122 d(n)d(n) Today’s puzzle  How far can you reach with a stack of n blocks, each 2 units long?
1 Randomized Algorithms Andreas Klappenecker [using some slides by Prof. Welch]
1 Discrete Probability Hsin-Lung Wu Assistant Professor Advanced Algorithms 2008 Fall.
Randomized QS. Ch5, CLRS Dr. M. Sakalli, Marmara University Picture 2006, RPI.
Lecture 2 MAS 714 Hartmut Klauck
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
4/10/2003CSE Quick&Heap CSE Algorithms Quicksort vs Heapsort: the “official” story Next time, we’ll get the “inside” story!
Sets, Combinatorics, Probability, and Number Theory Mathematical Structures for Computer Science Chapter 3 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProbability.
1 9/23/2015 MATH 224 – Discrete Mathematics Basic finite probability is given by the formula, where |E| is the number of events and |S| is the total number.
Randomized Algorithms (Probabilistic algorithm) Flip a coin, when you do not know how to make a decision!
Reading and Writing Mathematical Proofs
CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.
CS 3343: Analysis of Algorithms
Great Theoretical Ideas in Computer Science.
Elementary Sorting Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
BY Lecturer: Aisha Dawood. The hiring problem:  You are using an employment agency to hire a new office assistant.  The agency sends you one candidate.
CSCE 411 Design and Analysis of Algorithms Set 9: Randomized Algorithms Prof. Jennifer Welch Fall 2014 CSCE 411, Fall 2014: Set 9 1.
Introduction to Algorithms Jiafen Liu Sept
Chapter 5: Probability Analysis of Randomized Algorithms Size is rarely the only property of input that affects run time Worst-case analysis most common.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
CSC401 – Analysis of Algorithms Lecture Notes 2 Asymptotic Analysis Objectives: Mathematics foundation for algorithm analysis Amortization analysis techniques.
Randomized Algorithms CSc 4520/6520 Design & Analysis of Algorithms Fall 2013 Slides adopted from Dmitri Kaznachey, George Mason University and Maciej.
Probabilistic Analysis and Randomized Algorithm. Average-Case Analysis  In practice, many algorithms perform better than their worse case  The average.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner
Data Structures Haim Kaplan & Uri Zwick December 2013 Sorting 1.
ICS 353: Design and Analysis of Algorithms
Introduction to Algorithms Randomized Algorithms – Ch5 Lecture 5 CIS 670.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
Great Theoretical Ideas in Computer Science for Some.
Master Method (4. 3) Recurrent formula T(n) = a  T(n/b) + f(n) 1) if for some  > 0 then 2) if then 3) if for some  > 0 and a  f(n/b)  c  f(n) for.
CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,
1 Distributed Vertex Coloring. 2 Vertex Coloring: each vertex is assigned a color.
CSC317 1 Hiring problem-review Cost to interview (low C i ) Cost to fire/hire … (expensive C h ) n number of candidates m hired O (c i n + c h m) Independent.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
Great Theoretical Ideas in Computer Science.
PROBABILITY AND COMPUTING RANDOMIZED ALGORITHMS AND PROBABILISTIC ANALYSIS CHAPTER 1 IWAMA and ITO Lab. M1 Sakaidani Hikaru 1.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Probabilistic analysis
CS 583 Analysis of Algorithms
Ch 7: Quicksort Ming-Te Chi
CSCE 411 Design and Analysis of Algorithms
CS200: Algorithm Analysis
CS 583 Analysis of Algorithms
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Presentation transcript:

Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner

Math Background: Review & Beyond 1.Asymptotic notation 2.Math used in asymptotics 3.Recurrences 4.Probabilistic analysis 2 To do: [CLRS] 5 #2

Not All Algorithms are Deterministic Review a little probability Extended example: –Review probability –Analysis technique 3

Conditional Probability 4

Random Variables What is the probability of getting exactly 1 “tail” when flipping two normal coins? Pr{X=x} =  {s  S | X(s)=x} Pr{s} X = #Tails, x=1 S = TwoNormalCoinsFlipped = {HH, HT, TH, TT} Pr{#Tails=1} =  {coins  TwoNormalCoinsFlipped | #Tails(coins)=1} Pr{coins} = Pr{HT} + Pr{TH} = ¼ + ¼ = ½ 5

Random Variables Random variables X,Y “independent”  Pr{X=x  Y=y} = Pr{X=x}  Pr{Y=y} Expected values of random variables: –E[X] =  x (x  Pr{X=x}) –E[X+Y] = E[X]+E[Y]linearity of expectation –E[a  X] = a  E[X] –E[X  Y] = E[X]  E[Y]if X,Y independent 6

Hiring Problem Goal:Hire the most qualified person for a job. Algorithm we’ll use: 1.Decide how many people (n) to interview. 2.Hire the first candidate. 3.For each interviewee, in turn a.Interview. b.Hire, if better than current employee. Costs (n people interviewed  c i each) + (m people hired  c h each) = O(c i n + c h m) By definition, n  m. Assume c i  c h. 7 Easy so far, but what is m? ? ?

Hiring Problem: Worst-Case m=n In other words, –Hire every interviewee. –Interviewees (unluckily) arranged in ascending order of quality. Cost = O(c i n + c h n) = O(c h n) 8

Hiring Problem: Average-Case What is m likely to be? Wrong answer: m can be 1…n, so use the mean m=(n+1)/2. Instead, average cost over all possible interviewee orders. Number each interviewee 1…n. Consider each permutation of the set {1,…,n}. To compute this average, use probabilistic techniques to avoid listing every possible permutation. 9

Averaging via Expected Values 10 E[Hired? i ] = 1  Pr{#i hired} + 0  Pr{#i not hired} = Pr{#i hired} = 1/iEach has equal chance to be the best of first i. E[TotalHired] = E[  i=1..n Hired? i ] =  i=1..n E[Hired? i ] Expected algorithm cost = O(c i n + c h log n) random variable indicator random variable Define:TotalHired =  i=1…n Hired? i Hired? i = Pr{interviewee #i hired} = 1 if #i hired, 0 if #i not hired =  i=1..n 1/i = ln n + O(1)

On-Line vs. Off-Line Hiring Problem Let’s say the candidates come from an employment agency. Algorithm’s cost is greatly affected by the agency. –Agency could send best applicants first. Typical of well-run agencies. Waste of money to interview later applicants. –Agency could send worst applicants first. Maximizes their fees. We can randomize order, to minimize chance of worst case. –Protects against poor input distributions. (Accidental or malicious.) –Can still produce worst case, but only with low probability. 11

Randomizing Order of an Array 12 The key – use a loop invariant: Before i th iteration, for any (i-1)-permutation of elements from 1…n, A[1…i-1] contains the permutation with probability ((n-i+1)!)/(n!). Let n = # elements in A For i = 1 to n, swap A[i] with A[random(i…n)] Simple algorithm, with an interesting proof that it computes a uniform random permutation.

Randomizing Order of an Array Base case, i=1: A[1..0] contains the 0-permutation (no data) with probability (n!)/(n!)=1. Holds trivially. 13 Let n = # elements in A For i=1 to n, swap A[i] with A[random(i…n)] Proving loop invariant: Before i th iteration, for any (i-1)- permutation of elements from 1…n, A[1…i-1] contains the permutation with probability ((n-i+1)!)/(n!).

Randomizing Order of an Array Inductive case, show holds for i+1: For an arbitrary i-permutation, the permutation is obtained  –Event 1 : its first i-1 elements, an (i-1)-permutation, were obtained in the first i-1 iterations, and –Event 2 : the i th iteration chooses that i-permutation’s i th element. 14 Let n = # elements in A For i=1 to n, swap A[i] with A[random(i…n)] Proving loop invariant: Before i th iteration, for any (i-1)- permutation of elements from 1…n, A[1…i-1] contains the permutation with probability ((n-i+1)!)/(n!). Pr{Event 1  Event 2 } = Pr{Event 1 }  Pr{Event 2 | Event 1 } = (n-i+1)!/n!  1/(n-i+1) Inductive hypothesisEach remaining element equally likely

Randomizing Order of an Array Inductive case, show holds for i+1: For an arbitrary i-permutation, the permutation is obtained  –Event 1 : its first i-1 elements, an (i-1)-permutation, were obtained in the first i-1 iterations, and –Event 2 : the i th iteration chooses that i-permutation’s i th element. 15 Let n = # elements in A For i=1 to n, swap A[i] with A[random(i…n)] Proving loop invariant: Before i th iteration, for any (i-1)- permutation of elements from 1…n, A[1…i-1] contains the permutation with probability ((n-i+1)!)/(n!). Pr{Event 1  Event 2 } = Pr{Event 1 }  Pr{Event 2 | Event 1 } = (n-(i+1)+1)!/n!Algebra = (n-i)!/n!Algebra, cancelling term = (n-i+1)!/n!  1/(n-i+1)

Randomizing Order of an Array At termination (i=n+1) Invariant  any n-permutation of elements 1…n occurs in A[1..n] with probability 1/(n!). I.e., uniform random distribution. 16 Let n = # elements in A For i=1 to n, swap A[i] with A[random(i…n)] Proving loop invariant: Before i th iteration, for any (i-1)- permutation of elements from 1…n, A[1…i-1] contains the permutation with probability ((n-i+1)!)/(n!).

On-Line Hiring Problem Don’t want to interview all candidates. Instead, interview some until find one “good enough”. More realistic. Algorithm: 1.For each interviewee, a.Interview b.Hire and stop loop, if good enough. 17 Clearly, might not hire the best candidate. Might hire before we see the best one. Might not hire the best one, hoping for someone better. How to define to maximize likelihood of getting the best candidate? Cost = O(c i n + c h  1) = O(c i n)

On-Line Hiring Problem One possible algorithm: Interview k candidates, and hire the first one after that with a better score than previously found. 18 BestScore := -  For i=1 to k, BestScore = max(BestScore, Score(i)) For i=k+1 to n, If Score(i) > BestScore, Then Hire #i & Quit Hire #n Strategy to find best k: 1.Fix k, & compute the probability of getting best candidate. 2.Find k to maximize probability.

On-Line Hiring Problem Define:S = succeed in finding best candidate S i = succeed in finding best candidate & best is #i 19 Since we never hire from the first k, Pr{S 1 } = … = Pr{S k } = 0 Pr{S} =  i=k+1…n Pr{S i } Since S i ’s disjoint, Pr{S} =  i=1...n Pr{S i } Need to determine Pr{S i } for i  k+1…n. S i = candidate #i is best & candidates k+1…i-1 not chosen = candidate #i is best & best among 1…i-1 is among 1…k (not k+1…i-1) = 1/n  k/(i-1) = k/(n(i-1)) Independent Pr{S i } = ?

On-Line Hiring Problem Define:S = succeed in finding best candidate S i = succeed in finding best candidate & best is #i 20 Pr{S i } = 0for i  1…k Pr{S i } = k/(n(i-1))for i  k+1…n Pr{S} =  i=k+1…n Pr{S i } =  i=k+1…n k/(n(i-1)) = k/n  i=k+1…n 1/(i-1) = k/n  j=k…n-1 1/jRenaming: j=i+1 ? What now? ? = k/n (ln n - ln k) Alternative: use harmonic sum.

On-Line Hiring Problem Pr{S}  k/n (ln n - ln k)Find k to maximize Pr{S}. 21 Algorithm has better than 1/3 chance of getting best candidate. Plug in: Pr{S}  (n/e)/n (ln n - ln (n/e)) = 1/e (ln n - ln n + ln e) = 1/e  1 = 1/e  Differentiate w.r.t. k, and set to zero: 1/n (ln n - ln k - 1) = 0 ln k = ln n - 1 ln k = ln n/e k = n/e