Dynamic Programming cont. We repeat: The Dynamic Programming Template has three parts.

Similar documents
Optimization Methods. Lecture 16: Dynamic Programming

IEOR E4004: Introduction to OR: Deterministic Models

CMPSCI 311: Introduction to Algorithms Second Midterm Practice Exam SOLUTIONS

June 11, Dynamic Programming( Weighted Interval Scheduling)

UNIT 2. Greedy Method GENERAL METHOD

Homework solutions, Chapter 8

0/1 knapsack problem knapsack problem

Maximum Contiguous Subsequences

Essays on Some Combinatorial Optimization Problems with Interval Data

Advanced Operations Research Prof. G. Srinivasan Department of Management Studies Indian Institute of Technology, Madras

CSE 417 Dynamic Programming (pt 2) Look at the Last Element

THE TRAVELING SALESMAN PROBLEM FOR MOVING POINTS ON A LINE

Programming for Engineers in Python

Advanced Operations Research Prof. G. Srinivasan Dept of Management Studies Indian Institute of Technology, Madras

Dynamic Programming: An overview. 1 Preliminaries: The basic principle underlying dynamic programming

6.231 DYNAMIC PROGRAMMING LECTURE 3 LECTURE OUTLINE

Lecture 4: Divide and Conquer

Martingale Pricing Theory in Discrete-Time and Discrete-Space Models

Handout 8: Introduction to Stochastic Dynamic Programming. 2 Examples of Stochastic Dynamic Programming Problems

Problem Set 2: Answers

Lecture 10: The knapsack problem

Chapter wise Question bank

Handout 4: Deterministic Systems and the Shortest Path Problem

On the Optimality of a Family of Binary Trees Techical Report TR

Dynamic Programming (DP) Massimo Paolucci University of Genova

Chapter 15: Dynamic Programming

Introduction to Dynamic Programming

Problem Set 7. Problem 7-1.

Binary Decision Diagrams

Sublinear Time Algorithms Oct 19, Lecture 1

UGM Crash Course: Conditional Inference and Cutset Conditioning

Binary Decision Diagrams

arxiv: v1 [q-fin.rm] 1 Jan 2017

What is Greedy Approach? Control abstraction for Greedy Method. Three important activities

Sandringham School Sixth Form. AS Maths. Bridging the gap

More Advanced Single Machine Models. University at Buffalo IE661 Scheduling Theory 1

Iteration. The Cake Eating Problem. Discount Factors

Fibonacci Heaps Y Y o o u u c c an an s s u u b b m miitt P P ro ro b blle e m m S S et et 3 3 iin n t t h h e e b b o o x x u u p p fro fro n n tt..

6.231 DYNAMIC PROGRAMMING LECTURE 3 LECTURE OUTLINE

Heaps. Heap/Priority queue. Binomial heaps: Advanced Algorithmics (4AP) Heaps Binary heap. Binomial heap. Jaak Vilo 2009 Spring

SET 1C Binary Trees. 2. (i) Define the height of a binary tree or subtree and also define a height balanced (AVL) tree. (2)

Yao s Minimax Principle

Lecture outline W.B.Powell 1

CSE 100: TREAPS AND RANDOMIZED SEARCH TREES

Lecture 2 Dynamic Equilibrium Models: Three and More (Finite) Periods

1) S = {s}; 2) for each u V {s} do 3) dist[u] = cost(s, u); 4) Insert u into a 2-3 tree Q with dist[u] as the key; 5) for i = 1 to n 1 do 6) Identify

Heaps

Making Decisions. CS 3793 Artificial Intelligence Making Decisions 1

CSE 21 Winter 2016 Homework 6 Due: Wednesday, May 11, 2016 at 11:59pm. Instructions

Family Vacation. c 1 = c n = 0. w: maximum number of miles the family may drive each day.

COSC 311: ALGORITHMS HW4: NETWORK FLOW

Computational Finance Least Squares Monte Carlo

Reinforcement Learning. Slides based on those used in Berkeley's AI class taught by Dan Klein

Advanced Algorithmics (4AP) Heaps

Lecture 2: Making Good Sequences of Decisions Given a Model of World. CS234: RL Emma Brunskill Winter 2018

Homework #4. CMSC351 - Spring 2013 PRINT Name : Due: Thu Apr 16 th at the start of class

Slides credited from Hsu-Chun Hsiao

Final exam solutions

The Agent-Environment Interface Goals, Rewards, Returns The Markov Property The Markov Decision Process Value Functions Optimal Value Functions

Lecture 17: More on Markov Decision Processes. Reinforcement learning

Recall: Data Flow Analysis. Data Flow Analysis Recall: Data Flow Equations. Forward Data Flow, Again

CS360 Homework 14 Solution

Markov Decision Processes

Integer Solution to a Graph-based Linear Programming Problem

PARELLIZATION OF DIJKSTRA S ALGORITHM: COMPARISON OF VARIOUS PRIORITY QUEUES

a 13 Notes on Hidden Markov Models Michael I. Jordan University of California at Berkeley Hidden Markov Models The model

CSCE 750, Fall 2009 Quizzes with Answers

CEC login. Student Details Name SOLUTIONS

MS-E2114 Investment Science Lecture 4: Applied interest rate analysis

CHAPTER 5: DYNAMIC PROGRAMMING

(8m 2 5m + 2) - (-10m 2 +7m 6) (8m 2 5m + 2) + (+10m 2-7m + 6)

YEAR 12 Trial Exam Paper FURTHER MATHEMATICS. Written examination 1. Worked solutions

Markov Decision Processes

Optimal energy management and stochastic decomposition

LEC 13 : Introduction to Dynamic Programming

Roll No. :... Invigilator s Signature :.. CS/B.TECH(IT)/SEM-5/M(CS)-511/ OPERATIONS RESEARCH AND OPTIMIZATION TECHNIQUES

Geometric Sequences Ans

Stochastic Optimal Control

EE365: Markov Decision Processes

Supply Chains: Planning with Dynamic Demand

1 Shapley-Shubik Model

Finding optimal arbitrage opportunities using a quantum annealer

Molecular Phylogenetics

Trust Region Methods for Unconstrained Optimisation

CS 188: Artificial Intelligence

Notes on the EM Algorithm Michael Collins, September 24th 2005

2 all subsequent nodes. 252 all subsequent nodes. 401 all subsequent nodes. 398 all subsequent nodes. 330 all subsequent nodes

CS134: Networks Spring Random Variables and Independence. 1.2 Probability Distribution Function (PDF) Number of heads Probability 2 0.

6.231 DYNAMIC PROGRAMMING LECTURE 8 LECTURE OUTLINE

A Formal Study of Distributed Resource Allocation Strategies in Multi-Agent Systems

Algorithmic Game Theory and Applications. Lecture 11: Games of Perfect Information

Course Information and Introduction

CS221 / Spring 2018 / Sadigh. Lecture 7: MDPs I

Markov Decision Processes (MDPs) CS 486/686 Introduction to AI University of Waterloo

Lecture 7: MDPs I. Question. Course plan. So far: search problems. Uncertainty in the real world

CSE 316A: Homework 5

White-Box Testing Techniques I

Financial Optimization ISE 347/447. Lecture 15. Dr. Ted Ralphs

Reinforcement Learning

White-Box Testing Techniques I

Transcription:

Page 1 Dynamic Programming cont. We repeat: The Dynamic Programming Template has three parts. Subproblems Sometimes this is enough if the algorithm and its complexity is obvious. Recursion Algorithm Must not compute subproblems more than once Sometimes we just need the values of the solutions. Sometimes we also need the structure of the solution.

Page 2 Let us return to the shortest path problem. Is Dijkstra's algorithm a DPalgorithm? We have subproblems d[u] = length of shortest path from s to u. We have a type of recursion d[v] = d[u] + w[u,v] The problem is that we don't have a simple way of ordering the subproblems. In that sense, Dijkstra's algorithm isn't a true DP-algorithm. If we have a directed graph with no cycles ( A DAG = Directed Acyclic Graph ) things are simpler. In a DAG we can find a so called Topological Ordering. Topological Ordering: An ordering of the nodes such that (v[i], v[j]) is an edge i < j A topological ordering can be found in time O( E ) ( See textbook). Let's assume that the start node is v[1]. Set w[i,j] = if there is no edge (v[i], v[j]). Then d[1] = 0 d[k] = min ( d[i] + w[i,k] ) 1 i k The algorithm runs in O( n 2 )

Subset Sum Page 3: SS 1 We assume that we have n positive integers a[1], a[2],..., a[n]. We are given an integer M. We want to know if there is a subset of the integers with sum M. What are the natural subproblems here? We can try to get the sum M by using fewer than n integers. Or we can try to get a smaller sum than M. In fact, we will combine these two ideas. Set v[i,m] = 1 if there is a subset of a[1], a[2],..., a[i] with sum m and v[i,m] = 0 otherwise. If v[i,m] = 1 it must be either because we can get m just by using the numbers a[1], a[2],..., a[i-1] or because we can get the sum m - a[i] by using the same numbers. We get the recursion v[1, 0] = 1 For all i such that 2 i n and all m M such that a[i] m v[i,m] = max ( v[i-1,m], v[i-1,m-a[i]] ) We now try to construct an algorithm. We have to order the subproblems. We compute all v[i,m] by running an outer loop over 1 i n and an inner loop over 1 m M.

Page 4: SS 2 Set all v[i,j] = 0 For i 1 to n v[i,0] 1 For i 2 to n For m 1 to M If v[i-1, m] = 1 v[i,m] 1 Else If m > a[i] and v[i-1, m-a[i]] = 1 v[i, m] 1 Return v[n,m] When the algorithm stops, the value of v[n,m] tells us the solution to the problem. ( 1 = " It's possible", 0 = " It's not possible".) The complexity is O(n M).

Page 11 5 In Dynamic Programming-problems we have some value that we want In to Dynamic optimize. Programming- We express problems this value we with some some value array we want like to v[n] optimize. and try We to express find a recursion this value with formula some and array then like use v[n] it and to find the try all to values find a v[i]. recursion In some formula and then use it to find all values of v[i]. In some situations we just want cases this is all we want. In other cases we might want to find the actual to find these values. But the problem could also be about finding the actual "choices" leading to these values. If we have an algorithm which solves "choises" leeding to these values. If we have an algorithm which solves the the recursion equation we can often modify it so that it gives us the recursion equation, we can modify it so that it also gives us the actual choices. actual choices. For instance, in the previous problem we wanted to to find the values v[i,m]. If we know that v[i,m] = 1 and also want to find the terms in the sum, we can modify our algorithm: Set all v[i,j] = 0 For i 1 to n v[i,0] 1 choose[i,0] FALSE p[i,0] NULL For i 2 to n For m 1 to M If v[i-1, m] = 1 v[i,m] 1 choose[i,0] FALSE p[i,m] [i-1,m] Else If m > a[i] and v[i-1, m-a[i]] = 1 v[i, m] 1 choose[i,m] TRUE p[i,m] [i-1,m-a[i]] Return v[n,m] New

A simpler Subset Sum problem Page 6 One thing that makes the original Subset Sum problem hard is that we are allowed to use each number just once. If we can use the numbers multiple times we get a simpler DP-problem. Set v[m] = 1 if we can get m as a subset sum and 0 otherwise. Then we can compute the values by v[m] = 0 for all m < 0 v[0] = 1 v[m] = max ( v[m-a[k] ) 1 k n k

Page 7 We will return to the Subset Sum problem once more. Remember that we defined v[i,m] = 1 if there is a subset of a[1], a[2],..., a[i] with sum m and v[i,m] = 0 otherwise. We got the recursion formula v[1, 0] = 1 For all i such that 2 i n and all m M such that a[i] m v[i,m] = max ( v[i-1,m], v[i-1,m-a[i]] ) In lecture 5 we gave an algorithm that solved the problem. It's possible to give a recursive algorithm as well. A first try could look like: vrek[i,m] = If m < 0 Return 0 If m = 0 Return 1 If i = 1 and m = a[1] Return 1 If vrek[i-1, m] = 1 Return 1 If vrek[i-1, m-a[i]] = 1 Return 1 Return 0 We make the call vrek[n,m] to get the answer. But this solution is no good. The problem is that the algorithm uses repeated calls to subproblems that already have been solved.

Page 8 To get a better algorithm will have to keep track of all computed values of subproblems. To do this, we use an array comp[i,m], Set all comp[i,j] to FALSE Set all v[i,j] to 0 vrek[n,m] vrek[i,m] = If comp[i,m] Return v[i,m] If m < 0 Return 0 If m = 0 Return 1 If vrek[i-1, m] = 1 comp[i,m] TRUE v[i,m] 1 Return 1 If vrek[i-1, m-a[i]] = 1 comp[i,m] TRUE v[i,m] 1 Return 1 comp[i,m] TRUE v[i,m] 0 This technique of remembering already computed values is called Memoization. Sometimes it can be useful, but in most cases the bottom-up method should be preferred.

Matrix Chain Multiplication Page 9 We want to compute the product of two matrices A and B. A isa p xq- matrix and B is a q r-matrix. The cost (number of products of elements) is pqr. Let us assume that we want to compute a chain of matrices. We want to find the best way to multiply them. If we have three matrices A, B, C then we know from the associative law of multiplication that (AB)C = A(BC). But the costs of computing the product will normally differ! If we have a chain of matrices M[1] M[2]... M[n] what is the best way of computing the product? Subproblems: Set c[i,j] = smallest possible cost of computing M[i] M[i+1]... M[j]. Recursion: Let us first assume that the matrices have dimensions d[0] xd[1], d[1] xd[2],...,d[n-1] xd[n]. c[i,i] = 0 for all 1 i n c[i,j] = min ( c[i,k] + c[k+1,j] + d[i-1]d[k]d[j] ) where i k < j k

Why? Page 10 (M[i] M[i+1]... M[k] ) (M[k+1]... M[j]) c[i,k] operations d[i-1] d[k] - matrix c[k+1, j] operations d[k] d[j] - matrix d[i-1] d[k] d[j] operations All together: c[i,k] + c[k+1] + d[i-1] d[k] d[j] operations Now we have to find an algorithm using the recursion. Essentially we have to find suitable loops. We can try to first compute all c[i,j] with j-i = 1, then with j-i = 2 and so on. If we do this we are able to use the recursion formula.

For i 1 to n c[i,i] 0 For diff 1 to n-1 For i 1 to n - diff j i + diff min c[i+1,j] + d[i-1] d[i+1] d[j] best_k i For k i+1 to j - 1 If min > c[i,k] + c[k+1,j] + d[i-1] d[k] d[j] min c[i,k] + c[k+1,j] + d[i-1] d[k] d[j] best_k k c[i,j] min break[i,j] best_k Page 11 The value of c[1,n] gives the minimum number of operations. 3 The values of break[i,j] tells how the split should be done. The complexity is O(n )

Pretty Print Page 12 We have a set of n words. They have lengths l[i] ( number of characters). We want to print them on a page. Each line on the page contains space for M characters. There must be a space 1 between each pair of words. Set s[i,j] = l[k] + j - i. This will be the number if characters left on the line if the words i to j are put on the line. Let E = M - s[i,j] be the excess of space on the line. We want to put the words (in correct order) on lines so that the excesses are as small as possible. We can use a penalty function f ( ) and try to make a split of the words such that f( E1) + f(e2) +... i.e. the sum of the penalties from the lines is as small as possible. It's natural to use the Last Line Excluded rule (LLE), i.e. we give no penalty for excess on the last line. We now want to find the best way to arrange the words. It's simplest to first ignore LLE. Let w[k] = least penalty when using the first k words and not using LLE. Recursion: w[0] = 0 w[k] = min ( w[i-1] + f( M - s[i,k] ) ) where the min is taken over all 1 i k such that s[i,k] M To get the solution with LLE we compute min w[j] such that s[j+1, n] M

Editing distance and sequence alignment Page 13 We have two strings x[1], x[2],..., x[m] and y[1], y[2],..., y[n]. We want to align them so the number of positions where the alligned sequences are different is minimal. We are allowed to put gaps into the sequences. Ex: The sequences EXPONENTIAL and POLYNOMIAL can be aligned as EXPO NENTIAL POLYNOM_IAL Let D[p,q] = distance of best alignment of a[1],..., a[p] and b[1],..., a[q] We measure distance by adding a number α for each match between a character and a blank and adding ß for a match between two different characters. Then we get the recursion formula D[p,0] =αp D[0,q] = αq for all p,q D[p,q] = min ( D[p,q-1] + α, D [p-1,q] + α, D[p-1,q-1] + ß diff[a[p],b[q]] ) if p>1 and q > 1

TSP (Travelling Salesperson Problem) Page 14 Then input is a complete, weighted graph G. The goal is to find the lenght of a shortest cycle visisting every nod exactly once. We can give a DynP-solution to this problem. Let S be a set of nodes such that 1 (node 1) is in S. Assume j is in S. We let C[S, j] be the length of a shortest path in S starting at 1 and ending at j anf visiting every node in S exactly once. We can the find the solution to TSP by: C[{1},1] = 1 For k = 2 to n For all subsets S of {1,..., n} of size k such that 1 belongs to S For all j = 1 in S C[S, j] = min { C[ S-{j}, i] + w[i,j] : i in S and i = 1, j} Return min { C[S, i] + w[i,1] } So the returned value is the length of a shortest cycle visiting every 2 n node exactly once. The time complexity is O(n 2 ).