Download Algorithm Analysis - LAB: Data Structures - Project Report | CS 230 and more Study Guides, Projects, Research Data Structures and Algorithms in PDF only on Docsity!
20 - 1
Algorithm Analysis
PS5 due 11:59pm Wednesday, April 18 Final Project Phase 2 (Program Outline) due 1:30pm Tuesday, April 24 Wellesley College CS Lecture 20 Tuesday, April 17 Handout # 20 - 2
Overview of Today’s Lecture
• Motivation: determining the efficiency of algorithms
• Review of functions
• Best-case, average-case, and worst-case efficiency
• Asymptotic notation: a mathematical tool for
describing algorithm efficiency
• Examples: sorting algorithms, set implementations
Next time = another mathematical tool for analyzing
algorithms: recurrence equations
This is just an appetizer. For the full meal, take CS
(Fundamental Algorithms)
20 - 3
Determining the Efficiency of Algorithms
- What is an efficient algorithm?
- Time: Faster is better.
- How do you measure time? Wall clock? Computer clock? Abstract operations?
- Space: Less is better
- Which space? Input data? Intermediate data? Output data?
- Where space is can affect time: registers, cache, main memory, remote machnine
- Algorithm analysis provides tools for determining the efficiency of an algorithm and comparing algorithms.
- Algorithm analysis should be independent of
- specific implementations and coding tricks (PL, control constructs)
- specific computers (HW chip, OS, clock speed)
- particular set of data
- But size of data should matter
- Order of data can also matter 20 - 4
How do Your Functions Grow?
n^3 cubic 103 106 109 1012 1015 1018 2 n exponential 103 1030 10301 103010 1030103 10301030 n^2 quadratic 102 104 106 108 1010 1012 n · log 2 n 33 664 104 1.3x 10^5 1.7x 10^6 2x 10^8 n linear 1 102 103 104 105 106 log 2 n logarithmic 3.3 6.64 9.97 13.3 16.6 19. 1 constant 1 1 1 1 1 1 function name n=10 102 103 104 105 106 f(n) = For many problems, n · log2 n and less is considered good and n3 and greater is considered bad , but really depends on the problem. E.g., linear is bad for sorted array search and exponential is the best time for Towers of Hanoi.
20 - 7
Caution: What is n?
n is typically the number of elements in a collection (array, vector, list, tree, matrix, graph, stack, queue, set, bag, table, etc.) Changing the definition of n changes the analysis:
- Search on an unsorted square matrix is linear in the number of elements but quadratic in the side length.
- Search on a balanced BST is logarithmic in its number of nodes but linear its height.
- Search on a unsorted full tree is linear in its number of nodes but exponential in its height. 20 - 8
Asymptotics: Motivation
Fine-grained bean-counting exposes too much detail for comparing
functions.
Want a course-grained way to compare functions that ignores
constant factors and focuses.
Example:
r(n) = 0.1n^2
q(n) = 3n^2 + 2n + 1
p(n) = 100n + 1000
function n=1 n=10^3 n=10^6
20 - 9
Asymptotics: Summary
f ∈ o(g) f is way smaller than g f < g
f ∈ O(g) f is at most as big as g f ≤ g
f ∈ Θ(g) f is about the same as g f = g
f ∈ Ω(g) f is at least as big as g f ≥ g
f ∈ ω(g) f is way bigger than g f > g
Notation Pronunciation Loosely
- Each of ω(g), Ω(g), … denotes a set of functions. E.g., ω(g) is the set of
all functions way bigger than g, etc.
- The notation f = ω(g) is really shorthand for f ∈ ω(g) , and similarly for
the other notations.
- The phrases “is at least O(g)” and “is at most Ω(g)” are non-sensical. 20 - 10 Order of growth of some common functions
- O(1) ⊂ O(log 2 n) ⊂ O(n) ⊂ O(n * log 2 n) ⊂ O(n^2 ) ⊂ O(n^3 ) ⊂ O(2n)
- Intuitively, Θ(1) < Θ(log 2 n) < Θ(n) < Θ(n * log 2 n) < Θ(n^2 ) < Θ(n^3 ) < Θ(2n) Properties of growth-rate functions
- You can ignore low-order terms
- You can ignore a multiplicative constant in the high-order
term
- Θ(f(n)) + Θ(g(n)) = Θ(f(n) + g(n)), and similarly for other
notations.
Asymptotic Analysis