algorithm analysis. algorithm def an algorithm is a step-by-step procedure
TRANSCRIPT
AlgoritAlgorithmhm AAnalysnalysisisAlgoritAlgorithmhm AAnalysnalysisis
AlgorithmDef
An algorithm is a step-by-step procedure
AlgorithmExample - Compute the sum 10+11+12+13+…+30
public int sum (int nStart, int nEnd) {int sum = 0;for (int i = nStart; i nEnd; i++) {
sum += i;}return sum;
}
Take the first number and continuing add next number until the last number
sum = 0FOR i = nStart TO nEnd
sum = sum + iENDFORRETURN sum
Algorithm AnalysisRequirements
SimplicityReadabilityVerifiabilityValidityRobustnessModifiabilityReuseabilityEfficiency
public int sum (int nStart, int nEnd) {int sum = 0;for (int i = nStart; i nEnd; i++) {
sum += i;}return sum;
}
sum = 0FOR i = nStart TO nEnd
sum = sum + iENDFORRETURN sum
AlgorithmExample - Finding the largest value
5
8
2
7
AlgorithmExample - selectionSort
5
8
2
7
AlgorithmExample - Character permutations
A
B
C
B
A
C
A
C
B
C
A
B
B
C
A
C
B
Ann 321!
Asymtotic NotationNumber of statements - Mathematical relations
1 )1(
)1( -
1
1 1
1
6
)12)(1(
2
)1(
2
1
1
2
1
1
1
0
1
2
1
1
aa
aa
a
naai
aa
aa
nnni
nnn
ni
n
nnn
i
i
nn
i
i
n
i
n
i
n
i
Proof
n
1...111
naa
s nn 2
1 n
n ...321
n
n2222 ...321
1
210 ...
n
naaaa
n
nnaaaa ...321 321
Asymtotic NotationNumber of statements - Mathematical relationsProof by Induction
6
)672)(1(
6
)1(6
6
)12)(1()1(
6
)12)(1()1(
6
)672)(1(
6
)1)1(2)(1)1)((1( dvs,1for stemmer (*)at viseSkal
6
)12)(1( dvs ,for stemmer (*)at Anta
1for stemmer dvs 16
321
6
)112)(11(1 11:1
(*) 6
)12)(1(
2
222
1
21
1
2
2
1
1
2
1
2
21
1
2
1
2
kkk
kkkkk
kkkkii
kkk
kkkikn
kkkikn
nin
nnni
k
i
k
i
k
i
k
i
i
n
i
AlgorithmBasic Timing Analysis - Consecutive statements
count algorithm step
1 a = 4;1 b = 8;1 c = a;
3111)( nT Run time
AlgorithmBasic Timing Analysis - For loop
count algorithm step
1 for (i = 1;n+1 i <= n;n i++)n x = x + 1;
23)1(1)( nnnnnT
for (i = 1; i <= n; i++)x = x + 1;
Run time
AlgorithmBasic Timing Analysis - For loop, if statement
count algorithm step
1 max = array[0];1 for ( i = 1;n i < n;n-1 i++)n-1 if (array[i] > max)p max = array[i]; 0 p n-11 return max;
nnT
pnpnnnnT
4)(
131)1()1(11)(
max
Run time
AlgorithmBasic Timing Analysis
count algorithm step
1 sum = 0;1 + (n+1) + n for ( i = 1; i <= n; i++)1*n + (n+1)*n + n*n for ( j = 1; j <= n; j++)n*n sum = sum + 1;1 return sum;
342
*1111
**)1(*1)1(11)(
2
22
nn
nnnnnn
nnnnnnnnT
Run time
AlgorithmBasic Timing Analysis - Simple For loop
count algorithm step
1+(n+1)+n for ( i = 1; i <= n; i++)kn Sk
)()(
2)2()1(1)(
nnT
nkknnnnT
Run time
ComplexityAsymptotic Notation
AlgorithmBasic Timing Analysis - Simple For loop
count algorithm step
for ( i = 1; i < =n; i++)kn Sk
)()(
)(
nnT
knnT
Run time
ComplexityAsymptotic Notation
AlgorithmBasic Timing Analysis - Double For loop
for (i = 1; i <= n; i++)for (j = 1; j <= n; j++)
Sk
)()(
11)(
2
2
111 11 1
nnT
knknnknnkkknTn
i
n
i
n
i
n
j
n
i
n
j
AlgorithmBasic Timing Analysis - Single For loop + Double For loop
for (i = 1; i <= n; i++)Sk
for (i = 1; i <= n; i++)for (j = 1; j <= n; j++)
Sk
)()(
11)(
2
2
111 11 1
nnT
knknknnknknknnkknkknkknnTn
i
n
i
n
i
n
j
n
i
n
j
AlgorithmBasic Timing Analysis - Double For loop
for (i = 1; i <= n; i++)for (j = i; j <= n; j++)
Sk
)()(
222
1)1(1)(
2
2
111
nnT
nk
nk
nn
kinkkknTn
i
n
i
n
ij
n
i
n
ij
AlgorithmBasic Timing Analysis - Triple For loop
for (i = 1; i <= n; i++)for (j = i; j <= n; j++)
for (k=i; k <=j; k++) Sm
)()(
6
23
2
23
2
)1(
2
3
6
)12)(1(
2
1
1232
1
2
3
2
1
2
)2)(1(
)1(2
)1(1)1(...321
)1(1)(
3
23
2
1
2
11
2
1
11
111
nnT
mnnn
nnnnn
nnnn
m
nninim
ininm
inin
minm
ijmmmnT
n
i
n
i
n
i
n
i
n
i
n
i
n
i
n
ij
n
i
n
ij
j
ik
n
i
n
ij
j
ik
Different functions and their graphs
Expression Name--------------------------------------------1 constantlog n logarithmiclog2n log squaredn linearn log n n log nn2 quadraticn3 cubic2n exponential--------------------------------------------
AlgorithmGrowth of Common Functions
AlgorithmGeneral Rules
LoopsThe running time of a loop is at most the running time of the statementsinside the loop (including tests) times the number of iterations.
Nested LoopsAnalyze these inside out. The total running time of at statement insidea group of nested loops is the running time of the statement multiplied by the products of the sizes of all the loops.
Consecutive statementsThese just add.
if-then-elseThe running time of an if-then-else statement is never more thanthe running time of the test plus the larger of the running times of each part.
Basic AxiomsA 01
A 01
The time required to fetch an operand from memory is a constant:
The time required to store an operand in memory is a constant:
fetch
store
Memoryfetchstore
Basic AxiomsC 01
C 01
The time required to perform a simple assignment statementis the time required to fetch an operand plus the time required to store an operand: storefetchassignment
xfetch
store
xy
y
1y …
Basic AxiomsA 02
A 02
The time required to perform elementary operations,such as addition, subtraction, multiplication, division, and comparison,are all contants.
/
*
Basic AxiomsA 01 - A 02 - Example
Time required to perform the following statement:
storefetch 2
y
y
y
yy
1
1
Basic AxiomsA 03
A 03
The time required to call a method is a contant,and the time required to return from a method is a constant.
return
call
Basic AxiomsA 04
A 04
The time required to pass an argument to a methodis the same as the time required to store a value in memory:
storepass
Basic AxiomsA 04 - Example
Time required to perform the following statement:
)(
)(
2
xfcallstorefetch
storexfstorecallfetch
T
T
)(xfy
fetch xcall f
passing x
running time of f
assignment to y
Basic AxiomsA 01 - A 02 - A 03 - Example
n
i
i1
Running timeTime required to computethe following simple arithmetic series summation:
1 public class Example {2 public static int sum (int n) {3 int result = 0;4 for (int i = 0; i <= n; i++)5 result += i;6 return result;7 }8 }
resultreturn
iresultn
inc
ninb
ia
result
returnfetch
storefetch
storefetch
fetch
storefetch
storefetch
6
)2( 5
)2( 4
)1()2( 4
1 4
0 3
ττττ t
ττττt
nttT(n)
storefetch
returnstorefetch
226
25
2
1
21
Basic AxiomsA 05
A 05
The time required for the address calculationimplied by an array subscripting operation, a[i], is a constant.This time does not include the time to compute the subscript expression,nor does it include the time to access (fetch or store) the array element.
][
Basic AxiomsA 05 - Example
Time required to perform the following statement:
storefetch
storefetchfetchfetch
][
][
3
][iay
fetch afetch i
address subscripting calcutaion
fetch a[i]
assignment to y
Basic AxiomsA 01 - A 02 - A 03 - A 04 - Example - Horner’s Rule
n
i
ii xa
0
1 public class Example {2 public static int horner (int[] a, int n, int x) {3 int result = a[n];4 for (int i = n-1; i >= 0; i--)5 result = result * x + a[i];6 return result;7 }8 }
resultreturn
iresultn
inc
inb
nia
naresult
returnfetch
storexfetch
storefetch
fetch
storefetch
storefetch
6
)5( 5
)2( 4
0 )1()2( 4
1 2 4
][ 3 3
][
][
xstorefetch
returnstorefetch
ττττ t
ττττt
nttT(n)
][2
][1
21
29
28
Basic AxiomsA 01 - A 02 - A 03 - A 04 - Recursive Methods - Factorial [1/2]
0 1
0 1
0
0 1
!
1
n)!(n-n
n
ni
n
n n
i
1 public class Example {2 public static int factorial (int n) {3 if (n == 0)4 return 1;5 else6 return n * factorial(n-1);7 }8 }
)T(n-returncall
storefetch
returnfetch
fetchfetch
1
3 6
4
2 2 3
*
returncallxstorefetch
returnfetch
ττττ t
τττt
ntnT
ntnT
5
3
0 )1(
0 )(
2
1
2
1
Basic AxiomsA 01 - A 02 - A 03 - A 04 - Recursive Methods - Factorial [2/2]
0 1
0 1!
n)!(n-n
nn
returncallxstorefetch
returnfetch
ττττ t
τττt
ntnT
ntnT
5
3
0 )1(
0 )(
2
1
2
1
21
2
2
222
222
2
)0(
)(
...
3)3(2)3(
2)2()2(
)1()(
ntt
ntT
ktknT
tnTttnT
tnTttnT
tnTnT
Basic AxiomsA 01 … A 05 - Finding the Largest Element of an Array [1/2]
iani
max0
1 public class Example {2 public static int max (int[] a) {3 int result = a[0];4 for (int i = 1; i < a.length; i++)5 if (a[i] > result)6 result = a[i];7 return result;8 }9 }
storefetch
][storefetch
fetchstore
aa
n
in
τττt
τττττ t
ττττt
tnttaaanT
jiji
][3
2
1
)(max
1
1321110
3
28
2
),...,,,(
0
resultreturn
resultn
inc
lengthainb
ia
aresult
storefetch
storefetch
fetch
storefetch
fetch
storefetch
storefetch
3 7
a[i]result ?)(3 6
a[i] )1()4( 5
)1()2( 4
. )2( 4
1 4
]0[ 3 3
][
][
][
Basic AxiomsA 01 … A 05 - Finding the Largest Element of an Array [2/2]
iani
max0
)(max
1
1321110
0
),...,,,(
jiji aa
n
in tnttaaanT
ntntttnT
ni
HtHntt
ti
ntt
ti
ntt
iaaPptpnttnT
avg
n
inn
n
i
n
i
jij
ii
n
iiavg
ln)1()(
ln1
)1(
11
1
1
1
1)max( )(
3231
1321
31
21
3
1
121
0
1
1321
pi probabilitythat line 6 is executed
)ln()ln(11
1
1
1
11 nxdx
xiH n
nn
in
Asymtotic NotationDefinition 01 - An Asymtotic Upper Bound - Big Oh
00 )()(0 :0, |)())(( nnncgnfncnfngO
D 01
Consider a function f(n).We say that f(n) is big oh g(n), which we write f(n) O(g(n)),if there exists an integer n0 and a constant c > 0 such that for all integers n n0, 0 f(n) cg(n)
cg(n)
f(n)
n0
Asymtotic NotationDefinition 01 - An Asymtotic Upper Bound - Big OhAlternative definition
00 )()( :0,
))(()(
nnncgnfcn
ngOnf
D 01
Consider a function f(n) that is non-negative for all integers n >= 0.We say that f(n) is big oh g(n), which we write f(n) = O(g(n)),if there exists an integer n0 and a constant c > 0 such that for all integers n n0, f(n) cg(n)
cg(n)
f(n)
n0
Asymtotic NotationDefinition 01 - An Asymtotic Upper Bound - Big Oh
))(()(1 ngOnf
00 )()( :0,
))(()(
nnncgnfcn
ngOnf
00 )()(0 :, |)())(( nnncgnfncnfngO
cg(n)
f(n)
n0
))(()(1 ngOnf
Asymtotic NotationAn Asymtotic Upper Bound - Big Oh
)()(
50
501110
5050
5050
505)(
00
nOnf
nncn
cn
cn
cnn
nnf
f(n) = 5n + 50 We wish to show that f(n) O(n)
00 )()(0 :, |)())(( nnncgnfncnfngO
Asymtotic NotationAn Asymtotic Upper Bound - Big Oh 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
2
02
0
2
22
)(
)(10 ,1
0)5)(10(
0505
505)(
1cVelger
505)(
nOnf
nncnnfnc
nn
nn
nncnnf
nnf
f(n) = 5n + 50 We wish to show that f(n) = O(n2)
2
2
2
4)(3
2)(2
)(1
505 )(
nng
nng
nng
nnf
Asymtotic NotationT 01 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
T 01
If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1(n) + f2(n) = O(max(g1(n),g2(n))).
Proof
)))(),((max()()(
))()(max(
))()((2
)()()()(),max(
),max(2
2121
210
210
221121
0
210
210
ngngOnfnf
ngngc
ngngc
ngcngcnfnf
nn
nnn
ccc
Asymtotic NotationExamples 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
)( !
)!( 2
)2(
))((log
)( )(log
)( 3001004
)( 3001004
)1( 350
)( 201004
)( 20
10
210
32
22
323
22
n
n
nk
nOn
nO
On
nOn
nOnn
nOnn
nOnn
O
nOnn
nOn
Asymtotic NotationExample - 1+2+3+…+n 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
Outer BodyLevel Loop
1 12
1
10 or 10 or 10 or 1
)()( nOnT
Asymtotic NotationExample - Selection sort 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
Outer Body BodyLevel Outer Inner
Loop Loop
1+1 1+111+1 1+1
10 or 1
10 or 10 or 10 or 1
Asymtotic NotationNumber of statements 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
)1(41)1()1(441031
1
1
1
ininn
ij
n
ij
2
0
2
0
loopinner for count 9301 loopinner for count 5n
i
n
i
Outer loop
2
0
loopinner for count 92 loopouter for count 2n
i
Total number
Inner loop for a certain i
Asymtotic NotationNumber of statements 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
772
442244552
2
)1)(2(4)1()45(2
41)45(2
4452
1)-i-(n4 92
loopinner for count 92)(
2
22
2
0
2
0
2
0
2
0
2
0
nn
nnnnnn
nnnn
in
in
nTS
n
i
n
i
n
i
n
i
n
issort
)()( 2nOnTSssort
Asymtotic NotationT 01 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
T 02
If f(n) = f1(n) + f2(n) where f1(n) and f2(n) both are non-negative for all intergers n >= 0 such that limn-> f2(n)/f1(n) = L for some limit L >= 0,then f(n) = O(f1(n)).
Proof
))(()(
)( )(
)()()(
)()(
)()()(
)()()( )(
)(
)(
)(lim
1
021010
10211
2211
21
1020 01
2
1
2
nfOnf
Lcccnfc
nfLcnfc
nfcnfc
nfnfnf
nfLnfnnLnf
nfL
nf
nfn
Asymtotic NotationT 01 - Example 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
)(
)()()(
01
limlim)(
)(lim
)(
)(
)()()(
3
23
21
3
2
1
2
22
31
21
nO
nn
nfnfnf
nn
n
nf
nf
nnf
nnf
nfnfnf
nnn
f(n) = f1(n) + f2(n) = n3 + n2
Asymtotic NotationT 03 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
T 03
If f1(n) = O(g1(n)) and f2(n) = O(g2(n)) then f1(n) x f2(n) = O(g1(n) x g2(n)).
Proof
))()(()()(
))()(()()()()(
),,(
0 0)( 0 0)(
)()( )()(:,,,
2121
0210221121
210210
11
222211112121
ngngOnfnf
nnngngcngcngcnfnf
cccnnmaxn
nnfnnf
nnngcnfnnngcnfccnn
Asymtotic NotationT 03 - Example 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
f1(n) = n3 + n2 + n + 1 = O(n3)
f2(n) = n2 + n + 1 = O(n2)
f1(n) x f2(n) = O(n3 x n2) = O(n5)
Asymtotic NotationT 04 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
T 04
If f1(n) = O(g1(n)) and g2(n) is a function whose value is non-negativefor integers n >= 0, then f1(n) x g2(n) = O(g1(n) x g2(n)).
Proof
))()(()()(
)()()()(
0 0)(
)()(:,
2121
021021
2
010100
ngngOngnf
nnngngcngnf
nng
nnngcnfcn
Asymtotic NotationT 05 - Transitive Property 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
T 05
If f(n) = O(g(n)) and g(n) = O(h(n)) then f(n) = O(h(n)).
Proof
)(
)(
)()(
),,(
)()( )()(:,,,
0
021
11
210210
22112121
nhc
nnnhcc
nnngcnf
cccnnmaxn
nnnhcngnnngcnfccnn
Asymtotic NotationT 05 - Example 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
f1(n) = 7n3 = O(n3)
f2(n) = 2n2 = O(n2)
f1(n) + f2(n) = O(f1(n)) because lim f2(n)/ f1(n) = 0f1(n) + f2(n) = O(f1(n)) = O(n3)
Asymtotic NotationT 06 - Polynomials 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
T 06
Proof
m
mm
mm
m
m
i
ii
nOnf
aanananana
nanf
)(
0 ...
)(
012
21
1
0
m
n
c
m
ii
ng
mm
iimi
m
m
i
mii
mm
i
ii
m
i
ii
nOnf
nann
an
nnannananf
)(
1 1
1 )(
00)(0
000
Asymtotic NotationLogarithms 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
logn < n => logn = O(n)
Asymtotic NotationT 07 - Logarithms 00 )()( :0,
))(()(
nnncgnfcn
ngOnf
T 07
For every integer k >= 1, logkn = O(n).
Proof
0log
limln
1
ln1
log)1(lim
loglim :1
0log
lim:m1,2,3,...,kfor holdsit Assume
01
limln
1
1ln1
limlog
lim :1
:Induction
0log
lim :Hypothesis
''1
''
n
n
a
m
nan
nm
n
nmk
n
n
naan
n
nk
n
n
ma
n
ma
n
rulesHopitalLma
n
ma
n
nn
rulesHopitalLa
n
ka
n
Names of Common Big Oh Expressions
Expression Name--------------------------------------------O(1) constantO(log n) logarithmicO(log2n) log squaredO(n) linearO(n log n) n log nO(n2) quadraticO(n3) cubicO(2n) exponential--------------------------------------------
Asymtotic Lower Bound - Omega
00 )()( :0,
))(()(
nnncgnfcn
ngnf
D 01
Consider a function f(n) that is non-negative for all integers n >= 0.We say that f(n) is omega g(n), which we write f(n) = (g(n)),if there exists an integer n0 and a constant c > 0 such that for all integers n >= n0, f(n) >= cg(n)
cg(n)
f(n)
n0
Asymtotic Upper and Lower Bound - Theta
))(()( ))(()(
))(()(
ngnfngOnf
ngnf
D 01
Consider a function f(n) that is non-negative for all integers n >= 0.We say that f(n) is theta g(n), which we write f(n) = (g(n)),if and only if f(n) = O(g(n)) and f(n) = (g(n)).
c1g(n)
f(n)
n0
c2g(n)
Asymtotic Lower Bound - Little Oh
))(()( ))(()(
))(()(
ngnfngOnf
ngonf
D 01
Consider a function f(n) that is non-negative for all integers n >= 0.We say that f(n) is little oh g(n), which we write f(n) = o(g(n)),if and only if f(n) = O(g(n)) and f(n) (g(n)).
cg(n)
f(n)
n0
Asymtotic NotationBig Oh - Omega - Theta - Little Oh
021021 )()()(0 :0,, |)())(( nnngcnfngcnccnfng
00 )()(0 :0, |)())(( nnnfncgncnfng
00 )()(0 :0,0 |)())(( nnncgnfncnfngO
0101
0202
)()(0 :0,
)()(0 :0, |)())((
nnnfngcnc
nnngcnfncnfngo
Asymtotic NotationExample An Asymtotic Upper and Lower Bound - Theta
)()(
)()()()(
3)(
nnf
nnfnOnf
nnf
f(n) = n + 3
Asymtotic NotationExample An Asymtotic Upper and Lower Bound - Little Oh
00 )()(0 :, |)())(( nnncgnfncnfngO
)()(
)()()()(
3)(
2
22
nonf
nnfnOnf
nnf
f(n) = n + 3
Asymtotic NotationT 06 - Polynomials 00 )()( :0,
))(()(
nnncgnfcn
ngnf
T 08
Proof
m
mm
mm
m
m
i
ii
nnf
aanananana
nanf
)(
0 ...
)(
012
21
1
0
1
0
1
0
1
1
0)1(
1
1
0
)1(1
1
0
1
00
1
1
1 1
1
)(
m
iim
m
m
ii
mmm
m
iimi
mmm
m
i
mii
mmm
m
i
ii
mm
m
i
ii
mm
m
i
ii
an
an
nanna
nn
anna
nnanna
nana
nanananf
m
c
m
iim
ng
m
m
ii
m
m
ii
m
m
iim
m
iim
m
iim
nnf
nnan
annf
aa
n
aa
n
an
a
an
aan
a
)(
1
)(
11
1
01
11
0
1
00)(
1
00
1
00
1
00
1
00
1
0
Asymtotic NotationExample An Asymtotic Upper and Lower Bound - Theta
00 )()(0 :, |)())(( nnncgnfncnfngO
)()(
)()()()(
...)( 011
1
m
mm
mm
mm
nnf
nnfnOnf
ananananf
f(n) = amnm + am-1nm-1 + … + a1n + a0
Asymtotic NotationRules for Big Oh Analysis
00 )()(0 :, |)())(( nnncgnfncnfngO
Sequence S1;S2;…Sm;
Iteration for (S1; S2; S3)S4;
Conditional if (S1)S2;
elseS3;
O(max ( T1(n), T2(n),…,Tm(n))
O(max ( T1(n), T2(n) x (I(n)+1),T3(n) x I(n),T4(n) x I(n)
)
O(max ( T1(n), T2(n),T3(n)
)
Actual Lower BoundsAssuming a 100 MHz Clock, c = 1 Cycle, n0 = 0
yearsyearssns
yearsssnsn
hourssnsnsn
mssnsnsnn
mssnsnsn
nsnsnsnsn
nsnsnsns
KnKnnn
n 510293
3
2
101056.210)2(
3657.1012.510)(
05.32.1064010)(
2102.1024010)log(
5.1002.18010)(
2001003010)(log
10101010)1(
1024181
RecursionOne recursive call with half size - Binary Search
11
2
11)( n
nT
nnT
1)(log)( 2 nnT
))(log()( nnT
RecursionOne recursive call with half size - Binary Search - Proof I
1)(log )(
...
1)16(log 5141)8( )16( 16
1)8(log 4131)4( )8( 8
1)4(log 3121)2( )4( 4
1)2(log 2111)1( )2( 2
1)1(log 1 )1( 1
2
2
2
2
2
2
nnTn
TT
TT
TT
TT
TProof
11
2
11)( n
nT
nnT
1)(log)( 2 nnT
))(log()( nnT
RecursionOne recursive call with half size - Binary Search - Proof 2
1)(log
111)(log
11)2(log)(log
112
log
12
12
log2
Assume
1)1(log1)1(
2
2
22
2
2
2
k
k
k
k
kTkT
kkT
TProof
11
2
11)( n
nT
nnT
1)(log)( 2 nnT
))(log()( nnT
RecursionTwo recursive calls with half size - Sum
11
22
11)( n
nT
nnT
12)( nnT
)()( nnT
RecursionTwo recursive calls with half size - Sum - Proof I
12)(
...
1162 311301)8(2 )16( 16
182 151141)4(2 )8( 8
142 5161)2(2 )4( 4
122 3121)1(2 )2( 2
112 1 )1( 1
nnTn
TT
TT
TT
TT
TProof
11
22
11)( n
nT
nnT
12)( nnT
)()( nnT
RecursionTwo recursive calls with size (about) n-1 - Fibonacci number - Recursive
11)2()1(
101)(
nnTnT
nnnT
)2()( nOnT
nnT 2)(
RecursionTwo recursive calls with size (about) n-1 - Fibonacci number - Proof 1
k
k
kk
kk
kkk
kk
kk
kTkTkT
kTkT
T
T
2
22
22
222
222
122
1)2()1(
2)2( 2)1( Assume
21)1(
221)0(
1
11
21
221
21
21
1
00
Proof
11)2()1(
101)(
nnTnT
nnnT
)2()( nOnT
nnT 2)(
RecursionFibonacci number - Iterative
)()( nOnT
RecursionMore than two recursive calls in every level - Permutation
1)1(
11)(
nnnT
nnT
)!()( nnT
!)( nnT
RecursionMore than two recursive calls in every level - Permutation - Proof I
!)(
...
!5 120245)4(5 )5( 5
!4 2464)3(4 )4( 4
!3 623)2(3 )3( 3
!2 212)1(2 )2( 2
!1 1 )1( 1
nnTn
TT
TT
TT
TT
T
Proof
1)1(
11)(
nnnT
nnT
)!()( nnT
!)( nnT
Recursive calls
abnnT
abnnnT
abnnT
kbancnb
naT
nnT
kk
kk
ka
k
b
)(
log)(
)(
011 1
11)(
log
Recursive callsExamples
loop simple size half with call recursive One )( )2/()( )(
loop simple size half with calls recursive Two log)( )2/(2)(
size half with call recursive One log)( )2/()( log)(
size half with calls recursive Two )( )2/(2)( )(
1
11)(
log
nnTcnnTnTabnnT
nnnTcnnTnT
nnTcnTnTabnnnT
nnTcnTnTabnnT
ncnb
naT
nnT
kk
kk
ka
k
b
Recursive callsProof
nnmmk
km
mkmk
mm
mm
i
i
kb
kmmmm
i
mmmk
m
ii
ikmmm
m
ii
ik
m
m
m
ii
ikm
ii
im
ii
i
m
mk
m
m
m
m
mkmkmm
m
k
bb
k
k
naaanTab
nba
banTab
x
xxx
x
x
x
xxxx
nnnTncncmbmcaacaabTab
a
bcaabT
a
bc
a
bT
a
bT
a
bc
a
bT
a
bT
a
bc
a
bT
a
bT
bcbaTbcb
baTbT
kbancnb
naT
nnT
loglog
1
1
1
10
0
111
1
1
1
1
1
1)(
)(
1 )1(
1 )()1(
11
1 1
)log()()log1()1(1)(
)()(
)()()(
)()()(
)()()(
)()()()(
011 1
11)(
Complexity
Algorithm Running time------------------------------------------------------Linear search O(n)Binary search O(logn)Bubble sort O(n2)Quick sort O(nlogn)------------------------------------------------------
ENDENDENDEND