CS 331 DESIGN AND ANALYSIS OF ALGORITHMS DYNAMIC PROGRAMMING. Dr. Daisy Tang

Similar documents
Problem Set 9 Solutions

Dynamic Programming. Preview. Dynamic Programming. Dynamic Programming. Dynamic Programming (Example: Fibonacci Sequence)

Outline and Reading. Dynamic Programming. Dynamic Programming revealed. Computing Fibonacci. The General Dynamic Programming Technique

Calculation of time complexity (3%)

Dynamic Programming! CSE 417: Algorithms and Computational Complexity!

Design and Analysis of Algorithms

Math Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Dynamic Programming. Lecture 13 (5/31/2017)

Example: (13320, 22140) =? Solution #1: The divisors of are 1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 27, 30, 36, 41,

Introduction to Algorithms

A 2D Bounded Linear Program (H,c) 2D Linear Programming

Introduction to Algorithms

Chapter 3 Differentiation and Integration

U.C. Berkeley CS294: Beyond Worst-Case Analysis Luca Trevisan September 5, 2017

Finite Difference Method

Computational Biology Lecture 8: Substitution matrices Saad Mneimneh

Lecture 2 Solution of Nonlinear Equations ( Root Finding Problems )

HMMT February 2016 February 20, 2016

Exercises. 18 Algorithms

EEL 6266 Power System Operation and Control. Chapter 3 Economic Dispatch Using Dynamic Programming

College of Computer & Information Science Fall 2009 Northeastern University 20 October 2009

Lectures - Week 4 Matrix norms, Conditioning, Vector Spaces, Linear Independence, Spanning sets and Basis, Null space and Range of a Matrix

2.3 Nilpotent endomorphisms

Lecture Notes Introduction to Cluster Algebra

5 The Rational Canonical Form

Finding Dense Subgraphs in G(n, 1/2)

: Numerical Analysis Topic 2: Solution of Nonlinear Equations Lectures 5-11:

8.4 COMPLEX VECTOR SPACES AND INNER PRODUCTS

NP-Completeness : Proofs

Physics 2A Chapter 3 HW Solutions

Structure and Drive Paul A. Jensen Copyright July 20, 2003

Outline. Communication. Bellman Ford Algorithm. Bellman Ford Example. Bellman Ford Shortest Path [1]

OPTIMISATION. Introduction Single Variable Unconstrained Optimisation Multivariable Unconstrained Optimisation Linear Programming

Errors for Linear Systems

A Simple Research of Divisor Graphs

Combining Constraint Programming and Integer Programming

Chapter Newton s Method

Single-Facility Scheduling over Long Time Horizons by Logic-based Benders Decomposition

Complete subgraphs in multipartite graphs

International Mathematical Olympiad. Preliminary Selection Contest 2012 Hong Kong. Outline of Solutions

For now, let us focus on a specific model of neurons. These are simplified from reality but can achieve remarkable results.

Single Variable Optimization

Problem Do any of the following determine homomorphisms from GL n (C) to GL n (C)?

Absorbing Markov Chain Models to Determine Optimum Process Target Levels in Production Systems with Rework and Scrapping

MAE140 - Linear Circuits - Fall 10 Midterm, October 28

APPENDIX A Some Linear Algebra

Min Cut, Fast Cut, Polynomial Identities

The Second Anti-Mathima on Game Theory

Grover s Algorithm + Quantum Zeno Effect + Vaidman

Problem Set 6: Trees Spring 2018

8.6 The Complex Number System

= = = (a) Use the MATLAB command rref to solve the system. (b) Let A be the coefficient matrix and B be the right-hand side of the system.

CS 770G - Parallel Algorithms in Scientific Computing

Aggregation of Social Networks by Divisive Clustering Method

CISE301: Numerical Methods Topic 2: Solution of Nonlinear Equations

Math 261 Exercise sheet 2

arxiv: v2 [cs.ds] 1 Feb 2017

Lecture 4: Constant Time SVD Approximation

Every planar graph is 4-colourable a proof without computer

Annexes. EC.1. Cycle-base move illustration. EC.2. Problem Instances

Module 9. Lecture 6. Duality in Assignment Problems

Lecture 4: November 17, Part 1 Single Buffer Management

p 1 c 2 + p 2 c 2 + p 3 c p m c 2

Graph Reconstruction by Permutations

Complex Numbers. x = B B 2 4AC 2A. or x = x = 2 ± 4 4 (1) (5) 2 (1)

The Minimum Universal Cost Flow in an Infeasible Flow Network

MAE140 - Linear Circuits - Winter 16 Midterm, February 5

Maximum likelihood. Fredrik Ronquist. September 28, 2005

Compilers. Spring term. Alfonso Ortega: Enrique Alfonseca: Chapter 4: Syntactic analysis

Stanford University CS359G: Graph Partitioning and Expanders Handout 4 Luca Trevisan January 13, 2011

Curve Fitting with the Least Square Method

Homework 9 Solutions. 1. (Exercises from the book, 6 th edition, 6.6, 1-3.) Determine the number of distinct orderings of the letters given:

Dynamic Programming 4/5/12. Dynamic programming. Fibonacci numbers. Fibonacci: a first attempt. David Kauchak cs302 Spring 2012

O-line Temporary Tasks Assignment. Abstract. In this paper we consider the temporary tasks assignment

Formulas for the Determinant

COS 511: Theoretical Machine Learning. Lecturer: Rob Schapire Lecture # 15 Scribe: Jieming Mao April 1, 2013

Linear Feature Engineering 11

Lecture 10: May 6, 2013

Statistical Mechanics and Combinatorics : Lecture III

Transfer Functions. Convenient representation of a linear, dynamic model. A transfer function (TF) relates one input and one output: ( ) system

Difference Equations

An Integrated OR/CP Method for Planning and Scheduling

Tracking with Kalman Filter

U.C. Berkeley CS294: Spectral Methods and Expanders Handout 8 Luca Trevisan February 17, 2016

Assortment Optimization under MNL

Complex Variables. Chapter 18 Integration in the Complex Plane. March 12, 2013 Lecturer: Shih-Yuan Chen

find (x): given element x, return the canonical element of the set containing x;

Complex Numbers Alpha, Round 1 Test #123

Common loop optimizations. Example to improve locality. Why Dependence Analysis. Data Dependence in Loops. Goal is to find best schedule:

a b a In case b 0, a being divisible by b is the same as to say that

Modeling motion with VPython Every program that models the motion of physical objects has two main parts:

CHAPTER 17 Amortized Analysis

ORDINARY DIFFERENTIAL EQUATIONS EULER S METHOD

Lecture 4. Instructor: Haipeng Luo

On the Multicriteria Integer Network Flow Problem

form, and they present results of tests comparng the new algorthms wth other methods. Recently, Olschowka & Neumaer [7] ntroduced another dea for choo

Norms, Condition Numbers, Eigenvalues and Eigenvectors

The L(2, 1)-Labeling on -Product of Graphs

G = G 1 + G 2 + G 3 G 2 +G 3 G1 G2 G3. Network (a) Network (b) Network (c) Network (d)

= s j Ui U j. i, j, then s F(U) with s Ui F(U) G(U) F(V ) G(V )

Transcription:

CS DESIGN ND NLYSIS OF LGORITHMS DYNMIC PROGRMMING Dr. Dasy Tang

Dynamc Programmng Idea: Problems can be dvded nto stages Soluton s a sequence o decsons and the decson at the current stage s based on the results o the prevous stage

Example: Fbonacc Numbers F() F() F( n) F( n ) F( n ) n ssume n = 5 F(5) F () F() F () F () F () F() F () F () F () F () F() F() F() F()

I We Use Dvde-and-Conquer Dvde the problem nto subproblems. Solve each subproblem ndependently and then combne the solutons. For example F() s rst calculated n the let subtree and t wll be calculated agan n the rght subtree Subproblems are solved ndependently

But Dynamc Programmng 5 Works on phases 5 8... Based on the results n prevous phase we can calculate result or the current phase Can be solved n a or-loop F[] = F[] = For to n F[] = F[-] + F[-];

Greedy vs. Dynamc Programmng 6 Consder shortest path problem Suppose we wsh to nd a shortest path rom vertex to vertex j. Let be the vertces adjacent rom vertex. Whch o the vertces n should be the second vertex on the path? There s no way to make a decson at ths tme and guarantee that uture decsons leadng to an optmal sequence can be made. I on the other hand we wsh to nd a shortest path rom vertex to all other vertces n G then at each step a correct decson can be made based on the prncple o optmalty.

Greedy vs. Dynamc Programmng 7 Greedy: For problems that an optmal soluton can be ound by makng the decsons one at a tme and never makng an erroneous decson [e.g. Knapsack] Consder only decson sequence Dynamc programmng: For problems that t s not possble to make stepwse decsons based only on local normaton Consder many decson sequences Try all possbltes but elmnates mpossble cases based on the prncple o optmalty

Prncple o Optmalty 8 Prncple o optmalty: n optmal sequence o decsons has the property that whatever the ntal decson s the remanng decsons must consttute an optmal decson sequence wth regard to the state resultng rom the rst decson elmnates not optmal subsequences

Examples 9 The shortest path problem satses the Prncple o Optmalty Subpath o a shortest path s also a shortest path The longest path problem does not satsy the Prncple o Optmalty Consder a rng graph The subpath o a longest path mght not be a longest path

. Multstage Sngle-Source Sngle- Destnaton Shortest Path Problem The Problem: Gven: m columns (or stages) o n nodes each a source node a destnaton node. ssume edges only exst between stages. Goal: to nd the shortest path rom source ( ) to the destnaton (m- ) Stage number Node number o that stage

Straghtorward vs. Dynamc Programmng Straghtorward way: Try all possble paths Complexty: O( n m ) Dynamc programmng: Let M( j) = length o the shortest path rom the source () to ( j) c( k j) = dstance rom ( k) to (+ j) want to nd M(m- ) Dene unctonal equaton: n M ( j) mn k M () M ( k) c( Shortest path rom source () to destnaton (m-) Dene the relaton between current stage and the result o prevous stage k j)

Example 5 5 Source 5 Destnaton

M () mn M () mn M () mn 5 Remember the prevous node o ths shortest path M () mn 5 7 M () mn 5 5 M () mn M ( ) mn7 5 7 Can be used or constructng the shortest path at the end The shortest path s: ( ) () () () ()

M: node( j) \ stage( ) 7 7 5 lgorthm: assgn ntally M()= M() = M() = M()=; or = to (m ) // stages or j = to n // nodes M ( j) mn k calculate and return M(m ); n M ( k) c( k j)

Tme Complexty 5 There are O(mn) M( j) s to compute each M( j) takes O(n) computaton. So total complexty s O(mn ). total # o nodes n the graph Let N = m n nodes max # o stages max # o nodes n each stage mn = mn n = N n Note: use the Djkstra s algorthm the complexty s O(N )

Revst Dynamc Programmng 6 The problem can be dvded nto stages. Need to make decsons at each stage. The decson or the current stage (based on the results o the prevous stage) s ndependent rom that o the prevous stages. e.g. node o the multstage shortest path problem t s calculated rom m() m() and m() but t s ndependent rom how node ( or ) makes ts decson. Note that we nd shortest paths rom the source to all three nodes and and but some o them may not be used on the shortest path at all. But every decson must be optmal so that the overall result s optmal. Ths s called Prncple o Optmalty.

Problem : ll Pars Shortest Paths 7 The problem: Gven G = (V E) a drected graph wth n nodes Cost a cost adjacency matrx or G: Cost = Cost j = cost o <j> <j> E(G) otherwse Goal: to determne a matrx s.t. j = the length o the shortest path rom to j j n

Use Djkstra s algorthm call t or n tmes: Tme complexty s O(n ) The Dynamc Programmng way: Let jk = the length o the shortest path rom to j gong through no vertex o ndex greater than k Want to nd j n Observe that j = Cost Dene unctonal equaton: jk = mn { j k- k k- + kj k- } 8

9 Example: v v v 6 6 7 mn mn mn 6 mn mn mn 7 6

7 6 6 7 mn mn mn 5 mn 6 mn mn 7 5 6 7 mn mn mn 6 mn 6 mn mn

lgorthm: procedure llpars (n Cost ) // n Cost are nput s output { or to n do or j to n do (j) Cost(j); or k to n do or to n do or j to n do (j) mn {(j) (k)+ (kj)}; } There are O(n) k s to compute each takes O(n ) computaton total complexty o llpar s O(n ).

In order to prnt shortest paths or all pars o vertces we mody the algorthm: procedure llpaths (n Cost P) // n Cost are nput and P are output // P(j) contans the ntermedate node rom to j { or = to n do or j = to n do (j) = Cost(j); P(j) = j; or k = to n do or = to n do or j = to n do ((j) > (k)+ (kj) ) then P(j) = k; (j) = (k)+ (kj) ; or = to n do or j = to n do prnt(); Call PrntOut(P j); } procedure PrntOut (P j) { P(j) = j } then prnt(j); else let k = P(j); call PrntOut(P k); call PrntOut(P k j);

In-Class Exercse # Solve the all pars shortest path problem or the graph wth the ollowng weght matrx. Fnd and P and shortest paths or all pars o vertces. Show steps. = 5 5

Problem : Chaned Matrx Multplcaton The problem: Gven M M... M r (r matrces) [d d ] [d d ] [d r- d r ] Goal: nd the mnmum cost o multplyng r matrces

5 The dea: Dene C( j) = mnmum cost o computng M M +... M j C( ) = C ( ) d d d Want to nd C(r) Consder matrces rom to j : k = M M M...... (the total # o elementary multplcatons )...... M j M j k: dvde-pont k j We can dvde the sequence o matrces rom to j nto two parts wth dvdng pont k where k j. j

6 We dvde t nto two parts wth dvde-pont k j k d k d ] [ j k d d Dene the unctonal equaton: B j k j k d d d j k C k C j C ) ( ) ( mn ) ( Cost o multplyng and B k k k d d d d M M M... j j k k j k d d d d M M...

7 Example: 5 5 8 8 M M M M sze C ) ( sze 5 () 8 5 8 () 8 8 () C C C

sze C() mn ( C() C() 8 5)( C() C() 5 k 78 Smallest C() mn( C() C() 8 )( C() C() 8 5 k Smallest 8 + + ( C() C() 8 ) sze C() mn ( C() C() ) k ( () () 5 C C 8 It has the smallest cost when k =. Remember ths k-value and t can be used or obtanng the optmal soluton. The optmal multplcaton sequence or ths nstance s (M M ) (M M ).

9 lgorthm: procedure MnMult (r D P) // r D are nputs P s output { or to r do C( ) = or d to (r ) do or to (r d) do P C( r); } ; ) ( ) ( ) ( ) ( ) ( mn ) ( ; j D k D D j k C k C j C d j j k

nalyss: Matrx product o # o C(j) s wth ths sze Cost o computng C(j) sze.e. j = + r sze.e. j = + r............ sze r j = + (r ) r Total complexty s O(r ) Total # o C( j) s to compute s O(r ) each C( j) takes O(r) tme n the worst case

Problem : / Knapsack The problem: Gven n objects p p p n prot w w w n weght M = the capacty o the knapsack Goal: nd x x x n s.t. n p s maxmzed subject to x where x = or n w x M

The straght orward way: Try all possbltes. The complexty s O( n ). Example: 6 5 M w w w p p p n x x x x w p x 5 6 6 5 soluton

Does Greedy work? Ths problem can t be solved by Greedy (P / W order) Counterexample: M = 6 P = 5 W = Greedy X = () total prot = 5. Optmal Y = () total prot = 6. The Dynamc Programmng way: Let (X ) = max prot generated rom subject to the capacty X want to nd n (M) Dene unctonal equaton: ( X ) max ( X ) p ( X ) ( X W x x x x... ) x

Example: n = 6 p p p p p5 p6 w w w w w5 w6 5 7 M 65 To nd 6 (65) : 6 (65) max 5 (65) max (65) max (65) max (65) max (65) max (5) max (5) max...... 5 (6 ) max...... 5 (65 ) (65 ) (65 ) (65 ) (65 ) (65) (5) (5 ) (6 ) 5 (6 ) max{6 6} 6 (58) 7 max{6 57} 6 (55 ) max{5 6} 6 (5) max{5 } 5 (5) 5 max{5} 5 (65) (5) (95) 5 max{ 5} (55 ) 7 max{657} 6

The result: x x x x x x 5 6 6 5 5......... Thereore the complexty o / Knapsack s O( n ) 5

M+ n+ j-w j M - - (j-w ) - (j) n +p (j) (M+)(n+) entres and constant tme each O(Mn) 6

Example: M = 6 Object Object Object Object p 8 5 w 7 5 6 +8 7 7 7 7 7+5 8 7 9 Max prot s By tracng whch entres lead to ths max-prot soluton we can obtan the optmal soluton - Object and (or and ).

Problem 5: Travelng Salesman Problem (TSP) 8 The problem: Gven a drected graph G = (V E) wth edge cost tour (or Hamltonan Crcut) o G s dened as drected cycle rom a vertex to tsel that passes through every other vertces exactly once. Goal: nd a tour wth mnmum cost

Example: G: C 5 6 8 8 5 9 9 Tour : [ ] Cost = + 9 + + 8 = 9 Tour : [ ] Cost = 5 + + + 8 = 6 9

The straghtorward way: Ths s a permutaton problem. There are (n-)*(n-)* * combnatons o tours so the complexty s O(n!). The Dynamc Programmng way: ssume startng vertex s wthout loss o generalty. Let g(s) = length o the shortest path startng at vertex gong through all the vertces n S and termnatng at vertex Want to nd g(v {}) Dene unctonal equaton: g(s) = C S = g(s) = C S + C S S = g(s) = mn{ C k + g(k S {k}) } ks S

Use the above example: g g g g mnc g( ) C g C g k mn k mn C g C g k mn 9 8 9 6 5 k mn C g C g k mn 8 8 5 5 k mn C g C g k mn 8 9 6 9 5 k Tour: Cost: 5 5 55 5 There are O(n n ) g(s) s to compute each takes O(n) computaton. Thereore the complexty o TSP by dynamc programmng s O(n n ).

lgorthm: procedure TSP (C mnlength); //assume startng vertex s { or to n do g() = C(); or k to (n-) or all subsets S V {} contanng k vertces or ( such that and S) g(s) = mn ( C(j)+g(jS {j}) ); js path(s) = value o j that gave the mnmum; } g(v {}) mn ( C(j)+g(jV {j}) ); jn path(v {}) value o j that gave the mnmum; mnlength g(v {});