Shortest Path Problems

Slides:



Advertisements
Similar presentations
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Advertisements

Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
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.
Single-Source Shortest- paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 11 DFS Properties of DFS Topological sort.
CS420 lecture twelve Shortest Paths wim bohm cs csu.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
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 Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 Data Structures DFS, Topological Sort Dana Shapira.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Paths Definitions Single Source Algorithms
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
Topological Sorting and Least-cost Path Algorithms.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
CISC 235: Topic 11 Shortest Paths Algorithms. CISC 235 Topic 112 Outline Single-Source Shortest Paths Algorithm for Unweighted Graphs Algorithm for Weighted,
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
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.
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
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.
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Lecture 13 Algorithm Analysis
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
CS138A Elementary Graph Algorithms Peter Schröder.
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Topological Sort Minimum Spanning Tree
Many slides here are based on E. Demaine , D. Luebke slides
Elementary Graph Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Finding Shortest Paths
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms Searching in a Graph.
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

Shortest Path Problems Algorithms Shortest Path Problems

G = (V, E) weighted directed graph w: ER weight function Weight of a path p = <v0, v1,. . ., vn> Shortest path weight from u to v Shortest path from u to v: Any path from u to v with w(p) = (u,v) [v] predecessor of v on a path

            

Variants Single-source shortest paths: find shortest paths from source vertex to every other vertex Single-destination shortest paths: find shortest paths to a destination from every vertex Single-pair shortest-path find shortest path from u to v All pairs shortest paths

Lemma 25.1 Subpaths of shortest paths are shortest paths. Given G=(G,E) w: E  R Let p = p = <v1, v2,. . ., vk> be a shortest path from v1 to vk For any i,j such that 1 i j k, let pij be a subpath from vi to vj.. Then pij is a shortest path from vi to vj.

p 1 i j k

Corollary 25.2 Let G = (V,E) w: E  R Suppose shortest path p from a source s to vertex v can be decomposed into p’ s u v for vertex u and path p’. Then weight of the shortest path from s to v is (s,v) = (s,u) + w(u,v)

Lemma 25.3 Let G = (V,E) w: E  R Source vertex s For all edges (u,v)E (s,v) (s,u) + w(u,v)

u1 u2 u3 s v u4 un

Relaxation Shortest path estimate d[v] is an attribute of each vertex which is an upper bound on the weight of the shortest path from s to v Relaxation is the process of incrementally reducing d[v] until it is an exact weight of the shortest path from s to v

INITIALIZE-SINGLE-SOURCE(G, s) 1. for each vertex v V(G) 2. do d[v]  3. [v]  nil 4. d[s]  0

            

Relaxing an Edge (u,v) Question: Can we improve the shortest path to v found so far by going through u? If yes, update d[v] and [v]

RELAX(u,v,w) 1. if d[v] > d[u] + w(u,v) 2. then d[v]  d[u] + w(u,v) 3. [v]  u

EXAMPLE 1 s  s  Relax       v u v u

EXAMPLE 2 s  s  Relax       v u v u

Dijkstra’s Algorithm Problem: Solve the single source shortest-path problem on a weighted, directed graph G(V,E) for the cases in which edge weights are non-negative

Dijkstra’s Algorithm Approach maintain a set S of vertices whose final shortest path weights from the source s have been determined. repeat select vertex from V-S with the minimum shortest path estimate insert u in S relax all edges leaving u

DIJKSTRA(G,w,s) 1. INITIALIZE-SINGLE-SOURCE(G,s) 2. S  3. Q  V[G] 4. while Q  5. do u  EXTRACT-MIN(Q) 6. S  S {u} 7. for each vertex v Adj[u] 8. do RELAX(u,v,w)

            

Analysis of Dijkstra’s Algorithm Suppose priority Q is: an ordered (by d) linked list Building the Q O(V lg V) Each EXTRACT-MIN O(V) This is done V times O(V2) Each edge is relaxed one time O(E) Total time O(V2 + E) = O(V2)

Analysis of Dijkstra’s Algorithm Suppose priority Q is: a binary heap BUILD-HEAP O(V) Each EXTRACT-MIN O(lg V) This is done V times O(V lg V) Each edge is relaxation O(lg V) Each edge relaxed one time O(E lg V) Total time O(V lg V + E lg V))

Properties of Relaxation Lemma 25.4 G=(V,E) w: E  R (u,v)  E After relaxing edge (u,v) by executing RELAX(u,v,w) we have d[v]  d[u] + w(u,v)

Lemma 25.5 Given: G=(V,E) w: E  R source s  V Graph initialized by INITIALIZE-SINGLE-SOURCE(G,s) then d[v]  (s,v) for all v V and this invariant is maintained over all relaxation steps Once d[v] achieves a lower bound (s,v), it never changes

Corollary 25.6 Given: G=(V,E) w: E  R source s  V No path connects s to given v then after initialization d[v]  (s,v) and this inequality is maintained over all relaxation steps.

Lemma 25.7 Given: G=(V,E) w: E  R source s  V Let s - - u v be the shortest path in G for all vertices u and v. Suppose G initialized by INITIALIZE-SINGLE-SOURCE is followed by a sequence of relaxations including RELAX(u,v,w) Then d[u] = (s,u) prior to call implies that d[u] = (s,u) after the call

Bottom Line Therefore, relaxation causes the shortest path estimates to descend monotonically toward the actual shortest-path weights.

Shortest-Paths Tree of G(V,E) The shortest-paths tree at S of G(V,E) is a directed subgraph G’-(V’,E’), where V’ V, E’E, such that V’ is the set of vertices reachable from S in G G’ forms a rooted tree with root s, and for all v V’, the unique simple path from s to v in G’ is a shortest path from s to v in G

Goal We want to show that successive relaxations will yield a shortest-path tree

Lemma 25.8 Given: G=(V,E) w: E  R source s  V Assume that G contains no negative-weight cycles reachable from s. Then after the graph is initialized with INITIALIZE-SINGLE-SOURCE • the predecessor subgraph G forms a rooted tree with root s, and • any sequence of relaxation steps on edges in G maintains this property as an invariant.

Algorithms Bellman-Ford Algorithm Directed-Acyclic Graphs All Pairs-Shortest Path Algorithm

Why does Dijkstra’s greedy algorithm work? Because we know that when we add a node u to the set S, the value d is the length of the shortest path from s to u. But, this only works if the edges of the graph are nonnegative.

a 10 7 b c -4 Would Dikjstra’s Algorithm work with this graph?

a 6 7 b c -14 What is the length of the shortest path from a to c in this graph?

Bellman-Ford Algorithm The Bellman-Ford algorithm can be used to solve the general single source shortest path problem Negative weights are allowed The algorithm detects negative cycles and returns false if the graph contains one.

BELLMAN-FORD(G,w,s) 1 INITIALIZE-SINGLE-SOURCE(G,s) 2 for i  1 to |V[G]| -1 3 do for each edge (u,v)  E[G] 4 do RELAX(u,v,w) 5 for each edge (u,v)  E[G] 6 do if d[v] > d[u] + w(u,v) 7 then return false 8 return true

When there are no cycles of negative length, what is maximum number of edges in a shortest path when the graph has |V[G]| vertices and |E[G]| edges?

Dynamic Programming Formulation The following recurrence shows how the Bellman Ford algorithm computes the d values for paths of length k.

a 10 Processing order 1 2 3 (a,c) (b,a) (c,b) (c,d) (d,b) 7 b c -4 -5 9 d

Complexity of the Bellman Ford Algorithm Time complexity: Performance can be improved by adding a test to the loop to see if any d values were updated on the previous iteration, or maintain a queue of vertices whose d value changed on the previous iteration-only process these on the next iteration

Single-source shortest paths in directed acyclic graphs Topological sorting is the key to efficient algorithms for many DAG applications. A topological sort of a DAG is a linear ordering of all of its vertices such that if G contains an edge (u,v), then u appears before v in the ordering.

b e c g a d f a d c b e f g

DAG-SHORTEST-PATHS(G,w,s) 1 topologically sort the vertices of G 2 INITIALIZE-SINGLE-SOURCE(G,s) 3 for each vertex u taken in topological order 4 do for each vertex v  Adj[u] 5 do RELAX(u,v,w)

Topological Sorting TOPOLOGICAL-SORT(G) 1 call DFS(G) to compute finishing times f[v] for each vertex v 2 as each vertex is finished, insert it onto the front of a linked list 3 return the linked list of vertices

Depth-first search Goal: search all edges in the graph one time Strategy: Search deeper in the graph whenever possible Edges are explored out of the most recently discovered vertex v that still has unexplored edges leaving it. Backtrack when a dead end is encountered

Predecessor subgraph The predecessor subgraph of a depth first search forms a depth-first forest composed of depth-first trees The edges in E are called tree edges

Vertex coloring scheme All vertices are initially white A vertex is colored gray when it is discovered A vertex is colored black when it is finished (all vertices adjacent to the vertex have been examined completely)

Time Stamps Each vertex v has two time-stamps For every vertex u d[v] records when v is first discovered (and grayed) f[v] records when the search finishes examining its adjacency list (and is blackened) For every vertex u d[u] < f[u]

Color and Time Stamp Summary Vertex u is white before d[u] gray between d[u] and f[u] black after f[u] Time is a global variable in the pseudocode

DFS(G) 1 for each vertex u  V[G] 2 do color[u] white 3 [u] nil 4 time 0 5 do for each vertex u  Adj[u] 6 do if color[u] = white 7 then DFS-VISIT(u)

DFS-VISIT(u) 1 color[u] gray 2 d[u] time time time +1 3 for each vertex v  Adj[u] 4 do if color[v] = white 5 then [v] u 6 DFS-VISIT(v) 7 color[u] black 8 f[u] time time time +1

b e 1/ c a g f d

b 2/ e 1/ c a g f d

b 2/ e 3/ 1/ c a g f d

b 2/ e 3/ 4/ 1/ c a g f d

b 2/ e 3/ 4/5 1/ c a g f d

b 2/ e 3/6 4/5 1/ c a g f d

b 2/7 e 3/6 4/5 1/ c a g f d

b 2/7 e 3/6 8/ 4/5 1/ c a g f d

b 2/7 e 3/6 8/9 4/5 1/ c a g f d

b 2/7 e 3/6 8/9 4/5 1/ c a g 10/ f d

b 2/7 e 3/6 8/9 4/5 1/ c a g 10/ 11/ f d

b 2/7 e 3/6 8/9 4/5 1/ c a g 11/12 10/ f d

b 2/7 e 3/6 8/9 4/5 1/ c a g 11/12 10/13 f d

b 2/7 e 3/6 8/9 4/5 1/14 c a g 11/12 10/13 f d

Running time of DFS lines 1-3 of DFS lines 5-7 of DFS lines 2-6 of DFS-VISIT

Running Time of Topological Sort DFS Insertion in linked list

8 b 7 e 6 4 7 7 10 2 9 5 14 c 6 a g 1 3 12 13 9 f d

Running Time for DAG-SHORTEST-PATHS Topological sort Initialize-single source 3-5 each edge examined one time