Algorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Similar documents
Algorithms (IX) Guoqiang Li. School of Software, Shanghai Jiao Tong University

CS 170 DISCUSSION 8 DYNAMIC PROGRAMMING. Raymond Chan raychan3.github.io/cs170/fa17.html UC Berkeley Fall 17

Chapter 6. Dynamic Programming

Design and Analysis of Algorithms (I)

CS483 Design and Analysis of Algorithms

Dynamic Programming CPE 349. Theresa Migler-VonDollen

Algorithms (I) Introduction. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Chapter 3 Dynamic programming

Unit-5 Dynamic Programming 2016

CS Final - Review material

Dynamic Programming II

Dynamic Programming. Design and Analysis of Algorithms. Entwurf und Analyse von Algorithmen. Irene Parada. Design and Analysis of Algorithms

CSC 373: Algorithm Design and Analysis Lecture 8

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples

Algorithms for Data Science

1 Non greedy algorithms (which we should have covered

We ve done. Now. Next

Exercises Optimal binary search trees root

Chapter 11. Dynamic Programming

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Dynamic Programming Homework Problems

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Dynamic Programming

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2

CS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department

Lecture 8. Dynamic Programming

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015

Dynamic Programming. Lecture Overview Introduction

Introduction to Algorithms I

Steven Skiena. skiena

Optimization II: Dynamic Programming

Algorithm Design Techniques part I

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Longest Common Subsequence, Knapsack, Independent Set Scribe: Wilbur Yang (2016), Mary Wootters (2017) Date: November 6, 2017

So far... Finished looking at lower bounds and linear sorts.

Memoization/Dynamic Programming. The String reconstruction problem. CS124 Lecture 11 Spring 2018

6. Algorithm Design Techniques

15.Dynamic Programming

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK UNIT-III. SUB NAME: DESIGN AND ANALYSIS OF ALGORITHMS SEM/YEAR: III/ II PART A (2 Marks)

/463 Algorithms - Fall 2013 Solution to Assignment 3

Dynamic Programming Homework Problems

Divide and Conquer Strategy. (Page#27)

CS 491 CAP Intermediate Dynamic Programming

Module 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm

CS 231: Algorithmic Problem Solving

Subset sum problem and dynamic programming

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes

CSCE 411 Design and Analysis of Algorithms

Dynamic Programming Algorithms

Greedy Algorithms CHAPTER 16

Notes for Lecture 24

Lecture 3. Brute Force

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors

S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 165

15.4 Longest common subsequence

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

CS473-Algorithms I. Lecture 11. Greedy Algorithms. Cevdet Aykanat - Bilkent University Computer Engineering Department

Design and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 6, 2016 洪國寶

15.4 Longest common subsequence

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

We augment RBTs to support operations on dynamic sets of intervals A closed interval is an ordered pair of real

More Dynamic Programming

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CSE 4/531 Solution 3

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

Recursive-Fib(n) if n=1 or n=2 then return 1 else return Recursive-Fib(n-1)+Recursive-Fib(n-2)

Dynamic Programming. An Enumeration Approach. Matrix Chain-Products. Matrix Chain-Products (not in book)

CSED233: Data Structures (2017F) Lecture12: Strings and Dynamic Programming

PSD1A. DESIGN AND ANALYSIS OF ALGORITHMS Unit : I-V

1 Dynamic Programming

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

1 Variations of the Traveling Salesman Problem

LECTURE 3 ALGORITHM DESIGN PARADIGMS

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Algorithms Interview Questions

Coping with the Limitations of Algorithm Power Exact Solution Strategies Backtracking Backtracking : A Scenario

Chapter 8. NP-complete problems

Algorithms Dr. Haim Levkowitz

Lecture 13: Divide and Conquer (1997) Steven Skiena. skiena

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

Dynamic Programming. CSE 101: Design and Analysis of Algorithms Lecture 19

Framework for Design of Dynamic Programming Algorithms

EDIT DISTANCE. Given two words (strings), how can we define a notion of closeness. For example: Is PELICAN closer to PENGUIN or POLITICIAN?

Dynamic programming II - The Recursion Strikes Back

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Algorithm Design Paradigms

A4B33ALG 2015/10 ALG 10. Dynamic programming. abbreviation: DP. Sources, overviews, examples see.

CSE 417 Branch & Bound (pt 4) Branch & Bound

Search Algorithms. IE 496 Lecture 17

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Chapter 11: Graphs and Trees. March 23, 2008

Algorithm classification

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

CSE 417 Dynamic Programming (pt 4) Sub-problems on Trees

CS1 Lecture 31 Apr. 3, 2019

CS173 Longest Increasing Substrings. Tandy Warnow

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

11. APPROXIMATION ALGORITHMS

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Transcription:

Algorithms IV Dynamic Programming Guoqiang Li School of Software, Shanghai Jiao Tong University

Dynamic Programming

Shortest Paths in Dags, Revisited

Shortest Paths in Dags, Revisited The special distinguishing feature of a dag is that its nodes can be linearized:

Shortest Paths in Dags, Revisited The special distinguishing feature of a dag is that its nodes can be linearized: they can be arranged on a line so that all edges go from left to right. S 1 A 6 B 4 1 2 E 2 4 6 3 S C A B D E 1 1 2 C 3 1 D 1 2

Shortest Paths in Dags, Revisited The special distinguishing feature of a dag is that its nodes can be linearized: they can be arranged on a line so that all edges go from left to right. If we compute these dist values in the left-to-right order, we can always be sure that by the time we get to a node v, we already have all the information we need to compute dist(v). S 1 A 6 B 4 1 2 E 2 4 6 3 S C A B D E 1 1 2 C 3 1 D 1 2

Shortest Paths in Dags, Revisited Initialize all dist(.) value to ; dist(s)=0; for each v V\{s}, in linearized order do dist(v) = min (u,v) E {dist(u) + l(u, v)}; end

Shortest Paths in Dags, Revisited Initialize all dist(.) value to ; dist(s)=0; for each v V\{s}, in linearized order do dist(v) = min (u,v) E {dist(u) + l(u, v)}; end This algorithm is solving a collection of subproblems, {dist(u) u V}

Dynamic Programming This algorithm is solving a collection of subproblems, {dist(u) : u V}.

Dynamic Programming This algorithm is solving a collection of subproblems, {dist(u) : u V}. We start with the smallest of them, dist(s).

Dynamic Programming This algorithm is solving a collection of subproblems, {dist(u) : u V}. We start with the smallest of them, dist(s). We then proceed with progressively larger subproblems, where we are thinking of a subproblem as large if we need to have solved a lot of other subproblems before we can get to it.

Dynamic Programming This algorithm is solving a collection of subproblems, {dist(u) : u V}. We start with the smallest of them, dist(s). We then proceed with progressively larger subproblems, where we are thinking of a subproblem as large if we need to have solved a lot of other subproblems before we can get to it. Dynamic programming is a very powerful algorithmic paradigm in which a problem is solved by identifying a collection of subproblems and tackling them one by one, smallest first, using the answers to small problems to help figure out larger ones, until the whole lot of them is solved.

Dynamic Programming This algorithm is solving a collection of subproblems, {dist(u) : u V}. We start with the smallest of them, dist(s). We then proceed with progressively larger subproblems, where we are thinking of a subproblem as large if we need to have solved a lot of other subproblems before we can get to it. Dynamic programming is a very powerful algorithmic paradigm in which a problem is solved by identifying a collection of subproblems and tackling them one by one, smallest first, using the answers to small problems to help figure out larger ones, until the whole lot of them is solved. In dynamic programming we are not given a dag; the dag is implicit.

Longest Increasing Subsequences

The Problem In the longest increasing subsequence problem,

The Problem In the longest increasing subsequence problem, the input is a sequence of numbers a 1,..., a n.

The Problem In the longest increasing subsequence problem, the input is a sequence of numbers a 1,..., a n. A subsequence is any subset of these numbers taken in order, of the form a i1, a i2,..., a ik where 1 i 1 < i 2 <... < i k n,

The Problem In the longest increasing subsequence problem, the input is a sequence of numbers a 1,..., a n. A subsequence is any subset of these numbers taken in order, of the form a i1, a i2,..., a ik where 1 i 1 < i 2 <... < i k n, and an increasing subsequence is one in which the numbers are getting strictly larger. 5 2 8 6 3 6 9 7

The Problem In the longest increasing subsequence problem, the input is a sequence of numbers a 1,..., a n. A subsequence is any subset of these numbers taken in order, of the form a i1, a i2,..., a ik where 1 i 1 < i 2 <... < i k n, and an increasing subsequence is one in which the numbers are getting strictly larger. The task is to find the increasing subsequence of greatest length. 5 2 8 6 3 6 9 7

Graph Reformulation Let s create a graph of all permissible transitions:

Graph Reformulation Let s create a graph of all permissible transitions: establish a node i for each element a i,

Graph Reformulation Let s create a graph of all permissible transitions: establish a node i for each element a i, and add directed edges (i, j) whenever it is possible for a i and a j to be consecutive elements in an increasing subsequence: i < j and a i < a j 5 2 8 6 3 6 9 7

Graph Reformulation Let s create a graph of all permissible transitions: establish a node i for each element a i, and add directed edges (i, j) whenever it is possible for a i and a j to be consecutive elements in an increasing subsequence: i < j and a i < a j 5 2 8 6 3 6 9 7 This graph G = (V, E) is a dag, since all edges (i, j) have i < j

Graph Reformulation Let s create a graph of all permissible transitions: establish a node i for each element a i, and add directed edges (i, j) whenever it is possible for a i and a j to be consecutive elements in an increasing subsequence: i < j and a i < a j 5 2 8 6 3 6 9 7 This graph G = (V, E) is a dag, since all edges (i, j) have i < j There is a one-to-one correspondence between increasing subsequences and paths in this dag.

Graph Reformulation Let s create a graph of all permissible transitions: establish a node i for each element a i, and add directed edges (i, j) whenever it is possible for a i and a j to be consecutive elements in an increasing subsequence: i < j and a i < a j 5 2 8 6 3 6 9 7 This graph G = (V, E) is a dag, since all edges (i, j) have i < j There is a one-to-one correspondence between increasing subsequences and paths in this dag. Therefore, our goal is simply to find the longest path in the dag!

for j = 1 to n do L(j) = 1 + max{l(i) (i, j) E}; end return(max j L(j)); The Algorithm

The Algorithm for j = 1 to n do L(j) = 1 + max{l(i) (i, j) E}; end return(max j L(j)); L(j) is the length of the longest path - the longest increasing subsequence - ending at j (plus 1, since strictly speaking we need to count nodes on the path, not edges).

The Algorithm for j = 1 to n do L(j) = 1 + max{l(i) (i, j) E}; end return(max j L(j)); L(j) is the length of the longest path - the longest increasing subsequence - ending at j (plus 1, since strictly speaking we need to count nodes on the path, not edges). Any path to node j must pass through one of its predecessors, and therefore L(j) is 1 plus the maximum L(.) value of these predecessors.

The Algorithm for j = 1 to n do L(j) = 1 + max{l(i) (i, j) E}; end return(max j L(j)); L(j) is the length of the longest path - the longest increasing subsequence - ending at j (plus 1, since strictly speaking we need to count nodes on the path, not edges). Any path to node j must pass through one of its predecessors, and therefore L(j) is 1 plus the maximum L(.) value of these predecessors. If there are no edges into j, we take the maximum over the empty set, zero.

The Algorithm for j = 1 to n do L(j) = 1 + max{l(i) (i, j) E}; end return(max j L(j)); L(j) is the length of the longest path - the longest increasing subsequence - ending at j (plus 1, since strictly speaking we need to count nodes on the path, not edges). Any path to node j must pass through one of its predecessors, and therefore L(j) is 1 plus the maximum L(.) value of these predecessors. If there are no edges into j, we take the maximum over the empty set, zero. The final answer is the largest L(j), since any ending position is allowed.

The Analysis Q: How long does this step take?

The Analysis Q: How long does this step take? It requires the predecessors of j to be known;

The Analysis Q: How long does this step take? It requires the predecessors of j to be known; for this the adjacency list of the reverse graph G R, constructible in linear time is handy.

The Analysis Q: How long does this step take? It requires the predecessors of j to be known; for this the adjacency list of the reverse graph G R, constructible in linear time is handy. The computation of L(j) then takes time proportional to the indegree of j, giving an overall running time linear in E.

The Analysis Q: How long does this step take? It requires the predecessors of j to be known; for this the adjacency list of the reverse graph G R, constructible in linear time is handy. The computation of L(j) then takes time proportional to the indegree of j, giving an overall running time linear in E. This is at most O(n 2 ), the maximum being when the input array is sorted in increasing order.

This Is Dynamic Programming In order to solve our original problem, we have defined a collection of subproblems {L(j) 1 j n} with the following key property that allows them to be solved in a single pass:

This Is Dynamic Programming In order to solve our original problem, we have defined a collection of subproblems {L(j) 1 j n} with the following key property that allows them to be solved in a single pass: There is an ordering on the subproblems, and a relation that shows how to solve a subproblem given the answers to smaller subproblems, that is, subproblems that appear earlier in the ordering.

This Is Dynamic Programming In order to solve our original problem, we have defined a collection of subproblems {L(j) 1 j n} with the following key property that allows them to be solved in a single pass: There is an ordering on the subproblems, and a relation that shows how to solve a subproblem given the answers to smaller subproblems, that is, subproblems that appear earlier in the ordering. In our case, each subproblem is solved using the relation L(j) = 1 + max{l(i) (i, j) E} an expression which involves only smaller subproblems.

Why Not Recursion The formula for L(j) also suggests an alternative, recursive algorithm.

Why Not Recursion The formula for L(j) also suggests an alternative, recursive algorithm. Recursion is a very bad idea: the resulting procedure would require exponential time!

Why Not Recursion The formula for L(j) also suggests an alternative, recursive algorithm. Recursion is a very bad idea: the resulting procedure would require exponential time! e.g. (i, j) for all i < j. The formula for subproblem L(j) becomes L(j) = 1 + max{l(1), L(2),..., L(j 1)}

Why Not Recursion The formula for L(j) also suggests an alternative, recursive algorithm. Recursion is a very bad idea: the resulting procedure would require exponential time! e.g. (i, j) for all i < j. The formula for subproblem L(j) becomes L(j) = 1 + max{l(1), L(2),..., L(j 1)} Then why did recursion work so well with divide-and-conquer? The key point is that in divide-and-conquer, a problem is expressed in terms of subproblems that are substantially smaller, say, half the size.

Why Not Recursion The formula for L(j) also suggests an alternative, recursive algorithm. Recursion is a very bad idea: the resulting procedure would require exponential time! e.g. (i, j) for all i < j. The formula for subproblem L(j) becomes L(j) = 1 + max{l(1), L(2),..., L(j 1)} Then why did recursion work so well with divide-and-conquer? The key point is that in divide-and-conquer, a problem is expressed in terms of subproblems that are substantially smaller, say, half the size. In a typical dynamic programming formulation, a problem is reduced to subproblems that are only slightly smaller. Thus the full recursion tree generally has polynomial depth and an exponential number of nodes.

Edit Distance

The problem When a spell checker encounters a possible misspelling, it looks in its dictionary for other words that are close by. S N O W Y S U N N Y Cost: 3 S N O W Y S U N N Y Cost: 5

The problem When a spell checker encounters a possible misspelling, it looks in its dictionary for other words that are close by. Q: What is the appropriate notion of closeness in this case? S N O W Y S U N N Y Cost: 3 S N O W Y S U N N Y Cost: 5

The problem When a spell checker encounters a possible misspelling, it looks in its dictionary for other words that are close by. Q: What is the appropriate notion of closeness in this case? A natural measure of the distance between two strings is the extent to which they can be aligned, or matched up. S N O W Y S U N N Y Cost: 3 S N O W Y S U N N Y Cost: 5

The problem When a spell checker encounters a possible misspelling, it looks in its dictionary for other words that are close by. Q: What is the appropriate notion of closeness in this case? A natural measure of the distance between two strings is the extent to which they can be aligned, or matched up. Technically, an alignment is simply a way of writing the strings one above the other. S N O W Y S U N N Y Cost: 3 S N O W Y S U N N Y Cost: 5

The problem The cost of an alignment is the number of columns in which the letters differ. S N O W Y S U N N Y Cost: 3 S N O W Y S U N N Y Cost: 5

The problem The cost of an alignment is the number of columns in which the letters differ. And the edit distance between two strings is the cost of their best possible alignment. S N O W Y S U N N Y Cost: 3 S N O W Y S U N N Y Cost: 5

The problem The cost of an alignment is the number of columns in which the letters differ. And the edit distance between two strings is the cost of their best possible alignment. Edit distance is so named because it can also be thought of as the minimum number of edits S N O W Y S U N N Y Cost: 3 S N O W Y S U N N Y Cost: 5

The problem The cost of an alignment is the number of columns in which the letters differ. And the edit distance between two strings is the cost of their best possible alignment. Edit distance is so named because it can also be thought of as the minimum number of edits insertions, deletions, and substitutions of characters needed to transform the first string into the second. S N O W Y S U N N Y Cost: 3 S N O W Y S U N N Y Cost: 5

A Dynamic Programming Solution When solving a problem by dynamic programming, the most crucial question is,

A Dynamic Programming Solution When solving a problem by dynamic programming, the most crucial question is, What are the subproblems?

A Dynamic Programming Solution When solving a problem by dynamic programming, the most crucial question is, What are the subproblems? Our goal is to find the edit distance between two strings x[1,..., m] and y[1,..., n].

A Dynamic Programming Solution When solving a problem by dynamic programming, the most crucial question is, What are the subproblems? Our goal is to find the edit distance between two strings x[1,..., m] and y[1,..., n]. For every i, j with 1 i m and 1 j n, let

A Dynamic Programming Solution When solving a problem by dynamic programming, the most crucial question is, What are the subproblems? Our goal is to find the edit distance between two strings x[1,..., m] and y[1,..., n]. For every i, j with 1 i m and 1 j n, let E(i, j): the edit distance between some prefix of the first string, x[1,..., i], and some prefix of the second, y[1,..., j].

A Dynamic Programming Solution When solving a problem by dynamic programming, the most crucial question is, What are the subproblems? Our goal is to find the edit distance between two strings x[1,..., m] and y[1,..., n]. For every i, j with 1 i m and 1 j n, let E(i, j): the edit distance between some prefix of the first string, x[1,..., i], and some prefix of the second, y[1,..., j]. E(i, j) = min{1 + E(i 1, j), 1 + E(i, j 1), diff(i, j) + E(i 1, j 1)}, where diff(i, j) is defined to be 0 if x[i] = y[j] and 1 otherwise.

An Example Edit distance between EXPONENTIAL and POLYNOMIAL, subproblem E(4, 3) corresponds to the prefixes EXPO and POL. The rightmost column of their best alignment must be one of the following: O or Thus, E(4, 3) = min{1 + E(3, 3), 1 + E(4, 2); 1 + E(3, 2)}. (a) i 1 i m j 1 j n GOAL L (b) or O L P O L Y N O M I A L 0 1 2 3 4 5 6 7 8 9 10 E 1 1 2 3 4 5 6 7 8 9 10 X 2 2 2 3 4 5 6 7 8 9 10 P 3 2 3 3 4 5 6 7 8 9 10 O 4 3 2 3 4 5 5 6 7 8 9 N 5 4 3 3 4 4 5 6 7 8 9 E 6 5 4 4 4 5 5 6 7 8 9 N 7 6 5 5 5 4 5 6 7 8 9 T 8 7 6 6 6 5 5 6 7 8 9 I 9 8 7 7 7 6 6 6 6 7 8 A 10 9 8 8 8 7 7 7 7 6 7 L 11 10 9 8 9 8 8 8 8 7 6

The Algorithm for i = 0 to m do E(i, 0) = i; end for j = 1 to n do E(0, j) = j; end for i = 1 to m do for j = 1 to m do E(i, j) = min{1+e(i 1, j), 1+E(i, j 1), diff(i, j)+e(i 1, j 1)}; end end return(e(m, n));

The Algorithm for i = 0 to m do E(i, 0) = i; end for j = 1 to n do E(0, j) = j; end for i = 1 to m do for j = 1 to m do E(i, j) = min{1+e(i 1, j), 1+E(i, j 1), diff(i, j)+e(i 1, j 1)}; end end return(e(m, n)); The over running time is O(m n).

Subproblems of Dynamic Programming Finding the right subproblem takes creativity and experimentation.

Subproblems of Dynamic Programming Finding the right subproblem takes creativity and experimentation. But there are a few standard choices that seem to arise repeatedly in dynamic programming.

Subproblems of Dynamic Programming Finding the right subproblem takes creativity and experimentation. But there are a few standard choices that seem to arise repeatedly in dynamic programming. The input is x 1, x 2,... x n and a subproblem is x 1, x 2,..., x i.

Subproblems of Dynamic Programming Finding the right subproblem takes creativity and experimentation. But there are a few standard choices that seem to arise repeatedly in dynamic programming. The input is x 1, x 2,... x n and a subproblem is x 1, x 2,..., x i.the number of subproblems is therefore linear.

Subproblems of Dynamic Programming Finding the right subproblem takes creativity and experimentation. But there are a few standard choices that seem to arise repeatedly in dynamic programming. The input is x 1, x 2,... x n and a subproblem is x 1, x 2,..., x i.the number of subproblems is therefore linear. The input is x 1,... x n, and y 1,..., y m. A subproblem is x 1,..., x i and y 1,... y j.

Subproblems of Dynamic Programming Finding the right subproblem takes creativity and experimentation. But there are a few standard choices that seem to arise repeatedly in dynamic programming. The input is x 1, x 2,... x n and a subproblem is x 1, x 2,..., x i.the number of subproblems is therefore linear. The input is x 1,... x n, and y 1,..., y m. A subproblem is x 1,..., x i and y 1,... y j. The number of subproblems is O(mn).

Subproblems of Dynamic Programming Finding the right subproblem takes creativity and experimentation. But there are a few standard choices that seem to arise repeatedly in dynamic programming. The input is x 1, x 2,... x n and a subproblem is x 1, x 2,..., x i.the number of subproblems is therefore linear. The input is x 1,... x n, and y 1,..., y m. A subproblem is x 1,..., x i and y 1,... y j. The number of subproblems is O(mn). The input is x 1,..., x n and a subproblem is x i, x i+1,..., x j.

Subproblems of Dynamic Programming Finding the right subproblem takes creativity and experimentation. But there are a few standard choices that seem to arise repeatedly in dynamic programming. The input is x 1, x 2,... x n and a subproblem is x 1, x 2,..., x i.the number of subproblems is therefore linear. The input is x 1,... x n, and y 1,..., y m. A subproblem is x 1,..., x i and y 1,... y j. The number of subproblems is O(mn). The input is x 1,..., x n and a subproblem is x i, x i+1,..., x j. The number of subproblems is O(n 2 ).

Subproblems of Dynamic Programming Finding the right subproblem takes creativity and experimentation. But there are a few standard choices that seem to arise repeatedly in dynamic programming. The input is x 1, x 2,... x n and a subproblem is x 1, x 2,..., x i.the number of subproblems is therefore linear. The input is x 1,... x n, and y 1,..., y m. A subproblem is x 1,..., x i and y 1,... y j. The number of subproblems is O(mn). The input is x 1,..., x n and a subproblem is x i, x i+1,..., x j. The number of subproblems is O(n 2 ). The input is a rooted tree. A subproblem is a rooted subtree.

Knapsack

The Problem During a robbery, a burglar finds much more loot than he had expected and has to decide what to take.

The Problem During a robbery, a burglar finds much more loot than he had expected and has to decide what to take. His bag (or knapsack ) will hold a total weight of at most W pounds.

The Problem During a robbery, a burglar finds much more loot than he had expected and has to decide what to take. His bag (or knapsack ) will hold a total weight of at most W pounds. There are n items to pick from, of weight w 1,..., w n and dollar value v 1,..., v n.

The Problem During a robbery, a burglar finds much more loot than he had expected and has to decide what to take. His bag (or knapsack ) will hold a total weight of at most W pounds. There are n items to pick from, of weight w 1,..., w n and dollar value v 1,..., v n. Q: What s the most valuable combination of items he can put into his bag?

The Problem There are two versions of this problem:

The Problem There are two versions of this problem: there are unlimited quantities of each item available;

The Problem There are two versions of this problem: there are unlimited quantities of each item available; there is one of each item.

The Problem There are two versions of this problem: there are unlimited quantities of each item available; there is one of each item. Neither version of this problem is likely to have a polynomial time algorithm.

The Problem There are two versions of this problem: there are unlimited quantities of each item available; there is one of each item. Neither version of this problem is likely to have a polynomial time algorithm. However, using dynamic programming they can both be solved in O(n W) time, which is reasonable when W is small, but is not polynomial since the input size is proportional to log W rather than W.

Knapsack with Repetition For every w W let K(w) = maximum value achievable with a knapsack of capacity w.

Knapsack with Repetition For every w W let K(w) = maximum value achievable with a knapsack of capacity w. We express this in terms of smaller subproblems: K(w) = max i:w i w {K(w w i) + v i }

Knapsack with Repetition For every w W let K(w) = maximum value achievable with a knapsack of capacity w. We express this in terms of smaller subproblems: K(w) = max i:w i w {K(w w i) + v i } K(0) = 0; for w = 1 to W do K(w) = max i:wi w{k(w w i ) + v i }; end return(k(w));

Knapsack with Repetition For every w W let K(w) = maximum value achievable with a knapsack of capacity w. We express this in terms of smaller subproblems: K(w) = max i:w i w {K(w w i) + v i } K(0) = 0; for w = 1 to W do K(w) = max i:wi w{k(w w i ) + v i }; end return(k(w)); The over running time is O(n W).

Knapsack with Repetition Take W = 10, and Item Weight Value 1 6 $30 2 3 $14 3 4 $16 4 2 $9 and there are unlimited quantities of each item available.

Knapsack without Repetition For every w W and 0 j n,

Knapsack without Repetition For every w W and 0 j n, let K(w, j) = maximum value achievable with a knapsack of capacity w and items 1,, j We express this in terms of smaller subproblems: K(w, j) = max{k(w w j, j 1) + v j, K(w, j 1)}

Knapsack without Repetition For every w W and 0 j n, let K(w, j) = maximum value achievable with a knapsack of capacity w and items 1,, j We express this in terms of smaller subproblems: K(w, j) = max{k(w w j, j 1) + v j, K(w, j 1)} K(0) = 0; for j = 1 to n do for w = 1 to n do if w j > w then K(w, j) = K(w, j 1); K(w, j) = max{k(w w j, j 1) + v j, K(w, j 1)}; end end return(k(w, n));

Knapsack without Repetition For every w W and 0 j n, let K(w, j) = maximum value achievable with a knapsack of capacity w and items 1,, j We express this in terms of smaller subproblems: K(w, j) = max{k(w w j, j 1) + v j, K(w, j 1)} K(0) = 0; for j = 1 to n do for w = 1 to n do if w j > w then K(w, j) = K(w, j 1); K(w, j) = max{k(w w j, j 1) + v j, K(w, j 1)}; end end return(k(w, n)); The over running time is O(n W).

Knapsack without Repetition Take W = 9, and Item Weight Value 1 2 $3 2 3 $4 3 4 $5 4 5 $7 and there is only one of each item available.

Chain Matrix Multiplication

The Problem Suppose that we want to multiply four matrices, A, B, C, D, of dimensions 50 20, 20 1, 1 10, and 10 100, respectively.

The Problem Suppose that we want to multiply four matrices, A, B, C, D, of dimensions 50 20, 20 1, 1 10, and 10 100, respectively. Multiplying an m n matrix by an n p matrix takes m n p multiplications.

The Problem Suppose that we want to multiply four matrices, A, B, C, D, of dimensions 50 20, 20 1, 1 10, and 10 100, respectively. Multiplying an m n matrix by an n p matrix takes m n p multiplications. Parenthesization Cost computation Cost A ((B C) D) 20 1 10 + 20 10 100 + 50 20 100 120, 200 (A (B C)) D 20 1 10 + 50 20 10 + 50 10 100 60, 200 (A B) (C D) 50 20 1 + 1 10 100 + 50 1 100 7, 000

The Problem Suppose that we want to multiply four matrices, A, B, C, D, of dimensions 50 20, 20 1, 1 10, and 10 100, respectively. Multiplying an m n matrix by an n p matrix takes m n p multiplications. Parenthesization Cost computation Cost A ((B C) D) 20 1 10 + 20 10 100 + 50 20 100 120, 200 (A (B C)) D 20 1 10 + 50 20 10 + 50 10 100 60, 200 (A B) (C D) 50 20 1 + 1 10 100 + 50 1 100 7, 000 Q: How do we determine the optimal order, if we want to compute A 1 A 2... A n, where the A i s are matrices with dimensions m 0 m 1, m 1 m 2,..., m n 1 m n, respectively?

Binary Tree A particular parenthesization can be represented by a binary tree in which

Binary Tree A particular parenthesization can be represented by a binary tree in which the individual matrices correspond to the leaves,

Binary Tree A particular parenthesization can be represented by a binary tree in which the individual matrices correspond to the leaves, the root is the final product, and

Binary Tree A particular parenthesization can be represented by a binary tree in which the individual matrices correspond to the leaves, the root is the final product, and interior nodes are intermediate products.

Binary Tree A particular parenthesization can be represented by a binary tree in which the individual matrices correspond to the leaves, the root is the final product, and interior nodes are intermediate products. The possible orders in which to do the multiplication correspond to the various full binary trees with n leaves.

Subproblems For 1 i j n, let C(i, j) = minimum cost of multiplying A i A i+1... A j

Subproblems For 1 i j n, let C(i, j) = minimum cost of multiplying A i A i+1... A j C(i, j) = min i k<j {C(i, k) + C(k + 1, j) + m i 1 m k m j }

The Program

The Program for i = 1 to n do C(i, i) = 0; end for s = 1 to n 1 do for i = 1 to n s do j = i + s; C(i, j) = min i k<j {C(i, k) + C(k + 1, j) + m i 1 m k m j }; end end return(c(1, n));

The Program for i = 1 to n do C(i, i) = 0; end for s = 1 to n 1 do for i = 1 to n s do j = i + s; C(i, j) = min i k<j {C(i, k) + C(k + 1, j) + m i 1 m k m j }; end end return(c(1, n)); The over running time is O(n 3 ).

The Problem Suppose that we want to multiply four matrices, A, B, C, D, of dimensions 50 20, 20 1, 1 10, and 10 100, respectively.

Shortest Paths

Shortest Reliable Paths In a communications network, even if edge lengths faithfully reflect transmission delays, there may be other considerations involved in choosing a path. 1 A 2 B 4 S 5 5 1 T 2 C 3 D 1

Shortest Reliable Paths In a communications network, even if edge lengths faithfully reflect transmission delays, there may be other considerations involved in choosing a path. For instance, each extra edge in the path might be an extra hop fraught with uncertainties and dangers of packet loss. 1 A 2 B 4 S 5 5 1 T 2 C 3 D 1

Shortest Reliable Paths In a communications network, even if edge lengths faithfully reflect transmission delays, there may be other considerations involved in choosing a path. For instance, each extra edge in the path might be an extra hop fraught with uncertainties and dangers of packet loss. We would like to avoid paths with too many edges. 1 A 2 B 4 S 5 5 1 T 2 C 3 D 1

Shortest Reliable Paths Suppose then that we are given a graph G with lengths on the edges, along with two nodes s and t and an integer k,

Shortest Reliable Paths Suppose then that we are given a graph G with lengths on the edges, along with two nodes s and t and an integer k, and we want the shortest path from s to t that uses at most k edges.

Shortest Reliable Paths Suppose then that we are given a graph G with lengths on the edges, along with two nodes s and t and an integer k, and we want the shortest path from s to t that uses at most k edges. For each vertex v and each integer i k, let dist(v, i) = the length of the shortest path from s to v that uses i edges.

Shortest Reliable Paths Suppose then that we are given a graph G with lengths on the edges, along with two nodes s and t and an integer k, and we want the shortest path from s to t that uses at most k edges. For each vertex v and each integer i k, let dist(v, i) = the length of the shortest path from s to v that uses i edges. The starting values dist(v, 0) are for all vertices except s, for which it is 0.

Shortest Reliable Paths Suppose then that we are given a graph G with lengths on the edges, along with two nodes s and t and an integer k, and we want the shortest path from s to t that uses at most k edges. For each vertex v and each integer i k, let dist(v, i) = the length of the shortest path from s to v that uses i edges. The starting values dist(v, 0) are for all vertices except s, for which it is 0. dist(v, i) = min {dist(u, i 1) + l(u, v)} (u,v) E

Shortest Reliable Paths Find out the shortest reliable path from S to T, when k = 3. 1 A 2 B 4 S 5 5 1 T 2 C 3 D 1

The Traveling Salesman Problem A traveling salesman is getting ready for a big sales tour.

The Traveling Salesman Problem A traveling salesman is getting ready for a big sales tour. Starting at his hometown, he will conduct a journey in which each of his target cities is visited exactly once before he returns home. 2 A C 1 3 2 4 2 2 B 2 D 3 4 E

The Traveling Salesman Problem A traveling salesman is getting ready for a big sales tour. Starting at his hometown, he will conduct a journey in which each of his target cities is visited exactly once before he returns home. Q: Given the pairwise distances between cities, what is the best order in which to visit them, so as to minimize the overall distance traveled? 2 A C 1 3 2 4 2 2 B 2 D 3 4 E

The Traveling Salesman Problem A traveling salesman is getting ready for a big sales tour. Starting at his hometown, he will conduct a journey in which each of his target cities is visited exactly once before he returns home. Q: Given the pairwise distances between cities, what is the best order in which to visit them, so as to minimize the overall distance traveled? The brute-force approach is to evaluate every possible tour and return the best one. Since there are (n 1)! possibilities, this strategy takes O(n!) time. 2 A C 1 3 2 4 2 2 B 2 D 3 4 E

The Traveling Salesman Problem Denote the cities by 1,..., n, the salesman s hometown being 1,

The Traveling Salesman Problem Denote the cities by 1,..., n, the salesman s hometown being 1, and let D = (d ij ) be the matrix of intercity distances.

The Traveling Salesman Problem Denote the cities by 1,..., n, the salesman s hometown being 1, and let D = (d ij ) be the matrix of intercity distances. The goal is to design a tour that starts and ends at 1, includes all other cities exactly once, and has minimum total length.

The Subproblems For a subset of cities S {1, 2,..., n} that includes 1, and j S, let C(S, j) be the length of the shortest path visiting each node in S exactly once, starting at 1 and ending at j.

The Subproblems For a subset of cities S {1, 2,..., n} that includes 1, and j S, let C(S, j) be the length of the shortest path visiting each node in S exactly once, starting at 1 and ending at j. When S > 1, we define C(S, 1) =.

The Subproblems For a subset of cities S {1, 2,..., n} that includes 1, and j S, let C(S, j) be the length of the shortest path visiting each node in S exactly once, starting at 1 and ending at j. When S > 1, we define C(S, 1) =. For j 1 with j S we have C(S, j) = min i S:i j C(S\{j}, i) + d ij

Exercise 2 A C 1 3 2 4 2 2 B 2 D 3 4 E

The Program C(1, 1) = 0; for s = 2 to n do for all subsets S {1, 2,..., n} do C(S, 1) = ; for all j S and j 1 do C(S, j) = min i S:i j C(S\{j}, i) + d ij ; end end end return(min j C({1, 2,..., n}, j) + d j1 );

The Program C(1, 1) = 0; for s = 2 to n do for all subsets S {1, 2,..., n} do C(S, 1) = ; for all j S and j 1 do C(S, j) = min i S:i j C(S\{j}, i) + d ij ; end end end return(min j C({1, 2,..., n}, j) + d j1 );

The Program C(1, 1) = 0; for s = 2 to n do for all subsets S {1, 2,..., n} do C(S, 1) = ; for all j S and j 1 do C(S, j) = min i S:i j C(S\{j}, i) + d ij ; end end end return(min j C({1, 2,..., n}, j) + d j1 ); There are at most 2 n n subproblems, and each one takes linear time to solve. The total running time is therefore O(n 2 2 n ).

Independent Sets in Trees

The Problem A subset of nodes S V is an independent set of graph G = (V, E) if there are no edges between them. 1 2 3 4 5 6

The Problem A subset of nodes S V is an independent set of graph G = (V, E) if there are no edges between them. Finding the largest independent set in a graph is believed to be intractable. 1 2 3 4 5 6

The Problem A subset of nodes S V is an independent set of graph G = (V, E) if there are no edges between them. Finding the largest independent set in a graph is believed to be intractable. However, when the graph happens to be a tree, the problem can be solved in linear time, using dynamic programming. 1 2 3 4 5 6

The Subproblems

The Subproblems I(u) = size of largest independent set of subtree hanging from u.

The Subproblems I(u) = size of largest independent set of subtree hanging from u. I(u) = max{1 + I(w), I(w)} grandchildren w of u children w of u

Referred Materials Content of this lecture comes from Section 6 in [DPV07].