SlideShare a Scribd company logo
1 of 8
Download to read offline
Dynamic Programming
OOTA SUBBA REDDY
IIIT-Hyderabad
2015
Dynamic Programing
Definition
A dynamic programming algorithm solves each sub problem just
one time and save the answer in a table rather than solving the
same sub problem multiple times. This kind of algorithm is popular
in optimization problems, which usually have many solutions.
When you are developing dynamic programming algorithms you
should.
Characterize the structure an optimal solution
Recursively define the value of an optimal solution.
Computer the value of an optimal solution.
Construct the solution from the computer information.
Steps 1-3 are the most important here, but step four is useful if
you need to actual data that led to your optimized solution.
Rod Cutting
Problem
Given a rod of length n inches and an array of prices that contains
prices of all pieces of size smaller than n. Determine the maximum
value obtainable by cutting up the rod and selling the pieces.
length i 1 2 3 4 5 6 7 8
price pi 1 5 8 9 10 17 17 20
Optimal Substructure (recurrence formula as follow )
rn = max1 i n(pi + rn−i ) (1)
LIS(Longest Increasing Subsequence)
Given a sequence of integers S1, ...., Sn. Find a Subsequence
Si1 < Si2 < ... < Sik
with i1 < ...ik so that k is as large as possible.
Recurrence Formula:
Let L[i]=length of longest increasing subsequence in S1, ...Sn
that ends in Si
L[j] = 1 + maxL[i] : i < jandSi < Sj
Given 9,5,2,8,7,3,1,6,4 as input,
possible increasing subsequence is 5,7
better is 2,3,6 or 2,3,4 (either of which would be a correct)
Note: Similar problems
LDS (Longest Decreasing Subsequence)
LMIS (Longest Monotonically Increasing Subsequnce)
LMDS (Longest Monotonically Decreasing Subsequence)
Longest Palindrome Subsequence
Problem
Describe an O(n2) algorithm to find the length of the longest
palindromic subsequence of a given string.
Example: a longest palindromic subsequence of
ALGORITHMSISAGREATCLASS is ALGIMIGLA , which has
length 9.
Recurrence Formula:
T[i, j] =



1 if i = j
2+M[i+1,j-1] if A[i]=A[j] and i < j
maxM[i+1,j],M[i,j-1] if A[i]!=A[j] and i < j
0 if i < j



Note: Similar problems
LCS (Longest Common Subsequence)
LCS (Convert LCS to LIS only if unique characters exists in
Strings, try O(nlogn) approach)
Palindrome Partitioning
Problem
Given a string s, partition s such that every substring of the
partition is a palindrome. Return the minimum cuts needed for a
palindrome partitioning of s.
For example, given s = ”aab”, Return 1 since the palindrome
partitioning [”aa”,”b”] could be produced using 1 cut.
D[i] equals the minimum cut number for string [i,n].
D[i] = min(D[i], D[j+1]+1) j i, and [j+1, i] forms a
palindrome.
Note: Similar problems
MM (Matrix Chain Multiplication)
Mixtures (Check in SPOJ)
Word Break
Some Other Problems Using DP
EDIST (Edit Distance)
SHPATH ( Shortest Path Using Bellam-Ford Algorithm)
ALL PAIR SHPATH ( Floyd-Warshall Algorithm)
OBST (Optimal Binary Search Tree)
TSP (Travelling Salesman Problem)
Reliability Design
Thank You.

More Related Content

What's hot

Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingGopi Saiteja
 
L03 ai - knowledge representation using logic
L03 ai - knowledge representation using logicL03 ai - knowledge representation using logic
L03 ai - knowledge representation using logicManjula V
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programingrupali_2bonde
 
Travelling Salesman Problem
Travelling Salesman ProblemTravelling Salesman Problem
Travelling Salesman ProblemDaniel Raditya
 
Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Kumar
 
AI_Session 7 Greedy Best first search algorithm.pptx
AI_Session 7 Greedy Best first search algorithm.pptxAI_Session 7 Greedy Best first search algorithm.pptx
AI_Session 7 Greedy Best first search algorithm.pptxAsst.prof M.Gokilavani
 
Travelling Salesman Problem
Travelling Salesman ProblemTravelling Salesman Problem
Travelling Salesman ProblemShikha Gupta
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsSwapnil Agrawal
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problemsJyotsna Suryadevara
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programmingOye Tu
 
Master method
Master method Master method
Master method Rajendran
 
Introduction to NP Completeness
Introduction to NP CompletenessIntroduction to NP Completeness
Introduction to NP CompletenessGene Moo Lee
 
Natural Language processing Parts of speech tagging, its classes, and how to ...
Natural Language processing Parts of speech tagging, its classes, and how to ...Natural Language processing Parts of speech tagging, its classes, and how to ...
Natural Language processing Parts of speech tagging, its classes, and how to ...Rajnish Raj
 
Neural Architectures for Named Entity Recognition
Neural Architectures for Named Entity RecognitionNeural Architectures for Named Entity Recognition
Neural Architectures for Named Entity RecognitionRrubaa Panchendrarajan
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 

What's hot (20)

Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
L03 ai - knowledge representation using logic
L03 ai - knowledge representation using logicL03 ai - knowledge representation using logic
L03 ai - knowledge representation using logic
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Travelling Salesman Problem
Travelling Salesman ProblemTravelling Salesman Problem
Travelling Salesman Problem
 
Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
AI_Session 7 Greedy Best first search algorithm.pptx
AI_Session 7 Greedy Best first search algorithm.pptxAI_Session 7 Greedy Best first search algorithm.pptx
AI_Session 7 Greedy Best first search algorithm.pptx
 
Travelling Salesman Problem
Travelling Salesman ProblemTravelling Salesman Problem
Travelling Salesman Problem
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
Master method
Master method Master method
Master method
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Introduction to NP Completeness
Introduction to NP CompletenessIntroduction to NP Completeness
Introduction to NP Completeness
 
Natural Language processing Parts of speech tagging, its classes, and how to ...
Natural Language processing Parts of speech tagging, its classes, and how to ...Natural Language processing Parts of speech tagging, its classes, and how to ...
Natural Language processing Parts of speech tagging, its classes, and how to ...
 
Neural Architectures for Named Entity Recognition
Neural Architectures for Named Entity RecognitionNeural Architectures for Named Entity Recognition
Neural Architectures for Named Entity Recognition
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 

Similar to Dynamic Programming Techniques and Algorithms Explained

Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhdanielgetachew0922
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptxTekle12
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to AlgorithmsVenkatesh Iyer
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...TechVision8
 
Measuring algorithm performance
Measuring algorithm performanceMeasuring algorithm performance
Measuring algorithm performanceHabitamuAsimare
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)Mumtaz Ali
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
Dynamic programmng2
Dynamic programmng2Dynamic programmng2
Dynamic programmng2debolina13
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues listsJames Wong
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
StacksqueueslistsFraboni Ec
 

Similar to Dynamic Programming Techniques and Algorithms Explained (20)

dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
DATA STRUCTURE.pdf
DATA STRUCTURE.pdfDATA STRUCTURE.pdf
DATA STRUCTURE.pdf
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
 
NP-Completeness - II
NP-Completeness - IINP-Completeness - II
NP-Completeness - II
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
 
Measuring algorithm performance
Measuring algorithm performanceMeasuring algorithm performance
Measuring algorithm performance
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
 
Combinatorial Optimization
Combinatorial OptimizationCombinatorial Optimization
Combinatorial Optimization
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 
Lecture11
Lecture11Lecture11
Lecture11
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
Unit3_1.pdf
Unit3_1.pdfUnit3_1.pdf
Unit3_1.pdf
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Dynamic programmng2
Dynamic programmng2Dynamic programmng2
Dynamic programmng2
 
algo1-aa-gist_typed.pdf
algo1-aa-gist_typed.pdfalgo1-aa-gist_typed.pdf
algo1-aa-gist_typed.pdf
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues lists
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
Stacksqueueslists
 

Dynamic Programming Techniques and Algorithms Explained

  • 1. Dynamic Programming OOTA SUBBA REDDY IIIT-Hyderabad 2015
  • 2. Dynamic Programing Definition A dynamic programming algorithm solves each sub problem just one time and save the answer in a table rather than solving the same sub problem multiple times. This kind of algorithm is popular in optimization problems, which usually have many solutions. When you are developing dynamic programming algorithms you should. Characterize the structure an optimal solution Recursively define the value of an optimal solution. Computer the value of an optimal solution. Construct the solution from the computer information. Steps 1-3 are the most important here, but step four is useful if you need to actual data that led to your optimized solution.
  • 3. Rod Cutting Problem Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. length i 1 2 3 4 5 6 7 8 price pi 1 5 8 9 10 17 17 20 Optimal Substructure (recurrence formula as follow ) rn = max1 i n(pi + rn−i ) (1)
  • 4. LIS(Longest Increasing Subsequence) Given a sequence of integers S1, ...., Sn. Find a Subsequence Si1 < Si2 < ... < Sik with i1 < ...ik so that k is as large as possible. Recurrence Formula: Let L[i]=length of longest increasing subsequence in S1, ...Sn that ends in Si L[j] = 1 + maxL[i] : i < jandSi < Sj Given 9,5,2,8,7,3,1,6,4 as input, possible increasing subsequence is 5,7 better is 2,3,6 or 2,3,4 (either of which would be a correct) Note: Similar problems LDS (Longest Decreasing Subsequence) LMIS (Longest Monotonically Increasing Subsequnce) LMDS (Longest Monotonically Decreasing Subsequence)
  • 5. Longest Palindrome Subsequence Problem Describe an O(n2) algorithm to find the length of the longest palindromic subsequence of a given string. Example: a longest palindromic subsequence of ALGORITHMSISAGREATCLASS is ALGIMIGLA , which has length 9. Recurrence Formula: T[i, j] =    1 if i = j 2+M[i+1,j-1] if A[i]=A[j] and i < j maxM[i+1,j],M[i,j-1] if A[i]!=A[j] and i < j 0 if i < j    Note: Similar problems LCS (Longest Common Subsequence) LCS (Convert LCS to LIS only if unique characters exists in Strings, try O(nlogn) approach)
  • 6. Palindrome Partitioning Problem Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = ”aab”, Return 1 since the palindrome partitioning [”aa”,”b”] could be produced using 1 cut. D[i] equals the minimum cut number for string [i,n]. D[i] = min(D[i], D[j+1]+1) j i, and [j+1, i] forms a palindrome. Note: Similar problems MM (Matrix Chain Multiplication) Mixtures (Check in SPOJ) Word Break
  • 7. Some Other Problems Using DP EDIST (Edit Distance) SHPATH ( Shortest Path Using Bellam-Ford Algorithm) ALL PAIR SHPATH ( Floyd-Warshall Algorithm) OBST (Optimal Binary Search Tree) TSP (Travelling Salesman Problem) Reliability Design