Ads Unit 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

I

.
• •

v/? ~-= ~~ -. ✓
I
l~~~ ~' t!:)~' ~
- • .~VM~.
\ ~ ~ ) ......] ~~
, . ~~ (~ct ~ ~~~(9,A

// . ~ ~ . ~ -~~-1\.,LL : • '

/9 W"'ClM \ . ~ , ~~ j~~oJt ~ -
·~ ~~1- ~-~~ w\u~ ~ ~~
Ji ~ ~ ~i~ & - ~ ·- :·'
'\,di ~ £JI,-. ,. ~
---=::==-~--~ . • . I ~ q\~
➔~~
J) ➔ ~""'---
'> A<t~::i ~~IYoAW~
(' c'--t\ V ~"\ tJ oQ..1-v r\)""t>uJ
,us
~ ~\J ~ o.tr?
-, J e._Q_Q (' t.. p..) (

..-) ~ u,'\ 4, "wt-


..--9 i~e.d
_, ~ u

/ t~Y\-U t,c1'(\plt,u, ~~Pi


~ 11,h c.
/ i-p~u .~vt.J,J~ .
\U VlU.W • b
~ Q+b
ht& C.
4
• Al . ,

i \. f\) ~ ~; ~~ t" ~ (\ ~ l'\0 .~;


fl )

C.7 ~

~ ~~;
l J\-; ~¢") rW
\. bo~ . tu<-'/, I

~ i,o~a -._,;-f
"-'-~ ~J,1'.) Q_ ~ \Ln) ~ ~\..,,) '"
) "·.... . . i ln) ~ A litn)) _ -
\ l 1\) ~ t_·ol") tm- all (\ 2:. n D cw.A ......
t >o

~ ~ VJ)~t,{) le): c~ ~) .
.~ i ~ ~ I\,.~ Clt.\L l> ~ ~
~ ~ \,t>~ ~~~u.wl o.J ~
~~ .. )
i ~#.),~ •l11) rv'"' ..
I"(\ ,, iln)~ ~CJ\))
• • I

C. ~ , 1 ln ) £. :1 { ln) ~ ~ ·c_~ • ~ ~)

~~ ~lf()e~L ~'~- .... · no


.
/ ~<,_f.1.U.U_ll..U. ~ CJ>V. ~g-(\1 ~ J..i!>~~
~~ u,-{'02\J.')\.,\~

✓ ~v-u ... 9e-"y ;Q_WJ\~\4 ..


\ tn ') : -rt n-()4-n
·,Lt)') : ~ ~ ~
~~cJ. ~~~ f

"i?~.... ~ -- l~VJ t. Y\J) • b , --


g~ta.<,:--
~~ sol -tu <TuL .9U-(..U.)).h1•- -t, spe,.I,tfu 1~ j
\~~)
4 -; - --r l n) ~ ~,: l n - l) + n
-rL,')~1- ~
--- gu..,bS~
~'Q\ M"j R~U.U.U."'-.l~ OJ;/ ~ . ~. _j
~ .
- - - - -r,u.t

CD 5\L-bs~-n,di~ ~ &

~ ~ 1 rrr- i.,.,,,.,., 't t1'v .<iol


~ ~ ~ '~VTJ 1-l- ~ct- Wi~
~c.Ai ,~~c:5YI.
~~p.QJ ~ ~~~6'{)

J; .., > v
!fvrwo..L.J. ~o.1d.

t t1'l) s t lt>) + \ -\- ~ + .. . . I)

-rt,,) ~ t>t \+Q-\-1••-+0

-rt"), n~-\-\ - _n:a.... _.._ f)


I),-~

i ln) e: D t" IJ.


For ex~ple :
Consider a recurrence relation
T(n) = T (n - 1) + n

with initial condition T(O) = 0.


Let,

T(n) = T(n - 1) + n
... (3)
If n =1 then
T(l) = T(O) + 1
t.

=0+1 ·: Initial conditi


... T(l) = 1'
If n =2, then
T(2) = T{l) + 2
= 1+2
... T(2) =3
If n =3 then
T(3) = T(2) + 3
= 3+3
T(3) = 6 ,..
- (

By observing above generated equations we can derive a fonnula


) _ n(n+·l) _ n 2 n - ·
T(.n - - - - - + -
2 2 2
We can also denote T(n) in tenns of big oh notation as follows -
T(n) = 0 (n 2 )

But in practice, it is difficult to guess the pattern from forw


this method is not very often used.
for example - ,l~~ ~tt~b)
Consider, a recurrence relation ~ •
T(n) = T(n - 1) + n
With initial condition T(O) = 0
T(n - 1) = T(n - 1 - 1) + (n - 1) . . . (8)
Putting equation (8) in equation (7) we get,
T(n) = T(n - 2) + (n - 1) + n ... (9)
Let
T(n - 2) = T (n - 2 - 1) + (n - 2) ... (10)
Putting equation (10) in equation (9) we get,
T(n) = T (n - 3) + (n - 2) + (n - 1) + n

= T (n - k) + (n - k + 1) + (n - k + 2) + ... + n
If k = n then

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

T(n) = 0 + 1 + 2 + ... + n
n(n+l) n2 n
T(n) = - - - = - + -
2 2 2
Again we can denote T(n) in terms of big oh notation as
T(n) e O (n 2 ).
-
\

,~~ f\AJUM L)
fb t \t\1- o}
I

°IY'J: . ,~"~?;
t it toss- 0) oou.l- LL" tibo9~eA~ "5nurn~ ,j
~Oj i°'t!v..i- t""oi f L-11 ! Y++)
ii Ln•~z l \I n"'s-~) '. I
·
t c_ou.k cc. t ~ b Lt) ~~ l\ LI
;
~\' ,) I
11
'
tJJ~ ~ .o;
.-e.tuM, l -£;b (p-1) +-iibln-~)))
..
j
Offlul- :~ I \

~ b-o .g ~ 9i ? ~ f>u : 0 \ l ~ 3

'
The Tree Method, also known as the Recursion-Tr ee Method, is a technique for solving
recurrence relations, which describe the time complexity of an algorithm in terms of its
subproblem s. This method involves representing the recurrence relation as a tree,
where each level of the tree corresponds to a recursive call and the nodes represent
the sizes of subproblem s. The cost associated with each recursive call is assigned to

the edges of the tree.

Here are the general steps of the Tree Method:


1. Build the Recursion Tree:

• Construct a tree to represent the recursive calls of the algorithm. Each node in the
tree corresponds to a subproblem, and the edges represent the recursive calls.

2 . Calculate the east-at Each Level:

• Det e rmine the cost assoo~ted with each level of the tree. The cost includes the work
done at each leve l, such as computations, comparisons, or other operations.

3. Sum the Costs:


• Sum 1.Jpcfhe~sts at~clrJev~ to fi nd the mtalfim e"ComJalexily of the algorithm._

Example:
Consider the following recu rrence relati on for quicksort:
T (n ) == T(Icft partition) + T(right partition) I cost of p-armtioning
Recursion Tree:

• Each node represents a subproblem (a partition).


• The left child represents the left partition, and the right child represents the right
partition.
• The cost of partitioning is assigned to the edges.

LI Copycode

T (n)
I \
T( left) T( right)
I \ I \
T(left-left) T(left-right) T(rightaleft) T(right-right)
Costs:

current partition .
• The cost of partitio ning is typicall y 0( n ), where n is the size of the
cost is the sum of
• Each level in the tree corresp onds to a recursive call, and the total
the costs at each level.

Total nme Comple xity:


T (n) == Cost at level 1 I Cost at level 2 I· ... I Cost at level k

A..nalyz ng the recursi on tree provide s insights into the overall


time comple xity of the
relation involves
algorith m . The Tree Method is particul arly useful when the recurre nce
e how the work is
c ivio rg : he prob lem into multiple subprob lems, and it helps visualiz
~ist·'iou :ed across d ifferent levels of recursion.

You might also like