Dynamic Programming: Manhattan Tourist Problem Lecture 17.

Slides:



Advertisements
Similar presentations
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Advertisements

CSE 211 Discrete Mathematics
Great Theoretical Ideas in Computer Science
Single Source Shortest Paths
Great Theoretical Ideas in Computer Science for Some.
Introduction to Bioinformatics Algorithms Divide & Conquer Algorithms.
Dynamic Programming: Sequence alignment
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
 2004 SDU Lecture11- All-pairs shortest paths. Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer.
Outline The power of DNA Sequence Comparison The Change Problem
Chapter 3 The Greedy Method 3.
Great Theoretical Ideas in Computer Science.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Dynamic Programming: Edit Distance
Introduction to Bioinformatics Algorithms Dynamic Programming: Edit Distance.
Dynamic Programming Reading Material: Chapter 7..
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Introduction to Bioinformatics Algorithms Dynamic Programming: Edit Distance.
Great Theoretical Ideas in Computer Science.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Shortest Paths Definitions Single Source Algorithms
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Pertemuan 23 : Penerapan Dinamik Programming (DP) Mata kuliah : K0164-Pemrograman vers 01.
Discrete Math for CS Chapter 8: Directed Graphs. Discrete Math for CS digraph: A digraph is a graph G = (V,E) where V is a finite set of vertices and.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Backtracking.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
Sequence Alignment.
Space-Efficient Sequence Alignment Space-Efficient Sequence Alignment Bioinformatics 202 University of California, San Diego Lecture Notes No. 7 Dr. Pavel.
Introduction to Bioinformatics Algorithms Dynamic Programming: Edit Distance.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
SPANNING TREES Lecture 21 CS2110 – Spring
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Chapter 2 Graph Algorithms.
Pairwise Sequence Alignment (I) (Lecture for CS498-CXZ Algorithms in Bioinformatics) Sept. 22, 2005 ChengXiang Zhai Department of Computer Science University.
1 ELEC692 Fall 2004 Lecture 1b ELEC692 Lecture 1a Introduction to graph theory and algorithm.
Dynamic Programming: Sequence alignment CS 466 Saurabh Sinha.
An Introduction to Bioinformatics 2. Comparing biological sequences: sequence alignment.
Introduction to Algorithms Jiafen Liu Sept
COSC 2007 Data Structures II Chapter 14 Graphs I.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E.
DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
Graphs 2015, Fall Pusan National University Ki-Joune Li.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
SSSP in DAGs (directed acyclic graphs). DFS (depth first search) DFS(vertex v) { v.visited = TRUE; for each w adjacent to v do if(!w.visited) then dfs(w);
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Introduction to Bioinformatics Algorithms Dynamic Programming: Edit Distance.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Great Theoretical Ideas in Computer Science
Special Graphs: Modeling and Algorithms
Topological Sort (topological order)
More Graph Algorithms.
Discrete Mathematics for Computer Science
Sequence Alignment Using Dynamic Programming
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XII
Topological Sort CSE 373 Data Structures Lecture 19.
CS 583 Analysis of Algorithms
Dynamic Programming Longest Path in a DAG, Yin Tat Lee
CSCI2100 Data Structures Tutorial
All pairs shortest path problem
CSE 417: Algorithms and Computational Complexity
Important Problem Types and Fundamental Data Structures
Presentation transcript:

Dynamic Programming: Manhattan Tourist Problem Lecture 17

Manhattan Tourist Problem (MTP)

Imagine seeking a path (from source to sink) to travel (only eastward and southward) with the most number of attractions (*) in the Manhattan grid Sink * * * * * * * ** * * Source *

Manhattan Tourist Problem (MTP) Imagine seeking a path (from source to sink) to travel (only eastward and southward) with the most number of attractions (*) in the Manhattan grid Sink * * * * * * * ** * * Source *

Manhattan Tourist Problem: Formulation Goal: Find the longest path in a weighted grid. Input: A weighted grid G with two distinct vertices, one labeled “source” and the other labeled “sink” Output: A longest path in G from “source” to “sink”

MTP: An Example j coordinate i coordinate 13 source sink

MTP: Greedy Algorithm Is Not Optimal promising start, but leads to bad choices! source sink 18 22

MTP: Simple Recursive Program MT(n,m) if n=0 or m=0 return MT(n,m) x  MT(n-1,m)+ length of the edge from (n- 1,m) to (n,m) y  MT(n,m-1)+ length of the edge from (n,m-1) to (n,m) return max{x,y}

MTP: Simple Recursive Program MT(n,m) x  MT(n-1,m)+ length of the edge from (n- 1,m) to (n,m) y  MT(n,m-1)+ length of the edge from (n,m-1) to (n,m) return min{x,y} What’s wrong with this approach?

i source 1 5 S 1,0 = 5 S 0,1 = 1 Calculate optimal path score for each vertex in the graph Each vertex’s score is the maximum of the prior vertices score plus the weight of the respective edge in between MTP: Dynamic Programming j

MTP: Dynamic Programming (cont ’ d) source S 2,0 = 8 i S 1,1 = 4 S 0,2 = j

MTP: Dynamic Programming (cont ’ d) i source S 3,0 = 8 S 2,1 = 9 S 1,2 = 13 S 3,0 = 8 j

MTP: Dynamic Programming (cont ’ d) greedy alg. fails! i source S 3,1 = 9 S 2,2 = 12 S 1,3 = 8 j

MTP: Dynamic Programming (cont ’ d) i source j S 3,2 = 9 S 2,3 = 15

MTP: Dynamic Programming (cont ’ d) i source j S 3,3 = 16 (showing all back-traces) Done!

MTP: Recurrence Computing the score for a point (i,j) by the recurrence relation: s i, j = max s i-1, j + weight of the edge between (i-1, j) and (i, j) s i, j-1 + weight of the edge between (i, j-1) and (i, j) The running time is n x m for a n by m grid (n = # of rows, m = # of columns)

Manhattan Is Not A Perfect Grid What about diagonals? The score at point B is given by: s B = max of s A1 + weight of the edge (A 1, B) s A2 + weight of the edge (A 2, B) s A3 + weight of the edge (A 3, B) B A3A3 A1A1 A2A2

Manhattan Is Not A Perfect Grid (cont ’ d) Computing the score for point x is given by the recurrence relation: s x = max of s y + weight of vertex (y, x) where y є Predecessors(x) Predecessors (x) – set of vertices that have edges leading to x The running time for a graph G(V, E) (V is the set of all vertices and E is the set of all edges) is O(E) since each edge is evaluated once

Traveling in the Grid The only hitch is that one must decide on the order in which visit the vertices By the time the vertex x is analyzed, the values s y for all its predecessors y should be computed – otherwise we are in trouble. We need to traverse the vertices in some order Try to find such order for a directed cycle ???

DAG: Directed Acyclic Graph Since Manhattan is not a perfect regular grid, we represent it as a DAG DAG for Dressing in the morning problem

Topological Ordering A numbering of vertices of the graph is called topological ordering of the DAG if every edge of the DAG connects a vertex with a smaller label to a vertex with a larger label In other words, if vertices are positioned on a line in an increasing order of labels then all edges go from left to right.

Topological ordering 2 different topological orderings of the DAG

Longest Path in DAG Problem Goal: Find a longest path between two vertices in a weighted DAG Input: A weighted DAG G with source and sink vertices Output: A longest path in G from source to sink

Longest Path in DAG: Dynamic Programming Suppose vertex v has indegree 3 and predecessors {u 1, u 2, u 3 } Longest path to v from source is: In General: s v = max u (s u + weight of edge from u to v) sv =sv = max of s u 1 + weight of edge from u 1 to v s u 2 + weight of edge from u 2 to v s u 3 + weight of edge from u 3 to v

Traversing the Manhattan Grid 3 different strategies: a) Column by column b) Row by row c) Along diagonals a)b) c)