程序代写代做代考 C ant algorithm Divide and conquer algorithms

Divide and conquer algorithms
+ Associated recurrences
Sept. 3,
2020

Exc-ewplet I23
find selection
e x it;
else 4 max-_ a -43; iced- max =L;
liel tor) if(aTi3>max) f
(a -4. – exchange a ⇐
– n’s) ;
a -Ln3 ;
y=
– sort
SELECT
CaEl. -n-is) (ALT. – B)
sort (res)
if
for
if -43

{ RECURRENCE

wax
SELECTION sort
aTired- wax] # SELECT- SORT (a El.
– FB);
: Tcn)aC.n+
T Cn- i)
n

wax-_ a Ei);iud- a
wax —
array of M integers
TASK: SORTTHEM →

SOLVE RECURRENCE BY ITERATION
Tcm The – 2)
) 1-In- t)
– :e
1- ( l) =
– 1) . – – -he t
T (n ) +T
C.(It c-Tcu- 1)t-
= C- N e-The- e)
TTM -4 =C- th- 2) +The-3)
= C. th- t)
c- It
TCO) c-TH)=
tta Tin)= C M¥1) +Tco)= 2(U2).
-r- -27 )+
-.
tu
+ Tco ))

DIVIDE – D IV ID E
2. SOLVE
AND – CONQUER
PARADIGM
1.
PROBLEM INTO
SMALLER SUBPROBLEM S
3.
C O M B IN E
SOL. of THE ORIGINAL
THE SUBPROBLEMS RECURSIVELY
OF PRO LEM S IN TO A
THE SOLUTIONS SOB
PROBLEMS

SEARCH sorted array a.El.
Ex. 2:BINARY Input:
GOAL -:
Biusearch if (l
3 print” x at pos
. n] F ; × ,
andwhere itis
n
FIND
if x
is in the
array
,
m
Imi delle
ctfupore with x , and continue rec
right
.
la El. –
>=r and aEe3==x)
tothe left,or r3.x ) 4
; ) return
else Sez rel ; BiuSearchCafe. – r3, x ); } }
and {printxnot
aEl]!ex )
if(e>=r
m
=
ex
II;

in the

;s
.
l ” return ;
arras ;
if l X e a Enis ) Sr = m ; B iu Search Cate – – BxB

RECURRENCE Tcn)= T(Nz)+ C
SOLVE RECURRENCE
Assume n=2k ,
= TINK) +
1- Inly ) = THIS ) c- C
1- In ) T IN K )
c = Tlnly)+ c
K-_
logic
r


Tibet) =Tl%k)-1C
— z7
+ CANCEL
ADD
th –
TCh)= CtCt K
TC+Tcl) times
=

+Tcl)
c. login + T h ) =
e.K =
A ( login )

INHAT IF N t
Then
2K
?
2K- ‘ene
for some sok
K
:
2K login
flogMe
(2K- i) ETCn)ETht)

CK – 1) e -T a l E t c h ) E C – K c – T h ) Tin ) = A ( login).
“nice”
T C –
so
WE CAN ALWAYS ASSUME n hasthee
form we need
.

LOWERBOUND FOR THE SEARCH PROBLEM Q : C a u m e f i r e d x i n a r r a y a E l . – n] w i t h l e s s
login comparisons ? !
Suppose there is an alg. thatfinds x with
than A: No
P R :
— e-
a
posh
5=74
poss
I

.
.-
(logn (for
N’ answer
then each
) roseus (‘Y’ ‘
couepo.is / : :
i
allx ).

or
etc TT
172
etc .
Grupa
)
μ can
:: ::: i:
Chee path :-
pM .
..-
-n→MY—
Mz
labels
Impossible
n ele
w
E 2105M- ‘ =
.
.
.
be :c
described
, my by
T YY- he

that finds inhere is 1 )
.-
test, . –

E
NAIVE ALG :
EXPONENTIATION
MODULAR
2″ (mod m ).
Supper mis fixedforest. )
New ant an efficient
Suppose-m is
o 200
= 2199
100
– one year a 32.


Suppose we can do 106I 220multiplierper Sec.
Z199
M
7


T IM E
220. 225
2154 years → Age of Universe
DIVIDE-
AND – 220
CONQUER
210 × 210 =
2 . x 210×210
:
– =
= 221 =


22 ¥es
200 bits
..
.–.
106 See
=
25.220
Sec .
asa
2 long .
ally . function of n
.

zn, mod
y2″ ” x 2″ ” if n even zM2×zW2×£ , i f n o d d
(n)4
if In==o ) return):

exp
ifCn== 1) return2(Moda);
else
t= Mood-expMk)
4
if (Neveu) return txt (Modu)
else return
RECURRENCE
T th) =TfZ)-12
txtx2
(modal.
SOLUTION
:
T Int =
Allegri )
.

Ex. 4 A- :
MERGE
:
t
)
.
1- –
SORT IT
(
SORT LEFT HALF 7 ‘ SORT RIGHT HALF
(3) MERGETHE TWO HALVES 1514111/8471)/21613-2
←4 1514.487
14 S
6731 Recursively
ort Ilsa %
diniie
144421 11144444742

(A
[
mergeSort
pi
merge Sort ( A Ep .
=
=;
RT); Merge(A-E3,p,q, r)
mergeSort (A -19+1. .
merge ( A E3, Pilar);
fARMENd-al . page 31
:
RUNTIME for merge
: C- n (or C)In) )
lxlhy:
CopyAinto L R: ,
Write back in A
Oln) Oh,
TOTAL– Oln)
/→
.
:
[p. – r)) r-P
.
97 );
copy left Half in L copy Right Half ice R

f-
Laud
Rsimuet .
elem .
{ traverse t h e back
smaller Write i n A

RECURRENCE
TCn)= 2-T1¥)+ C.n
SOLVING T H E RECURRENCE Assumen= 2k;sokelogin.
2 ITIn12)+ c.n
2T 1%1 = 21 2T (my>+ c.z)+ c.n =3TIE)+2cm
T In ) =
IT(Mfa)= 2242T(Mas)+ c.Zz)+29=23Tf )+3cm
2″
:
:
.tl#…)=2-‘f24.T/nze)-cz)Yz.)xCkttcn— TfxC.kn
=N = n.
=
=
A Cn- login)
.
dd
I
TCl) + C.n.
logn

levee
0
half Merge cnztcuzzan
RECURSION
T R E E
for mergeSort entire input
level level
2
O
£ 1#
left right half
Melena . operations
I
merge
on £↳£# → each Mreeeue
:
:
( ← single leveeKO–o –oelements
Ateachlevel: TOTAL lineORK :
amountofwork is
K. en= logn. =
C. n
(todo merge)
CN enlogn

MASTER THEOREM RECURRENCE ( for DNIDTE- AND – CONQUER )
Tf}
combine step”
{Ctfmloodba), if log,a >ol coarsen)
Tent Ea

)
e- OInd); a71, b>l; calls
A: b: nd :
of
shrinkage factor
no-
ree .
time for SOLUTION

+ in, =
A (nlotsa toga) if toga=D KASEY

A (nd) , if logy < a , . o) (CASE3) MASTER THEOREM RECURRENCE (more generalform) Tent Ea - a 71, b> l;
A: b:
time for SOLUTION
T In’ =
,,
So :
TIE
Ct (flu).
logu)
-If
,
Tfnz) e- fin) ;
calls shrinkage factor
no-
ree .
fuel :
{ Afmloodba
.
of
(fans)
n loads” vs
Tcm ) =
if
Blue) )
( CASE 31

combine step”

,
) if
¥÷ – ,
Ring
→ more generalform
ceases
A (fin). logu) if n÷K=Aa KASEY
,
)
IG WINNER = pdyu ⇒Tfn)= AGNINNER) Josep .
Else MASTER TH DOES NOT APPLY
fn’÷g, flu