Maximum sum contiguous subsequence Longest common subsequence Matrix chain multiplication All pair shortest path Kna. Dynamic Programming

Similar documents
Dynamic Programming ACM Seminar in Algorithmics

CS483 Design and Analysis of Algorithms

Dynamic Programming. Shuang Zhao. Microsoft Research Asia September 5, Dynamic Programming. Shuang Zhao. Outline. Introduction.

Matrices: 2.1 Operations with Matrices

Activity selection. Goal: Select the largest possible set of nonoverlapping (mutually compatible) activities.

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Dynamic Programming. p. 1/43

Today s Outline. CS 362, Lecture 13. Matrix Chain Multiplication. Paranthesizing Matrices. Matrix Multiplication. Jared Saia University of New Mexico

Matrices and Vectors

General Methods for Algorithm Design

CS100: DISCRETE STRUCTURES. Lecture 3 Matrices Ch 3 Pages:

Section Summary. Sequences. Recurrence Relations. Summations Special Integer Sequences (optional)

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Matrix representation of a linear map

Question Paper Code :

Dynamic Programming (CLRS )

Dynamic Programming. Prof. S.J. Soni

Matrix Arithmetic. j=1

Chapter 8 Dynamic Programming

Lecture 4: An FPTAS for Knapsack, and K-Center

Stage-structured Populations

Dynamic Programming( Weighted Interval Scheduling)

CS325: Analysis of Algorithms, Fall Final Exam

Introduction. I Dynamic programming is a technique for solving optimization problems. I Key element: Decompose a problem into subproblems, solve them

1 Assembly Line Scheduling in Manufacturing Sector

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Lecture 18: More NP-Complete Problems

Matrix representation of a linear map

CSE 431/531: Analysis of Algorithms. Dynamic Programming. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Note that M i,j depends on two entries in row (i 1). If we proceed in a row major order, these two entries will be available when we are ready to comp

Discrete Optimization 2010 Lecture 10 P, N P, and N PCompleteness

CS Analysis of Recursive Algorithms and Brute Force

Section Summary. Sequences. Recurrence Relations. Summations. Examples: Geometric Progression, Arithmetic Progression. Example: Fibonacci Sequence

Matrix Multiplication

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Kevin James. MTHSC 3110 Section 2.1 Matrix Operations

Matrices BUSINESS MATHEMATICS

Dynamic Programming: Shortest Paths and DFA to Reg Exps

1. Introduction Recap

Chapter 8 Dynamic Programming

NP-Complete Problems

Section 9.2: Matrices.. a m1 a m2 a mn

Section 9.2: Matrices. Definition: A matrix A consists of a rectangular array of numbers, or elements, arranged in m rows and n columns.

Vector, Matrix, and Tensor Derivatives

Data Structures in Java

DAA Unit- II Greedy and Dynamic Programming. By Mrs. B.A. Khivsara Asst. Professor Department of Computer Engineering SNJB s KBJ COE, Chandwad

Lecture #8: We now take up the concept of dynamic programming again using examples.

Phys 201. Matrices and Determinants

Introduction to Bioinformatics Algorithms Homework 3 Solution

Mathematics 13: Lecture 10

Definition 2.3. We define addition and multiplication of matrices as follows.

Knowledge Discovery and Data Mining 1 (VO) ( )

Introduction to Hash Tables

Lecture 9. Greedy Algorithm

CS583 Lecture 11. Many slides here are based on D. Luebke slides. Review: Dynamic Programming

CS 470/570 Dynamic Programming. Format of Dynamic Programming algorithms:

. a m1 a mn. a 1 a 2 a = a n

Dynamic Programming. Reading: CLRS Chapter 15 & Section CSE 6331: Algorithms Steve Lai

A Fast Asymptotic Approximation Scheme for Bin Packing with Rejection

Lecture 19: Finish NP-Completeness, conp and Friends

CHAPTER 1. Relations. 1. Relations and Their Properties. Discussion

Section Summary. Definition of a Function.

Greedy Algorithms My T. UF

Quick Tour of Linear Algebra and Graph Theory

Quick Tour of Linear Algebra and Graph Theory

Arrays: Vectors and Matrices

Elementary maths for GMT

Determining an Optimal Parenthesization of a Matrix Chain Product using Dynamic Programming

MA2501 Numerical Methods Spring 2015

Algorithms: COMP3121/3821/9101/9801

Limitations of Algorithm Power

The Knapsack Problem. 28. April /44

MATRICES. a m,1 a m,n A =

Matrix Arithmetic. a 11 a. A + B = + a m1 a mn. + b. a 11 + b 11 a 1n + b 1n = a m1. b m1 b mn. and scalar multiplication for matrices via.

CS 598RM: Algorithmic Game Theory, Spring Practice Exam Solutions

Matrices. Chapter Definitions and Notations

Dynamic Programming. Reading: CLRS Chapter 15 & Section CSE 2331 Algorithms Steve Lai

Algorithms for Approximate String Matching

Introduction to algebraic codings Lecture Notes for MTH 416 Fall Ulrich Meierfrankenfeld

Greedy. Outline CS141. Stefano Lonardi, UCR 1. Activity selection Fractional knapsack Huffman encoding Later:

Mat 3770 Bin Packing or

ICS 6N Computational Linear Algebra Matrix Algebra

ME751 Advanced Computational Multibody Dynamics

INSTITIÚID TEICNEOLAÍOCHTA CHEATHARLACH INSTITUTE OF TECHNOLOGY CARLOW MATRICES

MATH 315 Linear Algebra Homework #1 Assigned: August 20, 2018

7.5 Operations with Matrices. Copyright Cengage Learning. All rights reserved.

NP-Complete problems

NP-Completeness. NP-Completeness 1

Math 4377/6308 Advanced Linear Algebra

ACO Comprehensive Exam October 18 and 19, Analysis of Algorithms

Day 1: Introduction to Vectors + Vector Arithmetic

Week 7 Solution. The two implementations are 1. Approach 1. int fib(int n) { if (n <= 1) return n; return fib(n 1) + fib(n 2); } 2.

Lecture 13. More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees.

Countable and uncountable sets. Matrices.

What is Dynamic Programming

Courses 12-13: Dynamic programming

The Cut-Norm and Combinatorial Optimization. Alan Frieze and Ravi Kannan

A FIRST COURSE IN LINEAR ALGEBRA. An Open Text by Ken Kuttler. Matrix Arithmetic

Knapsack and Scheduling Problems. The Greedy Method

Introduction to spectral alignment

Transcription:

Dynamic Programming Arijit Bishnu arijit@isical.ac.in Indian Statistical Institute, India. August 31, 2015

Outline 1 Maximum sum contiguous subsequence 2 Longest common subsequence 3 Matrix chain multiplication 4 All pair shortest path 5 Knapsack

Outline 1 Maximum sum contiguous subsequence 2 Longest common subsequence 3 Matrix chain multiplication 4 All pair shortest path 5 Knapsack

Maximum sum contiguous subsequence Let S be an array, such that S[i] R. We want to find a contiguous subsequence of S with the maximum sum. The problem can be solved with the following recurrence. Exercise S[i] = max {S[i 1] + a[i], a[i]} i, 1 i n 1 Find the indices k and l, 1 k l n, such that l maximum. i=k S[i] is

Outline 1 Maximum sum contiguous subsequence 2 Longest common subsequence 3 Matrix chain multiplication 4 All pair shortest path 5 Knapsack

Longest common subsequence (LCS) Let A = {a 1, a 2,..., a n } and B = {b 1, b 2,..., b m } be two strings over an alphabet set Σ. A subsequence of A is of the form a i1, a i2,..., a ik, where each i j is between 1 and n and 1 i 1 < i 2 < < i k n. Let L[i, j] denote the length of the LCS of {a 1, a 2,..., a i } and {b 1, b 2,..., b j }. Then, L[i, j] can be expressed as 0 if i = 0 or j = 0; L[i, j] = L[i 1, j 1] + 1 if a i = b j, i, j > 0 max{l[i 1, j], L[i, j 1]} if a i b j, i, j > 0 The final answer is L[n, m]. Exercise Find the subsequences of A and B that achieve the LCS.

Outline 1 Maximum sum contiguous subsequence 2 Longest common subsequence 3 Matrix chain multiplication 4 All pair shortest path 5 Knapsack

Matrix chain multiplication We have to find the matrix product M = M 1 M 2 M k M k+1 M k+2 M n, where M k is of size r k r k+1. Let C[i, j] be the minimum cost of multiplying the matrices M i M j in terms of scalar multiplications. We denote M i M j as M i,j. We can write M ij = M i,k 1 M k,j for i < k < j. C[i, j] = min i k j {C[i, k 1] + C[k, j] + r i r k r j+1 } C[1, n] is the final answer. Exercise Find out how to compute C[i, j].

Outline 1 Maximum sum contiguous subsequence 2 Longest common subsequence 3 Matrix chain multiplication 4 All pair shortest path 5 Knapsack

All pair shortest path Let G = (V, E) be a directed graph with weights on each edge. Let V = {1,..., n}. We want to find the distance among all vertices of G. Let i, j V and dij k be the length of the shortest path from i to j that does not pass through {k + 1, k + 2,..., n}. So, dij n is the final answer. { l[i, j] if k = 0 d k ij = { min d k 1 ij, d k 1 ik + d k 1 kj } if 1 k n

Outline 1 Maximum sum contiguous subsequence 2 Longest common subsequence 3 Matrix chain multiplication 4 All pair shortest path 5 Knapsack

Knapsack Let U = {u 1,..., u n } be a set of n items to be packed inside a knapsack of capacity C, C Z +. For 1 i n, let s i and v i be the size and value of the i-th item, where s i, v i Z +. We want to fill the knapsack with some items from U whose total size is at most C and the sum of the values of the items in the knapsack is maximized. Let V [i, j] denote the value obtained by filling a knapsack of size j with items taken from the first i items, {u 1,..., u i } in an optimal way. 0 if i = 0 or j = 0 V [i, j] = V [i 1, j] if j < s i max {V [i 1, j], V [i 1, j s i ] + v i } if i > 0 and j s i V [n, C] is the final answer. Exercise Find the subset of U that gives the optimal answer.