SlideShare a Scribd company logo
Dynamic Programming
Jaehyun Park
CS 97SI
Stanford University
June 29, 2015
Outline
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
Dynamic Programming 2
What is DP?
◮ Wikipedia definition: “method for solving complex problems
by breaking them down into simpler subproblems”
◮ This definition will make sense once we see some examples
– Actually, we’ll only see problem solving examples today
Dynamic Programming 3
Steps for Solving DP Problems
1. Define subproblems
2. Write down the recurrence that relates subproblems
3. Recognize and solve the base cases
◮ Each step is very important!
Dynamic Programming 4
Outline
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
1-dimensional DP 5
1-dimensional DP Example
◮ Problem: given n, find the number of different ways to write
n as the sum of 1, 3, 4
◮ Example: for n = 5, the answer is 6
5 = 1 + 1 + 1 + 1 + 1
= 1 + 1 + 3
= 1 + 3 + 1
= 3 + 1 + 1
= 1 + 4
= 4 + 1
1-dimensional DP 6
1-dimensional DP Example
◮ Define subproblems
– Let Dn be the number of ways to write n as the sum of 1, 3, 4
◮ Find the recurrence
– Consider one possible solution n = x1 + x2 + · · · + xm
– If xm = 1, the rest of the terms must sum to n − 1
– Thus, the number of sums that end with xm = 1 is equal to
Dn−1
– Take other cases into account (xm = 3, xm = 4)
1-dimensional DP 7
1-dimensional DP Example
◮ Recurrence is then
Dn = Dn−1 + Dn−3 + Dn−4
◮ Solve the base cases
– D0 = 1
– Dn = 0 for all negative n
– Alternatively, can set: D0 = D1 = D2 = 1, and D3 = 2
◮ We’re basically done!
1-dimensional DP 8
Implementation
D[0] = D[1] = D[2] = 1; D[3] = 2;
for(i = 4; i <= n; i++)
D[i] = D[i-1] + D[i-3] + D[i-4];
◮ Very short!
◮ Extension: solving this for huge n, say n ≈ 1012
– Recall the matrix form of Fibonacci numbers
1-dimensional DP 9
POJ 2663: Tri Tiling
◮ Given n, find the number of ways to fill a 3 × n board with
dominoes
◮ Here is one possible solution for n = 12
1-dimensional DP 10
POJ 2663: Tri Tiling
◮ Define subproblems
– Define Dn as the number of ways to tile a 3 × n board
◮ Find recurrence
– Uuuhhhhh...
1-dimensional DP 11
Troll Tiling
1-dimensional DP 12
Defining Subproblems
◮ Obviously, the previous definition didn’t work very well
◮ Dn’s don’t relate in simple terms
◮ What if we introduce more subproblems?
1-dimensional DP 13
Defining Subproblems
1-dimensional DP 14
Finding Recurrences
1-dimensional DP 15
Finding Recurrences
◮ Consider different ways to fill the nth column
– And see what the remaining shape is
◮ Exercise:
– Finding recurrences for An, Bn, Cn
– Just for fun, why is Bn and En always zero?
◮ Extension: solving the problem for n × m grids, where n is
small, say n ≤ 10
– How many subproblems should we consider?
1-dimensional DP 16
Outline
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
2-dimensional DP 17
2-dimensional DP Example
◮ Problem: given two strings x and y, find the longest common
subsequence (LCS) and print its length
◮ Example:
– x: ABCBDAB
– y: BDCABC
– “BCAB” is the longest subsequence found in both sequences, so
the answer is 4
2-dimensional DP 18
Solving the LCS Problem
◮ Define subproblems
– Let Dij be the length of the LCS of x1...i and y1...j
◮ Find the recurrence
– If xi = yj, they both contribute to the LCS
◮ Dij = Di−1,j−1 + 1
– Otherwise, either xi or yj does not contribute to the LCS, so
one can be dropped
◮ Dij = max{Di−1,j, Di,j−1}
– Find and solve the base cases: Di0 = D0j = 0
2-dimensional DP 19
Implementation
for(i = 0; i <= n; i++) D[i][0] = 0;
for(j = 0; j <= m; j++) D[0][j] = 0;
for(i = 1; i <= n; i++) {
for(j = 1; j <= m; j++) {
if(x[i] == y[j])
D[i][j] = D[i-1][j-1] + 1;
else
D[i][j] = max(D[i-1][j], D[i][j-1]);
}
}
2-dimensional DP 20
Outline
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
Interval DP 21
Interval DP Example
◮ Problem: given a string x = x1...n, find the minimum number
of characters that need to be inserted to make it a palindrome
◮ Example:
– x: Ab3bd
– Can get “dAb3bAd” or “Adb3bdA” by inserting 2 characters
(one ‘d’, one ‘A’)
Interval DP 22
Interval DP Example
◮ Define subproblems
– Let Dij be the minimum number of characters that need to be
inserted to make xi...j into a palindrome
◮ Find the recurrence
– Consider a shortest palindrome y1...k containing xi...j
– Either y1 = xi or yk = xj (why?)
– y2...k−1 is then an optimal solution for xi+1...j or xi...j−1 or
xi+1...j−1
◮ Last case possible only if y1 = yk = xi = xj
Interval DP 23
Interval DP Example
◮ Find the recurrence
Dij =
1 + min{Di+1,j, Di,j−1} xi = xj
Di+1,j−1 xi = xj
◮ Find and solve the base cases: Dii = Di,i−1 = 0 for all i
◮ The entries of D must be filled in increasing order of j − i
Interval DP 24
Interval DP Example
// fill in base cases here
for(t = 2; t <= n; t++)
for(i = 1, j = t; j <= n; i++, j++)
// fill in D[i][j] here
◮ Note how we use an additional variable t to fill the table in
correct order
◮ And yes, for loops can work with multiple variables
Interval DP 25
An Alternate Solution
◮ Reverse x to get xR
◮ The answer is n − L, where L is the length of the LCS of x
and xR
◮ Exercise: Think about why this works
Interval DP 26
Outline
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
Tree DP 27
Tree DP Example
◮ Problem: given a tree, color nodes black as many as possible
without coloring two adjacent nodes
◮ Subproblems:
– First, we arbitrarily decide the root node r
– Bv: the optimal solution for a subtree having v as the root,
where we color v black
– Wv: the optimal solution for a subtree having v as the root,
where we don’t color v
– Answer is max{Br, Wr}
Tree DP 28
Tree DP Example
◮ Find the recurrence
– Crucial observation: once v’s color is determined, subtrees can
be solved independently
– If v is colored, its children must not be colored
Bv = 1 +
u∈children(v)
Wu
– If v is not colored, its children can have any color
Wv = 1 +
u∈children(v)
max{Bu, Wu}
◮ Base cases: leaf nodes
Tree DP 29
Outline
Dynamic Programming
1-dimensional DP
2-dimensional DP
Interval DP
Tree DP
Subset DP
Subset DP 30
Subset DP Example
◮ Problem: given a weighted graph with n nodes, find the
shortest path that visits every node exactly once (Traveling
Salesman Problem)
◮ Wait, isn’t this an NP-hard problem?
– Yes, but we can solve it in O(n2
2n
) time
– Note: brute force algorithm takes O(n!) time
Subset DP 31
Subset DP Example
◮ Define subproblems
– DS,v: the length of the optimal path that visits every node in
the set S exactly once and ends at v
– There are approximately n2n
subproblems
– Answer is minv∈V DV,v, where V is the given set of nodes
◮ Let’s solve the base cases first
– For each node v, D{v},v = 0
Subset DP 32
Subset DP Example
◮ Find the recurrence
– Consider a path that visits all nodes in S exactly once and
ends at v
– Right before arriving v, the path comes from some u in
S − {v}
– And that subpath has to be the optimal one that covers
S − {v}, ending at u
– We just try all possible candidates for u
DS,v = min
u∈S−{v}
DS−{v},u + cost(u, v)
Subset DP 33
Working with Subsets
◮ When working with subsets, it’s good to have a nice
representation of sets
◮ Idea: Use an integer to represent a set
– Concise representation of subsets of small integers {0, 1, . . .}
– If the ith (least significant) digit is 1, i is in the set
– If the ith digit is 0, i is not in the set
– e.g., 19 = 010011(2) in binary represent a set {0, 1, 4}
Subset DP 34
Using Bitmasks
◮ Union of two sets x and y: x | y
◮ Intersection: x & y
◮ Symmetric difference: x ˆ y
◮ Singleton set {i}: 1 << i
◮ Membership test: x & (1 << i) != 0
Subset DP 35
Conclusion
◮ Wikipedia definition: “a method for solving complex problems
by breaking them down into simpler subproblems”
– Does this make sense now?
◮ Remember the three steps!
1. Defining subproblems
2. Finding recurrences
3. Solving the base cases
Subset DP 36

More Related Content

What's hot

1508.07756v1
1508.07756v11508.07756v1
1508.07756v1
Samir Crypticus
 
Permutations and Combinations IIT JEE+Olympiad Lecture 4
Permutations and Combinations IIT JEE+Olympiad Lecture 4Permutations and Combinations IIT JEE+Olympiad Lecture 4
Permutations and Combinations IIT JEE+Olympiad Lecture 4
Parth Nandedkar
 
Classifying Polynomials
Classifying PolynomialsClassifying Polynomials
Classifying Polynomials
Bernadeth Mesterio
 
Arithmetic progression
Arithmetic progressionArithmetic progression
Arithmetic progression
Chhavi Bansal
 
Ch4 Polynomials
Ch4 PolynomialsCh4 Polynomials
Ch4 Polynomials
applepi
 
Dividing Polynomials Slide Share
Dividing Polynomials Slide ShareDividing Polynomials Slide Share
Dividing Polynomials Slide Share
Kristen T
 
Backtracking
BacktrackingBacktracking
Backtracking
Vikas Sharma
 
Csr2011 june14 09_30_grigoriev
Csr2011 june14 09_30_grigorievCsr2011 june14 09_30_grigoriev
Csr2011 june14 09_30_grigoriev
CSR2011
 
Csr2011 june14 09_30_grigoriev
Csr2011 june14 09_30_grigorievCsr2011 june14 09_30_grigoriev
Csr2011 june14 09_30_grigoriev
CSR2011
 
235limit
235limit235limit
235limit
siva sankar
 
Magic graphs
Magic graphsMagic graphs
Magic graphs
Springer
 
On the Secrecy of RSA Private Keys
On the Secrecy of RSA Private KeysOn the Secrecy of RSA Private Keys
On the Secrecy of RSA Private Keys
Dharmalingam Ganesan
 
Integration presentation
Integration presentationIntegration presentation
Integration presentation
Urmila Bhardwaj
 
Decision Trees and Bayes Classifiers
Decision Trees and Bayes ClassifiersDecision Trees and Bayes Classifiers
Decision Trees and Bayes Classifiers
Alexander Jung
 
Bca1040 digital logic
Bca1040   digital logicBca1040   digital logic
Bca1040 digital logic
smumbahelp
 
11.2
11.211.2
11.2
nglaze10
 
Polynomials
PolynomialsPolynomials
Polynomials
Priyanka Sahu
 
Arithmetic Progression - $@mEe
Arithmetic Progression - $@mEeArithmetic Progression - $@mEe
Arithmetic Progression - $@mEe
Samee Smd
 
Ch08
Ch08Ch08
Polynomials
PolynomialsPolynomials
Polynomials
sandhyajayalekshmi
 

What's hot (20)

1508.07756v1
1508.07756v11508.07756v1
1508.07756v1
 
Permutations and Combinations IIT JEE+Olympiad Lecture 4
Permutations and Combinations IIT JEE+Olympiad Lecture 4Permutations and Combinations IIT JEE+Olympiad Lecture 4
Permutations and Combinations IIT JEE+Olympiad Lecture 4
 
Classifying Polynomials
Classifying PolynomialsClassifying Polynomials
Classifying Polynomials
 
Arithmetic progression
Arithmetic progressionArithmetic progression
Arithmetic progression
 
Ch4 Polynomials
Ch4 PolynomialsCh4 Polynomials
Ch4 Polynomials
 
Dividing Polynomials Slide Share
Dividing Polynomials Slide ShareDividing Polynomials Slide Share
Dividing Polynomials Slide Share
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Csr2011 june14 09_30_grigoriev
Csr2011 june14 09_30_grigorievCsr2011 june14 09_30_grigoriev
Csr2011 june14 09_30_grigoriev
 
Csr2011 june14 09_30_grigoriev
Csr2011 june14 09_30_grigorievCsr2011 june14 09_30_grigoriev
Csr2011 june14 09_30_grigoriev
 
235limit
235limit235limit
235limit
 
Magic graphs
Magic graphsMagic graphs
Magic graphs
 
On the Secrecy of RSA Private Keys
On the Secrecy of RSA Private KeysOn the Secrecy of RSA Private Keys
On the Secrecy of RSA Private Keys
 
Integration presentation
Integration presentationIntegration presentation
Integration presentation
 
Decision Trees and Bayes Classifiers
Decision Trees and Bayes ClassifiersDecision Trees and Bayes Classifiers
Decision Trees and Bayes Classifiers
 
Bca1040 digital logic
Bca1040   digital logicBca1040   digital logic
Bca1040 digital logic
 
11.2
11.211.2
11.2
 
Polynomials
PolynomialsPolynomials
Polynomials
 
Arithmetic Progression - $@mEe
Arithmetic Progression - $@mEeArithmetic Progression - $@mEe
Arithmetic Progression - $@mEe
 
Ch08
Ch08Ch08
Ch08
 
Polynomials
PolynomialsPolynomials
Polynomials
 

Similar to Dynamic programming

04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
MuradAmn
 
Dynamic Programming.pptx
Dynamic Programming.pptxDynamic Programming.pptx
Dynamic Programming.pptx
Thanga Ramya S
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Advance algebra
Advance algebraAdvance algebra
Advance algebra
lyra matalubos
 
20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers
Computer Science Club
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
giridaroori
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
MuhammadSheraz836877
 
Signal processingcolumbia
Signal processingcolumbiaSignal processingcolumbia
Signal processingcolumbia
vevin1986
 
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docxDivide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
jacksnathalie
 
Duality in Linear Programming Problem
Duality in Linear Programming ProblemDuality in Linear Programming Problem
Duality in Linear Programming Problem
RAVI PRASAD K.J.
 
C2 st lecture 4 handout
C2 st lecture 4 handoutC2 st lecture 4 handout
C2 st lecture 4 handout
fatima d
 
Calculus 08 techniques_of_integration
Calculus 08 techniques_of_integrationCalculus 08 techniques_of_integration
Calculus 08 techniques_of_integration
tutulk
 
Theory of Computation Introduction Session
Theory of Computation Introduction SessionTheory of Computation Introduction Session
Theory of Computation Introduction Session
Rushabh2428
 
Lesson 19: Optimization Problems
Lesson 19: Optimization ProblemsLesson 19: Optimization Problems
Lesson 19: Optimization Problems
Matthew Leingang
 
AAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxAAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptx
HarshitSingh334328
 
Proof Techniques
Proof TechniquesProof Techniques
Proof Techniques
CHANDANKUMARMANDAL5
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
Mohammed Hussein
 
Imc2020 day1&amp;2 problems&amp;solutions
Imc2020 day1&amp;2 problems&amp;solutionsImc2020 day1&amp;2 problems&amp;solutions
Imc2020 day1&amp;2 problems&amp;solutions
Christos Loizos
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Gopi Saiteja
 
November 3, 2014
November 3, 2014November 3, 2014
November 3, 2014
khyps13
 

Similar to Dynamic programming (20)

04 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x204 greedyalgorithmsii 2x2
04 greedyalgorithmsii 2x2
 
Dynamic Programming.pptx
Dynamic Programming.pptxDynamic Programming.pptx
Dynamic Programming.pptx
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Advance algebra
Advance algebraAdvance algebra
Advance algebra
 
20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
 
Signal processingcolumbia
Signal processingcolumbiaSignal processingcolumbia
Signal processingcolumbia
 
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docxDivide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
 
Duality in Linear Programming Problem
Duality in Linear Programming ProblemDuality in Linear Programming Problem
Duality in Linear Programming Problem
 
C2 st lecture 4 handout
C2 st lecture 4 handoutC2 st lecture 4 handout
C2 st lecture 4 handout
 
Calculus 08 techniques_of_integration
Calculus 08 techniques_of_integrationCalculus 08 techniques_of_integration
Calculus 08 techniques_of_integration
 
Theory of Computation Introduction Session
Theory of Computation Introduction SessionTheory of Computation Introduction Session
Theory of Computation Introduction Session
 
Lesson 19: Optimization Problems
Lesson 19: Optimization ProblemsLesson 19: Optimization Problems
Lesson 19: Optimization Problems
 
AAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxAAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptx
 
Proof Techniques
Proof TechniquesProof Techniques
Proof Techniques
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Imc2020 day1&amp;2 problems&amp;solutions
Imc2020 day1&amp;2 problems&amp;solutionsImc2020 day1&amp;2 problems&amp;solutions
Imc2020 day1&amp;2 problems&amp;solutions
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
November 3, 2014
November 3, 2014November 3, 2014
November 3, 2014
 

More from Nguyễn Anh

Báo cáo đồ họa máy tính - Computer graphics
Báo cáo đồ họa máy tính - Computer graphicsBáo cáo đồ họa máy tính - Computer graphics
Báo cáo đồ họa máy tính - Computer graphics
Nguyễn Anh
 
Game programming - Hexagon
Game programming - HexagonGame programming - Hexagon
Game programming - Hexagon
Nguyễn Anh
 
Nghiên cứu chuẩn ISO/IEC 9126 trong đánh giá chất lượng phần mềm
Nghiên cứu chuẩn ISO/IEC 9126 trong đánh giá chất lượng phần mềmNghiên cứu chuẩn ISO/IEC 9126 trong đánh giá chất lượng phần mềm
Nghiên cứu chuẩn ISO/IEC 9126 trong đánh giá chất lượng phần mềm
Nguyễn Anh
 
Ứng dụng ngôn ngữ UML trong phân tích và thiết kế website cho giảng viên Việ...
Ứng dụng ngôn ngữ UML trong phân tích và thiết kế  website cho giảng viên Việ...Ứng dụng ngôn ngữ UML trong phân tích và thiết kế  website cho giảng viên Việ...
Ứng dụng ngôn ngữ UML trong phân tích và thiết kế website cho giảng viên Việ...
Nguyễn Anh
 
Tìm hiểu các kỹ thuật kiểm thử phần mềm ứng dụng trong lập trình Java.
Tìm hiểu các kỹ thuật kiểm thử phần mềm  ứng dụng trong lập trình Java.Tìm hiểu các kỹ thuật kiểm thử phần mềm  ứng dụng trong lập trình Java.
Tìm hiểu các kỹ thuật kiểm thử phần mềm ứng dụng trong lập trình Java.
Nguyễn Anh
 
Sldie TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
Sldie TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀMSldie TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
Sldie TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
Nguyễn Anh
 
TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀMTÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
Nguyễn Anh
 
Tìm Hiểu Các Kỹ Thuật Kiểm Thử Phần Mềm và Một Số Ứng Dụng Trong Thực Tế
Tìm Hiểu Các Kỹ Thuật Kiểm Thử Phần Mềm và Một Số Ứng Dụng Trong Thực Tế Tìm Hiểu Các Kỹ Thuật Kiểm Thử Phần Mềm và Một Số Ứng Dụng Trong Thực Tế
Tìm Hiểu Các Kỹ Thuật Kiểm Thử Phần Mềm và Một Số Ứng Dụng Trong Thực Tế
Nguyễn Anh
 
Tìm hiểu về kỹ thuật Kiểm thử phần mềm
Tìm hiểu về kỹ thuật Kiểm thử phần mềmTìm hiểu về kỹ thuật Kiểm thử phần mềm
Tìm hiểu về kỹ thuật Kiểm thử phần mềm
Nguyễn Anh
 
Bảo trì phần mềm
Bảo trì phần mềmBảo trì phần mềm
Bảo trì phần mềm
Nguyễn Anh
 
Embedded beta2 new
Embedded beta2 newEmbedded beta2 new
Embedded beta2 new
Nguyễn Anh
 
Embedded linux edited
Embedded linux editedEmbedded linux edited
Embedded linux edited
Nguyễn Anh
 
Slide Các kỹ thuật bảo trì phần mềm
Slide Các kỹ thuật bảo trì phần mềmSlide Các kỹ thuật bảo trì phần mềm
Slide Các kỹ thuật bảo trì phần mềm
Nguyễn Anh
 
Các kỹ thuật bảo trì phần mềm
Các kỹ thuật bảo trì phần mềmCác kỹ thuật bảo trì phần mềm
Các kỹ thuật bảo trì phần mềm
Nguyễn Anh
 
TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀMTÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
Nguyễn Anh
 
Đào tạo ĐH
Đào tạo ĐHĐào tạo ĐH
Đào tạo ĐH
Nguyễn Anh
 
Cài đặt windows mà không cần phải kích hoạt
Cài đặt  windows mà không cần phải kích hoạtCài đặt  windows mà không cần phải kích hoạt
Cài đặt windows mà không cần phải kích hoạt
Nguyễn Anh
 
System hacking
System hackingSystem hacking
System hacking
Nguyễn Anh
 
Hoc internet
Hoc internetHoc internet
Hoc internet
Nguyễn Anh
 
Cach setup bios
Cach setup biosCach setup bios
Cach setup bios
Nguyễn Anh
 

More from Nguyễn Anh (20)

Báo cáo đồ họa máy tính - Computer graphics
Báo cáo đồ họa máy tính - Computer graphicsBáo cáo đồ họa máy tính - Computer graphics
Báo cáo đồ họa máy tính - Computer graphics
 
Game programming - Hexagon
Game programming - HexagonGame programming - Hexagon
Game programming - Hexagon
 
Nghiên cứu chuẩn ISO/IEC 9126 trong đánh giá chất lượng phần mềm
Nghiên cứu chuẩn ISO/IEC 9126 trong đánh giá chất lượng phần mềmNghiên cứu chuẩn ISO/IEC 9126 trong đánh giá chất lượng phần mềm
Nghiên cứu chuẩn ISO/IEC 9126 trong đánh giá chất lượng phần mềm
 
Ứng dụng ngôn ngữ UML trong phân tích và thiết kế website cho giảng viên Việ...
Ứng dụng ngôn ngữ UML trong phân tích và thiết kế  website cho giảng viên Việ...Ứng dụng ngôn ngữ UML trong phân tích và thiết kế  website cho giảng viên Việ...
Ứng dụng ngôn ngữ UML trong phân tích và thiết kế website cho giảng viên Việ...
 
Tìm hiểu các kỹ thuật kiểm thử phần mềm ứng dụng trong lập trình Java.
Tìm hiểu các kỹ thuật kiểm thử phần mềm  ứng dụng trong lập trình Java.Tìm hiểu các kỹ thuật kiểm thử phần mềm  ứng dụng trong lập trình Java.
Tìm hiểu các kỹ thuật kiểm thử phần mềm ứng dụng trong lập trình Java.
 
Sldie TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
Sldie TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀMSldie TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
Sldie TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
 
TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀMTÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
 
Tìm Hiểu Các Kỹ Thuật Kiểm Thử Phần Mềm và Một Số Ứng Dụng Trong Thực Tế
Tìm Hiểu Các Kỹ Thuật Kiểm Thử Phần Mềm và Một Số Ứng Dụng Trong Thực Tế Tìm Hiểu Các Kỹ Thuật Kiểm Thử Phần Mềm và Một Số Ứng Dụng Trong Thực Tế
Tìm Hiểu Các Kỹ Thuật Kiểm Thử Phần Mềm và Một Số Ứng Dụng Trong Thực Tế
 
Tìm hiểu về kỹ thuật Kiểm thử phần mềm
Tìm hiểu về kỹ thuật Kiểm thử phần mềmTìm hiểu về kỹ thuật Kiểm thử phần mềm
Tìm hiểu về kỹ thuật Kiểm thử phần mềm
 
Bảo trì phần mềm
Bảo trì phần mềmBảo trì phần mềm
Bảo trì phần mềm
 
Embedded beta2 new
Embedded beta2 newEmbedded beta2 new
Embedded beta2 new
 
Embedded linux edited
Embedded linux editedEmbedded linux edited
Embedded linux edited
 
Slide Các kỹ thuật bảo trì phần mềm
Slide Các kỹ thuật bảo trì phần mềmSlide Các kỹ thuật bảo trì phần mềm
Slide Các kỹ thuật bảo trì phần mềm
 
Các kỹ thuật bảo trì phần mềm
Các kỹ thuật bảo trì phần mềmCác kỹ thuật bảo trì phần mềm
Các kỹ thuật bảo trì phần mềm
 
TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀMTÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
TÌM HIỂU CÁC KỸ THUẬT KIỂM THỬ PHẦN MỀM
 
Đào tạo ĐH
Đào tạo ĐHĐào tạo ĐH
Đào tạo ĐH
 
Cài đặt windows mà không cần phải kích hoạt
Cài đặt  windows mà không cần phải kích hoạtCài đặt  windows mà không cần phải kích hoạt
Cài đặt windows mà không cần phải kích hoạt
 
System hacking
System hackingSystem hacking
System hacking
 
Hoc internet
Hoc internetHoc internet
Hoc internet
 
Cach setup bios
Cach setup biosCach setup bios
Cach setup bios
 

Recently uploaded

Mechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineeringMechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineering
sachin chaurasia
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Transcat
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
upoux
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
aryanpankaj78
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
Addu25809
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
Paris Salesforce Developer Group
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
um7474492
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
nedcocy
 
Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...
cannyengineerings
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
Blood finder application project report (1).pdf
Blood finder application project report (1).pdfBlood finder application project report (1).pdf
Blood finder application project report (1).pdf
Kamal Acharya
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
PriyankaKilaniya
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
upoux
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
Kamal Acharya
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
q30122000
 

Recently uploaded (20)

Mechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineeringMechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineering
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
 
Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
Blood finder application project report (1).pdf
Blood finder application project report (1).pdfBlood finder application project report (1).pdf
Blood finder application project report (1).pdf
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
 

Dynamic programming

  • 1. Dynamic Programming Jaehyun Park CS 97SI Stanford University June 29, 2015
  • 2. Outline Dynamic Programming 1-dimensional DP 2-dimensional DP Interval DP Tree DP Subset DP Dynamic Programming 2
  • 3. What is DP? ◮ Wikipedia definition: “method for solving complex problems by breaking them down into simpler subproblems” ◮ This definition will make sense once we see some examples – Actually, we’ll only see problem solving examples today Dynamic Programming 3
  • 4. Steps for Solving DP Problems 1. Define subproblems 2. Write down the recurrence that relates subproblems 3. Recognize and solve the base cases ◮ Each step is very important! Dynamic Programming 4
  • 5. Outline Dynamic Programming 1-dimensional DP 2-dimensional DP Interval DP Tree DP Subset DP 1-dimensional DP 5
  • 6. 1-dimensional DP Example ◮ Problem: given n, find the number of different ways to write n as the sum of 1, 3, 4 ◮ Example: for n = 5, the answer is 6 5 = 1 + 1 + 1 + 1 + 1 = 1 + 1 + 3 = 1 + 3 + 1 = 3 + 1 + 1 = 1 + 4 = 4 + 1 1-dimensional DP 6
  • 7. 1-dimensional DP Example ◮ Define subproblems – Let Dn be the number of ways to write n as the sum of 1, 3, 4 ◮ Find the recurrence – Consider one possible solution n = x1 + x2 + · · · + xm – If xm = 1, the rest of the terms must sum to n − 1 – Thus, the number of sums that end with xm = 1 is equal to Dn−1 – Take other cases into account (xm = 3, xm = 4) 1-dimensional DP 7
  • 8. 1-dimensional DP Example ◮ Recurrence is then Dn = Dn−1 + Dn−3 + Dn−4 ◮ Solve the base cases – D0 = 1 – Dn = 0 for all negative n – Alternatively, can set: D0 = D1 = D2 = 1, and D3 = 2 ◮ We’re basically done! 1-dimensional DP 8
  • 9. Implementation D[0] = D[1] = D[2] = 1; D[3] = 2; for(i = 4; i <= n; i++) D[i] = D[i-1] + D[i-3] + D[i-4]; ◮ Very short! ◮ Extension: solving this for huge n, say n ≈ 1012 – Recall the matrix form of Fibonacci numbers 1-dimensional DP 9
  • 10. POJ 2663: Tri Tiling ◮ Given n, find the number of ways to fill a 3 × n board with dominoes ◮ Here is one possible solution for n = 12 1-dimensional DP 10
  • 11. POJ 2663: Tri Tiling ◮ Define subproblems – Define Dn as the number of ways to tile a 3 × n board ◮ Find recurrence – Uuuhhhhh... 1-dimensional DP 11
  • 13. Defining Subproblems ◮ Obviously, the previous definition didn’t work very well ◮ Dn’s don’t relate in simple terms ◮ What if we introduce more subproblems? 1-dimensional DP 13
  • 16. Finding Recurrences ◮ Consider different ways to fill the nth column – And see what the remaining shape is ◮ Exercise: – Finding recurrences for An, Bn, Cn – Just for fun, why is Bn and En always zero? ◮ Extension: solving the problem for n × m grids, where n is small, say n ≤ 10 – How many subproblems should we consider? 1-dimensional DP 16
  • 17. Outline Dynamic Programming 1-dimensional DP 2-dimensional DP Interval DP Tree DP Subset DP 2-dimensional DP 17
  • 18. 2-dimensional DP Example ◮ Problem: given two strings x and y, find the longest common subsequence (LCS) and print its length ◮ Example: – x: ABCBDAB – y: BDCABC – “BCAB” is the longest subsequence found in both sequences, so the answer is 4 2-dimensional DP 18
  • 19. Solving the LCS Problem ◮ Define subproblems – Let Dij be the length of the LCS of x1...i and y1...j ◮ Find the recurrence – If xi = yj, they both contribute to the LCS ◮ Dij = Di−1,j−1 + 1 – Otherwise, either xi or yj does not contribute to the LCS, so one can be dropped ◮ Dij = max{Di−1,j, Di,j−1} – Find and solve the base cases: Di0 = D0j = 0 2-dimensional DP 19
  • 20. Implementation for(i = 0; i <= n; i++) D[i][0] = 0; for(j = 0; j <= m; j++) D[0][j] = 0; for(i = 1; i <= n; i++) { for(j = 1; j <= m; j++) { if(x[i] == y[j]) D[i][j] = D[i-1][j-1] + 1; else D[i][j] = max(D[i-1][j], D[i][j-1]); } } 2-dimensional DP 20
  • 21. Outline Dynamic Programming 1-dimensional DP 2-dimensional DP Interval DP Tree DP Subset DP Interval DP 21
  • 22. Interval DP Example ◮ Problem: given a string x = x1...n, find the minimum number of characters that need to be inserted to make it a palindrome ◮ Example: – x: Ab3bd – Can get “dAb3bAd” or “Adb3bdA” by inserting 2 characters (one ‘d’, one ‘A’) Interval DP 22
  • 23. Interval DP Example ◮ Define subproblems – Let Dij be the minimum number of characters that need to be inserted to make xi...j into a palindrome ◮ Find the recurrence – Consider a shortest palindrome y1...k containing xi...j – Either y1 = xi or yk = xj (why?) – y2...k−1 is then an optimal solution for xi+1...j or xi...j−1 or xi+1...j−1 ◮ Last case possible only if y1 = yk = xi = xj Interval DP 23
  • 24. Interval DP Example ◮ Find the recurrence Dij = 1 + min{Di+1,j, Di,j−1} xi = xj Di+1,j−1 xi = xj ◮ Find and solve the base cases: Dii = Di,i−1 = 0 for all i ◮ The entries of D must be filled in increasing order of j − i Interval DP 24
  • 25. Interval DP Example // fill in base cases here for(t = 2; t <= n; t++) for(i = 1, j = t; j <= n; i++, j++) // fill in D[i][j] here ◮ Note how we use an additional variable t to fill the table in correct order ◮ And yes, for loops can work with multiple variables Interval DP 25
  • 26. An Alternate Solution ◮ Reverse x to get xR ◮ The answer is n − L, where L is the length of the LCS of x and xR ◮ Exercise: Think about why this works Interval DP 26
  • 27. Outline Dynamic Programming 1-dimensional DP 2-dimensional DP Interval DP Tree DP Subset DP Tree DP 27
  • 28. Tree DP Example ◮ Problem: given a tree, color nodes black as many as possible without coloring two adjacent nodes ◮ Subproblems: – First, we arbitrarily decide the root node r – Bv: the optimal solution for a subtree having v as the root, where we color v black – Wv: the optimal solution for a subtree having v as the root, where we don’t color v – Answer is max{Br, Wr} Tree DP 28
  • 29. Tree DP Example ◮ Find the recurrence – Crucial observation: once v’s color is determined, subtrees can be solved independently – If v is colored, its children must not be colored Bv = 1 + u∈children(v) Wu – If v is not colored, its children can have any color Wv = 1 + u∈children(v) max{Bu, Wu} ◮ Base cases: leaf nodes Tree DP 29
  • 30. Outline Dynamic Programming 1-dimensional DP 2-dimensional DP Interval DP Tree DP Subset DP Subset DP 30
  • 31. Subset DP Example ◮ Problem: given a weighted graph with n nodes, find the shortest path that visits every node exactly once (Traveling Salesman Problem) ◮ Wait, isn’t this an NP-hard problem? – Yes, but we can solve it in O(n2 2n ) time – Note: brute force algorithm takes O(n!) time Subset DP 31
  • 32. Subset DP Example ◮ Define subproblems – DS,v: the length of the optimal path that visits every node in the set S exactly once and ends at v – There are approximately n2n subproblems – Answer is minv∈V DV,v, where V is the given set of nodes ◮ Let’s solve the base cases first – For each node v, D{v},v = 0 Subset DP 32
  • 33. Subset DP Example ◮ Find the recurrence – Consider a path that visits all nodes in S exactly once and ends at v – Right before arriving v, the path comes from some u in S − {v} – And that subpath has to be the optimal one that covers S − {v}, ending at u – We just try all possible candidates for u DS,v = min u∈S−{v} DS−{v},u + cost(u, v) Subset DP 33
  • 34. Working with Subsets ◮ When working with subsets, it’s good to have a nice representation of sets ◮ Idea: Use an integer to represent a set – Concise representation of subsets of small integers {0, 1, . . .} – If the ith (least significant) digit is 1, i is in the set – If the ith digit is 0, i is not in the set – e.g., 19 = 010011(2) in binary represent a set {0, 1, 4} Subset DP 34
  • 35. Using Bitmasks ◮ Union of two sets x and y: x | y ◮ Intersection: x & y ◮ Symmetric difference: x ˆ y ◮ Singleton set {i}: 1 << i ◮ Membership test: x & (1 << i) != 0 Subset DP 35
  • 36. Conclusion ◮ Wikipedia definition: “a method for solving complex problems by breaking them down into simpler subproblems” – Does this make sense now? ◮ Remember the three steps! 1. Defining subproblems 2. Finding recurrences 3. Solving the base cases Subset DP 36