Sei sulla pagina 1di 29

Design and Analysis of Algorithms

Lecture 13:
Solving Recurrence

Tauseef Iftikhar
Department of Computer Science
Government College University, Lahore.

1/22

Todays Agenda

Iterative Method

Recursion Tree Method

2/22

Iterative Method

Expand the recurrence

Work some algebra to express as a summation Work some


algebra to express as a summation

Evaluate the summation

3/22

Example 1
(
c
T (n) =
T (n 1) + c

ifn 1
ifn > 1

Iteration 1:
T (n) = T (n 1) + c (1)
putting value of T (n 1) in
(10). Iteration 2:

T (n 1) = T (n 1 1) + c

T (n) = T (n 2) + c + c
= T (n 2) + 2c

T (n 1) = T (n 2) + c

(2)

putting value of T (n 2) in (11).

T (n 2) = T (n 2 1) + c
T (n 2) = T (n 3) + c

T (n) = T (n 3) + c + 2c
= T (n 3) + 3c

(3)
4/22

Example 1

(
c
ifn 1
T (n) =
T (n 1) ifn > 1
what is kth iteration?

5/22

Example 1

(
c
ifn 1
T (n) =
T (n 1) ifn > 1
what is kth iteration?
T (n) = T (n k) + kc (4)
now if k = n 1

T (n) = O(n)

T (n) = T (1) + (n 1)c


= c + cn c
= cn

(5)

5/22

Example 2
(
c
T (n) =
T (n 1) + n

ifn 1
ifn > 1

Iteration 1:
T (n) = T (n 1) + n (6)
putting value of T (n 1).
Iteration 2:

T (n 1) = T (n 1 1) + n 1
T (n 1) = T (n 2) + n 1

T (n) = T (n 2) + n 1 + n
= T (n 2) + 2n 1(7)
putting value of T (n 2).
Iteration 3:
T (n) = T (n 3) + n 2 + 2n 1
= T (n 3) + 3n 3

(8)

6/22

Example 2
(
c
T (n) =
T (n 1) + n

ifn 1
ifn > 1

Iteration 1:
T (n) = T (n 1) + n (6)
putting value of T (n 1).
Iteration 2:

T (n 1) = T (n 1 1) + n 1
T (n 1) = T (n 2) + n 1

T (n) = T (n 2) + n 1 + n
= T (n 2) + 2n 1(7)
putting value of T (n 2).
Iteration 3:

T (n 2) = T (n 2 1) + n 2

T (n 2) = T (n 3) + n 2
T (n) = T (n 3) + n 2 + 2n 1
= T (n 3) + 3n 3

(8)

6/22

Example 2
(
c
T (n) =
T (n 1) + n

ifn 1
ifn > 1

what is kth iteration?

7/22

Example 2
(
c
T (n) =
T (n 1) + n

ifn 1
ifn > 1

what is kth iteration?


T (n) = T (n k) + nk k

(9)

now if k = n 1
T (n) = T (1) + n(n 1) (n 1)
= c + n2 n n + 1
= n2 2n + c
T (n) = O(n2 )

7/22

Example 3
(
c
T (n) =
T ( n2 ) + c

ifn 1
ifn > 1

Iteration 1:
n
T (n) = T ( ) + c (10)
2
putting value of T ( n2 ).
Iteration 2:
n
T (n) = T ( ) + c + c
4
n
= T ( ) + 2c (11)
4

n
n/2
T( ) = T(
)+c
2
2
n
n
T( ) = T( ) + c
2
4

putting value of T ( n4 ).
Iteration 3:
n
T (n) = T ( ) + c + 2c
8

8/22

Example 3
(
c
T (n) =
T ( n2 ) + c

ifn 1
ifn > 1

Iteration 1:
n
T (n) = T ( ) + c (10)
2
putting value of T ( n2 ).
Iteration 2:
n
T (n) = T ( ) + c + c
4
n
= T ( ) + 2c (11)
4
putting value of T ( n4 ).
Iteration 3:
n
T (n) = T ( ) + c + 2c
8

n
n/2
T( ) = T(
)+c
2
2
n
n
T( ) = T( ) + c
2
4

n
n/4
T( ) = T(
)+c
4
2
n
n
T( ) = T( ) + c
4
8
8/22

Example 3

(
c
ifn 1
T (n) =
n
T ( 2 ) ifn > 1
what is kth iteration?

9/22

Example 3

(
c
ifn 1
T (n) =
n
T ( 2 ) ifn > 1
what is kth iteration?
T (n) = T (

n
) + kc
2k

(13)

now recurrence stops at base-case if 2k = n k = log n


T (n) = T (1) + c log n
= c + c log n
T (n) = O(log n)

9/22

Recursion Tree Method

In a recursion tree, each node represents the cost of a single


subproblem somewhere in the set of recursive function invocations.
I

expanding the recurrence into a tree

summing the cost at each level

applying the substitution method

If you are sloppy then recursion tree can use to give good guess,
but if you are drawing tree carefully then it can be used as a direct
proof of running time.

10/22

Example 1: Rigorously drawn recursion tree

n
T (n) = 2T ( ) + c
2
c
c

c
T(n)

T( n2 )

T( n4 )

T( n2 )
..
.

T( n4 )
..
.

..
.

T( n4 )
..
.

..
.

T( n4 )
..
.

..
.

..
.

11/22

Example 1
c
c

c
Level0= 1 c

c
Level1= 2 c

..
.

..
.

..
.

..
.

..
.

..
.

..
.

..
.
Level2= 4 c

T( 2nk ) T( 2nk )

...

T(1) T(1)

...

T (n) = c

T( 2nk ) T( 2nk )

..
.

T(1) T(1)
k
X

2i

Levelk= 2k c = nc
Pk
i
i=0 2 c

i=0

What is height of the tree?


12/22

Example 1: Rigorously drawn recursion tree


Geometric series:
k
X
i=0

2i =

2k+1 1
= 2k+1 1
21

Tree yields the base-case when 2k = n k = log2 n


T (n) = c(2.2k 1)
T (n) = c(2.2log2 n 1)
= c(2nlog2 2 1)
= c(2n 1)
T (n) = O(n)

13/22

Example 2

n
T (n) = 2T ( ) + n
2
n
n
2

n
T(n)

T( n2 )

n
2

T( n4 )

T( n2 )
..
.

T( n4 )
..
.

..
.

T( n4 )
..
.

..
.

T( n4 )
..
.

..
.

..
.

14/22

Example 2: Rigorously drawn recursion tree


n
n
2
n
4

..
.

n
4

..
.

Level0= n

n
2

..
.

n
4

..
.

..
.

Level1= n

n
4

..
.

..
.

..
.

T( 2nk ) T( 2nk )

...

T( 2nk ) T( 2nk )

T(1) T(1)

...

T(1) T(1)

Level2= n
..
.
Levelk= n
Total cost = kn

What is height of the tree?

15/22

Example 2: Rigorously drawn recursion tree

T (n) = nk + n
Tree yields the base-case when 2k = n k = log2 n
T (n) = n log n + n
T (n) = O(n log n)

16/22

Example 3: Recursion tree for good guess

n
T (n) = 3T (b c) + (n2 )
4
T(n)

17/22

Example 3: Recursion tree for good guess

n
T (n) = 3T (b c) + (n2 )
4
cn2
T(n)
T(b n4 c) T(b n4 c) T(b n4 c)

17/22

Example 3: Recursion tree for good guess

cn2
2

n
c( 16
)

n
c( 16
)

n
c( 16
)

n
n
n
n
n
n
n
n
n
T( 16
) T( 16
) T( 16
) T( 16
) T( 16
) T( 16
) T( 16
) T( 16
) T( 16
)

.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
. . . . . . . . . . . . . . . . . . . . . . . . . . .

18/22

Example 3: Recursion tree for good guess

cn2
2

n
c( 16
)

n
c( 16
)

n
c( 16
)

n 2
n 2
n 2
n 2
n 2
n 2
n 2
n 2
n 2
) c( 16
) c( 16
) c( 16
) c( 16
) c( 16
) c( 16
) c( 16
) c( 16
)
c( 16

.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
. . .. . .. . . . . .. . .. . . . . .. . .. . .
T( 4nk )T( 4nk )T( 4nk )

...

T( 4nk )

T(1) T(1) T(1)

...

T(1)

19/22

Example 3: Recursion tree for good guess


Height of tree?
depth 0 = cn2
3
cn2
depth 1 = 16
3 2 2
) cn
depth 2 = ( 16
..
.
depth k= (nlog4 3 )
T (n) =
cn2

k1
X
3
( )i +(nlog4 3 )
16
i=0

20/22

Example 3: Recursion tree for good guess


Height of tree?
I

The subproblem size for a node at depth i


is 4ni

Subproblem size hits n = 1 when


that is, i = log4 n

cn2

depth 0 =
3
cn2
depth 1 = 16
3 2 2
) cn
depth 2 = ( 16
..
.
depth k=

(nlog4 3 )

T (n) =
cn2

k1
X
3
( )i +(nlog4 3 )
16

n
4i

= 1,

Cost
I

Number of nodes at depth i is 3i

Each node has a cost at depth i is c( 4ni )2

Cost over all nodes at depth i is


3 i
3i c( 4ni )2 = ( 16
) cn2

The bottom level have 3log4 n = nlog4 3


number of nodes each having cost T (1).

Total cost of all nodes at bottom is


nlog4 3 T (1) = (nlog4 3 )

i=0

20/22

Example 3: Recursion tree for good guess


T (n) = cn2

k1
X
3
( )i + (nlog4 3 )
16

(14)

i=0

( 3 )k 1
= cn2 163
+ (nlog4 3 )

1
16

(15)

Here we show some sloppiness by using infinite geometric series to


get rid of messy fraction in eq(15). We again start from eq(14).
k1
X
3
T (n) = cn
( )i + (nlog4 3 )
16
2

i=0

X
3
2
< cn
( )i + (nlog4 3 )
16
i=0

< cn2

1
+ (nlog4 3 )
3
1 ( 16
)
21/22

Example 3: Recursion tree for good guess

16
) + (nlog4 3 )
13
T (n) = O(n2 )
T (n) < cn2 (

22/22

Potrebbero piacerti anche