SlideShare a Scribd company logo
1 of 18
Introduction to Dynamic Programming
Portland Data Science Group
Created by Andrew Ferlitsch
Community Outreach Officer
June, 2017
What is it?
• A method of solving complex problems.
• Steps:
• Solve sub-problems.
• Store (Remember) solutions to sub-problems.
• Reuse Stored Solutions.
Complex Problem
Sub-Problem X
Sub-Problem Y
Sub-Problem X
Solution X
Solution Y
Solution Storage
Solve problem by
decomposing into
sub-problems X, Y
and X again.
Store solution to X
Reuse solution X when sub-problem X
Is encountered again.
How is this different from Functions?
• In Traditional Programming the solutions to
sub-problems is known.
• Steps:
• Programmer decomposes the problem into
sub-problems.
• Designs and Codes solution to sub-problem.
• Encapsulates solution to sub-problem in a function.
In Dynamic Programming,
a Solution is Discovered (not Coded!).
Traditional Design & Coded Program
Complex Problem
Sub-Problem X
Sub-Problem Y
Sub-Problem X
Function X
Function Y
Solutions to sub-problems were
pre-designed and coded, and stored
as re-usable functions (components).
Pre-designed solution reused.
Run-time
program
execution
Flow.
The solutions are already known.
Least Coin Problem – Traditional Solution
1 5 10 25
Problem: For any money amount, calculate the least number
of coins to carry in your pocket.
$.08 = 5 + 1 + 1 + 1 = 4 coins
$.23 = 10 + 10 + 1 + 1 + 1 = 5 coins
sum = 0
number_of_coins = 0
while sum < amount:
coin = largest_coin(amount – sum)
sum = sum + coin
number_of_coins = number_of_coins + 1
Solution can be pre-designed and code because the coins are multiples of each other.
Least Coin Problem – Coins not Multiples
1 4 5 15
$.08 = 4 + 4 = 2 coins (not 5 + 1 + 1 +1 = 4 coins)
$.23 = 15 + 4 + 4 = 3 coins (not 15 + 5 + 1 + 1 + 1 = 5 coins)
If we had used the previous pre-designed/coded solution, we would have gotten
the wrong answer!
Discover Solution using BFS Search Tree
1 4 5
Root of Search Tree
sum = 4
First Level of Search Tree, Possible Coin choices <= $.08
coins = BFS( $.08, [ 15, 5, 4, 1 ] )
No node for
15 cent coin
because > .08
Discover Solution using BFS Search Tree
1 4 5
Root of Search Tree
sum = 4
coins = BFS( $.08, [ 15, 5, 4, 1 ] )
1 4 5
sum = 5
1 4
No node for
15 cent coin
because > .07
No node for
15 and 5 cent coin
because > .04
Not expanded
Because goal
was found
Found Goal Node: $.08 = 4 + 4 ( 2 coins)
BFS will found solution at shallowest node, which is the least number of coins.
Put Solution in Solution Space
Dollar Amount Coins
.08 [ 4, 4 ]
Store the solution in a solution space
Now let’s solve: coins = BFS( $.23, [ 15, 5, 4, 1 ] )
Discover Solution using BFS Search Tree
1 4 5
Root of Search Tree
coins = BFS( $.23, [ 15, 5, 4, 1 ] )
15
First Level of Search Tree, Possible Coin choices <= $.23
Discover Solution using BFS Search Tree
1 4 5
Root of Search Tree
coins = BFS( $.23, [ 15, 5, 4, 1 ] )
15
4
4
Dollar Amount Coins
.08 [ 4, 4 ]
.23 [15, 4, 4]
Solve sub-problem $.22 Solve sub-problem $.08
Solve sub-problem $.19
Solve sub-problem $.18
Reuse Solved
Solution for
.08
Advanced – Initial Solution Space
1 4 5
coins = BFS( $.08, [ 15, 5, 4, 1 ] )
sum = 4
Dollar Amount Coins
.01 [ 1 ]
.04 [ 4 ]
.05 [ 5 ]
Initial Optimal
Solutions
Advanced– Prune Less Optimal Solution
1
1 4 5
sum = 5
coins = BFS( $.08, [ 15, 5, 4, 1 ] )
Dollar Amount Coins
.01 [ 1 ]
.02 [ 1, 1 ]
.04 [ 4 ]
.05 [ 5 ]
.06 [ 5, 1 ]
Add new
solutions
Prune Node since current
solution is less optimal than
solution in solution space.
Solve for sub-problem $.07
Advanced – Prune Nodes when Solution Exists
4
sum = 4
coins = BFS( $.08, [ 15, 5, 4, 1 ] )
4
sum = 8
Dollar Amount Coins
.01 [ 1 ]
.02 [ 1, 1 ]
.04 [ 4 ]
.05 [ 5 ]
.06 [ 5, 1 ]
.08 [ 4, 4 ]
Solve for sub-problem $.04
Add solution for $.08
No node for
1 cent coin
because solution
exists.
Advanced – Initial Solutions for $.23
1 4 5
coins = BFS( $.23, [ 15, 5, 4, 1 ] )
15
Dollar Amount Coins
.01 [ 1 ]
.02 [ 1, 1 ]
.04 [ 4 ]
.05 [ 5 ]
.06 [ 5, 1 ]
.08 [ 4, 4 ]
.15 [ 15 ]
Add solution for $.15
Advanced – Apply Sub-Solution $.08
1 4 5
coins = BFS( $.23, [ 15, 5, 4, 1 ] )
15
Dollar Amount Coins
.01 [ 1 ]
.02 [ 1, 1 ]
.04 [ 4 ]
.05 [ 5 ]
.06 [ 5, 1 ]
.08 [ 4, 4 ]
.15 [ 15 ]
Solve for $.22 Solve for $.08
Solve for $.19
4
4
Solve for $.18
Reuse solution for $.08
Advanced – Prune N-1 candidate solutions
1 4 5
coins = BFS( $.23, [ 15, 5, 4, 1 ] )
15
Solve for $.08
Solve for $.15
4
4
N = 2
4
Solve for $.18
If sub-solution is N deep, then only search n-1 levels deeper for alternate solution. If not found at
N-1 depth, then alternate solution must be at least N deep and therefore not a better solution.
1
Prune at N-1
Search Solution Space for Prior Solutions
That’s Dynamic Programming!

More Related Content

More from Andrew Ferlitsch

Machine Learning - Introduction to Recurrent Neural Networks
Machine Learning - Introduction to Recurrent Neural NetworksMachine Learning - Introduction to Recurrent Neural Networks
Machine Learning - Introduction to Recurrent Neural NetworksAndrew Ferlitsch
 
Machine Learning - Introduction to Convolutional Neural Networks
Machine Learning - Introduction to Convolutional Neural NetworksMachine Learning - Introduction to Convolutional Neural Networks
Machine Learning - Introduction to Convolutional Neural NetworksAndrew Ferlitsch
 
Machine Learning - Introduction to Neural Networks
Machine Learning - Introduction to Neural NetworksMachine Learning - Introduction to Neural Networks
Machine Learning - Introduction to Neural NetworksAndrew Ferlitsch
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesAndrew Ferlitsch
 
Machine Learning - Accuracy and Confusion Matrix
Machine Learning - Accuracy and Confusion MatrixMachine Learning - Accuracy and Confusion Matrix
Machine Learning - Accuracy and Confusion MatrixAndrew Ferlitsch
 
Machine Learning - Ensemble Methods
Machine Learning - Ensemble MethodsMachine Learning - Ensemble Methods
Machine Learning - Ensemble MethodsAndrew Ferlitsch
 
ML - Multiple Linear Regression
ML - Multiple Linear RegressionML - Multiple Linear Regression
ML - Multiple Linear RegressionAndrew Ferlitsch
 
ML - Simple Linear Regression
ML - Simple Linear RegressionML - Simple Linear Regression
ML - Simple Linear RegressionAndrew Ferlitsch
 
Machine Learning - Dummy Variable Conversion
Machine Learning - Dummy Variable ConversionMachine Learning - Dummy Variable Conversion
Machine Learning - Dummy Variable ConversionAndrew Ferlitsch
 
Machine Learning - Splitting Datasets
Machine Learning - Splitting DatasetsMachine Learning - Splitting Datasets
Machine Learning - Splitting DatasetsAndrew Ferlitsch
 
Machine Learning - Dataset Preparation
Machine Learning - Dataset PreparationMachine Learning - Dataset Preparation
Machine Learning - Dataset PreparationAndrew Ferlitsch
 
Machine Learning - Introduction to Tensorflow
Machine Learning - Introduction to TensorflowMachine Learning - Introduction to Tensorflow
Machine Learning - Introduction to TensorflowAndrew Ferlitsch
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine LearningAndrew Ferlitsch
 
Statistics - SoftMax Equation
Statistics - SoftMax EquationStatistics - SoftMax Equation
Statistics - SoftMax EquationAndrew Ferlitsch
 
Statistics - ArgMax Equation
Statistics - ArgMax EquationStatistics - ArgMax Equation
Statistics - ArgMax EquationAndrew Ferlitsch
 
AI - Introduction to Reinforcement Learning
AI - Introduction to Reinforcement LearningAI - Introduction to Reinforcement Learning
AI - Introduction to Reinforcement LearningAndrew Ferlitsch
 
Machine Learning - Neural Networks - Perceptron
Machine Learning - Neural Networks - PerceptronMachine Learning - Neural Networks - Perceptron
Machine Learning - Neural Networks - PerceptronAndrew Ferlitsch
 
AI - Introduction to Bellman Equations
AI - Introduction to Bellman EquationsAI - Introduction to Bellman Equations
AI - Introduction to Bellman EquationsAndrew Ferlitsch
 
AI - Introduction to Markov Principles
AI - Introduction to Markov PrinciplesAI - Introduction to Markov Principles
AI - Introduction to Markov PrinciplesAndrew Ferlitsch
 

More from Andrew Ferlitsch (20)

Machine Learning - Introduction to Recurrent Neural Networks
Machine Learning - Introduction to Recurrent Neural NetworksMachine Learning - Introduction to Recurrent Neural Networks
Machine Learning - Introduction to Recurrent Neural Networks
 
Machine Learning - Introduction to Convolutional Neural Networks
Machine Learning - Introduction to Convolutional Neural NetworksMachine Learning - Introduction to Convolutional Neural Networks
Machine Learning - Introduction to Convolutional Neural Networks
 
Machine Learning - Introduction to Neural Networks
Machine Learning - Introduction to Neural NetworksMachine Learning - Introduction to Neural Networks
Machine Learning - Introduction to Neural Networks
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
Machine Learning - Accuracy and Confusion Matrix
Machine Learning - Accuracy and Confusion MatrixMachine Learning - Accuracy and Confusion Matrix
Machine Learning - Accuracy and Confusion Matrix
 
Machine Learning - Ensemble Methods
Machine Learning - Ensemble MethodsMachine Learning - Ensemble Methods
Machine Learning - Ensemble Methods
 
ML - Multiple Linear Regression
ML - Multiple Linear RegressionML - Multiple Linear Regression
ML - Multiple Linear Regression
 
ML - Simple Linear Regression
ML - Simple Linear RegressionML - Simple Linear Regression
ML - Simple Linear Regression
 
Machine Learning - Dummy Variable Conversion
Machine Learning - Dummy Variable ConversionMachine Learning - Dummy Variable Conversion
Machine Learning - Dummy Variable Conversion
 
Machine Learning - Splitting Datasets
Machine Learning - Splitting DatasetsMachine Learning - Splitting Datasets
Machine Learning - Splitting Datasets
 
Machine Learning - Dataset Preparation
Machine Learning - Dataset PreparationMachine Learning - Dataset Preparation
Machine Learning - Dataset Preparation
 
Machine Learning - Introduction to Tensorflow
Machine Learning - Introduction to TensorflowMachine Learning - Introduction to Tensorflow
Machine Learning - Introduction to Tensorflow
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
 
Statistics - SoftMax Equation
Statistics - SoftMax EquationStatistics - SoftMax Equation
Statistics - SoftMax Equation
 
Statistics - ArgMax Equation
Statistics - ArgMax EquationStatistics - ArgMax Equation
Statistics - ArgMax Equation
 
Statistics - Basics
Statistics - BasicsStatistics - Basics
Statistics - Basics
 
AI - Introduction to Reinforcement Learning
AI - Introduction to Reinforcement LearningAI - Introduction to Reinforcement Learning
AI - Introduction to Reinforcement Learning
 
Machine Learning - Neural Networks - Perceptron
Machine Learning - Neural Networks - PerceptronMachine Learning - Neural Networks - Perceptron
Machine Learning - Neural Networks - Perceptron
 
AI - Introduction to Bellman Equations
AI - Introduction to Bellman EquationsAI - Introduction to Bellman Equations
AI - Introduction to Bellman Equations
 
AI - Introduction to Markov Principles
AI - Introduction to Markov PrinciplesAI - Introduction to Markov Principles
AI - Introduction to Markov Principles
 

Recently uploaded

SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 

AI - Introduction to Dynamic Programming

  • 1. Introduction to Dynamic Programming Portland Data Science Group Created by Andrew Ferlitsch Community Outreach Officer June, 2017
  • 2. What is it? • A method of solving complex problems. • Steps: • Solve sub-problems. • Store (Remember) solutions to sub-problems. • Reuse Stored Solutions. Complex Problem Sub-Problem X Sub-Problem Y Sub-Problem X Solution X Solution Y Solution Storage Solve problem by decomposing into sub-problems X, Y and X again. Store solution to X Reuse solution X when sub-problem X Is encountered again.
  • 3. How is this different from Functions? • In Traditional Programming the solutions to sub-problems is known. • Steps: • Programmer decomposes the problem into sub-problems. • Designs and Codes solution to sub-problem. • Encapsulates solution to sub-problem in a function. In Dynamic Programming, a Solution is Discovered (not Coded!).
  • 4. Traditional Design & Coded Program Complex Problem Sub-Problem X Sub-Problem Y Sub-Problem X Function X Function Y Solutions to sub-problems were pre-designed and coded, and stored as re-usable functions (components). Pre-designed solution reused. Run-time program execution Flow. The solutions are already known.
  • 5. Least Coin Problem – Traditional Solution 1 5 10 25 Problem: For any money amount, calculate the least number of coins to carry in your pocket. $.08 = 5 + 1 + 1 + 1 = 4 coins $.23 = 10 + 10 + 1 + 1 + 1 = 5 coins sum = 0 number_of_coins = 0 while sum < amount: coin = largest_coin(amount – sum) sum = sum + coin number_of_coins = number_of_coins + 1 Solution can be pre-designed and code because the coins are multiples of each other.
  • 6. Least Coin Problem – Coins not Multiples 1 4 5 15 $.08 = 4 + 4 = 2 coins (not 5 + 1 + 1 +1 = 4 coins) $.23 = 15 + 4 + 4 = 3 coins (not 15 + 5 + 1 + 1 + 1 = 5 coins) If we had used the previous pre-designed/coded solution, we would have gotten the wrong answer!
  • 7. Discover Solution using BFS Search Tree 1 4 5 Root of Search Tree sum = 4 First Level of Search Tree, Possible Coin choices <= $.08 coins = BFS( $.08, [ 15, 5, 4, 1 ] ) No node for 15 cent coin because > .08
  • 8. Discover Solution using BFS Search Tree 1 4 5 Root of Search Tree sum = 4 coins = BFS( $.08, [ 15, 5, 4, 1 ] ) 1 4 5 sum = 5 1 4 No node for 15 cent coin because > .07 No node for 15 and 5 cent coin because > .04 Not expanded Because goal was found Found Goal Node: $.08 = 4 + 4 ( 2 coins) BFS will found solution at shallowest node, which is the least number of coins.
  • 9. Put Solution in Solution Space Dollar Amount Coins .08 [ 4, 4 ] Store the solution in a solution space Now let’s solve: coins = BFS( $.23, [ 15, 5, 4, 1 ] )
  • 10. Discover Solution using BFS Search Tree 1 4 5 Root of Search Tree coins = BFS( $.23, [ 15, 5, 4, 1 ] ) 15 First Level of Search Tree, Possible Coin choices <= $.23
  • 11. Discover Solution using BFS Search Tree 1 4 5 Root of Search Tree coins = BFS( $.23, [ 15, 5, 4, 1 ] ) 15 4 4 Dollar Amount Coins .08 [ 4, 4 ] .23 [15, 4, 4] Solve sub-problem $.22 Solve sub-problem $.08 Solve sub-problem $.19 Solve sub-problem $.18 Reuse Solved Solution for .08
  • 12. Advanced – Initial Solution Space 1 4 5 coins = BFS( $.08, [ 15, 5, 4, 1 ] ) sum = 4 Dollar Amount Coins .01 [ 1 ] .04 [ 4 ] .05 [ 5 ] Initial Optimal Solutions
  • 13. Advanced– Prune Less Optimal Solution 1 1 4 5 sum = 5 coins = BFS( $.08, [ 15, 5, 4, 1 ] ) Dollar Amount Coins .01 [ 1 ] .02 [ 1, 1 ] .04 [ 4 ] .05 [ 5 ] .06 [ 5, 1 ] Add new solutions Prune Node since current solution is less optimal than solution in solution space. Solve for sub-problem $.07
  • 14. Advanced – Prune Nodes when Solution Exists 4 sum = 4 coins = BFS( $.08, [ 15, 5, 4, 1 ] ) 4 sum = 8 Dollar Amount Coins .01 [ 1 ] .02 [ 1, 1 ] .04 [ 4 ] .05 [ 5 ] .06 [ 5, 1 ] .08 [ 4, 4 ] Solve for sub-problem $.04 Add solution for $.08 No node for 1 cent coin because solution exists.
  • 15. Advanced – Initial Solutions for $.23 1 4 5 coins = BFS( $.23, [ 15, 5, 4, 1 ] ) 15 Dollar Amount Coins .01 [ 1 ] .02 [ 1, 1 ] .04 [ 4 ] .05 [ 5 ] .06 [ 5, 1 ] .08 [ 4, 4 ] .15 [ 15 ] Add solution for $.15
  • 16. Advanced – Apply Sub-Solution $.08 1 4 5 coins = BFS( $.23, [ 15, 5, 4, 1 ] ) 15 Dollar Amount Coins .01 [ 1 ] .02 [ 1, 1 ] .04 [ 4 ] .05 [ 5 ] .06 [ 5, 1 ] .08 [ 4, 4 ] .15 [ 15 ] Solve for $.22 Solve for $.08 Solve for $.19 4 4 Solve for $.18 Reuse solution for $.08
  • 17. Advanced – Prune N-1 candidate solutions 1 4 5 coins = BFS( $.23, [ 15, 5, 4, 1 ] ) 15 Solve for $.08 Solve for $.15 4 4 N = 2 4 Solve for $.18 If sub-solution is N deep, then only search n-1 levels deeper for alternate solution. If not found at N-1 depth, then alternate solution must be at least N deep and therefore not a better solution. 1 Prune at N-1
  • 18. Search Solution Space for Prior Solutions That’s Dynamic Programming!