0% found this document useful (0 votes)
30 views16 pages

CS341 Lec13 Annotated Mar12

Uploaded by

artem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views16 pages

CS341 Lec13 Annotated Mar12

Uploaded by

artem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CS 341: Algorithms

Lec 13: Dynamic Programming- Part 3

Armin Jamshidpey Yang Lu


Based on lecture notes by Éric Schost

David R. Cheriton School of Computer Science, University of Waterloo

Winter 2024

A. Jamshidpey Y. Lu (CS, UW) Lec 13: DP Part 3 Winter 2024 1/8


Edit Distance
Input: arrays A[1..n] and B[1..m] of characters

Output: minimum number of {add, delete, change} operations


that turn A into B

Example: A =snowy, B =sunny

s n o w y s n o w y
s u n n y s u n n y

3C 1A, 1C, 1D

s n o w y
s u n n y

2A, 1C, 2D
Examples: DNA sequences made of a, c, g, t

A. Jamshidpey Y. Lu (CS, UW) Lec 13: DP Part 3 Winter 2024 2/8


The recurrence

Definition: let D[i, j] be the edit distance between A[1..i] and


B[1..j]
D[0, j] = j for all j (add j characters)
D[i, 0] = i for all i (delete i characters)
D[i, j] is the min of three values
▶ D[i − 1, j − 1] (if A[i] = B[j]) or D[i − 1, j − 1] + 1
(otherwise)
▶ D[i − 1, j] + 1 (delete A[i] and match A[1..i − 1] with B[1..j])
▶ D[i, j − 1] + 1 (add B[j] and match A[1..i] with B[1..j − 1])

The algorithm computes all D[i, j], using two nested loops, so
runtime Θ(mn)

A. Jamshidpey Y. Lu (CS, UW) Lec 13: DP Part 3 Winter 2024 3/8


Optimal binary search trees
Input:
integers (or something else) 1, . . . , n
probabilities of access p1 , . . . , pn , with p1 + · · · + pn = 1

Output:
an optimal BST with keys 1, . . . , n
Pn
optimal: minimizes i=1 pi · (depth(i) + 1) = expected
number of tests for a search

A. Jamshidpey Y. Lu (CS, UW) Lec 13: DP Part 3 Winter 2024 4/8


Optimal binary search trees
Input:
integers (or something else) 1, . . . , n
probabilities of access p1 , . . . , pn , with p1 + · · · + pn = 1

Output:
an optimal BST with keys 1, . . . , n
Pn
optimal: minimizes i=1 pi · (depth(i) + 1) = expected
number of tests for a search

Example: p1 = p2 = p3 = p4 = p5 = 1/5: ?

A. Jamshidpey Y. Lu (CS, UW) Lec 13: DP Part 3 Winter 2024 4/8


Optimal binary search trees
Input:
integers (or something else) 1, . . . , n
probabilities of access p1 , . . . , pn , with p1 + · · · + pn = 1

Output:
an optimal BST with keys 1, . . . , n
Pn
optimal: minimizes i=1 pi · (depth(i) + 1) = expected
number of tests for a search

Example: p1 = p2 = p3 = p4 = p5 = 1/5: ?
See also
optimal static ordering for linked lists
Huffman trees
both built using greedy algorithms

A. Jamshidpey Y. Lu (CS, UW) Lec 13: DP Part 3 Winter 2024 4/8


Setting up the recurrence

Definition define M [i, j] by


M [i, j] = the minimal cost for items {i, . . . , j},
1≤i≤j≤n
M [i, j] = 0 for j < i

Recurrence
 k−1
X j
X 
M [i, j] = min M [i, k − 1] + pℓ + pk + M [k + 1, j] + pℓ
i≤k≤j
ℓ=i ℓ=k+1
  j
X
= min M [i, k − 1] + M [k + 1, j] + pℓ
i≤k≤j
ℓ=i

check: gives M [i, i] = pi

A. Jamshidpey Y. Lu (CS, UW) Lec 13: DP Part 3 Winter 2024 5/8


Algorithm

Pj
Remark: to get ℓ=i pℓ :
compute S[ℓ] = p1 + · · · + pℓ , for ℓ = 1, . . . , n
then pi + · · · + pj = S[j] − S[i − 1], with S[0] = 0

OptimalBST(p1 , . . . , pn , S0 , . . . , Sn )
1. for i = 1, . . . , n + 1
2. M [i, i − 1] ← 0
3. for d = 0, . . . , n − 1 d=j−i
4. for i = 1, . . . , n − d
5. j ←d+i
6. M [i, j] ← mini≤k≤j (M [i, k − 1] + M [k + 1, j]) + S[j] − S[i − 1]

Runtime O(n3 )

A. Jamshidpey Y. Lu (CS, UW) Lec 13: DP Part 3 Winter 2024 6/8


Independent Sets in Trees
An independent set of a graph G = (V, E), is S ⊆ V if there are
no edges between elements of S.

The maximum independent set problem (for a general graph):


input: G(V, E)
Output: An independent set of maximum cardinality.

Example (not a tree):

1 2

4 3

S = {1, 3}.
A. Jamshidpey Y. Lu (CS, UW) Lec 13: DP Part 3 Winter 2024 7/8
Algorithm (sketch)

I(v) := size of largest independent set of subtree rooted at v

A. Jamshidpey Y. Lu (CS, UW) Lec 13: DP Part 3 Winter 2024 8/8


Algorithm (sketch)

I(v) := size of largest independent set of subtree rooted at v

X X
I(v) = max{1 + I(u), I(u)}
grandchildren u of v children u of v

A. Jamshidpey Y. Lu (CS, UW) Lec 13: DP Part 3 Winter 2024 8/8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy