













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














© 1994, 2003, 2006, 2008, 2009
Algorithm Design & Analysis
Algorithm Design & Analysis
David Maier Portland State University
Lecture 1 David Maier (^1)
All material © 1994, 2003,2006, 2008, 2009 David Maier
Algorithm Design & Analysis Let’s Solve a Problem
S = a 1 a 2... a (^) n
Lecture 1 David Maier (^2)
© 1994, 2003, 2006, 2008, 2009
Algorithm Design & Analysis Common Subsequence S 1 = a 1 a 2... a (^) n S 2 = b 1 b 2... b (^) m S =S
Lecture 1 David Maier (^3)
Algorithm Design & Analysis Longest Common Subsequence
G A N G L I O N
Lecture 1 David Maier (^4)
© 1994, 2003, 2006, 2008, 2009
Algorithm Design & Analysis Recursion
R a b b c d a S a e b b a e e
R[i] ≠ S[j] LCS(i,j) = max(LCS( ),LCS( )) LCS(6,7) =
R[i] = S[j]
Lecture 1 David Maier (^7)
LCS(i,j) = LCS( ) + 1) LCS(6,5) =
Algorithm Design & Analysis Recursion 2 LCS(i,j) if i=0 or j=0 then LCS ← 0 else if l if R[i]R[i] ≠≠ S[j]S[j] ththen LCS ← max(LCS(i-1,j),LCS(i,j-1)) else LCS ← LCS(i-1,j-1)
Lecture 1 David Maier (^8)
c(LCS(i,j)) =
© 1994, 2003, 2006, 2008, 2009
Algorithm Design & Analysis Dynamic Programming Have matrix LCS(0-m,0-n) and fill it in R = a a b c a b S = c a b b
Ri a a b c a b Sj 0 1 2 3 4 5 6 0 0 0 0 0 0 0 0 c 1 0
Lecture 1 David Maier (^9)
a 2 0 b 3 0 b 4 0
Algorithm Design & Analysis Algortithm Longest
Lecture 1 David Maier (^10)
© 1994, 2003, 2006, 2008, 2009
Algorithm Design & Analysis Study of Algorithms
Lecture 1 David Maier (^13)
Algorithm Design & Analysis Study of Problems
Lecture 1 David Maier (^14)
© 1994, 2003, 2006, 2008, 2009
Algorithm Design & Analysis Complexity of an Algorithm
Lecture 1 David Maier (^15)
Algorithm Design & Analysis Time Complexity of a Problem
upper bound: lower bound:
Lecture 1 David Maier (^16)
© 1994, 2003, 2006, 2008, 2009
Algorithm Design & Analysis Asymptotic Notation 2 Note n 2 + 3n + 4 ∈ O(n 3 ) nn 2 2 + 3n + 4+ 3n + 4 ΘΘ(n(n 3 3 )^ ) Θ(n 3 ) O(n 3 )
Ω(g(n)) = {f(n) | there are positive c and n 0 such that } asymptotic
Lecture 1 David Maier (^19)
Algorithm Design & Analysis Asymptotic Notation 3
Theorem f(n) ∈ O(g(n)) and f(n)( ) ∈ Ω(g((g( ))n)) imply
Other facts f(n) ∈ Θ(h(n)) and h(n) ∈ Θ(g(n)) imply f(n) ∈ Θ(g(n))
Lecture 1 David Maier (^20)
f(n) ∈ Θ(g(n)) implies
© 1994, 2003, 2006, 2008, 2009
Algorithm Design & Analysis Heapsort
node labels are value at node all values in subtrees
Lecture 1 David Maier (^21)
Algorithm Design & Analysis Example Heap
Lecture 1 David Maier (^22)
© 1994, 2003, 2006, 2008, 2009
Algorithm Design & Analysis Remove Root Value
exchange value with 5
15 3
Lecture 1 David Maier (^25)
Algorithm Design & Analysis Formally
want A[i] for i between 1 and n/
Lecture 1 David Maier (^26)
© 1994, 2003, 2006, 2008, 2009
Algorithm Design & Analysis Heap Construction
max-heapifya eap y ((i,j) ,j) if i not a leaf and a child of i has a greater value then let k be child with larger value
Lecture 1 David Maier (^27)
build-max-heap for i = n downto 1 do
Algorithm Design & Analysis Example of Build-max-heap
Lecture 1 David Maier (^28)
© 1994, 2003, 2006, 2008, 2009
Algorithm Design & Analysis Heapsort Example
Lecture 1 David Maier (^31)
Algorithm Design & Analysis Lower Bounds on Sorting
Counting comparisons Assume A[1], A[2], ..., A[n] are distinct Represent algorithm by a decision treeRepresent algorithm by a decision tree
yes no
Lecture 1 David Maier (^32)
Fact: Binary tree of height h has at most leaves
© 1994, 2003, 2006, 2008, 2009
Algorithm Design & Analysis Consequence
Corollary: Any algorithm for sorting by comparisons needs compares for somesome c > 0c > 0 and largeand large nn.
n! ≥ n(n-1) ... (n/2) ≥ (n/2) n/
Lecture 1 David Maier (^33)
n log n ≥ log n! since
Algorithm Design & Analysis Dynamic Programming
Lecture 1 David Maier (^34)
© 1994, 2003, 2006, 2008, 2009
Algorithm Design & Analysis Has Necessary Properties
Lecture 1 David Maier (^37)
How many subproblems?
M 1 M 2 ... M (^) n M 1 ... M (^) n-1 M (^) n
Algorithm Design & Analysis Build a Table p 0 p 1 p 2 p 3 p 4 p (^5) 50 5 60 10 100 100 jj i 5 4 3 2 1 1 2 3 In 1000’s of
Lecture 1 David Maier (^38)
mults.
Algorithm Design & Analysis
- © 1994, 2003, 2006, 2008,