# Welcome back

## Find a book, put up your feet, stay awhile

Sign in with Facebook

Sorry, we are unable to log you in via Facebook at this time. Please try again later.

or

Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more

Download

Standard view

Full view

of .

Look up keyword

Like this

Share on social networks

0Activity

×

0 of .

Results for: No results containing your search query

P. 1

Divide and ConquerRatings: (0)|Views: 1|Likes: 0

Published by Saba Suhail

See more

See less

https://www.scribd.com/doc/134391068/Divide-and-Conquer

04/06/2013

text

original

Divide-and-Conquer1

Divide-and-Conquer

7 2

9 4

→

2 4 7 97

2

→

2 79

4

→

4 97

→

72

→

29

→

94

→

4

Divide-and-Conquer2

Outline and Reading

Divide-and-conquer paradigm (§5.2)Review Merge-sort (§4.1.1)Recurrence Equations (§5.2.1)

Iterative substitution

Recursion trees

Guess-and-test

The master method

Integer Multiplication (§5.2.2)

Divide-and-Conquer3

Divide-and-Conquer

Divide-and conqueris ageneral algorithm designparadigm:

Divide: divide the input data

S

intwo or more disjoint subsets

S

1

, S

2

, …

Recur: solve the subproblemsrecursively

Conquer: combine the solutionsfor

S

1

, S

2

, …, into a solution for

S

The base case for therecursion are subproblems of constant size Analysis can be done using

recurrence equations

Divide-and-Conquer4

Merge-Sort Review

Merge-sort on an inputsequence

S

with

n

elements consists of three steps:

Divide: partition

S

intotwo sequences

S

1

and

S

2

of about

n

/

2

elementseach

Recur: recursively sort

S

1

and

S

2

Conquer: merge

S

1

and

S

2

into a unique sortedsequence

Algorithm

mergeSort

(

S, C

)

Input

sequence

S

with

n

elements, comparator

C

Output

sequence

S

sortedaccording to

C

if

S.size

()

>

1(

S

1

,

S

2

)

←

partition

(

S

,

n

/2)

mergeSort

(

S

1

,

C

)

mergeSort

(

S

2

,

C

)

S

←

merge

(

S

1

,

S

2

)

Divide-and-Conquer5

Recurrence Equation Analysis

The conquer step of merge-sort consists of merging two sortedsequences, each with

n

/

2

elements and implemented by means of a doubly linked list, takes at most

bn

steps, for some constant

b

.Likewise, the basis case (

n

< 2)

will take at

b

most steps.Therefore, if we let

T

(

n

)

denote the running time of merge-sort:We can therefore analyze the running time of merge-sort byfinding a

closed form solution

to the above equation.

That is, a solution that has

T

(

n

)

only on the left-hand side.

≥+<=

2if )2/(2
2if )(

nbnnT
nbnT

Divide-and-Conquer6

Iterative Substitution

In the iterative substitution, or “plug-and-chug,” technique, weiteratively apply the recurrence equation to itself and see if we canfind a pattern:Note that base, T(n)=b, case occurs when 2

i

=n. That is, i = log n.So,Thus, T(n) is O(n log n).

ibnnT bnnT bnnT bnnT bnnbnT
bnnT nT

ii

+==+=+=+=++=+=

)2/(2
...4)2/(2
3)2/(2
2)2/(2
))2/())2/(2(2
)2/(2)(

4433222

nbnbnnT

log)(

+=

Divide-and-Conquer7

The Recursion Tree

Draw the recursion tree for the recurrence relation and look forapattern:

sizeT’sdepth

……

…n

/

2

i

2

i

i n

/

221

n

10

≥+<=

2if )2/(2
2if )(

nbnnT
nbnT

time

…bnbnbn

Total time =

bn

+

bn

log

n

(last level plus all previous levels)

Divide-and-Conquer8

Guess-and-Test Method

In the guess-and-test method, we guess a closed form solutionand then try to prove it is true by induction:Guess: T(n) < cnlog n.Wrong: we cannot make this last line be less than cn log n

nbncnncn
nbnncn
nbnnnc
nbnnT nT

logloglog)2log(log
log))2/log()2/((2
log)2/(2)(

+−=+−=+=+=

≥+<=

2if log)2/(2
2if )(

nnbnnT
nbnT

Divide-and-Conquer9

Guess-and-Test Method,Part 2

Recall the recurrence equation:Guess #2: T(n) < cnlog

2

n.

if c > b.

So, T(n) is O(n log

2

n).In general, to use this method, you need to have a good guessand you need to be good at induction proofs.

ncnnbncnncnncn
nbnncn
nbnnnc
nbnnT nT

2222

logloglog2log
log)2log(log
log))2/(log)2/((2
log)2/(2)(

≤++−=
+−=+=+=

≥+<=

2if log)2/(2
2if )(

nnbnnT
nbnT

Divide-and-Conquer10

Master Method

Many divide-and-conquer recurrence equations havethe form:The Master Theorem:

≥+<=

d nn f bnaT
d ncnT

if )()/(
if )(

.1somefor )()/( provided
)),((is)(then),(is)(if 3.
)log(is)(then),log(is)(if 2.
)(is)(then),(is)(if 1.

log1loglogloglog

<≤ΘΩΘΘΘ

++−

δ δ

ε ε

n f bnaf
n f nT nn f
nnnT nnn f
nnT nOn f

ak ak aaa

bbbbb

Divide-and-Conquer11

Master Method, Example 1

The form:The Master Theorem:Example:

≥+<=

d nn f bnaT
d ncnT

if )()/(
if )(

.1somefor )()/( provided
)),((is)(then),(is)(if 3.
)log(is)(then),log(is)(if 2.
)(is)(then),(is)(if 1.

log1loglogloglog

<≤ΘΩΘΘΘ

++−

δ δ

ε ε

n f bnaf
n f nT nn f
nnnT nnn f
nnT nOn f

ak ak aaa

bbbbb

nnT nT

+=

)2/(4)(

Solution: log

b

a=2, so case 1 says T(n) is O(n

2

).

Divide-and-Conquer12

Master Method, Example 2

The form:The Master Theorem:Example:

≥+<=

d nn f bnaT
d ncnT

if )()/(
if )(

.1somefor )()/( provided
)),((is)(then),(is)(if 3.
)log(is)(then),log(is)(if 2.
)(is)(then),(is)(if 1.

log1loglogloglog

<≤ΘΩΘΘΘ

++−

δ δ

ε ε

n f bnaf
n f nT nn f
nnnT nnn f
nnT nOn f

ak ak aaa

bbbbb

nnnT nT

log)2/(2)(

+=

Solution: log

b

a=1, so case 2 says T(n) is O(n log

2

n).

Divide-and-Conquer13

Master Method, Example 3

The form:The Master Theorem:Example:

≥+<=

d nn f bnaT
d ncnT

if )()/(
if )(

log1loglogloglog

<≤ΘΩΘΘΘ

++−

δ δ

ε ε

n f bnaf
n f nT nn f
nnnT nnn f
nnT nOn f

ak ak aaa

bbbbb

nnnT nT

log)3/()(

+=

Solution: log

b

a=0, so case 3 says T(n) is O(n logn).

Divide-and-Conquer14

Master Method, Example 4

The form:The Master Theorem:Example:

≥+<=

d nn f bnaT
d ncnT

if )()/(
if )(

log1loglogloglog

<≤ΘΩΘΘΘ

++−

δ δ

ε ε

n f bnaf
n f nT nn f
nnnT nnn f
nnT nOn f

ak ak aaa

bbbbb

2

)2/(8)(

nnT nT

+=

Solution: log

b

a=3, so case 1 says T(n) is O(n

3

).

Divide-and-Conquer15

Master Method, Example 5

The form:The Master Theorem:Example:

≥+<=

d nn f bnaT
d ncnT

if )()/(
if )(

log1loglogloglog

<≤ΘΩΘΘΘ

++−

δ δ

ε ε

n f bnaf
n f nT nn f
nnnT nnn f
nnT nOn f

ak ak aaa

bbbbb

3

)3/(9)(

nnT nT

+=

Solution: log

b

a=2, so case 3 says T(n) is O(n

3

).

Divide-and-Conquer16

Master Method, Example 6

The form:The Master Theorem:Example:

≥+<=

d nn f bnaT
d ncnT

if )()/(
if )(

log1loglogloglog

<≤ΘΩΘΘΘ

++−

δ δ

ε ε

n f bnaf
n f nT nn f
nnnT nnn f
nnT nOn f

ak ak aaa

bbbbb

1)2/()(

+=

nT nT

Solution: log

b

a=0, so case 2 says T(n) is O(log n).(binary search)

Divide-and-Conquer17

Master Method, Example 7

The form:The Master Theorem:Example:

≥+<=

d nn f bnaT
d ncnT

if )()/(
if )(

log1loglogloglog

<≤ΘΩΘΘΘ

++−

δ δ

ε ε

n f bnaf
n f nT nn f
nnnT nnn f
nnT nOn f

ak ak aaa

bbbbb

nnT nT

log)2/(2)(

+=

Solution: log

b

a=1, so case 1 says T(n) is O(n).(heap construction)

Divide-and-Conquer18

Iterative “Proof” of theMaster Theorem

Using iterative substitution, let us see if we can find a pattern:We then distinguish the three cases as

The first term is dominant

Each part of the summation is equally dominant

The summation is a geometric series

∑∑

−=−=

+=+==+++=
++=++=+=

1)(log0log1)(log0log2233
222

)/()1(
)/()1(
...)()/()/()/(
)()/()/(
))/())/((
)()/()(

niiianiiin

bbbb

bn f aT n
bn f aT a
n f bnaf bn f abnT a
n f bnaf bnT a
bnbn f bnaT a
n f bnaT nT

- Read and print without ads
- Download to keep your version
- Edit, email or read offline

© Copyright 2015 Scribd Inc.

Language

Choose the language in which you want to experience Scribd:

Sign in with Facebook

Sorry, we are unable to log you in via Facebook at this time. Please try again later.

or

Password Reset Email Sent

Join with Facebook

Sorry, we are unable to log you in via Facebook at this time. Please try again later.

or

By joining, you agree to our

read free for one week

Personalized recommendationsbased on books you love

Syncing across all your devices

Join with Facebook

or Join with EmailSorry, we are unable to log you in via Facebook at this time. Please try again later.

Already a member? Sign in.

By joining, you agree to our

to download

Personalized recommendationsbased on books you love

Syncing across all your devices

Continue with Facebook

Sign inJoin with emailSorry, we are unable to log you in via Facebook at this time. Please try again later.

By joining, you agree to our

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

CANCEL

OK

You've been reading!

NO, THANKS

OK

scribd