




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Class: Design/Analysis of Algorithms; Subject: Computer Science; University: University of the Pacific; Term: Fall 2007;
Typology: Study notes
1 / 28
This page cannot be seen from the preview
Don't miss anything!
)) (^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.,
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
Multiplications:
for^ i^ ←
1 to^ n^ do power^ ←^ power
*^ x p^ ←^ p^ +
a^ *^ poweri^
Additions:
return^
p
O( 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
=^ x ( x (2 x
2 ‐^ x^ +^ 3)
=^ x ( x ( x (
x^ ‐^ 1)^ +^
Computing
by^ last
formula
(from^ inside
out)
leads^ to
a^ faster
n multiplies
n additions
Horner’s
Rule
(Python)
#^ Poly
coefficients
are^ stored
in^ P
#^ Example:
‐>^ 3x^
+^ 2x^
def^ Horner(P,
x): n^ =^ len(P)
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([
Computing
n a
-^ Obvious
-^ Divide
Left
‐Right
Exponentiation
#^ Compute
a^n, #^ where
b^ is^
the^ binary
representation
of^ n
#^ Example:
b^ =^ [0,0,1,1]
‐>^ n^
def^ LeftRightBinaryExponentiation(a,b):
product
=^ a i^ =^ len(b)
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
(^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
-^ Solution:
Problem
Reduction
Analytic
Geometry
-^ Given
-^ Problem:
-^ Solution:
(^01) (^11112233) det^
yx yx yx
Least
Common
Multiple
-^ lcm(m,n)
-^ Straightforward
Optimization
Problem
Reduction
-^ Suppose
-^ Solution:
Reduction
to^ Graph
-^ See