Big O and Algorithms Part 1 032613 by

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

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

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

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

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

What affects an algorithm’s runtime • Computer architecture • Programming language and compiler External factors • Other processes running on the computer • Parameters (input) to the algorithm • Design of the algorithm Design factors 6

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

Example: Finding smallest m values output = 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) = input(j); j_out = j; end mark that j_out has been used as an output end return output See findmin. m 8

Example: Finding smallest m values • Constants that depend on implementation details, architecture, etc. Dominant factor 9

Key ideas in runtime analysis • 10

Asymptotic relationships • 11

Ordering of functions • See plot_functions. m 12

Example: comparing findmin algorithms 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) = input(j); j_out = j; end mark that j_out has been used as an output end return output = findmin_sort(input, m) sorted_input = sort(input, ascending); output = sorted_input(1…m); See findmin_cost_compare_script. m 13

Big-O • 14


Proving Big-O with induction • 16

Things to remember • 17

Next class • Analyzing pseudocode • Some key concepts to remember for midterm 18
- Slides: 18