Decrease & Conquer: Binary Search, Exponentiation, Russian Peasant Multiplication, Study notes of Algorithms and Programming

Various decrease-and-conquer algorithms, including binary search, exponentiation by squaring, russian peasant multiplication, fake-coin puzzle, josephus problem, euclid's algorithm, and selection problem. These algorithms reduce the instance size by a constant factor and are used to solve problems such as finding the median, computing exponentiation, and detecting the fake coin. The document also covers variable-size-decrease algorithms and their applications.

Typology: Study notes

Pre 2010

Uploaded on 08/16/2009

koofers-user-u68
koofers-user-u68 🇺🇸

10 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MoreDecreaseand
Conquer(5.55.6)
COMP157
Oct15,2007
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Decrease & Conquer: Binary Search, Exponentiation, Russian Peasant Multiplication and more Study notes Algorithms and Programming in PDF only on Docsity!

More

Decrease

and

Conquer

COMP

Oct

Decrease

by

Constant

Factor

In

this

variation

of

decrease

and

conquer,

instance

size

is

reduced

by

the

same

factor

(typically,

Examples:• Binary

search

and

the

method

of

bisection

  • Exponentiation

by

squaring

  • Multiplication

à la

russe

(Russian

peasant

method)

  • Fake

coin

puzzle

  • Josephus

problem

Russian

Peasant

Multiplication

The

problem:

Compute

the

product

of

two

positive

integers

Can

be

solved

by

a

decrease

‐by

‐half

algorithm

based

on

the

following

formulas.

For odd values of

n

:

n

m

=

  • 2

m + m

if

n

1

and

m

if

n

= 1

n

  • 1^2

For even values of

n

:

n

m

=

  • 2

m

n 2

Example

of

Russian

Peasant

Multiplication

Compute

20

26

n

m

20

26

10

52

5

104

104

2

208

1

416

416520

Note: Method reduces to adding

m

’s values corresponding to odd

n

’s.

Fake

Coin

Puzzle

There

are

n

identical

looking

coins,

one

of

which

is

fake. The

fake

coin

is

lighter

than

the

real

coins,

real

coins

all

weigh

the

same.

There

is

a

balance

scale,

which

can

tell

whether

two

sets

of

coins

weigh

the

same,

or

which

of

the

two

sets

is

heavier. Design

an

efficient

algorithm

for

detecting

the

fake

coin.

Fake

Coin:

Decrease

by

factor

of

Divide

coins

into

two

piles

of

n

coins

Heavier

pile

is

all

real

coins

Repeat

for

lighter

pile

(problem

of

n/

size)

Number

of

weighing:

W(n)

W(

n

W(1)

W(n)

log

2

n

Can

we

do

better

by

increasing

the

decrease

factor?

Josephus

Problem

if

n

is

even

(n

2k):

J(2k)

2J(k)

relates

Josephus’ position

in

original

circle

to

his

position

in

next

smaller

circle

if

n

is

odd

(n

2k+1):

J(2k+1)

2J(k)

base

case:

J(1)

To

determine

J(n),

we

need

a

closed

form

solution

(a

bit

tricky)

Josephus

Problem

Most

elegant

solution:

J(n)

one

bit

cyclic

left

shift

of

n

Examples:

J(6)

J(

2

2

J(11)

J(

2

2

Euclid’s

algorithm

is

based

on

repeated

application

of

equality

gcd(

m,

n

gcd(

n,

m

mod

n

Ex.:

gcd(80,44)

gcd(44,36)

gcd(36,

gcd(12,0)

One

can

prove

that

the

size,

measured

by

the

second

number,

decreases

at

least

by

half

after

two

consecutive

iterations.

Hence,

T(

n

)^

O(log

n

)

Euclid’s

Algorithm

Selection

Problem

Find

the

k

th

smallest

element

in

a

list

of

n

numbers

k

^1

or

k

n

Æ

usual,

smallest/largest

value

problem

median

:^

k

n/

Example:

median =

The

median

is

used

in

statistics

as

a

measure

of

an

average

value

of

a

sample.

It

is

a

better

(more

robust)

indicator

than

the

mean,

which

is

used

for

the

same

purpose.

Selection

Problem

The

sorting

‐based

algorithm:

Sort

and

return

the

k

‐th

element

Efficiency

(if

sorted

by

mergesort):

Θ

( n

log

n

)

A

faster

algorithm

is

based

on

using

the

quicksort

‐like

partition

of

the

list.

Let

s

be

a

split

position

obtained

by

a

partition:

Assuming

that

the

list

is

indexed

from

1

to

n

:

If

s

=

k

,^

the

problem

is

solved;

if

s

>

k

,^

look

for

the

k

th

smallest

elem.

in

the

left

part;

if

s

<

k

,^

look

for

the

(

k

s

)‐

th

smallest

elem.

in

the

right

part.

s

all are

A[

s

]^

all are

A[

s

]

Selection

by

Partitioning

Example:

4

1

10

9

7

12

8

2

15

Here:

n

=

9,

k

=

9/

=

5

4

1

10

9

7

12

8

2

15

4

1

2

9

7

12

8

10

15

2

1

4

9

7

12

8

10

15

‐‐‐

s

=

<

k

=

9

7

12

8

10

15

9

7

8

12

10

15

8

7

9

12

10

15

‐‐‐

s

=

k

=

8

7

7

8

‐‐‐

s

=

k

=

Solution:

median

is

8