Uploaded by Mukesh Naidu

2 algorithm analysis

advertisement
CSCE5150
Analysis of Computer Algorithms
Dr. Xuan Guo
1
Content
• Asymptotic notation
•
•
•
•
•
Big Oh
Big Omega
Big Theta
little Oh
little Omega
• Asymptotic Dominance
• Recursive algorithms
• Merge Sort
• Resolving Recurrences
• Substitution method
• Recursion trees
• Master method
2
1
Problem Solving: Main Steps
1.
2.
3.
4.
5.
6.
Problem definition
Algorithm design / Algorithm specification
Algorithm analysis
Implementation
Testing
[Maintenance]
3
Algorithm Analysis
• Goal
• Predict the resources that an algorithm requires
• What kind of resources?
•
•
•
•
Memory
Communication bandwidth
Hardware requirements
Running time
• Two approaches
• Empirical tests
• Mathematical analysis
4
2
Algorithm Analysis – Empirical Tests
• Steps:
• Implement algorithm in a given programming language
• Measure runtime with several inputs
• Infer running time for any input
• Pros:
• No math, straightforward method
• Cons:
• Not reliable, heavily dependent on
• the sample inputs
• programming language and environment
• We want to analyze algorithms to decide whether they are worth
implementing
5
Algorithm Analysis – Mathematical Analysis
• Use math to estimate the running time of an algorithm
• almost always dependent on the size of the input
• Algorithm1 running time is ๐‘›2 for an input of size ๐‘›
• Algorithm2 running time is ๐‘› × log ๐‘› for an input of size ๐‘›
• Pros:
• formal, rigorous
• no need to implement algorithms
• machine-independent
• Cons:
• math knowledge
6
3
Algorithm Analysis
• Best case analysis
• shortest running time for any input of size ๐‘›
• often meaningless, one can easily cheat
• Worst case analysis
•
•
•
•
longest running time for any input of size ๐‘›
it guarantees that the algorithm will not take any longer
provides an upper bound on the running time
worst case occurs often search for an item that does not exist
• Average case analysis
• running time averaged for all possible inputs
• it is hard to estimate the probability of all possible inputs
7
Random-Access Machine (RAM)
In order to predict running time, we need a (simple) computational model: the
Random-Access Machine (RAM)
• Instructions are executed sequentially
• No concurrent operations
• Each basic instruction takes a constant amount of time
•
•
•
•
arithmetic: add, subtract, multiply, divide, remainder, floor, ceiling, shift left/shift right
data movement: load, store, copy
control: conditional/unconditional branch, subroutine call and return
Loops and subroutine calls are not simple operations. They depend upon the size of the
data and the contents of a subroutine. “Sort” is not a single step operation.
• Each memory access takes exactly 1 step.
We measure the run time of an algorithm by counting the number of steps.
RAM model is useful and accurate in the same sense as the flat-earth model (which is useful)!
8
4
The RAM Model of Computation
• The worst-case (time) complexity of an algorithm is the function
defined by the maximum number of steps taken on any instance of
size ๐‘›.
• The best-case complexity of an algorithm is the function defined by
the minimum number of steps taken on any instance of size ๐‘›.
• The average-case complexity of the algorithm is the function defined
by an average number of steps taken on any instance of size ๐‘›.
• Each of these complexities defines a numerical function: time vs. size!
9
Asymptotic Notation – Big Oh, ๐‘‚
• In plain English: ๐‘‚(๐‘”(๐‘›)) are all functions ๐‘“(๐‘›) for
which there exists two positive constants ๐‘ and ๐‘›0
such that for all ๐‘› ≥ ๐‘›0 , 0 ≤ ๐‘“(๐‘›) ≤ ๐‘๐‘”(๐‘›)
• ๐‘”(๐‘›) is an asymptotic upper bound for ๐‘“(๐‘›)
• Intuitively, you can think of ๐‘‚ as “≤” for functions
• If ๐‘“(๐‘›) ∈ ๐‘‚(๐‘”(๐‘›)), we write ๐‘“(๐‘›) = ๐‘‚(๐‘”(๐‘›))
• The definition implies a constant ๐‘›0 beyond which
they are satisfied. We do not care about small values
of ๐‘›.
10
5
Examples
• Is 2๐‘›+1 = ๐‘‚ 2๐‘› ? Is 22๐‘› = ๐‘‚ 2๐‘› ?
11
Examples
• Is 2๐‘›+1 = ๐‘‚ 2๐‘› ? Is 22๐‘› = ๐‘‚ 2๐‘› ?
12
6
Asymptotic Notation – Big Omega, Ω
• In plain English: โ„ฆ(๐‘”(๐‘›)) are all function
๐‘“(๐‘›) for which there exists two positive
constants ๐‘ and ๐‘›0 such that for all ๐‘› ≥ ๐‘›0 ,
0 ≤ ๐‘๐‘” ๐‘› ≤ ๐‘“ ๐‘›
• ๐‘”(๐‘›) is an asymptotic lower bound for ๐‘“ ๐‘›
• Intuitively, you can think of โ„ฆ as “≥” for
functions
13
Asymptotic Notation – Theta, Θ
• In plain English: Θ(๐‘”(๐‘›)) are all function ๐‘“(๐‘›)
for which there exists three positive
constants ๐‘1 , ๐‘2 , and ๐‘›0 such that for all ๐‘› ≥
๐‘›0 , 0 ≤ ๐‘1 ๐‘”(๐‘›) ≤ ๐‘“(๐‘›) ≤ ๐‘2 ๐‘”(๐‘›)
• In other words, all functions that grow at the
same rate as ๐‘”(๐‘›)
• ๐‘”(๐‘›) is an asymptotically tight bound for ๐‘“(๐‘›)
• Intuitively, you can think of Θ as “=” for
functions
14
7
Asymptotic Notation – Theta, Θ
15
Asymptotic notation in equations and
inequalities
• On the right-hand side alone of an equation (or inequality) ≡ a
set of functions
• ex., ๐‘› = ๐‘‚ ๐‘›2 ↔ ๐‘› ∈ ๐‘‚ ๐‘›2
• In general, in a formula, stands for some anonymous function that
we do not care to name
• ex., 2๐‘›2 + 3๐‘› + 1 = 2๐‘›2 + Θ ๐‘› ↔ 2๐‘›2 + 3๐‘› + 1 = 2๐‘›2 + ๐‘“ ๐‘› , where
๐‘“ ๐‘› =Θ ๐‘›
• help eliminate inessential detail and clutter in a formula
• ex., ๐‘‡ ๐‘› = 2๐‘‡
๐‘›
2
+Θ ๐‘›
16
8
Asymptotic Notation – little oh, ๐‘œ
• In plain English: ๐‘œ(๐‘”(๐‘›)) are all function ๐‘“(๐‘›) for which for all
constant ๐‘ > 0, there exists a constant ๐‘›0 > 0 such that for all ๐‘› ≥
๐‘›0 , 0 ≤ ๐‘“(๐‘›) < ๐‘๐‘”(๐‘›)
• ๐‘œ(๐‘”(๐‘›)) is an asymptotic upper bound for ๐‘“(๐‘›), but not tight
• ๐‘“(๐‘›) becomes insignificantly relative to ๐‘”(๐‘›) as ๐‘› grows
• ๐‘“(๐‘›) grows asymptotically slower than ๐‘”(๐‘›)
• Similar to ๐‘‚(๐‘”(๐‘›)), intuitively, you can think of ๐‘œ as “<” for
functions
17
Asymptotic Notation – little oh, ๐‘œ
Examples
• ๐‘›1.999 = ๐‘œ ๐‘›2
•
๐‘›2
log ๐‘›
2
= ๐‘œ ๐‘›2
• ๐‘› ≠ ๐‘œ ๐‘›2
•
๐‘›2
1000
≠ ๐‘œ ๐‘›2
18
9
Asymptotic Notation – little Omega, ๐œ”
• In plain English: ๐œ”(๐‘”(๐‘›)) are all function ๐‘“(๐‘›) for which for all
constant ๐‘ > 0, there exists a constant ๐‘›0 > 0 such that for all ๐‘› ≥
๐‘›0 , 0 ≤ ๐‘๐‘”(๐‘›) < ๐‘“(๐‘›)
•
•
•
•
๐œ”(๐‘”(๐‘›)) is an asymptotic lower bound for ๐‘“(๐‘›), but not tight
๐‘“(๐‘›) becomes arbitrarily large relative to ๐‘”(๐‘›) as ๐‘› grows
๐‘“(๐‘›) grows asymptotically faster than ๐‘”(๐‘›)
Similar to โ„ฆ(๐‘”(๐‘›))
• Intuitively, you can think of ๐œ” as “>” for functions
19
Asymptotic Notation – little Omega, ๐œ”
Examples:
• ๐‘›2.0001 = ๐œ” ๐‘›2
• ๐‘›2 log ๐‘› = ๐œ” ๐‘›2
• ๐‘›2 ≠ ๐œ” ๐‘›2
20
10
Asymptotic Notation
• Asymptotic notation is a way to compare functions
•
•
•
•
•
๐‘‚ ≈≤
โ„ฆ ≈≥
Θ ≈=
๐‘œ ≈<
๐œ” ≈>
• When using asymptotic notations, sometimes,
• drop lower-order terms
• ignore constant coefficient in the leading term
21
Asymptotic Notation Multiplication by
Constant
• Multiplication by a constant does not change the asymptotic:
๐‘‚(๐‘ · ๐‘“(๐‘›)) → ๐‘‚(๐‘“(๐‘›))
โ„ฆ(๐‘ · ๐‘“(๐‘›)) → โ„ฆ(๐‘“(๐‘›))
Θ(๐‘ · ๐‘“(๐‘›)) → Θ(๐‘“(๐‘›))
• The “old constant” ๐ถ from the Big Oh becomes ๐‘ · ๐ถ.
22
11
Asymptotic Notation Multiplication by
Function
• But when both functions in a product are increasing, both are
important:
๐‘‚(๐‘“(๐‘›)) · ๐‘‚(๐‘”(๐‘›)) → ๐‘‚(๐‘“(๐‘›) · ๐‘”(๐‘›))
โ„ฆ(๐‘“(๐‘›)) · โ„ฆ(๐‘”(๐‘›)) → โ„ฆ(๐‘“(๐‘›) · ๐‘”(๐‘›))
Θ(๐‘“(๐‘›)) · Θ(๐‘”(๐‘›)) → Θ(๐‘“(๐‘›) · ๐‘”(๐‘›))
This is why the running time of two nested loops is ๐‘‚(๐‘›2 ).
23
Testing Dominance
๐‘” ๐‘›
• ๐‘“(๐‘›) dominates ๐‘”(๐‘›) if lim
= 0, which is the same as saying
๐‘›→∞ ๐‘“ ๐‘›
๐‘”(๐‘›) = ๐‘œ(๐‘“(๐‘›)).
• Note the little-oh – it means “grows strictly slower than”.
24
12
Dominance Rankings
• You must come to accept the dominance ranking of the basic
functions:
• ๐‘›! โ‰ซ 2๐‘› โ‰ซ ๐‘›3 โ‰ซ ๐‘›2 โ‰ซ ๐‘› log ๐‘› โ‰ซ ๐‘› โ‰ซ log ๐‘› โ‰ซ 1
25
Advanced Dominance Rankings
• Additional functions arise in more sophisticated analysis:
• ๐‘›! โ‰ซ ๐‘ ๐‘› โ‰ซ ๐‘›3 โ‰ซ ๐‘›2 โ‰ซ ๐‘›1+๐œ– โ‰ซ ๐‘› log ๐‘› โ‰ซ ๐‘› โ‰ซ ๐‘› โ‰ซ log ๐‘›
log ๐‘› โ‰ซ log ๐‘›เต—log log ๐‘› โ‰ซ log log ๐‘› โ‰ซ 1
2
โ‰ซ
26
13
Logarithms
• It is important to understand deep in your bones what logarithms
are and where they come from.
• A logarithm is simply an inverse exponential function.
• Saying ๐‘ ๐‘ฅ = ๐‘ฆ is equivalent to saying that ๐‘ฅ = log ๐‘ ๐‘ฆ.
• Logarithms reflect how many times we can double something
until we get to ๐‘› or halve something until we get to 1.
27
The Base is not Asymptotically Important
• Recall the definition, ๐‘ log๐‘ ๐‘ฅ = ๐‘ฅ and that
• log ๐‘ ๐‘Ž =
log๐‘ ๐‘Ž
log๐‘ ๐‘
1
• Thus, log 2 ๐‘› = (1/ log100 2) × log100 ๐‘›. Since
= 6.643 is just
log100 2
a constant, it does not matter in the Big Oh.
28
14
Analyzing Merge Sort
29
Analyzing Merge Sort – Merge Procedure
30
15
Analyzing Merge Sort
Input: {10,5,7,6,1,4,8,3}
Output: {1,3,4,5,6,7,9,10}
31
Analyzing Algorithms – recursive algorithms
• Divide-and-conquer paradigm
• solve a problem for a given input of size ๐‘›
• You don’t know how to solve it for any ๐‘›, but
• you can divide the problem into smaller subproblems (input sizes ๐‘›′ < ๐‘›)
• you can conquer (solve) the subproblems recursively – you need a base case: for a
small enough input solving the problem is straightforward
• you can combine the solutions to the subproblems to solve the original problem
with input size ๐‘›
• Recursive algorithms call themselves with a different input
• we cannot just count the number of instructions
32
16
Analyzing Merge Sort – running time
• In plain English, the cost of merge sort, i.e., ๐‘‡(๐‘›) is
• a constant if ๐‘› <= 1; Θ(1)
• the cost of dividing, solving the subproblems and combining the
subproblems, if ๐‘› > 1
• dividing: Θ(๐‘›)
• conquering (solving) subproblems: 2 × ๐‘‡(๐‘›/2)
• combining subproblems: Θ(๐‘›)
33
Analyzing Merge Sort – running time
• Formally
Θ(1), if ๐‘›= 1
๐‘‡(๐‘›) = แ‰Š
2T(๐‘›/2) + Θ(๐‘›), if ๐‘›> 1
How do we solve a recurrence?
(Solving a recurrence relation means obtaining a closedform solution: a non-recursive function of ๐‘›.)
34
17
Resolving Recurrences
• Substitution method
• Guess a solution and check if it is correct
• Pros: rigorous, formal solution
• Cons: requires induction, how do you guess?
• Recursion trees
• Build a tree representing all recursive calls, infer the solution
• Pros: intuitive, visual, can be used to guess
• Cons: not too formal
• Master method
• Check the cookbook and apply the appropriate case
• Pros: formal, useful for most recurrences
• Cons: have to memorize, useful for most recurrence
35
Resolving Recurrences – Substitution
Method
• Steps
• Guess the solution
• Use induction to show that the solution works
• What is induction?
• A method to prove that a given statement is true for all-natural numbers
๐‘˜
• Two steps
• Base case: show that the statement is true for the smallest value of ๐‘› (typically 0)
• Induction step: assuming that the statement is true for any ๐‘˜, show that it must also
hold for ๐‘˜ + 1
36
18
Resolving Recurrences – Substitution
Method, induction
• A more serious example
•๐‘‡ ๐‘› =แ‰
2๐‘‡
1
๐‘›
2
๐‘–๐‘“ ๐‘› = 1
+๐‘›
๐‘–๐‘“ ๐‘› > 1
• guess ๐‘‡ ๐‘› = ๐‘› log ๐‘› + ๐‘›
• ๐‘‡ ๐‘› = Θ ๐‘› log ๐‘›
37
38
19
Resolving Recurrences – Substitution
Method, induction
• When we want an asymptotic solution to a recurrence, we don’t
worry about the base cases in our proof.
• Since we are ultimately interested in an asymptotic solution to a
recurrence, it will always be possible to choose base cases that work.
• When we want an exact solution, then we have to deal with base
case.
39
Resolving Recurrences – Substitution
Method, induction
• When solving recurrences with asymptotic notation
• assume ๐‘‡(๐‘›) = Θ(1) for small enough ๐‘›, don’t worry about base case
• show upper and lower bounds separately (๐‘‚, โ„ฆ)
• Example
• ๐‘‡(๐‘›) = 2๐‘‡(๐‘›/2) + Θ(๐‘›)
40
20
Resolving Recurrences – Recursion Trees
• Draw a “recursion tree” for the recurrence
• Nodes are the cost of a single subproblem
• Nodes have as their children the subproblems they are decomposed into
• Example
• ๐‘‡(๐‘›) = 2๐‘‡(๐‘›/2) + ๐‘›
41
Example -- Recursion Tree Method
• Example
• ๐‘‡ ๐‘› =๐‘‡
๐‘›
4
+๐‘‡
๐‘›
2
+ Θ(๐‘›2 )
• For upper bound, rewrite as ๐‘‡ ๐‘› ≤ ๐‘‡
• For lower bound, as ๐‘‡ ๐‘› = Ω(๐‘›2 )
๐‘›
4
+๐‘‡
๐‘›
2
+ ๐‘๐‘›2
42
21
Asymptotic Behavior of a Geometric Series
• Sum a geometric series
3
3 2
4
4
• ๐‘† = ๐‘›2 + ๐‘›2 +
• σ๐‘›๐‘–=1 ๐‘Ž๐‘– = ๐‘Ž
1−๐‘Ÿ ๐‘›
1−๐‘Ÿ
๐‘›2 + โ‹ฏ +
, σ∞
๐‘–=1 ๐‘Ž๐‘– = ๐‘Ž
3 ๐‘˜
4
1
1−๐‘Ÿ
๐‘›2
if ๐‘Ÿ < 1
• A decreasing geometric series behaves asymptotically just like its
1st term: ๐‘† = Θ ๐‘›2
• By symmetry, if S were increasing, it would behave asymptotically
like its final term: ๐‘† = ๐‘›2 + 2๐‘›2 + 22 ๐‘›2 + โ‹ฏ + 2๐‘˜ ๐‘›2 = Θ 2๐‘˜ ๐‘›2
43
Resolving Recurrences – Master Method
• “Cookbook” for many divide-and-conquer recurrences of the
form ๐‘‡(๐‘›) = ๐‘Ž๐‘‡(๐‘›/๐‘) + ๐‘“(๐‘›), where
• ๐‘Ž≥1
• ๐‘≥1
• ๐‘“(๐‘›) is an asymptotically positive function
• What are ๐‘Ž, ๐‘, and ๐‘“(๐‘›)?
• ๐‘Ž is a constant corresponding to ...
• ๐‘ is a constant corresponding to ...
• ๐‘“(๐‘›) is a function corresponding to ...
44
22
Resolving Recurrences – Master Method
• Problem: You want to solve a recurrence of the form ๐‘‡(๐‘›) =
๐‘Ž๐‘‡(๐‘›/๐‘) + ๐‘“(๐‘›), where
• ๐‘Ž ≥ 1, ๐‘ ≥ 1 and ๐‘“(๐‘›) is asymptotically positive function
• Solution:
• Compare ๐‘›log๐‘ ๐‘Ž vs. ๐‘“(๐‘›), and apply the Master Theorem
45
Resolving Recurrences – Master Method
• Intuition: the larger of the two functions determines the solution
• The three cases compare ๐‘“(๐‘›) with ๐‘›log๐‘
๐‘Ž
• Case 1: ๐‘›log๐‘ ๐‘Ž is larger – ๐‘‚ definition
• Case 2: ๐‘“(๐‘›) and ๐‘›log๐‘ ๐‘Ž grow at the same rate – Θ definition
• Case 3: ๐‘“(๐‘›) is larger – โ„ฆ definition
• The three cases do not cover all possibilities
• but they cover most cases we are interested in
46
23
The Master Theorem
• Case 1: If ๐‘“ ๐‘› = ๐‘‚ ๐‘›
then ๐‘‡ ๐‘› = Θ ๐‘›log๐‘ ๐‘Ž
log๐‘ ๐‘Ž −๐œ–
for some ๐œ– > 0
• Case 2: If ๐‘“ ๐‘› = Θ ๐‘›log๐‘ ๐‘Ž
then ๐‘‡ ๐‘› = Θ ๐‘›log๐‘ ๐‘Ž log ๐‘›
• Case 3: If ๐‘“ ๐‘› = Ω ๐‘› log๐‘ ๐‘Ž +๐œ– for some ๐œ– > 0
and if ๐‘Ž๐‘“ ๐‘›Τ๐‘ ≤ ๐‘๐‘“ ๐‘› for some ๐‘ < 1 and all sufficiently large ๐‘› (regularity
condition),
then ๐‘‡ ๐‘› = Θ ๐‘“ ๐‘›
A function ๐‘“ ๐‘› is polynomially bounded if ๐‘“ ๐‘› = ๐‘‚ ๐‘›๐‘˜ for some constant ๐‘˜.
You need to memorize these rules and be able to use them
47
The Master Theorem
Case 2:
If ๐‘“ ๐‘› = Θ ๐‘›log๐‘
๐‘Ž
then ๐‘‡ ๐‘› = Θ ๐‘›log๐‘
log ๐‘›
๐‘Ž
๐‘˜
log ๐‘›
for some constant ๐‘˜ ≥ 0
๐‘˜+1
48
24
Resolving Recurrences – Master Method,
examples
๐‘‡(๐‘›) = ๐‘Ž๐‘‡(๐‘›/๐‘) + ๐‘“(๐‘›),
๐‘›log๐‘ ๐‘Ž vs. ๐‘“(๐‘›)
• Examples:
• ๐‘‡ ๐‘› = 4๐‘‡
๐‘›
• ๐‘‡ ๐‘› = 4๐‘‡
๐‘›
• ๐‘‡ ๐‘› = 4๐‘‡
๐‘›
2
2
2
+n
+ ๐‘›2
+ ๐‘›3
49
Resolving Recurrences – Master Method,
examples
• Examples:
• ๐‘‡ ๐‘› = 5๐‘‡
๐‘›
2
+ Θ ๐‘›3
50
25
Resolving Recurrences – Master Method,
examples
• Examples:
• ๐‘‡ ๐‘› = 27๐‘‡
๐‘›
3
+Θ
๐‘›3เต—
log ๐‘›
51
Idea of master theorem
52
26
Resolving Recurrences – Master Method
• Case 3: If ๐‘“ ๐‘› = Ω ๐‘› log๐‘ ๐‘Ž +๐œ– for some ๐œ– > 0
and if ๐‘Ž๐‘“ ๐‘›Τ๐‘ ≤ ๐‘๐‘“ ๐‘› for some ๐‘ < 1 and all sufficiently large ๐‘›,
then ๐‘‡ ๐‘› = Θ ๐‘“ ๐‘›
The weight decreases geometrically from the root to the leaves.
The root holds a constant fraction of the total weight.
Intuition: cost is dominated by root, we can discard the rest
53
Resolving Recurrences – Master Method
• Case 1: If ๐‘“ ๐‘› =
๐‘‚ ๐‘› log๐‘ ๐‘Ž −๐œ– for some ๐œ– > 0
then ๐‘‡ ๐‘› = Θ ๐‘›log๐‘ ๐‘Ž
๐‘“(๐‘›) is polynomially smaller
than ๐‘›log๐‘ ๐‘Ž
Intuition: cost is dominated by
the leaves
54
27
Resolving Recurrences – Master Method
• Case 2: If ๐‘“ ๐‘› = Θ ๐‘›log๐‘ ๐‘Ž
then ๐‘‡ ๐‘› = Θ ๐‘›log๐‘ ๐‘Ž log ๐‘›
๐‘“(๐‘›) grows at the same rate of
๐‘›log๐‘ ๐‘Ž
Intuition: cost ๐‘›log๐‘ ๐‘Ž at each
level, there are log ๐‘› levels
55
Resolving Recurrences – Master Method
Case 3: If ๐‘“ ๐‘› = Ω ๐‘› log๐‘ ๐‘Ž +๐œ– for some ๐œ– > 0
and if ๐‘Ž๐‘“ ๐‘›Τ๐‘ ≤ ๐‘๐‘“ ๐‘› for some ๐‘ < 1 and all sufficiently large ๐‘›,
then ๐‘‡ ๐‘› = Θ ๐‘“ ๐‘›
What’s with the Case 3 regularity condition?
Generally, not a problem. It always holds whenever ๐‘“ ๐‘› = ๐‘›๐‘˜ and
๐‘“ ๐‘› = Ω ๐‘› log๐‘ ๐‘Ž +๐œ– for constant ๐œ– > 0. So, you don’t need to
check it when ๐‘“ ๐‘› is a polynomial.
56
28
Download