CSC 282 – Algorithms Daniel Stefankovic – CSB 620 TA: Girts Folkmanis – CSB 614

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
Advertisements

A simple example finding the maximum of a set S of n numbers.
5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
CSC 282 – Algorithms Daniel Stefankovic – CSB 620 TA: Girts Folkmanis – CSB 614
Introduction to Algorithms
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Algorithms Recurrences Continued The Master Method.
The Substitution method T(n) = 2T(n/2) + cn Guess:T(n) = O(n log n) Proof by Mathematical Induction: Prove that T(n)  d n log n for d>0 T(n)  2(d  n/2.
Median/Order Statistics Algorithms
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
Administrative Sep. 20 (today) – HW1 due Sep. 21 8am – problem session Sep. 25 – HW3 (=QUIZ #1) due Sep. 27 – HW4 due Sep. 28 8am – problem session Oct.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
CSE 421 Algorithms Richard Anderson Lecture 12 Recurrences.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
Updates HW#1 has been delayed until next MONDAY. There were two errors in the assignment Merge sort runs in Θ(n log n). Insertion sort runs in Θ(n2).
Selection: Find the ith number
Recurrence Examples.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
CSE 421 Algorithms Richard Anderson Lecture 11 Recurrences.
Recurrence Relations Connection to recursive algorithms Techniques for solving them.
Administrative Sep. 25 (today) – HW3 (=QUIZ #1) due Sep. 27 – HW4 due Sep. 28 8am – problem session Oct. 2 Oct. 4 – QUIZ #2 (pages of DPV)
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.
Analysis of Recursive Algorithms October 29, 2014
Analysis of Algorithms
Analysis of Algorithms CS 477/677
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Project 2 due … Project 2 due … Project 2 Project 2.
Merge Sort Solving Recurrences The Master Theorem
CSC 413/513: Intro to Algorithms Merge Sort Solving Recurrences.
CS 2133:Data Structures Merge Sort Solving Recurrences The Master Theorem.
Algorithms Merge Sort Solving Recurrences The Master Theorem.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
Spring 2015 Lecture 2: Analysis of Algorithms
1Computer Sciences. 2 GROWTH OF FUNCTIONS 3.2 STANDARD NOTATIONS AND COMMON FUNCTIONS.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
CSC317 1 Quicksort on average run time We’ll prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing.
Randomized Quicksort (8.4.2/7.4.2) Randomized Quicksort –i = Random(p, r) –swap A[p]  A[i] –partition A(p, r) Average analysis = Expected runtime –solving.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Introduction to Algorithms: Divide-n-Conquer Algorithms
Mathematical Foundations (Solving Recurrence)
Order Statistics.
Order Statistics Comp 122, Spring 2004.
Lecture 11. Master Theorem for analyzing Recursive relations
Divide-and-Conquer 6/30/2018 9:16 AM
Randomized Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Randomized Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Richard Anderson Lecture 11 Recurrences
CS 3343: Analysis of Algorithms
Order Statistics Comp 550, Spring 2015.
Divide and Conquer (Merge Sort)
Using The Master Method Case 1
Order Statistics Def: Let A be an ordered set containing n elements. The i-th order statistic is the i-th smallest element. Minimum: 1st order statistic.
Order Statistics Comp 122, Spring 2004.
MASTER THEOREM.
Richard Anderson Lecture 12, Winter 2019 Recurrences
Richard Anderson Lecture 12 Recurrences
Presentation transcript:

CSC 282 – Algorithms Daniel Stefankovic – CSB 620 TA: Girts Folkmanis – CSB 614

Quiz – problem 1

Quiz – problem 2 45 l=1 r=2 m=1 B=5 1 l  1, r  n 2 while l < r do 3 m  (l+r)/2  4 if A[m] < B then 5 l  m 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ”

Quiz – problem 2 1 l  1, r  n 2 while l < r do 3 m  (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ”

Quiz – problem 2 1 l  1, r  n 2 while l < r do 3 m  (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ” B  A  B  A[l..r]

Quiz – problem 2 1 l  1, r  n 2 while l < r do 3 m  (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ” B  A  B  A[l..r] A[m]<B  (B  A  B  A[m+1..r])

Quiz – problem 2 1 l  1, r  n 2 while l < r do 3 m  (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ” B  A  B  A[l..r] DONE

Quiz – problem 2 1 l  1, r  n 2 while l < r do 3 m  (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ” B  A  B  A[l..r] DONE ?

Quiz – problem 2 1 l  1, r  n 2 while l < r do 3 m  (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ” B  A  B  A[l..r] Still need to prove that the algorithm terminates.

Quiz – problem 2 1 l  1, r  n 2 while l < r do 3 m  (l+r)/2  4 if A[m] < B then 5 l  m+1 6 else 7 r  m 8 fi 9 od ANSWER = “ is A[l]=B ” B  A  B  A[l..r] Q = r-l Q’ = r-(  (l+r)/2  +1) Q’ =  (l+r)/2  -l

Quiz – problem 3 PROBLEM: rotate an array in-place this type problem given on interviews in MSFT, GOOG homework

Recurrences T(n)  T(  n/2  ) + T(  n/2  ) + c.n T(n)=O(n log n) T(n)  2 T( n/2 ) + c.n We “showed” : for

Recurrences T(1) = O(1) Proposition: T(n)  d.n.lg n Proof: T(n)  2 T( n/2 ) + c.n  2 d (n/2).lg (n/2) + c.n = d.n.( lg n – 1 ) + cn = d.n.lg n + (c-d).n   d.n.lg n T(n)  2 T( n/2 ) + c.n

Recurrences T(n)  2 T( n/2 ) + c.n T(n)  T(  n/2  ) + T(  n/2  ) + c.n G(n) = T(n+2) G(n) = T(n+2)  T(  n/2  +1) + T(  n/2  +1) + c.n = G(  n/2  -1) + G(  n/2  -1) + c.n

Recurrences useful guess – substitute (prove) or use “Master theorem” T(n) = a T(n/b) + f(n) If f(n) = O(n c-  ) then T(n) =  (n c ) If f(n) =  (n c ) then T(n) =  (n c.log n) If f(n) =  (n c+  ) then T(n)=  (f(n)) if a.f(n/b)  d.f(n) for some d n 0 c=log b a

Recurrences T(n) = a T(n/b) + f(n) If f(n) = O(n c-  ) then T(n) =  (n c ) If f(n) =  (n c ) then T(n) =  (n c.log n) If f(n) =  (n c+  ) then T(n)=  (f(n)) if a.f(n/b)  d.f(n) for some d n 0 c=log b a T(n) = 3 T(n/2) +  (n) T(n) = 2T(n/2) +  (n.log n)

Finding the minimum min  A[1] for i from 2 to n do if A[i]<min then min  A[i] How many comparisons?

Finding the minimum How many comparisons? comparison based algorithm: The only allowed operation is comparing the elements

Finding the minimum

Finding the k-th smallest element k =  n/2  = MEDIAN

Finding the k-th smallest element

) sort each 5-tuple

Finding the k-th smallest element ) sort each 5-tuple

Finding the k-th smallest element ) sort each 5-tuple

Finding the k-th smallest element ) sort each 5-tuple

Finding the k-th smallest element ) sort each 5-tuple

Finding the k-th smallest element ) sort each 5-tuple TIME = ?

Finding the k-th smallest element ) sort each 5-tuple TIME =  (n)

Finding the k-th smallest element ) find median of the middle n/5 elements TIME = ?

Finding the k-th smallest element ) find median of the middle n/5 elements TIME = T(n/5)

Finding the k-th smallest element At least ? Many elements in the array are  X

Finding the k-th smallest element At least ? Many elements in the array are  X

Finding the k-th smallest element At least 3n/10 elements in the array are  X

Finding the k-th smallest element At least 3n/10 elements in the array are  X

Finding the k-th smallest element At least 3n/10 elements in the array are  X XX XX

Finding the k-th smallest element At least 3n/10 elements in the array are  X XX XX Recurse, time ?

Finding the k-th smallest element At least 3n/10 elements in the array are  X XX XX Recurse, time  T(7n/10)

Finding the k-th smallest element XX XX recurse

Finding the k-th smallest element XX XX recurse  n) T(n/5)  n) T(7n/10)

Finding the k-th smallest element T(n)  T(n/5) + T(7n/10) + O(n)

Finding the k-th smallest element T(n)  T(n/5) + T(7n/10) + O(n) T(n)  d.n Induction step: T(n)  T(n/5) + T(7n/10) + O(n)  d.(n/5) + d.(7n/10) + O(n)  d.n + (O(n) – dn/10)  d.n

Why 5-tuples? XX XX recurse  n)

Why 5-tuples? XX XX recurse  n) T(2n/3) T(n/3)

Why 5-tuples? T(n)  T(n/3) + T(2n/3) +  (n)

Why 5-tuples? T(n)  T(n/3) + T(2n/3) +  (n) T(n)  c.n.ln n Induction step: T(n) = T(n/3) + T(2n/3) +  (n)  c.(n/3).ln (n/3) + c.(2n/3).ln (2n/3) +  (n)  c.n.ln n - c.n.((1/3)ln 3+(2/3)ln 3/2)+  (n)  c.n.ln n