64
Lecture 3 Algorithm Analysis

Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Embed Size (px)

Citation preview

Page 1: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Lecture 3

Algorithm Analysis

Page 2: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Motivation

• Which algorithm to use?

Page 3: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Step-Counting

• Assumptions

Page 4: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Step-counting: equal work

• Adding/Multiplication/Division

• Trigonometric functions

Page 5: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Step-counting: no optimization

• Unrolling Loops

Page 6: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Sample code (simple operations)

Page 7: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Sample code (while loop)

Page 8: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Sample code (for loop)

Page 9: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Sample code: conditionals

Page 10: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Sample code: conditionals

Page 11: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Which branch do we follow?

• Best Case

• Average Case

• Worst Case

Page 12: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Code Sample

if (x < 0){

for (i = 0; i < n; i++){

print(n)

}

}else print false

Page 13: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Sample Codefor (i = 0; i < n; i++){

if (a[i] > x){

for (j = 0; j < n; j++){

b[j] = b[i]

}

}else{ b[j] = 0 }

}

Page 14: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Limitations of step-counting

• Recursion

• Do Constants Matter?

Page 15: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Asymptotic Analysis

• Generalization/Categorization

• Abstraction

Page 16: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Big-O

• Definition

Page 17: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Graph

Page 18: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Using Big-O

• Show 7n+8 = O(n)

Page 19: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Using Big-O

Page 20: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Using Big-O

• Show 7n+8 = O(n^2)

Page 21: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Using Big-O

Page 22: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Upper Bounds

Page 23: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Common LabelsFunction Name Example

Page 24: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Theorems

Page 25: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Theorems

Page 26: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Theorems

Page 27: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Function Properties

Page 28: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Useful Tool: limits

Page 29: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Properties of Big-O and friends

• Symmetry

• Transpose Symmetry

Page 30: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Properties of Big-O and friends

• Reflexivity

• Transitivity

Page 31: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Common Misunderstandings

• Bounds vs. best/worst cases

Page 32: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Asymptotic Analysis vs. Running time

Page 33: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Lecture 3

Algorithm Analysis

Page 34: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Motivation

• Which algorithm to use?

Page 35: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Step-Counting

• Assumptions

Page 36: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Step-counting: equal work

• Adding/Multiplication/Division

• Trigonometric functions

Page 37: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Step-counting: no optimization

• Unrolling Loops

Page 38: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Sample code (simple operations)

Page 39: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Sample code (while loop)

Page 40: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Sample code (for loop)

Page 41: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Sample code: conditionals

Page 42: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Sample code: conditionals

Page 43: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Which branch do we follow?

• Best Case

• Average Case

• Worst Case

Page 44: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Code Sample

if (x < 0){

for (i = 0; i < n; i++){

print(n)

}

}else print false

Page 45: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Sample Codefor (i = 0; i < n; i++){

if (a[i] > x){

for (j = 0; j < n; j++){

b[j] = b[i]

}

}else{ b[j] = 0 }

}

Page 46: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Limitations of step-counting

• Recursion

• Do Constants Matter?

Page 47: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Asymptotic Analysis

• Generalization/Categorization

• Abstraction

Page 48: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Big-O

• Definition

Page 49: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Graph

Page 50: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Using Big-O

• Show 7n+8 = O(n)

Page 51: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Using Big-O

Page 52: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Using Big-O

• Show 7n+8 = O(n^2)

Page 53: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Using Big-O

Page 54: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Upper Bounds

Page 55: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Common LabelsFunction Name Example

Page 56: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Theorems

Page 57: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Theorems

Page 58: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Theorems

Page 59: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Function Properties

Page 60: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Useful Tool: limits

Page 61: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Properties of Big-O and friends

• Symmetry

• Transpose Symmetry

Page 62: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Properties of Big-O and friends

• Reflexivity

• Transitivity

Page 63: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Common Misunderstandings

• Bounds vs. best/worst cases

Page 64: Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?

Asymptotic Analysis vs. Running time