dividepawang/courses/algo20/chap2.pdf · combine:-the securesire calls olp the sof" lathe...

30

Upload: others

Post on 03-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass
Page 2: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

Divide2- Conquer

Complexity Analysis{ ↳efficientoff-Algo Design Technique

÷÷:÷÷÷Data Structures

Page 3: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

q.io?:i::aE::e.. .problem for which ilp instance of

size nie giver.Rex : - Sorted army size n

find the element with keyin'

20

Divide: - Break The problem Cinsknainto subproblem financesof smaller

size)

conquest -7 Recursive call the abpOn each of the subproblems(stop when an instance is tuff'smad

Page 4: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

Combine: - The securesire calls olpthe sof

"

lathe subproblemCombine these solutions to obtainthe

so of the original prob (instancecompass with middle

Recourse into

insideone of the

Conquer Combine- -subarmp -

Binay ., Trivial

season 43¥56-c- n→

in:←

Imp tyg , Recurrence keen .

Tcn) - T (2) + 047

Page 5: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

gTCh)

''oc,- fTCF) tour

THE) loca ) ✓ depth

" I+ (3.)'

oui - -

K / = began

-147 KOCH

TIN - Olhgan)

Binary season

Page 6: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

#cyclic61%

121838T-1an

→ Powering a Number

Given a number ginteger nzo , find a

"

trainedn.ng.j.se -

Oln) alfo

DIE an wrath.ants

an -- fEna.

1-Ink 0 ( leggyw# in"¥ . no n odd

-.- -

r TINK TIE) -1041

Page 7: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

Sorting Algerost Quickest

Afi,-- ng

done & Divide

Recursively sort Ab. - . ITanimating)

IMerge 2 sorted lists Confine

ink a single sorted list

Pseudo code- Base case- Call with 1

. .. nlz

Call with Mee '.- M

Megee . -

Page 8: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

TCH = IT ( z, * 01nF"

.

-= alone * 09:D ten

/-

-

-

/ IT me

m.

i

,

-29"

--

- - 241,7.) t Essen(

gdokn Ca) t cntgn= on legist n 741

= OCnbg.mg -

Page 9: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

*worst-Case Lt'T =

-

Cnhgan = Ol )= OC )

Olnbgan,= Al )

Ouidesost A

nChoose was pirog rearranges

Page 10: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

suchthat x is put in its proper

posh in the sorted awayto § I conquer← →TEExo i am-

may notth

dividestooted

Combine : Trivial

Quick sort ( A,start,end

if Stan = - end zetumni ← PaAGMd)QuickSort (A

,start,i-17

Quick sort (S, its end)

Page 11: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

Partition (Aidan> endat A Estaing

Eine es.

eata t=⇒→ →j→

6 E i,be If-3,2 ' '

Money"

forward until SBTsooi ← it a swap ATD ⇐AED

i ← start [ indicates the boundary up't

je start -1A In elements

assed

TEX6 5 §To 8 ,

q µ

Page 12: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

i j → → . → →

6 5 35 8 is noTi

se-

← OHA- Estate # SED partition

⑤-681310€{⇒ -

a#- i -

pasting tie. qin-i

Time complexity CluicksortCI t TH-' I + TH

TIM = I +Tht)gahp nlz nee

media:-O n±fI← sinned:b.

Page 13: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

TCH - Cnt Tln -D=

Cn t Hn- e) t elm - a)

← Cnt Ccn - i ) + 1- (n-3) tuna1

!THX

= c. Ent n-it - - IT

= C - nlz = OCn4

Ohne)

InsertionSort--

wens

Slm)-91nF Cnt Tliiltdnni)-

-

-"n-

equally likelyA#-

Ne

Page 14: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

i÷:÷÷÷÷÷"*T (n- a) t Tfn-4 a- TIO

[ this t - --

-1¥= 21-147-1 - - Tcn-D

N- l

THI - 01Mt I E. Tci)-

J ith- -

Guessitheso# TINK OfstedThi s anloyn AI-

f) I can 't En ? ailgithiamine! in:

Page 15: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

this ohhaaai.i.IE/?IiihsiJ- 2

genes.is?as.✓

c- centralised . :tz tCn -

n-'

ibeen - Emu . ]i¥÷¥ish- -

= MII - HE2

= 4¥64 = 3nln8 -8

This cn-tluzhgn.MY" - Thay . her . #¥g

Page 16: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

THE cent a Cn -' I been - ¥-21 lgzI f

= an leg n + on - align - analyze- azhgz

= an Agm tf -earless) n - align-

-- gigs

-neo-

San Agn

Ice alwayfind-9>yeg①arose →

Tlnl I an bgn

Tla ) = Olnhgn)-

Page 17: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

in glide? sorting AlgoMime- complexity spacetime

Dirickson- - any case O (nbcsn)

n

:::÷.. . .ie::/sxYmi "- 6

'

6" -

G -

sadMemory is veg, in terms of ilp size

Read the ilp valuesHow much

Old t additionalamong ?ready ilp-

Joe?,Oln ) -in-placein -placeX -

Page 18: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

Polynomial MultiplicationSuppose we're two polynomials, eachof dagsea n- n ( n terms)

AHH a - -a. xtao

Pdx) - Ann X 't't bn.at/n-2e-.baXtbo--

The product pblgnonn.atA Blx) ← GCX)-

degree 2nsCl = Gqn.at/2mt62n.zX2n-3e - - Go

o - J n-'Ci o Sieg. .-2)→ 044

a - -0*4- K -- ing

'

i -- 2ns Ci = I aibk- Osi

,Kent - Noire

Cena -- Ann bn-2 ↳ yn,jtK= it fliptan-abna -

Page 19: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

Dir. & Cone .

1-AH an1on,

*E

)"

xtlam.it't't. -at)

A- Cx) = XtAni (x ) t AboutBLX ) = Xt Bni t Deol X )

cut. ACH . BLX)②-

= X2t Am. Bn .. txtlabzix- TO Anglo)

1- ABBA ③→

Page 20: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

-

EEIdng

- Master 's theoremwrote

" ÷:*..:*'

fCn7= Ofyhesba - e)- -

forgone E -O

⇒ Hnk @ (nhdba)

air:÷÷-

A -- 4-b-- 2

9h01 = n.

Hat off-e)Tink Olney ✓

-→Maire

Page 21: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

Or ⑥ aAhiBhi ArloBb

- i-Ahi Blot Alo Bhi-

only find this whole thing(m÷sm:.Multiply Poly Z .

TIM - 3T (Nz ) t In

This = ofglossyOng

Oln "54 /be

Karatsuba's Poly. Multi

Page 22: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

Matrix Multiplication-

ilp A - Tais] B- Ebij]

olp ( = Icij) igj -- I . - D= A - B.n

(Cig 's E aikbkj →Oln)K'-Mu

01ns) → NaivelyT ,

a "nee b

'

t÷ '' Ic.

: d n g :b ni ,

✓A 7 B

'

n

F- aetbgre-

✓-

u diss -- aaftbhne µ - ÷. - 1- nt, cetdlf t

.

U

y -- CITING In

Page 23: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

TIM - IT(E) t Oke). Feng =oln'-9Master's,- theorem

C

7 Olney -9=067= 0h31 = Naive Also

complexity

Strassen's Matrix Multi.

TCM -- 7T(hate Oln "

-

- Alm test)Cmx : Div a long.

Trick bredace

fabproblemsat

Page 24: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

(Poly Mutt . →

Prob. Large Integer multiplication

× 33021 . .Haine Algo] Dad → More

efficient

Page 25: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

Closest Pair problem

x

x*

A

:. :x

-

Maire Sol?. aeh pair

Ietp.to

↳ f- 'rdthe Smallest diet

OlneyDin. e Gna-

Page 26: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

Let us denote the setof points byP- Ep. . - - prig where pi has

co-ordinates (Riggi)for every pair pips EPdlpispj) denotes the Std.

Euclidean distance

Goal : -3 find a pair of points pigs; thatminimizes dlipispj)

n

emin d Cpi,is

igj.- A-it;

t -D planeSortthem 2- then find min

.

2- D planenloyn

Page 27: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

c'

,r f

l l.-

s.

.-

,

* (Il, pit x

--F-

s- -

* fix a o' x

X,-lI,

'

✓ c

×a

'

II,

×

I ✓

x.

:*

( left !: fright

-

.3)ketoses- pair in

, closest poorleft ,rim"

L )closest pair overall

conquer-$ combine

all points acrossAJI plait gownday

Page 28: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

Sort all the port in P by a-to → Px

also by y-co - pga

oR

Oo

Op

° o

g, ↳ a,o ooo

o Oo

o o

O-- - n e - -

.

S - min (do,der)t

O : set of points in The first'f7 poshof the dirt Px ( left half)

R : - final LIJ poshof Px ( sight haltf '

Recursively find closest pair in death

Page 29: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

Esther gone 9,1Eta ate

for which dues) ad ?--

KI. . -S.'

/'

f'

/ In.

•l

':/ , ! rl l f p

Sy : the listof Inknkl Let ¥1I ' b:p pirinpoints in S l L l had

sorted Gf-! -zper theiry - co-ordinates

-

band .✓ Is✓

7×7 "'

Page 30: Dividepawang/courses/ALGO20/chap2.pdf · Combine:-The securesire calls olp the sof" lathe subproblem Combine these solutions to obtainthe so of the original prob (instance compass

I - e

"l

l l

'

I µµ2d%I'

l

l : outI ×

I .

a

-i is

- -i- .: i

.

f- i. it- -i-I. it. . said

,

.' .

1

.

She

f:÷÷÷÷÷÷÷÷÷÷÷÷:-

of other portsfact : Atmot I point can lie in a

single block .

> compare with at most 15 other points