Dynamic Programming. What is dynamic programming? Break problem into subproblems Work backwards Can use ‘recursion’ ‘Programming’ - a mathematical term.

Slides:



Advertisements
Similar presentations
Dynamic Programming 25-Mar-17.
Advertisements

Dynamic Programming Introduction Prof. Muhammad Saeed.
Pointers.
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
MATH 224 – Discrete Mathematics
Dynamic Programming Rahul Mohare Faculty Datta Meghe Institute of Management Studies.
Counting the bits Analysis of Algorithms Will it run on a larger problem? When will it fail?
Dynamic Programming II By Harry Wiggins. Summary DP is a confusing name for a programming technique that dramatically reduces the runtime from exponential.
Introduction to Algorithms
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
15-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
1 Dynamic Programming Jose Rolim University of Geneva.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Dynamic Programming Optimization Problems Dynamic Programming Paradigm
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
1 Recurrences Algorithms Jay Urbain, PhD Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Dynamic programming  top-down vs. bottom-up  divide & conquer vs. dynamic programming  examples:
Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Télécom 2A – Algo Complexity (1) Time Complexity and the divide and conquer strategy Or : how to measure algorithm run-time And : design efficient algorithms.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Dynamic Programming. Algorithm Design Techniques We will cover in this class: ◦ Greedy Algorithms ◦ Divide and Conquer Algorithms ◦ Dynamic Programming.
DP (not Daniel Park's dance party). Dynamic programming Can speed up many problems. Basically, it's like magic. :D Overlapping subproblems o Number of.
Recursion, pt. 1 The Foundations. What is Recursion? Recursion is the idea of solving a problem in terms of itself. – For some problems, it may not possible.
Honors Track: Competitive Programming & Problem Solving Optimization Problems Kevin Verbeek.
Christopher Moh 2005 Competition Programming Analyzing and Solving problems.
1 Recursion Recursive method –Calls itself (directly or indirectly) through another method –Method knows how to solve only a base case –Method divides.
Dynamic Programming Louis Siu What is Dynamic Programming (DP)? Not a single algorithm A technique for speeding up algorithms (making use of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Topic 25 Dynamic Programming "Thus, I thought dynamic programming was a good name. It was something not even a Congressman could object to. So I used it.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Dynamic Programming. Many problem can be solved by D&C – (in fact, D&C is a very powerful approach if you generalize it since MOST problems can be solved.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
PROBLEM-SOLVING TECHNIQUES Rocky K. C. Chang November 10, 2015.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Alignment, Part II Vasileios Hatzivassiloglou University of Texas at Dallas.
递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be.
Dynamic Programming Sequence of decisions. Problem state.
RECURSION.
Decrease-and-Conquer Approach
Java 4/4/2017 Recursion.
Dynamic Programming.
Dynamic Programming.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Discrete Optimization
Presentation transcript:

Dynamic Programming

What is dynamic programming? Break problem into subproblems Work backwards Can use ‘recursion’ ‘Programming’ - a mathematical term Pascal’s DP Triangle

Comparison to normal recursion Explicitly solve subproblems first Avoid recalculation Not as flexible

Eg: Fibonacci numbers f(1) = f(2) = 1 f(n) = f(n – 1) + f(n – 2) …

Recursive Fibonacci int fibonacci(int n) { if (n <= 2) return 1; return fibonacci(n - 1) + fibonacci(n - 2); }

The problem f(2)f(1) f(3) f(2)f(1) f(3)f(2) f(4) f(5) f(2)f(1) f(3)f(2) f(4) f(2)f(1) f(3) f(2)f(1) f(3)f(2) f(4) f(5) f(6) f(7) Recursion: time vs memory

The solution: variables instead of functions int fibonacci(int n) { int f[...]; f[1] = f[2] = 1; for (int i = 3; i <= n; i++) f[i] = f[n - 1] + f[n - 2]; return f[n]; } f(7) f(6) f(4) f(1) f(5) f(2) f(3) DP: Arrays and ‘for’ loops

Golden Ratio Fibonacci Is the DP solution always the most efficient ?

Drawbacks | Advantages Space – the ‘curse of dimensionality’ Can be conceptually difficult Speed – polynomial (unlike recursion) Coding complexity (compared to full searches) Problems can often be considered from different angles

Different angles – eg. knapsack problem

Reducing dimensions f(7) f(6) f(4) f(1) f(5) f(2) f(3) last two required Not all examples are as obvious as this one… can reduce to constant amount of space instead of linear – removes necessity for dynamic array

Typical applications Find maximum/minimum possible… Calculate the number of ways in which… The time required is exponential and is not severely limited Working forwards yields too many complexities … (Guji paths…) Consider DP instead of recursion or a full search when:

Eg: Subset Sums [Spring 98 USACO] Summary: in how many ways can the set of numbers 1 to n be divided into two subsets with identical sums? Eg with the set {1, 2, 3} there is one way: {1, 2} and {3} since (1 + 2) = (3) Note: {3} and {1, 2} is not counted as another way…

The underlying DP problem:

The recursion Let P[T, N] be the number of possible ways in which a total of T can be reached by adding together numbers up to and including N. (Numbers cannot be used more than once) Thus P[T, N] = P[T, N – 1] + P[T – N, N - 1] More specifically: if there are 2 ways of making 3 using numbers up to 4, then with the use of 5 you know there are 2 ways of making 8, and you can add this to the original 1 way you had of making 8 using numbers up to 4.

The process

You can remove a dimension by keeping a spare; but you can remove the spare if you order is correct

Missing unnecessary information In the previous example we have no idea which combinations could produce those sums; only the number of possible combinations. The fact that the details of the combinations were not specified as necessary for the solution is another clue pointing towards DP. If we were required to calculate the combinations, either a full search or much more complicated DP would be necessary which kept track of all sub-possibilities…

IOI example: number game

Too many possibilities? Work backwards… 2121

21 43 Too many possibilities? Work backwards…

Too many possibilities? Work backwards…

Too many possibilities? Work backwards… First: A; Second: B First: C; Second: D

So… You only need a 2D array which gives the optimum first and second player score for subsequences of different length. But this can be compressed into a 1D array and a spare, and with care the spare can be removed. All thanks to DP!

Why not chess?