Algorithm Course Dr. Aref Rashad February 20131 Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

Single Source Shortest Paths
© DEEDS 2008Graph Algorithms1 Remarks on Dijkstra  Determining the minimum marginal element (code line: v = the vertex in M 2 with minimum v.L; ) accounts.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
Introduction To Algorithms CS 445 Discussion Session 6 Instructor: Dr Alon Efrat TA : Pooja Vaswani 03/21/2005.
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.
Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
Introduction to Algorithms 6.046J/18.401J/SMA5503
Chapter 25: All-Pairs Shortest-Paths
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
CSE 780 Algorithms Advanced Algorithms Shortest path Shortest path tree Relaxation Bellman-Ford Alg.
Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
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 Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1.1 Data Structure and Algorithm Lecture 11 Application of BFS  Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source.
1 Dijkstra's Shortest Path Algorithm Find shortest path from s to t. s 3 t
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
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)
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
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.
Analysis of Algorithms CS 477/677 Shortest Paths Instructor: George Bebis Chapter 24.
All-Pairs Shortest Paths
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
Shortest Paths Introduction to Algorithms Shortest Paths CSE 680 Prof. Roger Crawfis.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Topological Sorting and Least-cost Path Algorithms.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
CISC 235: Topic 11 Shortest Paths Algorithms. CISC 235 Topic 112 Outline Single-Source Shortest Paths Algorithm for Unweighted Graphs Algorithm for Weighted,
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.
CS223 Advanced Data Structures and Algorithms 1 The Bellman-Ford Shortest Path Algorithm Neil Tang 03/11/2010.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,
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.
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.
Introduction to Algorithms Jiafen Liu Sept
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)
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
1 Introduction to Algorithms L ECTURE 17 (Chap. 24) Shortest paths I 24.2 Single-source shortest paths 24.3 Dijkstra’s algorithm Edsger Wybe Dijkstra
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Lecture 13 Algorithm Analysis
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Shortest paths Given: A single source vertex (given s) in a weighted, directed graph. Want to compute a shortest path for each possible destination. (Single.
Algorithms and Data Structures Lecture XIII
Lecture 11 Topics Application of BFS Shortest Path
Algorithms and Data Structures Lecture XIII
CSE 417: Algorithms and Computational Complexity
Dijkstra Algorithm examples
Presentation transcript:

Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms

The Shortest Path Problem  Given a directed, weighted graph G = ( V, E )  What is the shortest path from the start vertex to some end vertex?  Minimize the sum of the edge weights s 3 t

Bellman–Ford algorithm computes single-source shortest paths in a weighted digraph (where some of the edge weights may be negative). Dijkstra's algorithm accomplishes the same problem with a lower running time, but requires edge weights to be non- negative. Thus, Bellman–Ford is usually used only when there are negative edge weights. Bellman–Ford runs in O(V·E) time, where V and E are the number of vertices and edges respectively. The Shortest Path Problem

In a graph in which edges have costs.. Find the shortest path from a source to a destination Surprisingly.. While finding the shortest path from a source to one destination, we can find the shortest paths to all over destinations as well! Dijkstra's Shortest Path Algorithm

Problem: From a given source vertex s V, find the shortest- path weights d(s, v)for all v V. IDEA: Greedy. 1.Maintain a set S of vertices whose shortest-path distances from s are known. 2.At each step add to S the vertex v V –S whose distance estimate from s is minimal. 3.Update the distance estimates of vertices adjacent to v. Dijkstra Algorithm

Dijkstra's Shortest Path Algorithm  For a graph, G = ( V, E )  Dijkstra’s algorithm keeps two sets of vertices: S Vertices whose shortest paths have already been determined V-S Remainder  Also d Best estimates of shortest path to each vertex p Predecessors for each vertex

Dijkstra's Shortest Path Algorithm Find shortest path from s to t. s 3 t

Dijkstra's Shortest Path Algorithm s 3 t        0 distance label S = { } PQ = { s, 2, 3, 4, 5, 6, 7, t }

s 3 t        0 distance label S = { } PQ = { s, 2, 3, 4, 5, 6, 7, t } delmin Dijkstra's Shortest Path Algorithm

s 3 t    14  0 distance label S = { s } PQ = { 2, 3, 4, 5, 6, 7, t } decrease key  X   X X Dijkstra's Shortest Path Algorithm

s 3 t    14  0 distance label S = { s } PQ = { 2, 3, 4, 5, 6, 7, t }  X   X X delmin Dijkstra's Shortest Path Algorithm

9 2 s 3 t    14  0 S = { s, 2 } PQ = { 3, 4, 5, 6, 7, t }  X   X X Dijkstra's Shortest Path Algorithm

s 3 t    14  0 S = { s, 2 } PQ = { 3, 4, 5, 6, 7, t }  X   X X decrease key X 33 Dijkstra's Shortest Path Algorithm

s 3 t    14  0 S = { s, 2 } PQ = { 3, 4, 5, 6, 7, t }  X   X X X 33 delmin Dijkstra's Shortest Path Algorithm

s 3 t    14  0 S = { s, 2, 6 } PQ = { 3, 4, 5, 7, t }  X   X X X X X 32 Dijkstra's Shortest Path Algorithm

s 3 t   14  0 S = { s, 2, 6 } PQ = { 3, 4, 5, 7, t }  X   X X 44 X delmin  X 33 X 32 Dijkstra's Shortest Path Algorithm

s 3 t   14  0 S = { s, 2, 6, 7 } PQ = { 3, 4, 5, t }  X   X X 44 X 35 X 59 X 24  X 33 X 32 Dijkstra's Shortest Path Algorithm

s 3 t   14  0 S = { s, 2, 6, 7 } PQ = { 3, 4, 5, t }  X   X X 44 X 35 X 59 X delmin  X 33 X 32 Dijkstra's Shortest Path Algorithm

s 3 t   14  0 S = { s, 2, 3, 6, 7 } PQ = { 4, 5, t }  X   X X 44 X 35 X 59 XX 51 X 34  X 33 X 32 Dijkstra's Shortest Path Algorithm

s 3 t   14  0 S = { s, 2, 3, 6, 7 } PQ = { 4, 5, t }  X   X X 44 X 35 X 59 XX 51 X 34 delmin  X 33 X Dijkstra's Shortest Path Algorithm

s 3 t   14  0 S = { s, 2, 3, 5, 6, 7 } PQ = { 4, t }  X   X X 44 X 35 X 59 XX 51 X X 50 X 45  X 33 X 32 Dijkstra's Shortest Path Algorithm

s 3 t   14  0 S = { s, 2, 3, 5, 6, 7 } PQ = { 4, t }  X   X X 44 X 35 X 59 XX 51 X X 50 X 45 delmin  X 33 X 32 Dijkstra's Shortest Path Algorithm

s 3 t   14  0 S = { s, 2, 3, 4, 5, 6, 7 } PQ = { t }  X   X X 44 X 35 X 59 XX 51 X X 50 X 45  X 33 X 32 Dijkstra's Shortest Path Algorithm

s 3 t   14  0 S = { s, 2, 3, 4, 5, 6, 7 } PQ = { t }  X   X X 44 X 35 X 59 XX 51 X 34 X 50 X 45 delmin  X 33 X Dijkstra's Shortest Path Algorithm

s 3 t   14  0 S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { }  X   X X 44 X 35 X 59 XX 51 X 34 X 50 X 45  X 33 X 32 Dijkstra's Shortest Path Algorithm

s 3 t   14  0 S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { }  X   X X 44 X 35 X 59 XX 51 X 34 X 50 X 45  X 33 X 32 Dijkstra's Shortest Path Algorithm

Running time is O(V 2 ) using linear array for priority queue. O((V + E) lg V) using binary heap. Time= O(V)·T(EXTRACT-MIN)+ O(E)·T(DECREASE-KEY)

Given a directed graph G = (V, E), with edge weights Cvw, find shortest path from node s to node t. Dijkstra. Can fail if negative edge-costs exist. u t sv Allow for negative weights The Shortest Paths Problem

BELLMAN-FORD(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 for i ← 1 to |V[G]| 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 Bellman-Ford Algorithm Time Complexity is O(VE). Can have negative- weight edges. Will “detect” reachable negative-weight cycles.

Bellman-Ford Algorithm Example 0    z uv x y 6 5 – –2 –4 2

Example 0  7  6 z uv x y 6 5 – –2 –4 2 Bellman-Ford Algorithm

Example z uv x y 6 5 – –2 –4 2 Bellman-Ford Algorithm

z uv x y 6 5 – –2 –4 2 ExampleBellman-Ford Algorithm

z uv x y 6 5 – –2 –4 2 ExampleBellman-Ford Algorithm

Another Example

Shortest Path Using Dynamic Programming

Shortest Paths: Dynamic Programming  Def. OPT(i, v) = length of shortest v-t path P using at most i edges.  Case 1: P uses at most i-1 edges.  OPT(i, v) = OPT(i-1, v)  Case 2: P uses exactly i edges.  if (v, w) is first edge, then OPT uses (v, w), and then selects best w-t path using at most i-1 edges

Start End a c d g f e b Shortest Paths: Dynamic Programming

b = min(6+g, 5+e, 7+f) End g f e b Shortest Paths: Dynamic Programming

g = min(6+d, 14) e = min(3+c, 7+d, 7+g) f = 2+c c d g f e 14 a Shortest Paths: Dynamic Programming

c = min(5, 11+d) d = Start a c d Shortest Paths: Dynamic Programming

b = min(6+g, 5+e, 7+f) g = min(6+d, 14) e = min(3+c, 7+d, 7+g) f = 2+c c = min(5, 11+d) d = 3 via “a to d” Shortest Paths: Dynamic Programming

b = min(6+g, 5+e, 7+f) g = min(6+d, 14) e = min(3+c, 7+d, 7+g) f = 2+c c = min(5, 11+d) d = 3 via “a to d” Shortest Paths: Dynamic Programming

b = min(6+g, 5+e, 7+f) g = min(6+3, 14) e = min(3+c, 7+3, 7+g) f = 2+c c = min(5, 11+3) d = 3 via “a to d” Shortest Paths: Dynamic Programming

b = min(6+g, 5+e, 7+f) g = min(9, 14) e = min(3+c, 10, 7+g) f = 2+c c = min(5, 14) d = 3 via “a to d” Shortest Paths: Dynamic Programming

b = min(6+g, 5+e, 7+f) g = min(9, 14) e = min(3+c, 10, 7+g) f = 2+c c = min(5, 14) d = 3 via “a to d” Shortest Paths: Dynamic Programming

b = min(6+g, 5+e, 7+f) g = 9 via “a to d to g” e = min(3+c, 10, 7+g) f = 2+c c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming a d

b = min(6+g, 5+e, 7+f) g = 9 via “a to d to g” e = min(3+c, 10, 7+g) f = 2+c c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming 6 g 14 a d

b = min(6+9, 5+e, 7+f) g = 9 via “a to d to g” e = min(3+5, 10, 7+9) f = 2+5 c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

b = min(15, 5+e, 7+f) g = 9 via “a to d to g” e = min(8, 10, 16) f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

b = min(15, 5+e, 7+f) g = 9 via “a to d to g” e = min(8, 10, 16) f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

b = min(15, 5+e, 7+f) g = 9 via “a to d to g” e = 8 via “a to c to e” f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

b = min(15, 5+e, 7+f) g = 9 via “a to d to g” e = 8 via “a to c to e” f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

b = min(15, 5+8, 7+7) g = 9 via “a to d to g” e = 8 via “a to c to e” f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

b = min(15, 13, 14) g = 9 via “a to d to g” e = 8 via “a to c to e” f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

b = min(15, 13, 14) g = 9 via “a to d to g” e = 8 via “a to c to e” f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

b = 13 via “a to c to e to b” g = 9 via “a to d to g” e = 8 via “a to c to e” f = 7 via “a to c to f” c = 5 via “a to c” d = 3 via “a to d” Shortest Paths: Dynamic Programming

Start End a c d 7 6 g f e b Shortest Path = 13 Analysis.  (VE) time,  (V 2 ) space Shortest Paths: Dynamic Programming