SlideShare a Scribd company logo
Dynamic Programming
Dynamic Programming
•

Well known algorithm design techniques:.
– Divide-and-conquer algorithms

•

Another strategy for designing algorithms is dynamic
programming.
– Used when problem breaks down into recurring small
subproblems

•

Dynamic programming is typically applied to
optimization problems. In such problem there can be
many solutions. Each solution has a value, and we
wish to find a solution with the optimal value.
Divide-and-conquer
• Divide-and-conquer method for algorithm design:
• Divide: If the input size is too large to deal with in a
straightforward manner, divide the problem into two or
more disjoint subproblems
• Conquer: conquer recursively to solve the subproblems

• Combine: Take the solutions to the subproblems and
“merge” these solutions into a solution for the original
problem
Divide-and-conquer - Example
Dynamic Programming
Dynamic Programming is a general algorithm design technique
for solving problems defined by recurrences with overlapping
subproblems
• Invented by American mathematician Richard Bellman in the
1950s to solve optimization problems and later assimilated by CS

• “Programming” here means “planning”
• Main idea:
- set up a recurrence relating a solution to a larger instance to
solutions of some smaller instances
- solve smaller instances once
- record solutions in a table
- extract solution to the initial instance from that table
5
Dynamic programming
• Dynamic programming is a way of improving on inefficient divideand-conquer algorithms.
•

By “inefficient”, we mean that the same recursive call is made
over and over.

•

If same subproblem is solved several times, we can use table to
store result of a subproblem the first time it is computed and thus
never have to recompute it again.

• Dynamic programming is applicable when the subproblems are
dependent, that is, when subproblems share subsubproblems.

•

“Programming” refers to a tabular method
Difference between DP and Divideand-Conquer
• Using Divide-and-Conquer to solve these
problems is inefficient because the same
common subproblems have to be solved many
times.

• DP will solve each of them once and their
answers are stored in a table for future use.
Dynamic Programming vs. Recursion
and Divide & Conquer
• In a recursive program, a problem of size n is
solved by first solving a sub-problem of size n-1.
• In a divide & conquer program, you solve a
problem of size n by first solving a sub-problem
of size k and another of size k-1, where 1 < k <
n.
• In dynamic programming, you solve a problem
of size n by first solving all sub-problems of all
sizes k, where k < n.
Elements of Dynamic Programming
(DP)
DP is used to solve problems with the following characteristics:
• Simple subproblems
– We should be able to break the original problem to smaller
subproblems that have the same structure

•

Optimal substructure of the problems
– The optimal solution to the problem contains within optimal
solutions to its subproblems.

•

Overlapping sub-problems
– there exist some places where we solve the same subproblem more
than once.
Steps to Designing a
Dynamic Programming Algorithm
1. Characterize optimal substructure
2. Recursively define the value of an optimal
solution

3. Compute the value bottom up
4. (if needed) Construct an optimal solution
Principle of Optimality
• The dynamic Programming works on a principle
of optimality.
• Principle of optimality states that in an optimal
sequence of decisions or choices, each sub
sequences must also be optimal.
Example Applications of Dynamic
Programming
•
•
•
•
•
•

1/0 Knapsack
Optimal Merge portions
Shortest path problems
Matrix chain multiplication
Longest common subsequence
Mathematical optimization
Example 1: Fibonacci numbers
• Recall definition of Fibonacci numbers:
F(n) = F(n-1) + F(n-2)
F(0) = 0
F(1) = 1
• Computing the nth Fibonacci number recursively (top-down):
F(n)
F(n-1)
F(n-2)

+

+
F(n-3)

F(n-2)
F(n-3)

+

F(n-4)

...
13
Fibonacci Numbers
• Fn= Fn-1+ Fn-2
n≥2
• F0 =0, F1 =1
• 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …

• Straightforward recursive procedure is slow!
• Let’s draw the recursion tree
Fibonacci Numbers
Fibonacci Numbers
•

How many summations are there? Using Golden Ratio

• As you go farther and farther to the right in this sequence, the ratio
of a term to the one before it will get closer and closer to the Golden
Ratio.
• Our recursion tree has only 0s and 1s as leaves, thus we have 1.6n
summations
• Running time is exponential!
Fibonacci Numbers
• We can calculate Fn in linear time by remembering
solutions to the solved subproblems – dynamic
programming

• Compute solution in a bottom-up fashion
• In this case, only two values need to be
remembered at any time
Matrix Chain Multiplication
• Given : a chain of matrices {A1,A2,…,An}.
• Once all pairs of matrices are parenthesized, they can
be multiplied by using the standard algorithm as a subroutine.
• A product of matrices is fully parenthesized if it is either
a single matrix or the product of two fully parenthesized
matrix products, surrounded by parentheses. [Note: since
matrix multiplication is associative, all parenthesizations yield the

same product.]
Matrix Chain Multiplication cont.
• For example, if the chain of matrices is {A, B, C,
D}, the product A, B, C, D can be fully
parenthesized in 5 distinct ways:
(A ( B ( C D ))),
(A (( B C ) D )),
((A B ) ( C D )),
((A ( B C )) D),
((( A B ) C ) D ).

• The way the chain is parenthesized can have a
dramatic impact on the cost of evaluating the
product.
Matrix Chain Multiplication Optimal
Parenthesization
• Example: A[30][35], B[35][15], C[15][5]
minimum of A*B*C
A*(B*C) = 30*35*5 + 35*15*5 = 7,585
(A*B)*C = 30*35*15 + 30*15*5 = 18,000
• How to optimize:
– Brute force – look at every possible way to
parenthesize : Ω(4n/n3/2)
– Dynamic programming – time complexity of Ω(n3) and
space complexity of Θ(n2).
Matrix Chain Multiplication Structure of
Optimal Parenthesization
• For n matrices, let Ai..j be the result of AiAi+1….Aj
• An optimal parenthesization of AiAi+1…An splits
the product between Ak and Ak+1 where 1  k <
n.
• Example, k = 4
(A1A2A3A4)(A5A6)

Total cost of A1..6 = cost of A1..4 plus total
cost of multiplying these two matrices
together.
Matrix Chain Multiplication
Overlapping Sub-Problems
• Overlapping sub-problems helps in reducing the
running time considerably.
– Create a table M of minimum Costs
– Create a table S that records index k for each optimal subproblem
– Fill table M in a manner that corresponds to solving the
parenthesization problem on matrix chains of increasing
length.
– Compute cost for chains of length 1 (this is 0)
– Compute costs for chains of length 2

A1..2, A2..3, A3..4, …An-1…n

– Compute cost for chain of length n
A1..n
Each level relies on smaller sub-strings

More Related Content

What's hot

Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
manalishipra
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
Simplilearn
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Bharat Bhushan
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
contact2kazi
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
Ga
GaGa
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
Jyotsna Suryadevara
 
Divide&Conquer & Dynamic Programming
Divide&Conquer & Dynamic ProgrammingDivide&Conquer & Dynamic Programming
Divide&Conquer & Dynamic Programming
Guillaume Guérard
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
Fahim Ferdous
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
Krish_ver2
 
Parsing
ParsingParsing
Parsing
khush_boo31
 
Approximation Algorithms
Approximation AlgorithmsApproximation Algorithms
Approximation Algorithms
Nicolas Bettenburg
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
Amrinder Arora
 
Master theorem
Master theoremMaster theorem
Master theorem
fika sweety
 
Greedy algorithm
Greedy algorithmGreedy algorithm
NP completeness
NP completenessNP completeness
NP completeness
Amrinder Arora
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
Jenny Galino
 

What's hot (20)

Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Ga
GaGa
Ga
 
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
 
Divide&Conquer & Dynamic Programming
Divide&Conquer & Dynamic ProgrammingDivide&Conquer & Dynamic Programming
Divide&Conquer & Dynamic Programming
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
 
Parsing
ParsingParsing
Parsing
 
Approximation Algorithms
Approximation AlgorithmsApproximation Algorithms
Approximation Algorithms
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Master theorem
Master theoremMaster theorem
Master theorem
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
NP completeness
NP completenessNP completeness
NP completeness
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 

Viewers also liked

Dynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain MultiplicationDynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain Multiplication
Pecha Inc.
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Shakil Ahmed
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
Respa Peter
 
Elements of dynamic programming
Elements of dynamic programmingElements of dynamic programming
Elements of dynamic programming
Tafhim Islam
 
Matrix multiplication
Matrix multiplicationMatrix multiplication
Matrix multiplication
International Islamic University
 
Dynamic programming Basics
Dynamic programming BasicsDynamic programming Basics
Dynamic programming Basics
Kvishnu Dahatonde
 
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
height
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Gopi Saiteja
 
A software approach to mathematical programming
A software approach to mathematical programmingA software approach to mathematical programming
A software approach to mathematical programming
Arian Razmi Farooji
 
A Multiple-Shooting Differential Dynamic Programming Algorithm
A Multiple-Shooting Differential Dynamic Programming AlgorithmA Multiple-Shooting Differential Dynamic Programming Algorithm
A Multiple-Shooting Differential Dynamic Programming Algorithm
Etienne Pellegrini
 
(floyd's algm)
(floyd's algm)(floyd's algm)
(floyd's algm)
Jothi Lakshmi
 
Dynamic programming
Dynamic programming Dynamic programming
Dynamic programming
Zohaib HUSSAIN
 
The Floyd–Warshall algorithm
The Floyd–Warshall algorithmThe Floyd–Warshall algorithm
The Floyd–Warshall algorithm
José Juan Herrera
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
ElifTech
 
Unit 7 dynamic programming
Unit 7   dynamic programmingUnit 7   dynamic programming
Unit 7 dynamic programming
Nageswara Rao Thots
 
LCS Problem
LCS ProblemLCS Problem
LCS Problem
Hoang Nguyen
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
guest862df4e
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
Amrinder Arora
 
Greedy method class 11
Greedy method class 11Greedy method class 11
Greedy method class 11
Kumar
 

Viewers also liked (20)

Dynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain MultiplicationDynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain Multiplication
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
Elements of dynamic programming
Elements of dynamic programmingElements of dynamic programming
Elements of dynamic programming
 
Matrix multiplication
Matrix multiplicationMatrix multiplication
Matrix multiplication
 
Dynamic programming Basics
Dynamic programming BasicsDynamic programming Basics
Dynamic programming Basics
 
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
Approximate Dynamic Programming: A New Paradigm for Process Control & Optimiz...
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
A software approach to mathematical programming
A software approach to mathematical programmingA software approach to mathematical programming
A software approach to mathematical programming
 
A Multiple-Shooting Differential Dynamic Programming Algorithm
A Multiple-Shooting Differential Dynamic Programming AlgorithmA Multiple-Shooting Differential Dynamic Programming Algorithm
A Multiple-Shooting Differential Dynamic Programming Algorithm
 
(floyd's algm)
(floyd's algm)(floyd's algm)
(floyd's algm)
 
Dynamic programming
Dynamic programming Dynamic programming
Dynamic programming
 
The Floyd–Warshall algorithm
The Floyd–Warshall algorithmThe Floyd–Warshall algorithm
The Floyd–Warshall algorithm
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
 
Unit 7 dynamic programming
Unit 7   dynamic programmingUnit 7   dynamic programming
Unit 7 dynamic programming
 
LCS Problem
LCS ProblemLCS Problem
LCS Problem
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
 
Greedy method class 11
Greedy method class 11Greedy method class 11
Greedy method class 11
 

Similar to Dynamic programming class 16

Dynamic programming prasintation eaisy
Dynamic programming prasintation eaisyDynamic programming prasintation eaisy
Dynamic programming prasintation eaisy
ahmed51236
 
Dynamicpgmming
DynamicpgmmingDynamicpgmming
Dynamicpgmming
Muhammad Wasif
 
DynamicProgramming.pptx
DynamicProgramming.pptxDynamicProgramming.pptx
DynamicProgramming.pptx
SaimaShaheen14
 
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer methodModule 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
JyoReddy9
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
giridaroori
 
Lecture11
Lecture11Lecture11
Lecture11
Nv Thejaswini
 
Lecture+9+-+Dynamic+Programming+I.pdf
Lecture+9+-+Dynamic+Programming+I.pdfLecture+9+-+Dynamic+Programming+I.pdf
Lecture+9+-+Dynamic+Programming+I.pdf
ShaistaRiaz4
 
Unit 5
Unit 5Unit 5
Unit 5
Unit 5Unit 5
Unit 5
guna287176
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
jannatulferdousmaish
 
Chap12 slides
Chap12 slidesChap12 slides
Chap12 slides
BaliThorat1
 
Undecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsUndecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation Algorithms
Muthu Vinayagam
 
Linear Programing.pptx
Linear Programing.pptxLinear Programing.pptx
Linear Programing.pptx
AdnanHaleem
 
Linear programing
Linear programing Linear programing
Linear programing
Deepak Pradhan
 
Algorithms
Algorithms Algorithms
Algorithms
yashodhaHR2
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
Bulbul Agrawal
 
Dynamic programming 2
Dynamic programming 2Dynamic programming 2
Dynamic programming 2
Roy Thomas
 
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
 
Convex optmization in communications
Convex optmization in communicationsConvex optmization in communications
Convex optmization in communications
Deepshika Reddy
 
Optimum Engineering Design - Day 4 - Clasical methods of optimization
Optimum Engineering Design - Day 4 - Clasical methods of optimizationOptimum Engineering Design - Day 4 - Clasical methods of optimization
Optimum Engineering Design - Day 4 - Clasical methods of optimization
SantiagoGarridoBulln
 

Similar to Dynamic programming class 16 (20)

Dynamic programming prasintation eaisy
Dynamic programming prasintation eaisyDynamic programming prasintation eaisy
Dynamic programming prasintation eaisy
 
Dynamicpgmming
DynamicpgmmingDynamicpgmming
Dynamicpgmming
 
DynamicProgramming.pptx
DynamicProgramming.pptxDynamicProgramming.pptx
DynamicProgramming.pptx
 
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer methodModule 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
Lecture11
Lecture11Lecture11
Lecture11
 
Lecture+9+-+Dynamic+Programming+I.pdf
Lecture+9+-+Dynamic+Programming+I.pdfLecture+9+-+Dynamic+Programming+I.pdf
Lecture+9+-+Dynamic+Programming+I.pdf
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 5
Unit 5Unit 5
Unit 5
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
 
Chap12 slides
Chap12 slidesChap12 slides
Chap12 slides
 
Undecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsUndecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation Algorithms
 
Linear Programing.pptx
Linear Programing.pptxLinear Programing.pptx
Linear Programing.pptx
 
Linear programing
Linear programing Linear programing
Linear programing
 
Algorithms
Algorithms Algorithms
Algorithms
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
 
Dynamic programming 2
Dynamic programming 2Dynamic programming 2
Dynamic programming 2
 
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
 
Convex optmization in communications
Convex optmization in communicationsConvex optmization in communications
Convex optmization in communications
 
Optimum Engineering Design - Day 4 - Clasical methods of optimization
Optimum Engineering Design - Day 4 - Clasical methods of optimizationOptimum Engineering Design - Day 4 - Clasical methods of optimization
Optimum Engineering Design - Day 4 - Clasical methods of optimization
 

More from Kumar

Graphics devices
Graphics devicesGraphics devices
Graphics devices
Kumar
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithms
Kumar
 
region-filling
region-fillingregion-filling
region-filling
Kumar
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
Kumar
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons derication
Kumar
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
Kumar
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
Kumar
 
Xml basics
Xml basicsXml basics
Xml basics
Kumar
 
XML Schema
XML SchemaXML Schema
XML Schema
Kumar
 
Publishing xml
Publishing xmlPublishing xml
Publishing xml
Kumar
 
DTD
DTDDTD
DTD
Kumar
 
Applying xml
Applying xmlApplying xml
Applying xml
Kumar
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
Kumar
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee application
Kumar
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XML
Kumar
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB Fundmentals
Kumar
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programming
Kumar
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programming
Kumar
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC Drivers
Kumar
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
Introduction to J2EE
Kumar
 

More from Kumar (20)

Graphics devices
Graphics devicesGraphics devices
Graphics devices
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithms
 
region-filling
region-fillingregion-filling
region-filling
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons derication
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
 
Xml basics
Xml basicsXml basics
Xml basics
 
XML Schema
XML SchemaXML Schema
XML Schema
 
Publishing xml
Publishing xmlPublishing xml
Publishing xml
 
DTD
DTDDTD
DTD
 
Applying xml
Applying xmlApplying xml
Applying xml
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee application
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XML
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB Fundmentals
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programming
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programming
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC Drivers
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
Introduction to J2EE
 

Recently uploaded

The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 

Recently uploaded (20)

The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 

Dynamic programming class 16

  • 2. Dynamic Programming • Well known algorithm design techniques:. – Divide-and-conquer algorithms • Another strategy for designing algorithms is dynamic programming. – Used when problem breaks down into recurring small subproblems • Dynamic programming is typically applied to optimization problems. In such problem there can be many solutions. Each solution has a value, and we wish to find a solution with the optimal value.
  • 3. Divide-and-conquer • Divide-and-conquer method for algorithm design: • Divide: If the input size is too large to deal with in a straightforward manner, divide the problem into two or more disjoint subproblems • Conquer: conquer recursively to solve the subproblems • Combine: Take the solutions to the subproblems and “merge” these solutions into a solution for the original problem
  • 5. Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems • Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems and later assimilated by CS • “Programming” here means “planning” • Main idea: - set up a recurrence relating a solution to a larger instance to solutions of some smaller instances - solve smaller instances once - record solutions in a table - extract solution to the initial instance from that table 5
  • 6. Dynamic programming • Dynamic programming is a way of improving on inefficient divideand-conquer algorithms. • By “inefficient”, we mean that the same recursive call is made over and over. • If same subproblem is solved several times, we can use table to store result of a subproblem the first time it is computed and thus never have to recompute it again. • Dynamic programming is applicable when the subproblems are dependent, that is, when subproblems share subsubproblems. • “Programming” refers to a tabular method
  • 7. Difference between DP and Divideand-Conquer • Using Divide-and-Conquer to solve these problems is inefficient because the same common subproblems have to be solved many times. • DP will solve each of them once and their answers are stored in a table for future use.
  • 8. Dynamic Programming vs. Recursion and Divide & Conquer • In a recursive program, a problem of size n is solved by first solving a sub-problem of size n-1. • In a divide & conquer program, you solve a problem of size n by first solving a sub-problem of size k and another of size k-1, where 1 < k < n. • In dynamic programming, you solve a problem of size n by first solving all sub-problems of all sizes k, where k < n.
  • 9. Elements of Dynamic Programming (DP) DP is used to solve problems with the following characteristics: • Simple subproblems – We should be able to break the original problem to smaller subproblems that have the same structure • Optimal substructure of the problems – The optimal solution to the problem contains within optimal solutions to its subproblems. • Overlapping sub-problems – there exist some places where we solve the same subproblem more than once.
  • 10. Steps to Designing a Dynamic Programming Algorithm 1. Characterize optimal substructure 2. Recursively define the value of an optimal solution 3. Compute the value bottom up 4. (if needed) Construct an optimal solution
  • 11. Principle of Optimality • The dynamic Programming works on a principle of optimality. • Principle of optimality states that in an optimal sequence of decisions or choices, each sub sequences must also be optimal.
  • 12. Example Applications of Dynamic Programming • • • • • • 1/0 Knapsack Optimal Merge portions Shortest path problems Matrix chain multiplication Longest common subsequence Mathematical optimization
  • 13. Example 1: Fibonacci numbers • Recall definition of Fibonacci numbers: F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1 • Computing the nth Fibonacci number recursively (top-down): F(n) F(n-1) F(n-2) + + F(n-3) F(n-2) F(n-3) + F(n-4) ... 13
  • 14. Fibonacci Numbers • Fn= Fn-1+ Fn-2 n≥2 • F0 =0, F1 =1 • 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … • Straightforward recursive procedure is slow! • Let’s draw the recursion tree
  • 16. Fibonacci Numbers • How many summations are there? Using Golden Ratio • As you go farther and farther to the right in this sequence, the ratio of a term to the one before it will get closer and closer to the Golden Ratio. • Our recursion tree has only 0s and 1s as leaves, thus we have 1.6n summations • Running time is exponential!
  • 17. Fibonacci Numbers • We can calculate Fn in linear time by remembering solutions to the solved subproblems – dynamic programming • Compute solution in a bottom-up fashion • In this case, only two values need to be remembered at any time
  • 18. Matrix Chain Multiplication • Given : a chain of matrices {A1,A2,…,An}. • Once all pairs of matrices are parenthesized, they can be multiplied by using the standard algorithm as a subroutine. • A product of matrices is fully parenthesized if it is either a single matrix or the product of two fully parenthesized matrix products, surrounded by parentheses. [Note: since matrix multiplication is associative, all parenthesizations yield the same product.]
  • 19. Matrix Chain Multiplication cont. • For example, if the chain of matrices is {A, B, C, D}, the product A, B, C, D can be fully parenthesized in 5 distinct ways: (A ( B ( C D ))), (A (( B C ) D )), ((A B ) ( C D )), ((A ( B C )) D), ((( A B ) C ) D ). • The way the chain is parenthesized can have a dramatic impact on the cost of evaluating the product.
  • 20. Matrix Chain Multiplication Optimal Parenthesization • Example: A[30][35], B[35][15], C[15][5] minimum of A*B*C A*(B*C) = 30*35*5 + 35*15*5 = 7,585 (A*B)*C = 30*35*15 + 30*15*5 = 18,000 • How to optimize: – Brute force – look at every possible way to parenthesize : Ω(4n/n3/2) – Dynamic programming – time complexity of Ω(n3) and space complexity of Θ(n2).
  • 21. Matrix Chain Multiplication Structure of Optimal Parenthesization • For n matrices, let Ai..j be the result of AiAi+1….Aj • An optimal parenthesization of AiAi+1…An splits the product between Ak and Ak+1 where 1  k < n. • Example, k = 4 (A1A2A3A4)(A5A6) Total cost of A1..6 = cost of A1..4 plus total cost of multiplying these two matrices together.
  • 22. Matrix Chain Multiplication Overlapping Sub-Problems • Overlapping sub-problems helps in reducing the running time considerably. – Create a table M of minimum Costs – Create a table S that records index k for each optimal subproblem – Fill table M in a manner that corresponds to solving the parenthesization problem on matrix chains of increasing length. – Compute cost for chains of length 1 (this is 0) – Compute costs for chains of length 2 A1..2, A2..3, A3..4, …An-1…n – Compute cost for chain of length n A1..n Each level relies on smaller sub-strings