Title: Relevant Mathematics
1Relevant Mathematics
Thinking about Algorithms Abstractly
Logic Quantifiers The Time Complexity of an
Algorithm Classifying Functions Adding Made
Easy Recurrence Relations
- Jeff Edmonds
- York University
COSC 3101
Lecture ?
2Some Math
Done here
In Iterative Algs (GCD) Repeated here
3Some Math
You are on your own.
Done here
4Some Math
In Iterative Algs (Insertion Sort) In
Recursive Algs (Multiplying) Repeated here.
In Recursive Algs (Multiplying, Towers of
Hanoi,Merge Sort) Repeated here.
5Quantifers
6Quantifiers, Do you understand them?
? g, " b, Loves(b, g)
A(I)P(I) means algorithm A gives the required
output for problem P on
instance I. Time(A,I) is the running time of
algorithm A on instance
I. T(n) some function like n2.
- Express
- Problem P is computable by some algorithm.
- Problem P is computable in time T(n).
- Problem P is computable in polynomial time.
- The computational class Exponential Time
is strictly bigger than the computational
class Polynomial Time.
7Understand Quantifiers!!!
" b, Loves(b, MM)
8Understand Quantifiers!!!
" b, Loves(b, MM)
Every boy loves Marilin Monro
False
True
9Understand Quantifiers!!!
" b, Loves(b, MM)
Every boy loves Marilin Monro
b vs MM?
b is a variable.
MM is an object. (cannot appear in ? or ?) The
statement is about MM.
10Understand Quantifiers!!!
" b, Loves(b, g)
Every boy loves g
b vs g?
b is a free variable. (appears in ? or ?) The
statement is about the set of all boys.
g is a bound variable. (does not appear in ? or
?) The statement is about g.
x5 loop i 1..9 x end loop
11Understand Quantifiers!!!
" b, Loves(b, g)
g is a bound variable. (does not appear in ? or
?) The statement is about g.
Loved(g) " b, Loves(b, g)
12Understand Quantifiers!!!
g, " b, Loves(b, g)
There is a girl whom every boy loves
False
True
13Understand Quantifiers!!!
" b, g, Loves(b, g)
Every boy loves some girl.
False
True
Still True
14Understand Quantifiers!!!
" b, g, Loves(b, g)
g, " b, Loves(b, g)
vs
- Could be a separate girl for each boy.
15Understand Quantifiers!!!
We read this left to right.
I produce the object when it is a .
I produce the object when it is a ".
I can always win if and only if statement is true.
16Understand Quantifiers!!!
Easy
I produce Sam
And prove Sam does not loves MM.
I can win. Hence, statement is true.
17Understand Quantifiers!!!
18Understand Quantifiers!!!
Instead play the game.
Sam does not loves MM.
I cannot win. Hence, statement is false.
19Understand Quantifiers!!!
I prove Sam does loves MM.
I can always win. Hence, statement is true.
20Understand Quantifiers!!!
I prove x does loves MM.
This proof needs to work without knowing x
I can always win. Hence, statement is true.
21Understand Quantifiers!!!
I give an alg with input x, and output a proof
that x loves MM.
I can always win. Hence, statement is true.
22Understand Quantifiers!!!
I claim there is a girl whom every boy loves.
Oh yeah! Prove it.
I produce MM
I proceed to prove every boy loves MM
23Understand Quantifiers!!!
I proceed to prove every boy loves MM
No! I produce boy x.
I give an alg with input x, and output a proof
that x loves MM.
I can always win. Hence, statement is true.
24Understand Quantifiers!!!
I claim Every boy loves some girl.
No! I produce boy x.
I proceed to prove x loves some girl.
I produce girl y
I prove x loves y.
I can always win. Hence, statement is true.
25Understand Quantifiers!!!
I claim Every boy loves some girl.
No! I produce boy x.
I proceed to prove x loves some girl.
I give an alg with input x, and output an yand
a proof that x loves y.
26Understand Quantifiers!!!
I give an alg with input y, and output an xand
a proof that x loves y.
You give Sam,I give Beth.
You give Bob, I give MM.
27Understand Quantifiers!!!
g, " b, Loves(b, g)
Ø
" g, Ø " b, Loves(b, g)
" g, b, Ø Loves(b, g)
" g, b, ØLoves(b, g)
Ø moves right
28Understand Quantifiers!!!
The relation is true. xy x (5-x) 5
I can always win. Hence, the statement is true.
29Understand Quantifiers!!!
The relation is true. xy x 5/x 5
I can always win. Hence, the statement is true.
What a minute the statement is false!
30Understand Quantifiers!!!
I can always win. Hence, the negated statement is
true and the original statement is false.
31Classifying Functions
Giving an idea of how fast a function grows
without going into too much detail.
32Definition of Theta
f(n) ?(g(n))
33Definition of Theta
f(n) ?(g(n))
- f(n) is sandwiched between c1g(n) and c2g(n)
34Definition of Theta
f(n) ?(g(n))
- f(n) is sandwiched between c1g(n) and c2g(n)
- for some sufficiently small c1 ( 0.0001)
- for some sufficiently large c2 ( 1000)
35Definition of Theta
f(n) ?(g(n))
- For all sufficiently large n
36Definition of Theta
f(n) ?(g(n))
- For all sufficiently large n
- For some definition of sufficiently large
37Definition of Theta
3n2 7n 8 ?(n2)
38Definition of Theta
3n2 7n 8 ?(n2)
True
7n 8 1n2 7 8/n 1n
39Definition of Theta
3n2 7n 8 ?(n)
The reverse statement
40Definition of Theta
3n2 7n 8 n?(1)
Polynomial time
c1, c2, n0, " n ³ n0 nc1 f(n) nc2
41Definition of Theta
3n2 7n 8 2?(n)
Exponential time
c1, c2, n0, " n ³ n0 2c1n f(n) 2c2n
42Order of Quantifiers
f(n) ?(g(n))
?
43Order of Quantifiers
f(n) ?(g(n))
- No! It cannot be a different c1 and c2
- for each n.
44The Time Complexity of a Problem P
Merge, Quick, and Heap Sort can sort N numbers
using O(N log N) comparisons between the values.
Theorem No algorithm can sort faster.
The Time Complexity of a Problem P
The minimum time needed by an algorithm to solve
it.
45The Time Complexity of a Problem P
The minimum time needed by an algorithm to solve
it.
Upper Bound
- Problem P is computable in time Tupper(n)
- if there is an algorithm A which
- outputs the correct answer
- in this much time
- given any input instance I.
A, " I, A(I)P(I) and Time(A,I)
Tupper(I)
Eg Sorting computable in Tupper(n) O(n2) time.
46The Time Complexity of a Problem P
The minimum time needed by an algorithm to solve
it.
A, " I, A(I)P(I) and Time(A,I)
Tupper(I)
" I, A, A(I)P(I) and Time(A,I)
Tupper(I) What does this say?
One algorithm A, that works for every instance
IvsA different algorithm AI for each instance I.
47The Time Complexity of a Problem P
The minimum time needed by an algorithm to solve
it.
A, " I, A(I)P(I) and Time(A,I)
Tupper(I)
" I, A, A(I)P(I) and Time(A,I)
Tupper(I) What does this say?
True for any problem P and time Tupper. Given
fixed I. Its output is P(I).
Let AP(I) be the algorithm that outputs the
string P(I).
48The Time Complexity of a Problem P
The minimum time needed by an algorithm to solve
it.
Lower Bound
Time Tlower(n) is a lower bound for problem p if
no algorithm solve the problem faster.
There may be algorithms that give the correct
answer or run quickly on some inputs instance.
49The Time Complexity of a Problem P
The minimum time needed by an algorithm to solve
it.
Lower Bound Time Tlower(n) is a lower bound for
problem p if no algorithm solve the problem
faster.
But for every algorithm, there is at least one
instance I for which either the algorithm gives
the wrong answer or it runs in too much time.
Eg No algorithm can sort N values in Tlower
sqrt(N) time.
50The Time Complexity of a Problem P
The minimum time needed by an algorithm to solve
it.
Upper Bound
A, " I, A(I)P(I) and Time(A,I)
Tupper(I)
Lower Bound
" A, I, A(I) ? P(I) or Time(A,I) ³
Tlower(I)
There is and there isnt a faster
algorithmare almost negations of each other.
51Prover-Adversary Game
Upper Bound
A, " I, A(I)P(I) and Time(A,I)
Tupper(I)
I have an algorithm A that I claim works and is
fast.
Oh yeah, I have an input I for which it does not.
- I win if A on input I gives
- the correct output
- in the allotted time.
What we have been doing all along.
52Prover-Adversary Game
Lower Bound
I have an algorithm A that I claim works and is
fast.
Oh yeah, I have an input I for which it does not .
- I win if A on input I gives
- the wrong output or
- runs slow.
53Prover-Adversary Game
Lower Bound
I have an algorithm A that I claim works and is
fast.
Lower bounds are very hard to prove, because I
must consider every algorithm no matter how
strange.
54Quantifiers, Do you understand them?
? g, " b, Loves(b, g)
A(I)P(I) means algorithm A gives the required
output for problem P on
instance I. Time(A,I) is the running time of
algorithm A on instance
I. T(n) some function like n2.
- Express
- Problem P is computable by some algorithm.
- Problem P is computable in time T(n).
- Problem P is computable in polynomial time.
- The computational class Exponential Time
is strictly bigger than the computational
class Polynomial Time.
55- Problem P is computable by some algorithm.
-
- Problem P is computable in time T(n).
- Problem P is computable in polynomial time.
-
- Problem P is not computable in polynomial time.
- Problem P is computable in exponential time.
- The computational class Exponential Time
is strictly bigger than the computational
class Polynomial Time.
? A, " I, A(I)P(I)
? A, " I, A(I)P(I) Time(A,I) T(I)
? A, c, n0," I, A(I)P(I) (I lt n0 or
Time(A,I) Ic)
? A, c, n0, ? I, A(I)?P(I) or (I n0
Time(A,I) gt Ic)
? A, c, n0," I, A(I)P(I) (I lt n0 or
Time(A,I) 2cI)
56Some Math
You are on your own.
57The Time Complexity of an Algorithm
- Specifies how the running time depends on the
size of the input.
58Purpose?
59Purpose
- To estimate how long a program will run.
- To estimate the largest input that can reasonably
be given to the program. - To compare the efficiency of different
algorithms. - To help focus on the parts of code that are
executed the largest number of times. - To choose an algorithm for an application.
60Time Complexity Is a Function
- Specifies how the running time depends on the
size of the input. - A function mapping
- size of input
- time T(n) executed .
61Definition of Time?
62Definition of Time
- of seconds (machine dependent).
- lines of code executed.
- of times a specific operation is performed
(e.g., addition). - Which?
63Definition of Time
- of seconds (machine dependent).
- lines of code executed.
- of times a specific operation is performed
(e.g., addition). - These are all reasonable definitions of time,
because they are within a constant factor of each
other.
64Size of Input Instance?
83920
65Size of Input Instance
- Size of paper
- of bits
- of digits
- Value
- - n 2 in2
- - n 17 bits
- - n 5 digits
- - n 83920
Which are reasonable?
66Size of Input Instance
- Size of paper
- of bits
- of digits
- Value
- - n 2 in2
- - n 17 bits
- - n 5 digits
- - n 83920
67Size of Input Instance
- Size of paper
- of bits
- of digits
- Value
- - n 2 in2
- - n 17 bits
- - n 5 digits
- - n 83920
68Size of Input Instance
- Size of paper
- of bits
- of digits
- Value
- - n 2 in2
- - n 17 bits
- - n 5 digits
- - n 83920
- Intuitive
- Formal
- Reasonable
of bits 3.32 of digits
69Size of Input Instance
- Size of paper
- of bits
- of digits
- Value
- - n 2 in2
- - n 17 bits
- - n 5 digits
- - n 83920
- Intuitive
- Formal
- Reasonable
- Unreasonable
of bits log2(Value) Value 2 of bits
70Two Example Algorithms
- Sum N array entries
- A(1) A(2) A(3)
- Factor value N
- Input N5917 Output N9761.
- Algorithm N/2, N/3, N/4, . ?
Time N
Time N
Is this reasonable?
No! One is considered fast and the other slow!
71Two Example Algorithms
N hard drive 60G lt 236
- Sum N array entries
- A(1) A(2) A(3)
- Factor value N
- Input N5917 Output N9761.
- Algorithm N/2, N/3, N/4, . ?
Time N
N crypto key 2100
Time N
Standard input size?
72Two Example Algorithms
- Sum N array entries
- A(1) A(2) A(3)
- Factor value N
- Input N5917 Output N9761.
- Algorithm N/2, N/3, N/4, . ?
size n N
Time N
size n log N
Time N
Size of Input Instance?
73Two Example Algorithms
- Sum N array entries
- A(1) A(2) A(3)
- Factor value N
- Input N5917 Output N9761.
- Algorithm N/2, N/3, N/4, . ?
size n N
n
Time N
size n log N
Time N
2n
Time for you to solve problem instance as a
function of time for me to give you the problem
instance.
74Two Example Algorithms
- Sum N array entries
- A(1) A(2) A(3)
- Factor value N
- Input N5917 Output N9761.
- Algorithm N/2, N/3, N/4, . ?
size n N
n
Time N
size n log N
Time N
2n
Linear vs Exponential Time!
75Size of Input Instance?
76Size of Input Instance
10
- of elements n 10 elements
77Size of Input Instance
10
- of elements n 10 elements
Is this reasonable?
78Size of Input Instance
10
- of elements n 10 elements
Reasonable
If each element has size c of bits c
of elements
79Size of Input Instance
10
- of elements n 10 elements
Reasonable
If each element is in 1..n each element has
size log n of bits n log n n
80Time Complexity Is a Function
- Specifies how the running time depends on the
size of the input. - A function mapping
- of bits n needed to represent the input
- of operations T(n) executed .
81Which Input of size n?
- There are 2n inputs of size n.Which do we
considerfor the time T(n)?
82Which Input of size n?
83Which Input of size n?
84What is the height of tallest person in the class?
Bigger than this?
Smaller than this?
Need to look at every person
Need to look at only one person
85Time Complexity of Algorithm
The time complexity of an algorithm isthe
largest time required on any input of size n.
- O(n2) Prove that for every input of size n, the
algorithm takes no more than cn2 time. - O(n2) Find one input of size n, for which the
algorithm takes at least this much time. - ? (n2) Do both.
86Time Complexity of Problem
The time complexity of a problem is the time
complexity of the fastest algorithm that solves
the problem.
- O(n2) Provide an algorithm that solves the
problem in no more than this time. - O(n2) Prove that no algorithm can solve it
faster. - ? (n2) Do both.
-
87Classifying Functions
n
Note The universe contains approximately 1050
particles.
88Classifying Functions
Functions
Exp
Constant
Polynomial
Exponential
Double Exp
Poly Logarithmic
89Classifying Functions
Polynomial
n?(1)
?
Cubic
Quadratic
Linear
?(n3)
?(n4)
?(n2)
?(n)
log(n) not absorbedbecause not Mult-constant
90Classifying Functions
We consider two (of many) levels in this
hierarchy
Functions
Ignore Power-constant
Exp.
Poly.
2?(n)
n?(1)
Ignore Mult-constant
?(24n / n100)
?(24n)
?(n3 log(n))
?(n2)
Individuals
824n / n100
824n n3
8n2
7n3log(n)
91BigOh and Theta?
- 5n2 log n 8n 2log n ?(n2 log n)
Drop low-order terms. Drop multiplicative
constant.
92Which Functions are Polynomials?
- nc
- n0.0001
- n10000
- 5n2 8n 2log n
- 5n2 log n
- 5n2.5
Drop low-order terms. Drop constant, log, (and
poly) factors. Ignore power constant. Write
n?(1)
93Which Functions are Exponential?
- 2n
- 20.0001 n
- 210000 n
- 8n
- 2n / n100
- 2n n100
Drop low-order terms. Drop constant, log, and
poly factors. Ignore power constant. Write
2?(n)
94Notations
95Definition of Theta
f(n) ?(g(n))
96Definition of Theta
f(n) ?(g(n))
- f(n) is sandwiched between c1g(n) and c2g(n)
97Definition of Theta
f(n) ?(g(n))
- f(n) is sandwiched between c1g(n) and c2g(n)
- for some sufficiently small c1 ( 0.0001)
- for some sufficiently large c2 ( 1000)
98Definition of Theta
f(n) ?(g(n))
- For all sufficiently large n
99Definition of Theta
f(n) ?(g(n))
- For all sufficiently large n
- For some definition of sufficiently large
100Definition of Theta
3n2 7n 8 ?(n2)
101Order of Quantifiers
f(n) ?(g(n))
- No! It cannot be a different c1 and c2
- for each n.
102Understand Quantifiers!!!
- Could be a separate girl for each boy.
103End of first part of math review
104Adding The Classic Techniques
n
105Arithmetic Sum
?i1..n i 1 2 3 . . . n
?
Gauss
106Adding Made Easy
number of white dots
1
2
3
. . .
n-1
n
S
1 2 . . . . . . . . n
107Adding Made Easy
number of white dots
1
2
3
. . .
n-1
n
S
number of yellow dots
n
n-1
n-2
. . .
2
1
S
1 2 . . . . . . . . n
n . . . . . . . 2 1
108Adding Made Easy
number of yellow dots
109Arithmetic Sum
?i1..n i 1 2 3 . . . n Q( of
terms last term)
Gauss
True when ever terms increase slowly
110Geometric Sum
?i0..n 2i 1 2 4 8 . . . 2n
?
111Geometric Sum
112Geometric Sum
?i0..n 2i 1 2 4 8 . . . 2n
2 last term - 1
113Geometric Sum
?i0..n ri r0 r1 r2 . . . rn
?
114Geometric Sum
S
2
3
n
n
1
Sr
r
r
r
.
.
.
r
r
-
-
n
1
S
Sr
1
r
-
n
1
r
1
S
-
r
1
115Geometric Sum
When rgt1?
-
n
1
r
1
?i0..n ri
-
r
1
116Geometric Sum
When rgt1
-
n
1
r
1
Biggest Term
?i0..n ri
?(rn)
-
r
1
117Geometric Increasing
?i0..n ri r0 r1 r2 . . . rn
Q(biggest term)
True when ever terms increase quickly
118Geometric Sum
When rlt1?
1 r
-
?i0..n ri
-
1 r
119Geometric Sum
When rlt1
1 r
-
?i0..n ri
Biggest Term
?(1)
-
1 r
120Bounded Tail
?i0..n ri r0 r1 r2 . . . rn
Q(1)
True when ever terms decrease quickly
121Harmonic Sum
?i1..n 1/i 1/1 1/2 1/3 1/4 1/5
1/n ?
122Sum of Shrinking Function
n
f(i) 1
?i1..n f(i) n
123Sum of Shrinking Function
n
f(i) ?
?i1..n f(i) n1/2
124Sum of Shrinking Function
f(i) 1/2i
?i1..n f(i) 2
125Sum of Shrinking Function
n
f(i) 1/i
?i1..n f(i) ?
126Harmonic Sum
127Harmonic Sum
?i1..n 1/i 1/1 1/2 1/3 1/4 1/5
1/n Q(log(n))
128Approximating Sum by Integrating
?i1..n f(i) ?x1..n f(x) dx
The area under the curve approximates the sum
129Adding Made Easy
(For , -, , ? , exp, log functions f(n))
130Adding Made Easy
131Adding Made Easy
- Geometric Like If f(n) ³ 2O(n), then
?i1..n f(i) ?(f(n)). - Arithmetic Like If f(n) n?(1)-1, then
?i1..n f(i) ?(n f(n)). - Harmonic If f(n) 1/n , then
?i1..n f(i) logen ?(1). - Bounded Tail If f(n) n-1-O(1), then
?i1..n f(i) ?(1). - (For , -, , ? , exp, log functions f(n))
This may seem confusing, but it is really not. It
should help you compute most sums easily.
132Recurrence Relations
- T(1) 1
- T(n) a T(n/b) f(n)
133Recurrence Relations Time of Recursive Program
procedure Eg(In) n In if(n1)
then put Hi else
loop i1..f(n) put Hi
loop i1..a In/b
In cut in b pieces Eg(In/b)
- Recurrence relations
- arise from the timing
- of recursive programs.
- Let T(n) be the of His
- on an input of size n.
134Recurrence Relations Time of Recursive Program
procedure Eg(In) n In if(n1)
then put Hi else
loop i1..f(n) put Hi
loop i1..a In/b
In cut in b pieces Eg(In/b)
- Given size 1,
- the program outputs
- T(1)1 His.
135Recurrence Relations Time of Recursive Program
procedure Eg(In) n In if(n1)
then put Hi else
loop i1..f(n) put Hi
loop i1..a In/b
In cut in b pieces Eg(In/b)
- Given size n,
- the stackframe outputs
- f(n) His.
136Recurrence Relations Time of Recursive Program
procedure Eg(In) n In if(n1)
then put Hi else
loop i1..f(n) put Hi
loop i1..a In/b
In cut in b pieces Eg(In/b)
- Recursing
- on an instances of size n/b
- generates T(n/b) His.
137Recurrence Relations Time of Recursive Program
procedure Eg(In) n In if(n1)
then put Hi else
loop i1..f(n) put Hi
loop i1..a In/b
In cut in b pieces Eg(In/b)
- Recursing
- a times
- generates aT(n/b) His.
138Recurrence Relations Time of Recursive Program
procedure Eg(In) n In if(n1)
then put Hi else
loop i1..f(n) put Hi
loop i1..a In/b
In cut in b pieces Eg(In/b)
- For a total of
- T(1) 1
- T(n) aT(n/b) f(n)
- His.
139Evaluating T(n) aT(n/b)f(n)
Dominated by Top Level or Base Cases
140Evaluating T(n) aT(n/b)f(n)
141Merge Sort Sort
Time T(n)
2T(n/2) Q(n)
Q(n log(n))
Total work at any level Q(n)
levels Q(log(n))
142Evaluating T(n) aT(n/b)f(n)
2T(n/2) Q(n)
2iQ(n/2i) Q(n)
All levels basically the same.
Total Work T(n) ?i0..h aif(n/bi)
Q(n) log(n)
143Evaluating T(n) aT(n/b)f(n)
144Logs
log 27
log 9
145Logs
log? 27
log? 9
Which base?
146Logs
log2 27
log2 c
logc 27
logc 27
log2 9
log2 c
logc 9
logc 9
It does not matter.
Which is easiest?
147Logs
3
log3 27
log3 33
2
log3 9
log3 32
Please no calculators in exams.
148Evaluating T(n) 4T(n/2) n3/log5n
149Evaluating T(n) 4T(n/2) n3/log5n
150Evaluating T(n) 4T(n/2) n3/log5n
- Time for top level f(n) n3/log5n
- Time for base cases?
151Evaluating T(n) 4T(n/2) n3/log5n
- Time for top level f(n) n3 /log5n
- Time for base cases
log ?/log ?
log a/log b
?(n )
?(n )
152Evaluating T(n) 4T(n/2) n3/log5n
- Time for top level f(n) n3 /log5n
- Time for base cases
log 4/log 2
log a/log b
?(n ) ?
?(n )
153Evaluating T(n) 4T(n/2) n3/log5n
- Time for top level f(n) n3/log5n
- Time for base cases
- Dominated? c ?
log 4/log 2
log a/log b
?(n ) ?(n2)
?(n )
log a/log b ?
154Evaluating T(n) 4T(n/2) n3/log5n
- Time for top level f(n) n3/log5n
- Time for base cases
- Dominated? c 3 gt 2 log a/log b
log 4/log 2
log a/log b
?(n ) ?(n2)
?(n )
Hence, T(n) ?
155Evaluating T(n) 4T(n/2) n3/log5n
- Time for top level f(n) n3/log5n
- Time for base cases
- Dominated? c 3 gt 2 log a/log b
log 4/log 2
log a/log b
?(n ) ?(n2)
?(n )
Hence, T(n) ?(top) ?(f(n)) ?(n3/log5n).
156Evaluating T(n) 4T(n/2) 2n
bigger
- Time for top level f(n) 2n
- Time for base cases
- Dominated? c ? gt 2 log a/log b
log 4/log 2
log a/log b
?(n ) ?(n2)
?(n )
Hence, T(n) ?(top) ?(f(n)) ?(2n).
157Evaluating T(n) 4T(n/2) n log5n
- Time for top level f(n) n log5n
- Time for base cases
- Dominated? c ? 2 log a/log b
log 4/log 2
log a/log b
?(n ) ?(n2)
?(n )
158Evaluating T(n) 4T(n/2) log5n
- Time for top level f(n) log5n
- Time for base cases
- Dominated? c ? 2 log a/log b
log 4/log 2
log a/log b
?(n ) ?(n2)
?(n )
159Evaluating T(n) 4T(n/2) n2
- Time for top level f(n) n2
- Time for base cases
- Dominated? c ? 2 log a/log b
log 4/log 2
log a/log b
?(n ) ?(n2)
?(n )
?(f(n) log(n) ) ?(n2 log(n)).
160Evaluating T(n) 4T(n/2) n2 log5n
- Time for top level f(n) n2 log5n
- Time for base cases
- Dominated? c 2 2 log a/log b
log 4/log 2
log a/log b
?(n ) ?(n2)
?(n )
?(f(n) log(n) ) ?(n2 log6(n)).
161Evaluating T(n) aT(n/b)f(n)
162Evaluating T(n) aT(n-b)f(n)
Work in Level
stack frames
Work in stack frame
Instance size
n
n-b
h ?
h n/b
base case 0 n-hb h n/b
163Evaluating T(n) 1T(n-b)f(n)
Work in Level
stack frames
Work in stack frame
Instance size
h ?
?(f(n)) or ?(nf(n))
Total Work T(n) ?i0..h f(bi)
164Evaluating T(n) aT(n/b)f(n)
165End of second part of math review
More Math
- Graphs Searching Algorithms