Daa

82
Algorithm Correctness & Analysis

Transcript of Daa

Page 1: Daa

Algorithm Correctness & Analysis

Page 2: Daa

SummaryConfidence in algorithms from

testing and correctness proofCorrectness of recursive algorithms

proved directly by inductionExamples: Fibonacci numbers,

maximum, multiplication

Page 3: Daa

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

Page 4: Daa

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

Page 5: Daa

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))

Page 6: Daa

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

Page 7: Daa

Recursive Maximum

Page 8: Daa

Recursive maximum

Page 9: Daa

Recursive Multiplication

Page 10: Daa

Recursive Multiplication

Page 11: Daa

Recursive Multiplication

Page 12: Daa

Analysis

Page 13: Daa

Summary

Page 14: Daa

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

Page 15: Daa

Big oh

Page 16: Daa

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.)

Page 17: Daa

Second example

Note that we need

Page 18: Daa

Big Omega

Page 19: Daa

Big Theta

Page 20: Daa

True or false

Page 21: Daa

Adding Big Ohs

Page 22: Daa

Continue…

Page 23: Daa

Multiplying Big Ohs

Page 24: Daa

Types of Analysis

Page 25: Daa

Example…

Page 26: Daa

Time Complexity

Page 27: Daa

Multiplication

Page 28: Daa

Bubble sort

Page 29: Daa

Analysis Trick

Page 30: Daa

Example

Page 31: Daa

Lies, Damn Lies, and Big-Os

Page 32: Daa

Algorithms and Problems

Page 33: Daa

Algorithms Analysis 2

Page 34: Daa

The Heap

Page 35: Daa

Contd…

Page 36: Daa

To Delete the Minimum

But we have lost the tree structure

Page 37: Daa

Contd…

But we have violated the structure condition

Page 38: Daa

Contd…

Page 39: Daa

Contd…

Page 40: Daa

What Does it work?

Page 41: Daa

Contd…

Page 42: Daa

Contd…

Page 43: Daa

To Insert a New Element

Page 44: Daa

Contd…

Page 45: Daa

Contd…

Page 46: Daa

Contd…

Page 47: Daa

Why Does it Work

Page 48: Daa

Contd…

Page 49: Daa

Implementing a Heap

Page 50: Daa

Analysis of Priority Queue Operation

Page 51: Daa

Analysis of l(n)

Page 52: Daa

Contd…

Page 53: Daa

Example

Page 54: Daa

Heapsort

Page 55: Daa

Building a Heap Top Down

Page 56: Daa

Contd…

Page 57: Daa

Contd…

Page 58: Daa

Building a Heap Bottom Up

Page 59: Daa

Continue…

Page 60: Daa

Continue…

Page 61: Daa

Algorithms Course Notes Algorithm Analysis 3

summaryAnalysis of recursive algorithms:Recurrence relationsHow to derive themHow to solve them

Page 62: Daa

Deriving Recurrence Relations

Page 63: Daa

Continue…

Page 64: Daa

Example

Page 65: Daa

Continue…

Page 66: Daa

Analysis of Multiplication

Page 67: Daa

Solving Recurrence Relations

Page 68: Daa

The Multiplication Example

Page 69: Daa

Repeated Substitution

Page 70: Daa

Warning

Page 71: Daa

Reality Check

Page 72: Daa

Merge Sorting

Page 73: Daa

Continue…

Page 74: Daa

A General Theorem

Page 75: Daa

Proof Sketch

Page 76: Daa

Continue…

Page 77: Daa

Geometric Progressions

Page 78: Daa

Back to the Proof

Page 79: Daa

Continue…

Page 80: Daa

Messy Details

Page 81: Daa

Continue…

Page 82: Daa

Example