Docsity

Algorithm Design and Analysis - Lecture Slides | CS 410, Study notes of Computer Science

Toggle sidebar
bg1
CS 410/584, Algorithm Design & Analysis: Lecture 1
1© 1994, 2003, 2006, 2008, 2009
David Maier
Algorithm Design & Analysis
Algorithm Design & Analysis
David Maier
Portland State University
David Maier 1
Lecture 1
All material © 1994, 2003,
2006, 2008, 2009
David Maier
Algorithm Design & Analysis
Let’s Solve a Problem
Longest Common Subsequence (LCS)
S = a1 a2 . . . a
n
A subsequence is
A B S Q U A T U L A T E
David Maier 2
Lecture 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Algorithm Design and Analysis - Lecture Slides | CS 410 and more Study notes Computer Science in PDF only on Docsity!

© 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

Longest Common Subsequence (LCS)

S = a 1 a 2... a (^) n

A subsequence is

A B S Q U A T U L A T E

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

where S is a subsequence of

A B S Q U A T U L A T E

Lecture 1 David Maier (^3)

C A L E F A C T I O N

Algorithm Design & Analysis Longest Common Subsequence

G A N G L I O N

Given sequences S1 and S2 want to

H A G I O L A T R Y

Lecture 1 David Maier (^4)

Given sequences S1 and S2, want to

find

© 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)

Count comparisons

Lecture 1 David Maier (^8)

Count comparisons

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

  1. Set LCS[i,j] ← 0 if i=0 or j=
  2. for j = 1 to n do forfor ii = 11 toto mm dodo if R[i] ≠ S[j] then LCS[i,j] ← max(LCS[i-1,j],LCS[i,j-1]) else LCS[i,j] ← LCS[i-1,j-1]+
  3. return LCS[ ]

Lecture 1 David Maier (^10)

  1. return LCS[ ]

Time complexity

© 1994, 2003, 2006, 2008, 2009

Algorithm Design & Analysis Study of Algorithms

  • Design techniques
  • Specific algorithms
  • Proofs of correctness
  • Complexity analysis
    • measure
    • time it takes, space it consumes

Lecture 1 David Maier (^13)

  • worst case
  • average case

Algorithm Design & Analysis Study of Problems

  • What’s the best any algorithm can

do?

  • Relatedness

Lecture 1 David Maier (^14)

  • Intractability of problems

© 1994, 2003, 2006, 2008, 2009

Algorithm Design & Analysis Complexity of an Algorithm

Amount of time and space required as

a function of size of input

T(n) = maximum time required

S(n) = maximum space required

I t i Ti

Lecture 1 David Maier (^15)

Input size Time measures

Algorithm Design & Analysis Time Complexity of a Problem

Minimum time over all algorithms

to get

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

  • upper bound

Lecture 1 David Maier (^19)

  • upper bound
  • tight bound
  • lower bound

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

Heap: binary tree where all leaves at

depth

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

Swap

Pretend array runs

Remake heap: k h

exchange value with 5

15 3

Lecture 1 David Maier (^25)

Algorithm Design & Analysis Formally

Input: a1 a2 ... an to sort

Initialize A[i] ←

build-max-heap

want A[i] for i between 1 and n/

Lecture 1 David Maier (^26)

© 1994, 2003, 2006, 2008, 2009

Algorithm Design & Analysis Heap Construction

Assume:

Want:

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

Two elements

  • Optimal substructure: solution contains optimal solutions to subproblemsoptimal solutions to subproblems
  • Overlapping subproblems: if done recursively

Lecture 1 David Maier (^34)

© 1994, 2003, 2006, 2008, 2009

Algorithm Design & Analysis Has Necessary Properties

  • optimal substructure M 1 ... M (^) n
  • overlapping subproblems M 1 ... M (^) n M 1 ... M (^) n

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, 
  • 1,3 1,1 + 2,3 + 505 Mult[i,j] Values - 1,2 + 3,3 + 5060
  • 2,4 2,2 + 3,4 + 560 - 2,3 + 4,4 + 510
    • Lecture 1 David Maier
  • 3 , - Mult[i,j] Values Algorithm Design & Analysis
  • 1,4 1,1 + 2,4 + 50* * - 1,2 + 3,4 + 50* * - 1,3 + 4,4 + 50* *
  • 2,5 2,2 + 3,5 + 5* * - 2,3 + 4,5 + 5* * - 2 4 + 5 5 + 5* *
    • Lecture 1 David Maier
      • 2 ,4 + 5,5 + 5* *