Negative-Weight edges:

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
CS420 Lecture 9 Dynamic Programming. Optimization Problems In optimization problems a set of choices are to be made to arrive at an optimum, and sub problems.
CS138A Single Source Shortest Paths Peter Schröder.
Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Shortest Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Shortest Paths Definitions Single Source Algorithms
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
chapter chapter chapter253.
1 Dynamic Programming Jose Rolim University of Geneva.
Longest Common Subsequence
1 Dynamic programming algorithms for all-pairs shortest path and longest common subsequences We will study a new technique—dynamic programming algorithms.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Divide-and-Conquer & Dynamic Programming Divide-and-Conquer: Divide a problem to independent subproblems, find the solutions of the subproblems, and then.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Minimum weight spanning trees
Introduction to Algorithms Jiafen Liu Sept
Introduction to Algorithms Jiafen Liu Sept
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Dynamic Programming Csc 487/687 Computing for Bioinformatics.
Many slides here are based on D. Luebke slides
Advanced Algorithms Analysis and Design
Least common subsequence:
Algorithms and Data Structures Lecture XIII
Challenging Problem 2: Challenging problem 2 will be given on the website (week 4) at 9:30 am on Saturday (Oct 1, 2005). 2. The due time is Sunday (Oct.
CSCE 411 Design and Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
برنامه نویسی پویا (Dynamic Programming)
ICS 353: Design and Analysis of Algorithms
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Announcement 2: A 2 hour midterm (open book) will be given on March (Tuesday) during the lecture time. 2018/12/4.
Page 620 Single-Source Shortest Paths
CS 3343: Analysis of Algorithms
2018/12/27 chapter25.
Advanced Algorithms Analysis and Design
2-optimal Euler path problem Euler circuit in a directed graph:
Lecture 11 Topics Application of BFS Shortest Path
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Dynamic Programming.
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
2019/2/25 chapter25.
Ch. 15: Dynamic Programming Ming-Te Chi
The Bellman-Ford Shortest Path Algorithm Neil Tang 03/27/2008
Evaluation of the Course (Modified)
Dynamic Programming.
Chapter 24: Single-Source Shortest Paths
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Longest Common Subsequence
Advanced Algorithms Analysis and Design
Midterm: week 7 in the lecture for 2 hours
Longest common subsequence (LCS)
Chapter 24: Single-Source Shortest Paths
Analysis of Algorithms CS 477/677
Negative-Weight edges:
Negative-Weight edges:
CS 3013: DS & Algorithms Shortest Paths.
Longest Common Subsequence
Chapter 24: Single-Source Shortest-Path
Chapter 15: Dynamic Programming
Chapter 15: Dynamic Programming
Longest Common Subsequence
Advanced Algorithms Analysis and Design
Presentation transcript:

Negative-Weight edges: Edge weight may be negative. negative-weight cycles– the total weight in the cycle (circuit) is negative. If no negative-weight cycles reachable from the source s, then for all v V, the shortest-path weight remains well defined,even if it has a negative value. If there is a negative-weight cycle on some path from s to v, we define = - . 2019/5/23 chapter25

a b -4 h i 3 -1 2 4 3 c d 6 8 5 -8 3 5 11 g s -3 e 3 f 2 7 j -6 Figure1 Negative edge weights in a directed graph.Shown within each vertex is its shortest-path weight from source s.Because vertices e and f form a negative-weight cycle reachable from s,they have shortest-path weights of - . Because vertex g is reachable from a vertex whose shortest path is - ,it,too,has a shortest-path weight of - .Vertices such as h, i ,and j are not reachable from s,and so their shortest-path weights are , even though they lie on a negative-weight cycle. 2019/5/23 chapter25

Relaxation: The process of relaxing an edge (u,v) consists of testing whether we can improve the shortest path to v found so far by going through u and,if so,updating d[v] and [v]. RELAX(u,v,w) if d[v]>d[u]+w(u,v) then d[v]  d[u]+w(u,v) (based on Lemma 25.3) [v]  u 2019/5/23 chapter25

u v u v 2 2 5 9 5 6 RELAX(u,v) RELAX(u,v) u v u v 2 2 5 7 5 6 (a) (b) Figure2 Relaxation of an edge (u,v).The shortest-path estimate of each vertex is shown within the vertex. (a)Because d[v]>d[u]+w(u,v) prior to relaxation, the value of d[v] decreases. (b)Here, d[v] d[u]+w(u,v) before the relaxation step,so d[v] is unchanged by relaxation. 2019/5/23 chapter25

Bellman-Ford algorithm: The Bellman-Ford algorithm solves the single-source shortest-paths problem in the more general case in which edge weights can be negative. It report FALSE if a negative-weight circuit exists. 2019/5/23 chapter25

Continue: BELLMAN-FORD(G,w,s): INITIALIZE-SINGLE-SOURCE(G,s) for i  1 to |V[G]| -1 do for each edge (u,v)  E[G] do RELAX(u,v,w) for each edge (u,v) E[G] do if d[v]>d[u]+w(u,v) then return FALSE return TRUE Time complexity: O(|V||E|). 2019/5/23 chapter25

6 7 9 2 5 -2 8 -3 -4 z u v x y (a) 2019/5/23 chapter25

u 5 v 6 8 -2 6 -3 8 7 z -4 2 7 7 8 9 x y (b) 2019/5/23 chapter25

u 5 v 6 4 -2 6 -3 8 7 z -4 2 7 7 2 9 x y (c) 2019/5/23 chapter25

u 5 v 2 4 -2 6 -3 8 7 z -4 2 7 7 2 9 x y (d) 2019/5/23 chapter25

u 5 v 2 4 -2 6 -3 8 7 z -4 2 7 7 -2 9 x y (e) 2019/5/23 chapter25

Proof: We prove it by induction on i. Theorem: (hard) after the i-th iteration, the cost obtained is at most the cost of a shortest path from s to any node v containing at most i edges. Proof: We prove it by induction on i. The theorem is true for i=1. The shortest path from s to v containing at most one edge is the the edge (s, v) (if exists). Assume that the theorem is true for i=k. Then we are going to show that the theorem is true for i=k+1. Let P: s-> v1-> v2 …->vm-> v be the shortest path from s to v containing at most k+1 edges. Then P1: s-> v1-> v2 …->vm must be the shortest path from s to vm containing at most k edges. By assumption, P1 has been obtained after (k)-th iteration. In the (k+1)-th iteration, we tried to RELAX(vm, v, w) on node vm. Thus, path P is obtained after the (k+1)-th iteration. 2019/5/23 chapter25

Corollary: If negative-weight circuit exists in the given graph, in the n-th iteration, the cost of a shortest path from s to some node v will be further reduced. Demonstrated by the following example. 2019/5/23 chapter25

An example with negative-weight cycle 5  1 6 -2 8 7 7 9 2 2 5 -8 An example with negative-weight cycle 2019/5/23 chapter25

7 6  8 5 -2 1 2 9 -8 2019/5/23 chapter25

7 6 16  11 9 8 5 -2 1 2 -8 2019/5/23 chapter25

7 6 16 12 11 1 9 8 5 -2 2 -8 2019/5/23 chapter25

6 16 12 11 1 9 7 8 5 -2 2 -8 2019/5/23 chapter25

5 6 11 1 6 -2 8 7 12 7 9 2 6 15 2 5 -8 8 1 2019/5/23 chapter25

6 15 12 11 8 7 5 -2 1 2 9 -8 2019/5/23 chapter25

5 6 11 1 6 -2 8 7 12 7 9 2 5 15 2 5 -8 8 x 2019/5/23 chapter25

Longest common subsequence Definition 1: Given a sequence X=x1x2...xm, another sequence Z=z1z2...zk is a subsequence of X if there exists a strictly increasing sequence i1i2...ik of indices of X such that for all j=1,2,...k, we have xij=zj. Example 1: If X=abcdefg, Z=abdg is a subsequence of X. X=abcdefg, Z=ab d g 2019/5/23 chapter25

Definition 2: Given two sequences X and Y, a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y. Example 2: X=abcdefg and Y=aaadgfd. Z=adf is a common subsequence of X and Y. X=abc defg Y=aaaadgfd Z=a d f 2019/5/23 chapter25

Longest common subsequence may not be unique. Example: abcd acbd Definition 3: A longest common subsequence of X and Y is a common subsequence of X and Y with the longest length. (The length of a sequence is the number of letters in the seuqence.) Longest common subsequence may not be unique. Example: abcd acbd Both acd and abd are LCS. 2019/5/23 chapter25

Longest common subsequence problem Input: Two sequences X=x1x2...xm, and Y=y1y2...yn. Output: a longest common subsequence of X and Y. A brute-force approach Suppose that mn. Try all subsequence of X (There are 2m subsequence of X), test if such a subsequence is also a subsequence of Y, and select the one with the longest length. 2019/5/23 chapter25

Charactering a longest common subsequence Theorem (Optimal substructure of an LCS) Let X=x1x2...xm, and Y=y1y2...yn be two sequences, and Z=z1z2...zk be any LCS of X and Y. 1. If xm=yn, then zk=xm=yn and Z[1..k-1] is an LCS of X[1..m-1] and Y[1..n-1]. 2. If xm yn, then zkxm implies that Z is an LCS of X[1..m-1] and Y. 2. If xm yn, then zkyn implies that Z is an LCS of X and Y[1..n-1]. 2019/5/23 chapter25

The recursive equation Let c[i,j] be the length of an LCS of X[1...i] and Y[1...j]. c[i,j] can be computed as follows: 0 if i=0 or j=0, c[i,j]= c[i-1,j-1]+1 if i,j>0 and xi=yj, max{c[i,j-1],c[i-1,j]} if i,j>0 and xiyj. Computing the length of an LCS There are nm c[i,j]’s. So we can compute them in a specific order. 2019/5/23 chapter25

The algorithm to compute an LCS 1. for i=1 to m do 2. c[i,0]=0; 3. for j=0 to n do 4. c[0,j]=0; 5. for i=1 to m do 6. for j=1 to n do 7. { 8. if x[I] ==y[j] then 9. c[i,j]=c[i-1,j-1]+1; 10 b[i,j]=1; 11. else if c[i-1,j]>=c[i,j-1] then 12. c[i,j]=c[i-1,j] 13. b[i,j]=2; 14. else c[i,j]=c[i,j-1] 15. b[i,j]=3; 14 } b[i,j] stores the directions. 1—diagnal, 2-up, 3-forward. 2019/5/23 chapter25

Example 3: X=BDCABA and Y=ABCBDAB. 2019/5/23 chapter25

Constructing an LCS (back-tracking) We can find an LCS using b[i,j]’s. We start with b[n,m] and track back to some cell b[0,i] or b[i,0]. The algorithm to construct an LCS 1. i=m 2. j=n; 3. if i==0 or j==0 then exit; 4. if b[i,j]==1 then { i=i-1; j=j-1; print “xi”; } 5. if b[i,j]==2 i=i-1 6. if b[i,j]==3 j=j-1 7. Goto Step 3. The time complexity: O(nm). 2019/5/23 chapter25

Shortest common supersequence Definition: Let X and Y be two sequences. A sequence Z is a supersequence of X and Y if both X and Y are subsequence of Z. Shortest common supersequence problem: Input: Two sequences X and Y. Output: a shortest common supersequence of X and Y. Example: X=abc and Y=abb. Both abbc and abcb are the shortest common supersequences for X and Y. 2019/5/23 chapter25

Let c[i,j] be the length of an LCS of X[1...i] and X[1...j]. Recursive Equation: Let c[i,j] be the length of an LCS of X[1...i] and X[1...j]. c[i,j] can be computed as follows: j if i=0 i if j=0, c[i,j]= c[i-1,j-1]+1 if i,j>0 and xi=yj, min{c[i,j-1]+1,c[i-1,j]+1} if i,j>0 and xiyj. 2019/5/23 chapter25

2019/5/23 chapter25