Big O and Algorithms (Part 1)
-
Upload
garrison-neal -
Category
Documents
-
view
21 -
download
2
description
Transcript of Big O and Algorithms (Part 1)
![Page 1: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/1.jpg)
Big O and Algorithms (Part 1)
Discrete Structures (CS 173)Madhusudan Parthasarathy, University of Illinois 1
by Google today
![Page 2: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/2.jpg)
Announcements
• Midterm 2: how did it go?
• All the usual homeworks due this week
2
![Page 3: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/3.jpg)
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: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/4.jpg)
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: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/5.jpg)
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
5
![Page 6: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/6.jpg)
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
6See findmin.m
![Page 7: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/7.jpg)
Example: Finding smallest m values
minvals = findmin(vals, m)
7
Constants that depend on implementation details, architecture, etc.
Dominant factor
![Page 8: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/8.jpg)
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
8
![Page 9: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/9.jpg)
Asymptotic relationships
9If , then for non-zero
![Page 10: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/10.jpg)
Ordering of functions
10
![Page 11: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/11.jpg)
11
![Page 12: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/12.jpg)
12
![Page 13: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/13.jpg)
Example: comparing findmin algorithms
13
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: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/14.jpg)
Big-O: motivation
14
![Page 15: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/15.jpg)
Big-O is iff there are such that for every
O(
15
![Page 16: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/16.jpg)
16
![Page 17: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/17.jpg)
is if is and is
17
![Page 18: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/18.jpg)
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 .
18
![Page 19: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/19.jpg)
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
19
![Page 20: Big O and Algorithms (Part 1)](https://reader035.fdocuments.us/reader035/viewer/2022062407/56812b33550346895d8f3d91/html5/thumbnails/20.jpg)
Next class• Analyzing pseudocode
20