Analysis of Algorithms & Big-O
CS16: Introduction to Algorithms & Data Structures Spring 2020
Analysis of Algorithms & Big-O CS16: Introduction to Algorithms - - PowerPoint PPT Presentation
Analysis of Algorithms & Big-O CS16: Introduction to Algorithms & Data Structures Spring 2020 Outline Running time Big- O Big- and Big- 2 What is a Good Algorithm? 3 What is a Good Algorithm In CS16
Analysis of Algorithms & Big-O
CS16: Introduction to Algorithms & Data Structures Spring 2020
Outline
What is a “Good” Algorithm?
3What is a “Good” Algorithm
Bitcoin Annual Footprints
5 digiconomist.comQ: How should we measure running time?
A Simple Algorithm
Measuring Running Time
Q: Was that useful?
Experimental Running Time
Measuring Running Time
Environment
Q: What is a useful measure of running time?
A Simple Algorithm
13 function sum_array(array) // Input: an array of integers // Output: the sum of the integers if array.length = 0 return error sum = 0 for i in [0, array.length-1]: sum = sum + array[i] return sumKnuth’s Observation
Knuth’s Observation
Elementary Operations
Towards a Useful Measure of Running Time
Towards a Useful Measure of Running Time
18“Running time” = Number of elementary operations
Running time ≠ Experimental time
A Simple Algorithm
19 function sum_array(array) // Input: an array of integers // Output: the sum of the integers if array.length = 0 return error sum = 0 for i in [0, array.length-1]: sum = sum + array[i] return sum 1op 1op 3ops per loop loop 1op 1opTowards a Useful Measure of Running Time
A Simple Algorithm
21Towards a Useful Measure of Running Time
22Worst-case running time = Number of elementary operations
A Simple Algorithm
23Towards a Useful Measure of Running Time
A Simple Algorithm
25 function sum_array(array) // Input: an array of integers // Output: the sum of the integers if array.length = 0 return error sum = 0 for i in [0, array.length-1]: sum = sum + array[i] return sum 1op 1op 3ops n 1op 1opTowards a Useful Measure of Running Time
26Worst-case running time = T(n): Number of elementary operations
as a function of input size n
Running Times
Constant
independent of input sizeLinear
depends on input sizeQuadratic
depends on square of input sizeConstant Running Time
Activity #1
function argmax(array) // Input: an array // Output: the index of the maximum value index = 0 for i in [1, array.length): if array[i] > array[index]: index = i return index 1op loop 3ops per loop 1op per loop (sometimes) 1opActivity #1
function argmax(array) // Input: an array // Output: the index of the maximum value index = 0 for i in [1, array.length): if array[i] > array[index]: index = i return index 1op loop 3ops per loop 1op per loop (sometimes) 1opActivity #1
function argmax(array) // Input: an array // Output: the index of the maximum value index = 0 for i in [1, array.length): if array[i] > array[index]: index = i return index 1op loop 3ops per loop 1op per loop (sometimes) 1opLinear Running Time
Activity #2
function possible_products(array): // Input: an array // Output: a list of all possible products // between any two elements in the list products = [] for i in [0, array.length): for j in [0, array.length): products.append(array[i] * array[j]) return products 1op loop loop per loop 4ops per loop per loop 1opActivity #2
function possible_products(array): // Input: an array // Output: a list of all possible products // between any two elements in the list products = [] for i in [0, array.length): for j in [0, array.length): products.append(array[i] * array[j]) return products 1op loop loop per loop 4ops per loop per loop 1opActivity #2
function possible_products(array): // Input: an array // Output: a list of all possible products // between any two elements in the list products = [] for i in [0, array.length): for j in [0, array.length): products.append(array[i] * array[j]) return products 1op loop loop per loop 4ops per loop per loop 1opQuadratic Running Time
Running Times
Constant
independent of input sizeLinear
depends on input sizeQuadratic
depends on square of input sizeQ: how do we compare running times?
Which Algorithm is Better?
Which Algorithm is Better?
Which Algorithm is Better?
What is Running Time?
42Asymptotic worst-case running time = Number of elementary operations
as a function of input size n when n tends to infinity
In CS “running time” usually means asymptotic worst-case running time…but not always! we will learn about other kinds of running times
Comparing Running Times
43Comparing asymptotic running times = TA(n) is better than TB(n) if for large enough n TA(n) grows slower than TB(n)
Q: can we formalize all this mathematically?
Big-O
Definition (Big-O): TA(n) is O(TB(n)) if there exists positive constants c and n0 such that: TA(n) ≤ c∙TB(n) for all n ≥ n0
Big-O
don’t need to prove it)
46Big-O Examples
Definition (Big-O): TA(n) is O(TB(n)) if there exists positive constants c and n0 such that: TA(n) ≤ c∙TB(n) for all n ≥ n0
2n+10 ≤ 3n ⟺ -n + 10 ≤ 0 ⟺ n ≥ 10c n0
What is n0 ?
We don’t care what happens heren0
We only care what happens heren
T(n)Experimental measurement Big-O
More Big-O Examples
50 n2 ≤ c∙n ⟺ n ≤ c Not possible when n grows & c is constantBig-O & Growth Rate
51Activity #3
Big-O & Growth Rate
52Activity #3
Big-O & Growth Rate
53Activity #3
Eyeballing Big-O
Eyeballing Big-O
More Eyeballing Big-O
More Eyeballing Big-O
Big-O
growth rate
Definition (Big-O): TA(n) is O(TB(n)) if there exists positive constants c and n0 such that: TA(n) ≤ c∙TB(n) for all n ≥ n0
Big-Omega
TB(n)’s growth rate
Definition (Big-Ω): TA(n) is Ω(TB(n)) if there exists positive constants c and n0 such that: TA(n) ≥ c∙TB(n) for all n ≥ n0
Big-Theta
Definition (Big-𝝨): TA(n) is 𝝨(TB(n)) if it is O(TB(n)) and Ω(TB(n)).
More Examples
61Activity #4
More Examples
62Activity #4
More Examples
63Activity #4
More Examples
64 T(n) Big-O Big-Ω Big-𝝨 an + b? ?
𝝨(n) an2 + bn + c? ?
𝝨(n2) a? ?
𝝨(1) 3n + an40? ?
𝝨(3n) an + b log n? ?
𝝨(n)Running Times
O(1) independent of input size O(n) depends on input size O(n2) depends on square of input size O(2n) exponential in input size O(n3) depends on cube of input size O(n70) depends on 70th powerAdditional Readings
Announcements
attending lab
References
sprinter of all time
hurdles, 2011 and 2017 World Champion
holder, Olympic marathon bronze medalist
medalist, greatest marathoner of the modern era
69