Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

More Transformations - Design and Analysis of Algorithms - Slides | COMP 157, Study notes of Algorithms and Programming

Material Type: Notes; Class: Design/Analysis of Algorithms; Subject: Computer Science; University: University of the Pacific; Term: Fall 2007;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-kx5-1
koofers-user-kx5-1 🇺🇸

10 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
More
Transformations
(Sections6.5,6.6)
COMP157
Oct24,2007
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download More Transformations - Design and Analysis of Algorithms - Slides | COMP 157 and more Study notes Algorithms and Programming in PDF only on Docsity!

More

Transformations(Sections

COMP157Oct^ 24,

)) (^1) ( log( 2 (^2) )( 2

2 (^10)

−=

n n ih h i

i

Stage^ 1:

Build^ heap

for^ a^ given

list^ of^ n

keys

worst‐case:^ C (

n )^ =^

C(n)^ є^ O(n)

Stage^ 2:

Repeat

operation

of^ root

removal

n ‐^1 times

(fix^ heap)

worst‐case:^ C (

n )^ =^

C(n)^ є^ O(n

log^ n)

Both^ worst

‐case^ and

average

‐case^ efficiency:

Θ( n log

n )

In‐place:

yes Stability:

no^ (e.g.,

nodes at level 1 1)

i

Analysis

of^ Heapsort −^1 ∑=^1 log 22 n i

i

Polynomial

Evaluation

Given^ a

polynomial

of^ degree

n p ( x )^ =^ a

n^ x+^ a^ nn

n ‐^1 x +^ ‐ 1 … +^ ax^1

+^ a^0

and^ a^ specific

value^ of

x ,^ find^

the^ value

of^ p^ at

that^ point.

Brute‐force

algorithm

p^ ←^ a^0

;^ power

←^1

Multiplications:

for^ i^ ←

1 to^ n^ do power^ ←^ power

*^ x p^ ←^ p^ +

a^ *^ poweri^

Additions:

return^

p

O( n )

n

n^22 =∑ i^1 = n^ n^ =^1 ∑ i =^0

Horner’s

Rule

Example:

p (x)^ =

(^4 3 2) x ‐^ x 2 +^3 x +

x^ – 5 (^3) = x (2 x (^2) ‐ x +^3 x

+^ 1)^ – 5

=^ x ( x (2 x

2 ‐^ x^ +^ 3)

+^ 1)^ – 5

=^ x ( x ( x (

x^ ‐^ 1)^ +^

3)^ +^ 1)^

Computing

by^ last

formula

(from^ inside

out)

leads^ to

a^ faster

algorithm x ( x ( x (2 x

‐^ 1)^ +

3)^ +^ 1)

‐^5

n multiplies

n additions

O( n )

Horner’s

Rule

(Python)

#^ Poly

coefficients

are^ stored

in^ P

#^ Example:

P^ =^ [1,

2,^ 3]

‐>^ 3x^

+^ 2x^

+^1

def^ Horner(P,

x): n^ =^ len(P)

‐^1

p^ =^ P[n]i^ =^ n‐

while^

(i>=0):p^ =^ x*p

+^ P[i] i^ =^ i‐

return

p print^

"Evaluate

2x^^

‐^ x^+

3x^^

+x^ ‐^5

at^ x=3"

print^

Horner([

‐1,2],

Computing

n a

-^ Obvious

brute

‐force

n‐^1 multiplies

-^ Divide

and^ conquer:

n^ a=^ (a

n/2^2 )^

log^ (n)^2

multiplies

Left

‐Right

Exponentiation

#^ Compute

a^n, #^ where

b^ is^

the^ binary

representation

of^ n

#^ Example:

b^ =^ [0,0,1,1]

‐>^ n^

=^12

def^ LeftRightBinaryExponentiation(a,b):

product

=^ a i^ =^ len(b)

‐^2

while^

(i>=0):product

=^ product*product if^ (b[i]==1):

product

=^ product*a

i^ =^ i‐

return

product

Computing

n a

Right‐to

‐left^ binary

exponentiation Scan^ n ’s

binary^

expansion

from^ right

to^ left^

and^ compute

n^ a as^ the

product

of^ terms

i^ (^2) a corresponding

to^ 1’s^ in

this^ expansion.

Example Compute

(^13) a by^ the

right‐to

‐left^ binary

exponentiation.

Here,^ n

=^13 =^

1101.^21

(^8) a

(^4) a

(^2) a

a^ :^

i^ (^2) a terms

(^8) a*^

(^4) a*

a^ :^

product

Efficiency:

same^ as

that^ of

left‐to

‐right^ binary

exponentiation

Problem

Reduction

-^ Prof

X^ noticed

that^

when

his^ wife

wanted

to

boil^ water,

she^ took

the^ kettle

from

the

cabinet,

filled

it^ with

water

and^ placed

it^ on

the^ stove. • One^ day,

while

his^ wife

was^ gone,

Prof^

X

needed

to^ boil

some

water.

He^ noticed

the

kettle

in^ the

sink.

-^ Solution:

Prof^

X^ put

the^ kettle

in^ the

cabinet

and^ proceeded

to^ follow

his^ wife’s

routine.

Problem

Reduction

This^ variation

of^ transform

‐and‐

conquer

solves

a

problem

by^ a^

transforming

it^ into

different

problem

for^ which

an^ algorithm

is^ already

available.To^ be^ of

practical

value,

the^ combined

time

of

the^ transformation

and^ solving

the^ other

problem

should

be^ smaller

than

solving

the

problem

as^ given

by^ another

method.

Analytic

Geometry

-^ Given

three

points:

p1^ =^ (x1,y1),

p2^ =^

(x2,y2), p

=^ (x3,

y3)

-^ Problem:

Is^ p

to^ left

of^ p1p2?

-^ Solution:

Only

if

(^01) (^11112233) det^

yx yx yx

Least

Common

Multiple

-^ lcm(m,n)

is^ smallest

integer

divisible

by^ both

m^ and

n

-^ Straightforward

solution:

multiply

prime

factorizations • Transform

problem:

lcm(m,n)

=^ mn

/^ gcd(m,n)

Now^

solve

gcd^ problem

using

Euclid’s

algorithm

Optimization

Problem

Reduction

-^ Suppose

we^ find

a^ minimum

of^ f(x)

and^ we

have^

an^ algorithm

for^ finding

maximum?

-^ Solution:

min^ [

f(x)^ ]

=^ ‐max

[^ ‐f(x)

]

Reduction

to^ Graph

-^ See

lecture

on^ state

‐space

search