Computational Complexity
15-150
1
Computational Complexity 15-150 1 Desirable Properties of - - PowerPoint PPT Presentation
Computational Complexity 15-150 1 Desirable Properties of Programs What is the most desirable property of a program? Correctness Nobody cares about incorrect programs. Assuming you have a correct program, what is the next
15-150
1
next desirable property?
resources
2
integers
to run these two programs to find out P2 takes too much time.
3
4
running a program
execution
5
6
7
algorithm is not a good idea
compared.
increase?
8
How many steps are needed for evalution as a function of the value of n? (This is really wrong but convenient – bear with me.) 𝑋 𝑜 = Number of steps executed when argument has value 𝑜
9
Do you see a pattern here?
10
Do you see a pattern here?
argument 1 less
step there)
11
This is called a recurrence.
closed form
12
n steps Repeated Expansion
13
Case Definition Solution 𝑜 = 0 1 1 𝑜 > 0 𝑋
!"# 𝑜 = 3 + 𝑋 !"#(𝑜 − 1)
= 3 + 3 𝑜 − 1 + 1 = 3𝑜 + 3 − 3 + 1 = 3𝑜 + 1 3𝑜 + 1 𝑋
!"# 𝑜 = 3𝑜 + 1
14
What is exp’ computing?
𝑓𝑦𝑞! 𝑜 = 2"
What is the cost of square?
2 (body and *)
15
What is the recurrence for 𝑋
!"#$(𝑜) ? (𝑜 is again the value of the argument)
How many cases should we consider? 3 Why 5? Why 6? Function body, mod, compare =, if, div Function body, mod, compare =, if, -, *
16
Why? Why?
17
In general we do really care about the constants (1, 7 or 13)
18
Do you see another simplification? For odd 𝑜, 𝑜 − 1 𝑒𝑗𝑤 2 = 𝑜 𝑒𝑗𝑤 2 where you can pick 𝑙 = max(𝑙$, 𝑙%)
19
Again with some kindergarten algebra, we can easily find a closed form 1 + log% 𝑜 steps 𝑋!"#! 𝑜 = 𝑙 log% 𝑜 + 1 + 𝑙& = 𝑙 log% 𝑜 + 𝑙 + 𝑙& = 𝑙 log% 𝑜 + 𝑙′ For modeling purposes, it is convenient to assume that 𝑜 = 2' for some 𝑙 ≥ 0
20
50 100 150 200 250 300 350 10 20 30 40 50 60 70 80 90 100 3n+1 13log_2(n)+15
For small n, the first algorithm seems to be better As n increases, the second algorithm, is much better. We are making one very important but incorrect assumption?
21
constant multipliers.
22
constants 𝑑 > 0 and 𝑜' ≥ 0, such that for all 𝑜 ≥ 𝑜', 𝑑 𝑜 ≥ 𝑔 𝑜 .
23
24
25
is always on the right side of =/∈
26
We also need to know the complexity of the @ function
27
What does 𝑜 represent?
The length/size of the first list.
The size of the second list is not important. (Why?) This is indeed the correct way to model work (and span).
𝑋
@ 𝑜 = 𝑏;𝑜 + 𝑏< = 𝑃(𝑜)
28
Substituting 𝑋
@ 𝑜 = 𝑏"𝑜 + 𝑏# and combining constants we get
29
𝑋
$%& 𝑜 = 𝑏"
2 𝑜' + (𝑐"
(− 𝑏"
2 )n + 𝑐# = 𝑃(𝑜') Lower order terms
30
You only need to know the complexity of trevh 𝑋
ABCDE 𝑜 = 𝑑;𝑜 + 𝑑< = 𝑃(𝑜)
Tail recursive reverse is substantially more efficient.
31
the algorithm.
usually sufficient.
recurrences.
expansions.
32