SlideShare a Scribd company logo
Algorithms
Practice &
Problem Solving
Women Who Code
Meet the Organizers
Xochitl Watts
Twitter: @XochitlWatts
https://www.a2omini.com
Kelly KappJennifer Sweezey
What are algorithms?
(In mathematics, computing, and related subjects) An algorithm is an
effective method for solving a problem using a finite sequence of actions to be
performed.
- Wikipedia
Essentials to Solving a Problem
A data structure holds data and allows a specific set of operations to be
performed on a data set.
Data Structures
1) Record
2) Linked List
3) Stack
4) Queue
5) Set
6) Map
7) Graph
8) Tree
9) Heaps
Algorithm Design
1) Divide and Conquer
2) Greedy Method
3) Dynamic Programming
4) Graph traversal methods
5) Branch and Bound
Dynamic Programming (DP)
1) DP builds its solution by constructing solutions to smaller instances of the
same problem.
2) A problem has an optimal substructure if the sub-solutions of an optimal
solution of the problem are optimal solutions for their sub-problems.
3) The problem contains overlapping sub-problems.
4) DP arrives to a solution using bottom up traversal, building the solution to
many sub-problems that may or may not be required.
Memoize & re-use solutions to subproblems that help solve the problem
Example: Compute n-th Fibonacci number
Recursion
function fib(int n) {
If (n <= 2) {
return 1;
}
return fib(n-1) + fib(n-2);
}
Dynamic Programming
long a[] = new long[n + 1];
a[1] = a[2] = 1
for (int i=3; i <=n; i++) {
a[i] = a[i-1] + a[i-2];
}
return a[n];
Memoization
Memoization aims to prevent recomputations by storing (memorizing) the
return values of the function calls.
http://en.wikipedia.org/wiki/Memoization
Example: Compute n-th Fibonacci number
Recursion Dynamic Programming
n Time (MSEC)
10 0
20 1
30 8
40 922
50 113770
n Time (MSEC)
10 0
20 0
30 0
40 0
50 0
DP Template - NORA
1) Notation: Develop a mathematical notation that can express any solution
and any sub-solution for the problem at hand.
2) Optimality: Prove that the optimal substructure holds - sub-solutions of
an optimal solution are optimal solutions for sub-problems.
3) Recurrence: Develop a recurrence relation that relates a solution to its
sub-solutions using math notation.
4) Algorithm: Write out the algorithm, iterate over all the parameters of the
recurrence relation to compute the results for the actual problem that
needed to be solved.
Fibonacci Finding Challenge
https://www.hackerrank.com/challenges/fibonacci-finding-easy
Coin Change Challenge
https://www.hackerrank.com/challenges/coin-change
Python Solution: Fibonacci Finding Challenge
def fibonacci(n):
if n < 2:
return n
if not n in memory.keys():
memory[n] = fibonacci(n-1) + fibonacci(n-2)
return memory[n]
memory = {}
n = int(raw_input())
print(fibonacci(n))
Python Solution: Coin Change Challenge
Recursion
def count(sum_, coins):
if len(coins) == 0:
return 0
if sum_ < 0:
return 0
if sum_ == 0:
return 1
return count(sum_ - coins[0], coins) + count(sum_, coins[1:])
if __name__ == "__main__":
import sys
N, M = map(int, sys.stdin.readline().strip().split(' '))
coins = map(int, sys.stdin.readline().strip().split(' '))
print count(N, coins)
Python Solution: Coin Change Challenge
Memoization
count_dict = {}
def count(sum_, coins):
if len(coins) == 0:
return 0
if sum_ < 0:
return 0
if sum_ == 0:
return 1
key = (sum_, tuple(coins))
if key not in count_dict:
count_dict[key] = count(sum_ - coins[0], coins) + count(sum_, coins[1:])
return count_dict[key]
if __name__ == "__main__":
import sys
N, M = map(int, sys.stdin.readline().strip().split(' '))
coins = map(int, sys.stdin.readline().strip().split(' '))
print count(N, coins)
Python Solution: Coin Change Challenge
Dynamic Programming
def count(N, coins):
numWays = [[1] + N * [0] for j in xrange(len(coins) + 1)]
for i in xrange(1, len(coins) + 1):
for j in xrange(1, N + 1):
numWays[i][j] = numWays[i-1][j] + (numWays[i][j - coins[i-1]]
if coins[i-1] <= j else 0)
return numWays[-1][-1]
if __name__ == "__main__":
import sys
N, M = map(int, sys.stdin.readline().strip().split(' '))
coins = map(int, sys.stdin.readline().strip().split(' '))
print count(N, coins)
Reference
Analysis and Design of Algorithms - It takes 4 hours to read the whole book.

More Related Content

What's hot

Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
paramalways
 
Applications of numerical methods
Applications of numerical methodsApplications of numerical methods
Applications of numerical methods
Daffodil International University
 
Sol1
Sol1Sol1
Sol1
berhe1
 
Backtracking based integer factorisation, primality testing and square root c...
Backtracking based integer factorisation, primality testing and square root c...Backtracking based integer factorisation, primality testing and square root c...
Backtracking based integer factorisation, primality testing and square root c...
csandit
 
Introduction to conventional machine learning techniques
Introduction to conventional machine learning techniquesIntroduction to conventional machine learning techniques
Introduction to conventional machine learning techniques
Xavier Rafael Palou
 
Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...
Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...
Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...
Ahmed Gad
 
Binary Class and Multi Class Strategies for Machine Learning
Binary Class and Multi Class Strategies for Machine LearningBinary Class and Multi Class Strategies for Machine Learning
Binary Class and Multi Class Strategies for Machine Learning
Paxcel Technologies
 
Linear Algebra – A Powerful Tool for Data Science
Linear Algebra – A Powerful Tool for Data ScienceLinear Algebra – A Powerful Tool for Data Science
Linear Algebra – A Powerful Tool for Data Science
Premier Publishers
 
A01
A01A01
A01
lksoo
 
Problem solving
Problem solvingProblem solving
Problem solving
hamza239523
 
Classification case study + intro to cnn
Classification case study + intro to cnnClassification case study + intro to cnn
Classification case study + intro to cnn
Vincent Tatan
 
Using sage maths to solve systems of linear equations
Using sage maths to solve systems of linear equationsUsing sage maths to solve systems of linear equations
Using sage maths to solve systems of linear equations
Robert Geofroy
 
Learning multifractal structure in large networks (Purdue ML Seminar)
Learning multifractal structure in large networks (Purdue ML Seminar)Learning multifractal structure in large networks (Purdue ML Seminar)
Learning multifractal structure in large networks (Purdue ML Seminar)
Austin Benson
 
MATLAB Code + Description : Real-Time Object Motion Detection and Tracking
MATLAB Code + Description : Real-Time Object Motion Detection and TrackingMATLAB Code + Description : Real-Time Object Motion Detection and Tracking
MATLAB Code + Description : Real-Time Object Motion Detection and Tracking
Ahmed Gad
 
Algorithm and Data Structures - Basic of IT Problem Solving
Algorithm and Data Structures - Basic of IT Problem SolvingAlgorithm and Data Structures - Basic of IT Problem Solving
Algorithm and Data Structures - Basic of IT Problem Solving
coolpie
 
00 - 30 Dec - Introduction
00 - 30 Dec - Introduction00 - 30 Dec - Introduction
00 - 30 Dec - Introduction
Neeldhara Misra
 
07 dimensionality reduction
07 dimensionality reduction07 dimensionality reduction
07 dimensionality reduction
Marco Quartulli
 
A tour of the top 10 algorithms for machine learning newbies
A tour of the top 10 algorithms for machine learning newbiesA tour of the top 10 algorithms for machine learning newbies
A tour of the top 10 algorithms for machine learning newbies
Vimal Gupta
 
Curse of dimensionality
Curse of dimensionalityCurse of dimensionality
Curse of dimensionality
Nikhil Sharma
 
Probability And Stats Intro2
Probability And Stats Intro2Probability And Stats Intro2
Probability And Stats Intro2
mailund
 

What's hot (20)

Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Applications of numerical methods
Applications of numerical methodsApplications of numerical methods
Applications of numerical methods
 
Sol1
Sol1Sol1
Sol1
 
Backtracking based integer factorisation, primality testing and square root c...
Backtracking based integer factorisation, primality testing and square root c...Backtracking based integer factorisation, primality testing and square root c...
Backtracking based integer factorisation, primality testing and square root c...
 
Introduction to conventional machine learning techniques
Introduction to conventional machine learning techniquesIntroduction to conventional machine learning techniques
Introduction to conventional machine learning techniques
 
Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...
Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...
Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...
 
Binary Class and Multi Class Strategies for Machine Learning
Binary Class and Multi Class Strategies for Machine LearningBinary Class and Multi Class Strategies for Machine Learning
Binary Class and Multi Class Strategies for Machine Learning
 
Linear Algebra – A Powerful Tool for Data Science
Linear Algebra – A Powerful Tool for Data ScienceLinear Algebra – A Powerful Tool for Data Science
Linear Algebra – A Powerful Tool for Data Science
 
A01
A01A01
A01
 
Problem solving
Problem solvingProblem solving
Problem solving
 
Classification case study + intro to cnn
Classification case study + intro to cnnClassification case study + intro to cnn
Classification case study + intro to cnn
 
Using sage maths to solve systems of linear equations
Using sage maths to solve systems of linear equationsUsing sage maths to solve systems of linear equations
Using sage maths to solve systems of linear equations
 
Learning multifractal structure in large networks (Purdue ML Seminar)
Learning multifractal structure in large networks (Purdue ML Seminar)Learning multifractal structure in large networks (Purdue ML Seminar)
Learning multifractal structure in large networks (Purdue ML Seminar)
 
MATLAB Code + Description : Real-Time Object Motion Detection and Tracking
MATLAB Code + Description : Real-Time Object Motion Detection and TrackingMATLAB Code + Description : Real-Time Object Motion Detection and Tracking
MATLAB Code + Description : Real-Time Object Motion Detection and Tracking
 
Algorithm and Data Structures - Basic of IT Problem Solving
Algorithm and Data Structures - Basic of IT Problem SolvingAlgorithm and Data Structures - Basic of IT Problem Solving
Algorithm and Data Structures - Basic of IT Problem Solving
 
00 - 30 Dec - Introduction
00 - 30 Dec - Introduction00 - 30 Dec - Introduction
00 - 30 Dec - Introduction
 
07 dimensionality reduction
07 dimensionality reduction07 dimensionality reduction
07 dimensionality reduction
 
A tour of the top 10 algorithms for machine learning newbies
A tour of the top 10 algorithms for machine learning newbiesA tour of the top 10 algorithms for machine learning newbies
A tour of the top 10 algorithms for machine learning newbies
 
Curse of dimensionality
Curse of dimensionalityCurse of dimensionality
Curse of dimensionality
 
Probability And Stats Intro2
Probability And Stats Intro2Probability And Stats Intro2
Probability And Stats Intro2
 

Viewers also liked

Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Sahil Kumar
 
11 - 03 Feb - From Recursion to Dynamic Programming
11 - 03 Feb - From Recursion to Dynamic Programming11 - 03 Feb - From Recursion to Dynamic Programming
11 - 03 Feb - From Recursion to Dynamic Programming
Neeldhara Misra
 
Numerical analysis m5 l2slides
Numerical analysis  m5 l2slidesNumerical analysis  m5 l2slides
Numerical analysis m5 l2slides
SHAMJITH KM
 
Better Open Source Enterprise C++ Web Services
Better Open Source Enterprise C++ Web ServicesBetter Open Source Enterprise C++ Web Services
Better Open Source Enterprise C++ Web Services
WSO2
 
Milot Shala - C++ (OSCAL2014)
Milot Shala - C++ (OSCAL2014)Milot Shala - C++ (OSCAL2014)
Milot Shala - C++ (OSCAL2014)
Open Labs Albania
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
Andrey Karpov
 
Fibonacci surce code
Fibonacci surce codeFibonacci surce code
Fibonacci surce code
German Niebles
 
Tora (2 phase itteration, simplex method)
Tora (2 phase itteration, simplex method)Tora (2 phase itteration, simplex method)
Tora (2 phase itteration, simplex method)
Zeeshan Mirza
 
2. lp iterative methods
2. lp   iterative methods2. lp   iterative methods
2. lp iterative methods
Hakeem-Ur- Rehman
 
LINEAR PROGRAMMING Assignment help
LINEAR PROGRAMMING Assignment helpLINEAR PROGRAMMING Assignment help
LINEAR PROGRAMMING Assignment help
john mayer
 
Artificial variable technique big m method (1)
Artificial variable technique big m method (1)Artificial variable technique big m method (1)
Artificial variable technique big m method (1)
ਮਿਲਨਪ੍ਰੀਤ ਔਜਲਾ
 
Two Phase Method- Linear Programming
Two Phase Method- Linear ProgrammingTwo Phase Method- Linear Programming
Two Phase Method- Linear Programming
Manas Lad
 
Big m method
Big m methodBig m method
Big m method
Luckshay Batra
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
Krish_ver2
 
Dynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack ProblemDynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack Problem
Amrita Yadav
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 
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
 
Simplex two phase
Simplex two phaseSimplex two phase
Simplex two phase
Shakti Ranjan
 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
Leslie Samuel
 

Viewers also liked (19)

Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
11 - 03 Feb - From Recursion to Dynamic Programming
11 - 03 Feb - From Recursion to Dynamic Programming11 - 03 Feb - From Recursion to Dynamic Programming
11 - 03 Feb - From Recursion to Dynamic Programming
 
Numerical analysis m5 l2slides
Numerical analysis  m5 l2slidesNumerical analysis  m5 l2slides
Numerical analysis m5 l2slides
 
Better Open Source Enterprise C++ Web Services
Better Open Source Enterprise C++ Web ServicesBetter Open Source Enterprise C++ Web Services
Better Open Source Enterprise C++ Web Services
 
Milot Shala - C++ (OSCAL2014)
Milot Shala - C++ (OSCAL2014)Milot Shala - C++ (OSCAL2014)
Milot Shala - C++ (OSCAL2014)
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
 
Fibonacci surce code
Fibonacci surce codeFibonacci surce code
Fibonacci surce code
 
Tora (2 phase itteration, simplex method)
Tora (2 phase itteration, simplex method)Tora (2 phase itteration, simplex method)
Tora (2 phase itteration, simplex method)
 
2. lp iterative methods
2. lp   iterative methods2. lp   iterative methods
2. lp iterative methods
 
LINEAR PROGRAMMING Assignment help
LINEAR PROGRAMMING Assignment helpLINEAR PROGRAMMING Assignment help
LINEAR PROGRAMMING Assignment help
 
Artificial variable technique big m method (1)
Artificial variable technique big m method (1)Artificial variable technique big m method (1)
Artificial variable technique big m method (1)
 
Two Phase Method- Linear Programming
Two Phase Method- Linear ProgrammingTwo Phase Method- Linear Programming
Two Phase Method- Linear Programming
 
Big m method
Big m methodBig m method
Big m method
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
Dynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack ProblemDynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack Problem
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
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)
 
Simplex two phase
Simplex two phaseSimplex two phase
Simplex two phase
 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
 

Similar to Algorithms practice and problem solving - dynamic programming

Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
Dr. C.V. Suresh Babu
 
Algorithm_Dynamic Programming
Algorithm_Dynamic ProgrammingAlgorithm_Dynamic Programming
Algorithm_Dynamic Programming
Im Rafid
 
Design and Analysis of Algorithm Brute Force 1.ppt
Design and Analysis of Algorithm Brute Force 1.pptDesign and Analysis of Algorithm Brute Force 1.ppt
Design and Analysis of Algorithm Brute Force 1.ppt
moiza354
 
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 prasintation eaisy
Dynamic programming prasintation eaisyDynamic programming prasintation eaisy
Dynamic programming prasintation eaisy
ahmed51236
 
Dynamicpgmming
DynamicpgmmingDynamicpgmming
Dynamicpgmming
Muhammad Wasif
 
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
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdf
pasinduneshan
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
reddyprasad reddyvari
 
Understanding Basics of Machine Learning
Understanding Basics of Machine LearningUnderstanding Basics of Machine Learning
Understanding Basics of Machine Learning
Pranav Ainavolu
 
DAA UNIT 3
DAA UNIT 3DAA UNIT 3
DAA UNIT 3
SURBHI SAROHA
 
Types of Algorithms.ppt
Types of Algorithms.pptTypes of Algorithms.ppt
Types of Algorithms.ppt
ALIZAIB KHAN
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
jannatulferdousmaish
 
chapter 1
chapter 1chapter 1
chapter 1
yatheesha
 
Algorithm hierarchy
Algorithm hierarchyAlgorithm hierarchy
Algorithm hierarchy
Changyu Yang
 
Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design Patterns
Ashwin Shiv
 
Class 26: Objectifying Objects
Class 26: Objectifying ObjectsClass 26: Objectifying Objects
Class 26: Objectifying Objects
David Evans
 
Dynamic Programming.pptx
Dynamic Programming.pptxDynamic Programming.pptx
Dynamic Programming.pptx
MuktarHossain13
 
Solving Optimization Problems using the Matlab Optimization.docx
Solving Optimization Problems using the Matlab Optimization.docxSolving Optimization Problems using the Matlab Optimization.docx
Solving Optimization Problems using the Matlab Optimization.docx
whitneyleman54422
 

Similar to Algorithms practice and problem solving - dynamic programming (20)

Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
 
Algorithm_Dynamic Programming
Algorithm_Dynamic ProgrammingAlgorithm_Dynamic Programming
Algorithm_Dynamic Programming
 
Design and Analysis of Algorithm Brute Force 1.ppt
Design and Analysis of Algorithm Brute Force 1.pptDesign and Analysis of Algorithm Brute Force 1.ppt
Design and Analysis of Algorithm Brute Force 1.ppt
 
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 prasintation eaisy
Dynamic programming prasintation eaisyDynamic programming prasintation eaisy
Dynamic programming prasintation eaisy
 
Dynamicpgmming
DynamicpgmmingDynamicpgmming
Dynamicpgmming
 
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
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
 
complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdf
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
 
Understanding Basics of Machine Learning
Understanding Basics of Machine LearningUnderstanding Basics of Machine Learning
Understanding Basics of Machine Learning
 
DAA UNIT 3
DAA UNIT 3DAA UNIT 3
DAA UNIT 3
 
Types of Algorithms.ppt
Types of Algorithms.pptTypes of Algorithms.ppt
Types of Algorithms.ppt
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
 
chapter 1
chapter 1chapter 1
chapter 1
 
Algorithm hierarchy
Algorithm hierarchyAlgorithm hierarchy
Algorithm hierarchy
 
Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design Patterns
 
Class 26: Objectifying Objects
Class 26: Objectifying ObjectsClass 26: Objectifying Objects
Class 26: Objectifying Objects
 
Dynamic Programming.pptx
Dynamic Programming.pptxDynamic Programming.pptx
Dynamic Programming.pptx
 
Solving Optimization Problems using the Matlab Optimization.docx
Solving Optimization Problems using the Matlab Optimization.docxSolving Optimization Problems using the Matlab Optimization.docx
Solving Optimization Problems using the Matlab Optimization.docx
 

Recently uploaded

Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
manishkhaire30
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
Bill641377
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
Timothy Spann
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
Lars Albertsson
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
jitskeb
 
Intelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicineIntelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicine
AndrzejJarynowski
 
The Ipsos - AI - Monitor 2024 Report.pdf
The  Ipsos - AI - Monitor 2024 Report.pdfThe  Ipsos - AI - Monitor 2024 Report.pdf
The Ipsos - AI - Monitor 2024 Report.pdf
Social Samosa
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
apvysm8
 
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
xclpvhuk
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Kiwi Creative
 
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data LakeViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
Walaa Eldin Moustafa
 
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docxDATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
SaffaIbrahim1
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
Sachin Paul
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
v7oacc3l
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
ihavuls
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
vikram sood
 
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
nuttdpt
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
Social Samosa
 
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
nyfuhyz
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 

Recently uploaded (20)

Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
 
Intelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicineIntelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicine
 
The Ipsos - AI - Monitor 2024 Report.pdf
The  Ipsos - AI - Monitor 2024 Report.pdfThe  Ipsos - AI - Monitor 2024 Report.pdf
The Ipsos - AI - Monitor 2024 Report.pdf
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
 
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
 
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data LakeViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
 
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docxDATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
 
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
 
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 

Algorithms practice and problem solving - dynamic programming

  • 2. Meet the Organizers Xochitl Watts Twitter: @XochitlWatts https://www.a2omini.com Kelly KappJennifer Sweezey
  • 3. What are algorithms? (In mathematics, computing, and related subjects) An algorithm is an effective method for solving a problem using a finite sequence of actions to be performed. - Wikipedia
  • 4. Essentials to Solving a Problem A data structure holds data and allows a specific set of operations to be performed on a data set. Data Structures 1) Record 2) Linked List 3) Stack 4) Queue 5) Set 6) Map 7) Graph 8) Tree 9) Heaps Algorithm Design 1) Divide and Conquer 2) Greedy Method 3) Dynamic Programming 4) Graph traversal methods 5) Branch and Bound
  • 5. Dynamic Programming (DP) 1) DP builds its solution by constructing solutions to smaller instances of the same problem. 2) A problem has an optimal substructure if the sub-solutions of an optimal solution of the problem are optimal solutions for their sub-problems. 3) The problem contains overlapping sub-problems. 4) DP arrives to a solution using bottom up traversal, building the solution to many sub-problems that may or may not be required. Memoize & re-use solutions to subproblems that help solve the problem
  • 6. Example: Compute n-th Fibonacci number Recursion function fib(int n) { If (n <= 2) { return 1; } return fib(n-1) + fib(n-2); } Dynamic Programming long a[] = new long[n + 1]; a[1] = a[2] = 1 for (int i=3; i <=n; i++) { a[i] = a[i-1] + a[i-2]; } return a[n];
  • 7. Memoization Memoization aims to prevent recomputations by storing (memorizing) the return values of the function calls. http://en.wikipedia.org/wiki/Memoization
  • 8. Example: Compute n-th Fibonacci number Recursion Dynamic Programming n Time (MSEC) 10 0 20 1 30 8 40 922 50 113770 n Time (MSEC) 10 0 20 0 30 0 40 0 50 0
  • 9. DP Template - NORA 1) Notation: Develop a mathematical notation that can express any solution and any sub-solution for the problem at hand. 2) Optimality: Prove that the optimal substructure holds - sub-solutions of an optimal solution are optimal solutions for sub-problems. 3) Recurrence: Develop a recurrence relation that relates a solution to its sub-solutions using math notation. 4) Algorithm: Write out the algorithm, iterate over all the parameters of the recurrence relation to compute the results for the actual problem that needed to be solved.
  • 12. Python Solution: Fibonacci Finding Challenge def fibonacci(n): if n < 2: return n if not n in memory.keys(): memory[n] = fibonacci(n-1) + fibonacci(n-2) return memory[n] memory = {} n = int(raw_input()) print(fibonacci(n))
  • 13. Python Solution: Coin Change Challenge Recursion def count(sum_, coins): if len(coins) == 0: return 0 if sum_ < 0: return 0 if sum_ == 0: return 1 return count(sum_ - coins[0], coins) + count(sum_, coins[1:]) if __name__ == "__main__": import sys N, M = map(int, sys.stdin.readline().strip().split(' ')) coins = map(int, sys.stdin.readline().strip().split(' ')) print count(N, coins)
  • 14. Python Solution: Coin Change Challenge Memoization count_dict = {} def count(sum_, coins): if len(coins) == 0: return 0 if sum_ < 0: return 0 if sum_ == 0: return 1 key = (sum_, tuple(coins)) if key not in count_dict: count_dict[key] = count(sum_ - coins[0], coins) + count(sum_, coins[1:]) return count_dict[key] if __name__ == "__main__": import sys N, M = map(int, sys.stdin.readline().strip().split(' ')) coins = map(int, sys.stdin.readline().strip().split(' ')) print count(N, coins)
  • 15. Python Solution: Coin Change Challenge Dynamic Programming def count(N, coins): numWays = [[1] + N * [0] for j in xrange(len(coins) + 1)] for i in xrange(1, len(coins) + 1): for j in xrange(1, N + 1): numWays[i][j] = numWays[i-1][j] + (numWays[i][j - coins[i-1]] if coins[i-1] <= j else 0) return numWays[-1][-1] if __name__ == "__main__": import sys N, M = map(int, sys.stdin.readline().strip().split(' ')) coins = map(int, sys.stdin.readline().strip().split(' ')) print count(N, coins)
  • 16. Reference Analysis and Design of Algorithms - It takes 4 hours to read the whole book.