Escolar Documentos
Profissional Documentos
Cultura Documentos
–Recurrence relations
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
RETURN
• To help focus on the parts of code that are executed the largest
number of times.
Which?
83920
83920 1’’
2’’
• Size of paper: n = 2 in2
• # of bits: n = 17 bits
• Which are reasonable?
# of digits: n = 5 digits
• Value: n = 83920
83920 1’’
2’’
• Size of paper: n = 2 in2 • Intuitive
• # of bits: n = 17 bits
• # of digits: n = 5 digits
• Value: n = 83920
83920 1’’
2’’
• Size of paper: n = 2 in2 • Intuitive
• # of bits: n = 17 bits • Formal
• # of digits: n = 5 digits
• Value: n = 83920
83920 1’’
2’’
• Size of paper: n = 2 in2 • Intuitive
• # of bits: n = 17 bits • Formal
• # of digits: n = 5 digits • Reasonable
• Value: n = 83920 # of bits =
3.32 * # of digits
83920 1’’
2’’
• Size of paper - n = 2 in2 • Intuitive
• # of bits - n = 17 bits • Formal
• # of digits - n = 5 digits • Reasonable
• Value - n = 83920 • Unreasonable
# of bits = log2(Value)
Value = 2#ofbits
COSC 3101 Winter, 2004
Two Example Algorithms
• Sum N array entries:
A(1) + A(2) + A(3) + …
• Factor value N:
Input N=5917 & Output N=97*61.
Algorithm: N/2, N/3, N/4, …. ?
Time?
Is this reasonable?
No!
One is considered fast and the other slow!
COSC 3101 Winter, 2004
Two Example Algorithms
• Sum N array entries:
A(1) + A(2) + A(3) + …
• Factor value N:
Input N=5917 & Output N=97*61.
Algorithm N/2, N/3, N/4, …. ?
14,23,25,30,31,52,62,79,88,98
• # of elements: n = 10 elements
• # of elements: n = 10 elements
Is this reasonable?
• A function mapping:
– “size” of input “time” T(n) executed .
• Or more precisely:
– # of bits n needed to represent the input # of operations T(n) executed .
n (n+1) = 2S
n (n+1) = 2S
n (n+1) = 2S
n (n+1) = 2S
n (n + 1)
S=
2
COSC 3101 Winter, 2004
1 + 2 + 3 + ... + n-1 + n = S
n (n+1) = 2S
1 2........n
COSC 3101 Winter, 2004
1 + 2 + 3 + ... + n-1 + n = S
= number of white dot
n + n-1 + n-2 + ... + 2 + 1 = S
n (n+1) = 2S
n ....... 2 1 1 2........n
COSC 3101 Winter, 2004
1 + 2 + 3 + ... + n-1 + n = S
= number of white dot
n + n-1 + n-2 + ... + 2 + 1 = S
n (n+1) = 2S
n (n+1) = 2S
n (n + 1) n
S= n
2
n
n
n+1 n+1 n+1 n+1 n+1
COSC 3101
Note = Θ (# of terms · last term)) Winter, 2004
Arithmetic Sum
∑i=1..n i=1+2+3+...+n
= Θ (# of terms · last term)
n +1
Sr = r + r + r +... + r + r
2 3 n
n +1
S − Sr = 1 −r
n +1
−1
r
S=
r −1
COSC 3101 Winter, 2004
Geometric Sum
n +1
When r>1?
r −1
∑i= 0..n ri =
r −1
= Θ (biggest term)
1 −r
= Θ (1)
n
f(i) = 1
∑i=1..n f(i) = n
COSC 3101 Winter, 2004
Sum of Shrinking Function
n
f(i) = ?
∑i=1..n f(i) = n1/2
COSC 3101 Winter, 2004
Sum of Shrinking Function
∞
f(i) = 1/2i
∑i=1..n f(i) = 2
COSC 3101 Winter, 2004
Sum of Shrinking Function
n
f(i) = 1/i
∑i=1..n f(i) = ?
COSC 3101 Winter, 2004
Harmonic Sum
Arithmetic Sums
n
n
1 c +1
∑
i =0
c
i ≅ ∫ x dx
c
=
c +1
n = θ (n c +1
) = θ (n ⋅ n c
)
0
Geometric Sums
n
n
1
∑
i =0
b i
≅ ∫ b dx x
=
ln b
(b n
− 1) = θ (b n
)
0
= θ (last term)
(True whenever terms increase rapidly)
–Geometric Like
–Arithmetic Like
–Harmonic
–Bounded Tail
Birds
Fish
Mammals Giraffe
Reptiles
Dogs
Exp.
8·2n / n100 + n3
Irrelevant
20.5n < 8· 2n / n100 + n3 < 2n
COSC 3101 Significant Less significant Winter, 2004
Adding Made Easy
Four Classes of Functions
3
4
Classic example:
∑i=1..n r i = 1 + r + r + r +. . . + r
2 3 n
n +1
r −1
= = θ(r
n
) Last Term
r −1
= θ(f(n)) when r>1.
Even bigger!
Functions in between?
n n
(The strongest function determines the class.)
8·2(i+1)
i100
f(i+1) = (i+1)100 8·2i = 2
f(i) (i+1)100
i100
1
= 2 (1+1/ )100 ≥ 1.9 ≥ 1/.51
i
f (i )
Suppose that (*) ∃n0 > 0 < 1∀i ≥ n0
f (i + 1)
n n
1 n
Thus ∑f (i ) ≤ ∑α
i =1 i =1
n −i
f (n ) <
1 −α
f (n ) → ∑f (i ) ∈ O (f (n ))
i =1
f (i ) (1 + 1/ i )100 1
Thus < 1 iff < 1 ↔ 1 + 1/ i < 2.01 ↔ i > .01 = 143.8
f (i + 1) 2 2 −1
n n
Thus (*) holds for n0 = 144 → ∑f (i ) ∈O (f (n )) → ∑f (i ) ∈θ (f (n ))
i =1 i =1
Done
Simple example:
∑i=1..n 1 = n · 1
∑i=1..n i=1+2+3+...+n
∑i=1..n i =
1 + . . . + n/2 + . . . + n
∑i=1..n i =
1 + . . . + n/2 + . . . + n
∑i=1..n i2 = 1 2 + 2 2 + 3 2 + . . . + n 2
∑i=1..n i =
12 + . . . + (n/2)2 + . . . + n2
1
/4 n 2
Again half the terms
are roughly the same.
∑i=1..n i =
12 + . . . + (n/2)2 + . . . + n2
1
/4 n 2
Again half the terms
are roughly the same.
∑i=1..n i2 = θ(n · n2)
COSC 3101 Winter, 2004
Arithmetic Like:
f(n) = n ⇒ ∑i=1..n f(i) = θ(n·f(n))
θ(1)-1
∑i=1..n f(i)
≈ area under curve
/2 · f(n/2)
n
θ(n · f(n))
= n/2 · f(n/2)
= area of small square
≤ ∑i=1..n f(i)
≈ area under curve
≤ area of big square
= n · f(n)
θ(n · f(n))
?
= /2 · f( /2)
n n
f(n) = nθ(1)
∑i=1..n f(i) = ?
f(n) = nθ(1)
∑i=1..n 2i = 21 + 22 + 23 + . . . + 2n
f(n) = nθ(1)
∑i=1..n 2i = 21 + 22 + 23 + . . . + 2n
⇒ = θ(n·f(n)) = θ(n · 2n)
n
1 1
Upper Extreme: ∑i
i =1
1000
≅
1001
n 1001
=
1001
n ⋅ f (n )
All functions
in between.
Half done
n
f(i) = 1
∑i=1..n f(i) = n
COSC 3101 Winter, 2004
Sum of Shrinking Function
n
f(i) = ?1/i1/2
∑i=1..n f(i) = n1/2
COSC 3101 Winter, 2004
Sum of Shrinking Function
n
f(i) = 1/i
∑i=1..n f(i) = log n
COSC 3101 Winter, 2004
Sum of Shrinking Function
∞
f(i) = 1/2i
∑i=1..n f(i) = 2
COSC 3101 Winter, 2004
Arithmetic Like:
f(n) = n ⇒ ∑i=1..n f(i) = θ(n·f(n))
θ(1)-1
∑i=1..n f(i) = ?
θ(n · f(n))
θ(n · f(n))
1 n
1 n
1
⇒ log2 n ≤ ∑ ≤ log2 n + 1 ⇒ ∑ = θ (log2 n )
2 i =1 i i =1 i
COSC 3101 Winter, 2004
Arithmetic Like:
f(n) = nθ(1)-1
⇒ ∑i=1..n f(i) = θ(n·f(n))
not included
All functions
in between.
COSC 3101
(For +, -, ⋅ , ÷ , exp, log functions f(n))
Winter, 2004
Adding Made Easy
Done
Harmonic
1 n
1 n
1
⇒ log2 n ≤ ∑ ≤ log2 n + 1 ⇒ ∑ = θ (log2 n )
2 i =1 i i =1 i
∑i=1..n 1/i
= 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + …+ 1/n
= Θ (log(n))
COSC 3101 Winter, 2004
Adding Made Easy
All functions
in between.
1
2i
No Lower Extreme: ∑i=1..n 22 = θ(1).
COSC 3101 Winter, 2004
Bounded Tail
Conclusion
Case 1: Case 2:
f (n ) = cb n log n
an d e
f (n ) = cn d loge n
c >0
c >0
0 <b <1 d < −1
a >0 e ∈ ( −∞, +∞)
d ∈ ( −∞, +∞)
e ∈ (−∞, +∞)
COSC 3101 Winter, 2004
Adding Made Easy
Done
COSC 3101 Winter, 2004
Adding Made Easy
Missing Functions
n
n
n l o ng
logn
/n
1
/nlogn
COSC 3101 Winter, 2004
Adding Made Easy
• Geometric Like: If f(n) ≥ 2Ω(n), then ∑i=1..n f(i) = θ(f(n)).
• Arithmetic Like: If f(n) = nθ(1)-1, then ∑i=1..n f(i) = θ(n · f(n)).
• Harmonic: If f(n) = 1/n , then ∑i=1..n f(i) = logen + θ(1).
• Bounded Tail: If f(n) ≤ n-1-Ω(1), then ∑i=1..n f(i) = θ(1).
(For +, -, ⋅ , ÷ , exp, log functions f(n))
T(1) = 1
T(n) = a T(n/b) + f(n)
loop i=1..a
Eg(n/b)
COSC 3101 Winter, 2004
Recurrence Relations
≈ Time of Recursive Program
procedure Eg(int n)
if(n≤ 1) then Given size 1,
put “Hi” the program outputs
else T(1)=1 Hi’s.
loop i=1..f(n)
put “Hi”
loop i=1..a
Eg(n/b)
•Recurrence Relation:
T(1) = 1 & T(n) = aT(n/b) + f(n)
which is bigger?
Guess
•Guess: G(n) = cn = cn
α logb
•Guess: G(n) = cn = cn
α = cn logb
= cn log2
T(1) = 1
• T(n) = a T(n/b) + f(n)
T(n) = f(n)
a
f(n/b)
a
T(n/b) T(n/b) T(n/b)
T(n/b2)T(n/b2)T(n/ b2T(n/
) b2)
T(n/b2)T(n/b2)T(n/ b2T(n/
) b2) T(n/b2)T(n/b2)T(n/ b2T(n/
) b 2) T(n/b2)T(n/b2)T(n/ b2T(n/
) b2) T(n/b2)T(n/b2)T(n/ b2T(n/
)
T(n/b2)T(n/b2)T(n/ b2T(n/
) b2) T(n/b2)T(n/b2)T(n/ b2T(n/
) b 2) T(n/b2)T(n/b2)T(n/ b2T(n/
) b2) T(n/b2)T(n/b2)T(n/ b2T(n/
)
11111111111111111111111111111111 . . . . . . 111111111111111111111111111111111
COSC 3101 Winter, 2004
Evaluating: T(n) = aT(n/b)+f(n)
Level
0
1
2
0
1
2
0 n
1
2
0 n
1 n/b
2
0 n
1 n/b
2 n/b2
0 n
1 n/b
2 n/b2
i n/bi
h n/bh
0 n
1 n/b
2 n/b2
i n/bi
h n/bh
0 n
1 n/b
2 n/b2
i n/bi
h n/bh = 1
base case
COSC 3101 Winter, 2004
Evaluating: T(n) = aT(n/b)+f(n)
Level Instance
size
0 n
1 n/b
2 n/b2
i n/bi
h n/bh = 1
0 n
1 n/b
2 n/b2
i n/bi
i n/bi
h = logn /logb 1
i n/bi f(n/bi)
i n/bi f(n/bi)
i n/bi f(n/bi) ai
i n/bi f(n/bi) ai
i n/bi f(n/bi) ai
COSC 3101
loga
/logb Winter, 2004
Evaluating: T(n) = aT(n/b)+f(n)
Level Instance Work # stack Work in Level
size in stack frames
frame
0 n f(n) 1
1 n/b f(n/b) a
2 n/b2 f(n/b2) a2
i n/bi f(n/bi) ai
If a Geometric Sum
∑i= 0..n xi = θ(max(first term, last term))
Suppose T (n ) = aT (n / b ) + f (n ), a ≥ 1, b > 1
Suppose T (n ) = aT (n / b ) + f (n ), a ≥ 1, b > 1
AND
∃c < 1,n0 >0 such that af (n / b ) ≤ cf (n ) ∀n ≥ n0
THEN T (n ) ∈ θ (f (n ))
Additional regularity condition
2 log n /2 logn /2
↔ n > 21.15 / 0.15 = 203.2.
5 5
1 log256 1 8
e . g . Let n0 = 256 → c ≥ = = 0.975
2 log128 2 7
Thus regularity condition holds for n0 = 256, c = 0.98
Thus T (n ) = θ (f (n )) = θ (n 3 /log5 n )
a = 4 log a
n b =n
2
b = 2
f (n ) = 2n
Thus T (n ) = θ (f (n )) = θ (2n )
a = 4
n logb a = n 2
b = 2
f (n ) = n log5 n
a = 4
n logb a = n 2
b = 2
f (n ) = n 2
hh == ?/b
n
/b n
/b
n
n-hb T(0) a a · T(0)
|base case| = 0 = n-hb
Likely dominated by base cases Exponential
h = /b
n
COSC 3101 Winter, 2004
Evaluating: T(n) = 1T(n-b)+f(n)
Work
Instance # stack Work in
Level in stack
size frames Level
frame
0 n f(n) 1 f(n)
1 n-b f(n-b) 1 f(n-b)
2 n-2b f(n-2b) 1 f(n-2b)