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

PDF
Anything but simple Mathematica
PDF
Machine Learning Basics
PPTX
Basics of Machine Learning
PPTX
Elements of dynamic programming
PDF
Me 443 1 what is mathematica Erdi Karaçal Mechanical Engineer University of...
PPTX
Cryptography an application of vectors and matrices
PDF
Me 443 2 tour of mathematica Erdi Karaçal Mechanical Engineer University of...
PPTX
sentiment analysis using support vector machine
Anything but simple Mathematica
Machine Learning Basics
Basics of Machine Learning
Elements of dynamic programming
Me 443 1 what is mathematica Erdi Karaçal Mechanical Engineer University of...
Cryptography an application of vectors and matrices
Me 443 2 tour of mathematica Erdi Karaçal Mechanical Engineer University of...
sentiment analysis using support vector machine

What's hot (20)

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

Viewers also liked (19)

PPTX
Dynamic Programming
PDF
11 - 03 Feb - From Recursion to Dynamic Programming
PDF
Numerical analysis m5 l2slides
PDF
Better Open Source Enterprise C++ Web Services
PDF
Milot Shala - C++ (OSCAL2014)
PPTX
Static analysis of C++ source code
TXT
Fibonacci surce code
PDF
Tora (2 phase itteration, simplex method)
PDF
2. lp iterative methods
PPTX
LINEAR PROGRAMMING Assignment help
PPT
Artificial variable technique big m method (1)
PPTX
Two Phase Method- Linear Programming
PPTX
Big m method
PPT
5.3 dynamic programming 03
PPTX
Dynamic Programming-Knapsack Problem
PPT
Lecture 8 dynamic programming
PPTX
dynamic programming complete by Mumtaz Ali (03154103173)
PPT
Simplex two phase
PDF
How to Become a Thought Leader in Your Niche
Dynamic Programming
11 - 03 Feb - From Recursion to Dynamic Programming
Numerical analysis m5 l2slides
Better Open Source Enterprise C++ Web Services
Milot Shala - C++ (OSCAL2014)
Static analysis of C++ source code
Fibonacci surce code
Tora (2 phase itteration, simplex method)
2. lp iterative methods
LINEAR PROGRAMMING Assignment help
Artificial variable technique big m method (1)
Two Phase Method- Linear Programming
Big m method
5.3 dynamic programming 03
Dynamic Programming-Knapsack Problem
Lecture 8 dynamic programming
dynamic programming complete by Mumtaz Ali (03154103173)
Simplex two phase
How to Become a Thought Leader in Your Niche
Ad

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

PPT
Dynamic pgmming
PPTX
Algorithm_Dynamic Programming
PPT
Design and Analysis of Algorithm Brute Force 1.ppt
PPTX
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
PPTX
Dynamic programming prasintation eaisy
PPT
Dynamicpgmming
PPTX
Module 2ppt.pptx divid and conquer method
PDF
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
PDF
complexity analysis.pdf
PPT
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
PPTX
Understanding Basics of Machine Learning
PPTX
DAA UNIT 3
PPT
Types of Algorithms.ppt
PDF
Computer algorithm(Dynamic Programming).pdf
PPT
chapter 1
PDF
BCS401 ADA First IA Test Question Bank.pdf
PDF
Algorithm hierarchy
PPT
Design and Analysis of algorithms unit 1 notes
PPTX
Algorithms Design Patterns
PPTX
Class 26: Objectifying Objects
Dynamic pgmming
Algorithm_Dynamic Programming
Design and Analysis of Algorithm Brute Force 1.ppt
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
Dynamic programming prasintation eaisy
Dynamicpgmming
Module 2ppt.pptx divid and conquer method
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
complexity analysis.pdf
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
Understanding Basics of Machine Learning
DAA UNIT 3
Types of Algorithms.ppt
Computer algorithm(Dynamic Programming).pdf
chapter 1
BCS401 ADA First IA Test Question Bank.pdf
Algorithm hierarchy
Design and Analysis of algorithms unit 1 notes
Algorithms Design Patterns
Class 26: Objectifying Objects

Recently uploaded (20)

PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PDF
Introduction to Data Science and Data Analysis
PDF
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
PPTX
CYBER SECURITY the Next Warefare Tactics
PPTX
retention in jsjsksksksnbsndjddjdnFPD.pptx
PPTX
IMPACT OF LANDSLIDE.....................
PPT
lectureusjsjdhdsjjshdshshddhdhddhhd1.ppt
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
SAP 2 completion done . PRESENTATION.pptx
PDF
Data Engineering Interview Questions & Answers Batch Processing (Spark, Hadoo...
DOCX
Factor Analysis Word Document Presentation
PDF
How to run a consulting project- client discovery
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPTX
Topic 5 Presentation 5 Lesson 5 Corporate Fin
PDF
Votre score augmente si vous choisissez une catégorie et que vous rédigez une...
PPTX
Leprosy and NLEP programme community medicine
PPTX
New ISO 27001_2022 standard and the changes
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked 2025}
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Introduction to Data Science and Data Analysis
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
CYBER SECURITY the Next Warefare Tactics
retention in jsjsksksksnbsndjddjdnFPD.pptx
IMPACT OF LANDSLIDE.....................
lectureusjsjdhdsjjshdshshddhdhddhhd1.ppt
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
SAP 2 completion done . PRESENTATION.pptx
Data Engineering Interview Questions & Answers Batch Processing (Spark, Hadoo...
Factor Analysis Word Document Presentation
How to run a consulting project- client discovery
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Topic 5 Presentation 5 Lesson 5 Corporate Fin
Votre score augmente si vous choisissez une catégorie et que vous rédigez une...
Leprosy and NLEP programme community medicine
New ISO 27001_2022 standard and the changes
Capcut Pro Crack For PC Latest Version {Fully Unlocked 2025}
Qualitative Qantitative and Mixed Methods.pptx
IBA_Chapter_11_Slides_Final_Accessible.pptx

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.