daa
TRANSCRIPT
Algorithm Correctness & Analysis
SummaryConfidence in algorithms from
testing and correctness proofCorrectness of recursive algorithms
proved directly by inductionExamples: Fibonacci numbers,
maximum, multiplication
CorrectnessHow do we know that an algorithm works?Logical method for checking correctness
TestingCorrectness proof
Testing vs. Correctness ProofsTesting: try the algorithm on sample inputsCorrectness Proof: Prove mathematically;
testing may not found obscure bugsUsing testing alone can be dangerous
Correctness of Recursive algorithmsTo prove correctness of recursive algorithm:
Prove it by induction on the size of the problem being solved
Base of recursion is base of inductionNeed to prove the recursive calls are given
sub-problems, i.e., no infinite recursionInductive step: assume that recursive calls
work correctly, and use this assumption to prove that the current call works correctly
Recursive Fibonacci NumbersFibonacci numbers: F0 = 0, F1 =1, and for all n ≥ 2,
Fn = Fn-2 + Fn-1
function fib(n) Comment return Fn
1. If n ≤ 1 then return (n)2. else return (fib(n-1) + fib (n-2))
Recursive Fibonacci NumbersClaim: For all n ≥ 0, fib(n) return Fn
Base: For n =0, fib(n ) returns 0 as claimed. For n = 1, fib(n) returns 1 as claimed.Induction: Suppose that n ≥ 2 and for all 0 ≤ m < n, fib (m) returns Fm .
Required to prove fib(n) returns Fn
What does fib(n) returns?
fib(n-1) + fib(n-2) = Fn-1 + Fn-2 (by Ind. Hyp.)
= Fn
Recursive Maximum
Recursive maximum
Recursive Multiplication
Recursive Multiplication
Recursive Multiplication
Analysis
Summary
Constant Multiples
Analyze the resource usage of an algorithm to within a constant multiple.
Why? Because other constant multiples creep in when translating
from an algorithm to executable code: Programmer abilityProgrammer effectivenessCompilerComputer hardwareRecall: Measure resource usage as a function of input size
Big oh
Example
Most big-Os can be proved by induction.
First Example: log n = O(n).
Claim : For all n >1, log n <n . The proof is by induction on n.
The claim is trivially for n=1,since 0<1. Now suppose n > 1 and
log n < n. Then,
log (n+1)
< log 2n
= log n+1
< n+1 (by ind. hyp.)
Second example
Note that we need
Big Omega
Big Theta
True or false
Adding Big Ohs
Continue…
Multiplying Big Ohs
Types of Analysis
Example…
Time Complexity
Multiplication
Bubble sort
Analysis Trick
Example
Lies, Damn Lies, and Big-Os
Algorithms and Problems
Algorithms Analysis 2
The Heap
Contd…
To Delete the Minimum
But we have lost the tree structure
Contd…
But we have violated the structure condition
Contd…
Contd…
What Does it work?
Contd…
Contd…
To Insert a New Element
Contd…
Contd…
Contd…
Why Does it Work
Contd…
Implementing a Heap
Analysis of Priority Queue Operation
Analysis of l(n)
Contd…
Example
Heapsort
Building a Heap Top Down
Contd…
Contd…
Building a Heap Bottom Up
Continue…
Continue…
Algorithms Course Notes Algorithm Analysis 3
summaryAnalysis of recursive algorithms:Recurrence relationsHow to derive themHow to solve them
Deriving Recurrence Relations
Continue…
Example
Continue…
Analysis of Multiplication
Solving Recurrence Relations
The Multiplication Example
Repeated Substitution
Warning
Reality Check
Merge Sorting
Continue…
A General Theorem
Proof Sketch
Continue…
Geometric Progressions
Back to the Proof
Continue…
Messy Details
Continue…
Example