03/26/13

18
03/26/13 Big O and Algorithms (Part 1) Discrete Structures (CS 173) Derek Hoiem, University 1 by Wolfdog1

description

Big O and Algorithms (Part 1). 03/26/13. b y Wolfdog1. Discrete Structures (CS 173) Derek Hoiem, University of Illinois. Announcements. Midterm next Tuesday (April 2) Does not include big-O/algorithms More difficult than Midterm 1 All the usual homeworks due this week - PowerPoint PPT Presentation

Transcript of 03/26/13

Page 1: 03/26/13

03/26/13

Big O and Algorithms (Part 1)

Discrete Structures (CS 173)Derek Hoiem, University of Illinois 1

by Wolfdog1

Page 2: 03/26/13

Announcements

• Midterm next Tuesday (April 2)– Does not include big-O/algorithms– More difficult than Midterm 1

• All the usual homeworks due this week

• No homeworks due next week (except maybe reading quiz next Thursday)– Exam prep materials will be up by this Thurs

2

Page 3: 03/26/13

This week

• How do we characterize the computational cost of an algorithm?

• How do we compare the speed of two algorithms?

• How do we compute cost based on code or pseudocode?

3

Page 4: 03/26/13

Today

• How do we characterize the computational cost of an algorithm?

• How do we compare the speed of two algorithms?

• How do we compute cost based on code or pseudocode?

4

Page 5: 03/26/13

What affects an algorithm’s runtime

• Computer architecture

• Programming language and compiler

• Other processes running on the computer

• Parameters (input) to the algorithm

• Design of the algorithm

5

Page 6: 03/26/13

What affects an algorithm’s runtime

• Computer architecture

• Programming language and compiler

• Other processes running on the computer

• Parameters (input) to the algorithm

• Design of the algorithm

6

External factors

Design factors

Page 7: 03/26/13

How should we think about an algorithm’s computational cost?

• Time taken?

• Number of instructions called?

• Expected time as a function of the inputs?

• How quickly the cost grows as a function of the inputs

7

Page 8: 03/26/13

Example: Finding smallest m valuesoutput = findmin(input, m) % returns m smallest inputs

for each ith output (there are m of these) for each jth input (there are n of these)

if j is not used and input(j) < output(i) output(i) = input(j);

j_out = j; end end mark that j_out has been used as an outputendreturn output

8See findmin.m

Page 9: 03/26/13

Example: Finding smallest m values

minvals = findmin(vals, m)

9

Constants that depend on implementation details, architecture, etc.

Dominant factor

Page 10: 03/26/13

Key ideas in runtime analysis

1. Model how algorithm speed depends on the size of the input/parameters– Ignore factors that depend on architecture or details of compiled

code

2. We care mainly about asymptotic performance– If the input size is small, we’re not usually worried about speed

anyway

3. We care mainly about dominant terms– An algorithm that takes time takes almost as long (as a ratio) as

one that takes time if is large

10

Page 11: 03/26/13

Asymptotic relationships

11If , then for non-zero

Page 12: 03/26/13

Ordering of functions

12See plot_functions.m

Page 13: 03/26/13

Example: comparing findmin algorithms

See findmin_cost_compare_script.m13

output = findmin(input, m) % can be implemented different ways

for each ith output (there are m of these) for each jth input (there are n of these)

if j is not used and input(j) < output(i) output(i) = input(j);

j_out = j; end end mark that j_out has been used as an outputendreturn output

output = findmin_sort(input, m)sorted_input = sort(input, ascending);output = sorted_input(1…m);

Page 14: 03/26/13

Big-O is iff there are such that for every

O(

14

Page 15: 03/26/13

is if is and is

15

Page 16: 03/26/13

Proving Big-O with inductionClaim: is Definition: is iff there are such that

for every .Choose and : ,

Proof: Suppose is an integer. I need to show for . I will use induction on .Base case: Induction: Suppose for . We need to show .

16

Page 17: 03/26/13

Things to remember• Model algorithm complexity in terms of how much the cost increases as the

input/parameter size increases

• In characterizing algorithm computational complexity, we care about– Large inputs– Dominant terms

• if the dominant terms in are equivalent or dominated by the dominant terms in

• if the dominant terms in are equivalent to those in

17

Page 18: 03/26/13

Next class• Analyzing pseudocode

• Some key concepts to remember for midterm

18