Lec04 Annotated
Lec04 Annotated
CSC236
Week 04: Runtime Analysis
Ahmed Ashraf
Motivating example
def fact(n):
if n == 0:
return 1
else:
return n * fact(n-1)
=) if
* n
=
0
I
This is !
saying n
n .
(n-1) ! otherwise .
Factorial
Program
def fact(n):
if n == 0:
return 1 -- Tol
else:
return n * fact(n-1)
Analysis ** T(n 1)
-
Time Complexity
T(0) = c and
T(n) = T(n 1) + d for n > 0
Runtime complexity
For Factorial program
(
c if n = 0
T(n) =
T(n 1) + d if n > 0
Derivation 0
↓
T(n) =
T(n-1
For n<1 T(n) ( T(n-2) d) d
=
+
+
((T(n -
3) +
d) d)+ +
d
T(n-37 +
36
: "
T(n -k) + kd
Let k =
n
,
T(n) =
TC) +
nd
T(n) =
c +
nd
that
They are the time steps consumed
by operations are
predefined ,
and not
depend on the input n .
>
Assumption
good enough
We would like to that T(n) is linear for
say now
in n .
That is the
, given algorithm takes linear time (in its
input)
to Cule
-
n!
comy
For this we
develop a
language .
Outline
Motivating example
f is
eventually bounded above by a scalar multiple of g
~
Cg(n)
.
I~
R ↑
runtime f(n)
time
iof input
Abuse of notation :
Sometimes we write :
: f
=
O(g) to mean f G
Olg>
1 F =
0
E
(9) and h =
0
C
(g)
h
# F
=
3 .
and so on .
fini =
log (n) ,
1 . n G 0 (n) because i
for all nx0 .
In
general , f(n) EO(fini) .
2 .
ntO( because n n"
for all nx,
In
general ,
n t O(n") where K = 1 .
3 .
n GO(2") because ns2" for all nx 1
In
general ,
ne0(a") for all a <, 2 .
Three bigs
asymptotic
lower bound
(Big) ⌦
Given a function f : N ! N, and a function g : N ! N we
write f 2 ⌦(g) if there exists a positive real number C and a
positive integer n0 such that
an~
runtime f(nz
~
R
(g(n)
input
-
size of
Examples :
In
general , f(n) (1(finl) .
2 .
E (n) because n
> n"
for all nx
In
general ,
nE -(n) where K 1.
"
3 .
n - /log(n)) because n < 2 for alt nx 1 .
:
log(n) is an
increasing function
log(n) <
for all nx1
=>
n
.
Three bigs light
bound
(Big) ⇥
Given a function f : N ! N, and a function g : N ! N we
write f 2 ⇥(g) if f 2 O(g) and f 2 ⌦(g). In other words, there
exists positive real numbers C and D, and positive integer n0 such
that
~
Cg(n)
.
~
R W
↑runtime
-
f(n)
Dg(n)
iof input
Examples :
1 -
Of t
Note n n E O(nY) because
nn +n -
-> n
2
An 1
,
2 I
Motivating example
Es= en-pit
En -i =
E
↑
n
Here the runtime tin)
i
n
=
,
Repeated Substitution
For ny
, I F(n)
=
F(n 1)
-
+
2n
For n>, 2 ,
F(n) =
( F(n-2) 2(n-1))
+
+ 2n
"
F(n-K) +
2 (n-i)
Let k =
n
,
F(n)
=
F(0) +
2
(i)
!
=
1 + n(n + 1)
So we claim F(n) =
n2+ n + 1
Proof :
(by simple induction)
Becase :
For n
=
0
,
F(0) =
1
and 02 +
0 +
1 = 1 checked v
Inactive hypothesis :
Assume that for
F(k) k =
2
+
k +
1
In
e
step :
We have F(k 1) +
=
F(k) +
2(k 1) +
=>
k2 +
k 1 +
+
2k +
2
=
k +
2k +
1 +
k 1 +
+
1
=
(k 1)=
+
(k 1) +
+
1
F(n) =
n
+
n
+
B
Exercise: Developing Closed-Form
Let T : N ! N be a function given by
(
1, if n = 0
T (n) = (3)
2T (n 1) + 1, if n 1
For nx1
,
T(n) =
2 T (n-17+ 1
For n = 2 T(n) 2
(2T(n-2) 1) + 1
=
+
,
2 +
1
For n= 4 ,
T(n) =
2(2T( 4) - +
1) +
2 +
2 +
1
-
For n<, k
,
T(n) =
"T(n-k)
2 + 2 +
2 - ... +
2 +
2 +
1
k then
If n
=
2 2
,
T(n)
=
2 T(O) + + + ... +
2 +
2 +
1
T(n) =
2 +
2" - 1
n +
1
T(n) 2 I
= -
S
We leave this as an exercise for you
to
show this
by induction
Exercise: Developing Closed-Form
Let T : N ! N be a function given by
8
>
> 1 if n = 0,
>
>
<3 if n = 1,
T (n) = n 1 (4)
>
> X
>
:3T (n 1) +
> 2T (n i) otherwise.
i=2
For n32 ,
T(n) =
3 T(n-D + 2 /T(13 +
T(2) +
.. .
+
T(n -
2)
For n, 3
,
T(n-17 =
3 Y(n-27 +
2 (T(1) +
T(2) +
.. .
+
T(n-3))
Subtracting the second from the first ,
we
get
T(n) Y(n-1) 3T(n-1) 3 T(n -2) 2 T(n 2)
= - +
-
-
=> T(n) -
4T(n 1 -
+
T(n-2) =
y
Recall that solve this the ansatz
we can
using
T(n) =
Y
(see (ec2)