Introduction To Algorithms CS 445 Discussion Session 6 Instructor: Dr Alon Efrat TA : Pooja Vaswani 03/21/2005.

Slides:



Advertisements
Similar presentations
Fibonacci Numbers F n = F n-1 + F n-2 F 0 =0, F 1 =1 – 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 … Straightforward recursive procedure is slow! Why? How slow? Lets.
Advertisements

Single Source Shortest Paths
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
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
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
CS38 Introduction to Algorithms Lecture 5 April 15, 2014.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
Introduction To Algorithms CS 445 Discussion Session 8 Instructor: Dr Alon Efrat TA : Pooja Vaswani 04/04/2005.
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 8a-ShortestPathsMore Shortest Paths in a Graph (cont’d) Fundamental 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 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Path Algorithms
Shortest Paths Definitions Single Source Algorithms
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.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
Shortest Paths1 C B A E D F
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Dijkstra's algorithm.
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.
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
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.
CS223 Advanced Data Structures and Algorithms 1 The Bellman-Ford Shortest Path Algorithm Neil Tang 03/11/2010.
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.
Graph Theory Chapter 8. Varying Applications (examples) Computer networks Distinguish between two chemical compounds with the same molecular formula but.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
All-Pairs Shortest Paths & Essential Subgraph 01/25/2005 Jinil Han.
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
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.
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
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
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
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.
CS38 Introduction to Algorithms Lecture 3 April 8, 2014.
Algorithms and Data Structures Lecture XIII
Algorithms and Data Structures Lecture XIII
Graph Theory Dijkstra's Algorithm.
CS 3013: DS & Algorithms Shortest Paths.
Single-Source Shortest Path & Minimum Spanning Trees
Presentation transcript:

Introduction To Algorithms CS 445 Discussion Session 6 Instructor: Dr Alon Efrat TA : Pooja Vaswani 03/21/2005

2 This Lecture Single-source shortest paths in weighted graphs –Shortest-Path Problems –Properties of Shortest Paths, Relaxation –Dijkstra’s Algorithm –Bellman-Ford Algorithm

3 Shortest Path Generalize distance to weighted setting Digraph G = (V,E) with weight function W: E  R (assigning real values to edges) Weight of path p = v 1  v 2  …  v k is Shortest path = a path of the minimum weight Applications –static/dynamic network routing –robot motion planning –map/route generation in traffic

4 Shortest-Path Problems Shortest-Path problems –Single-source (single-destination). Find a shortest path from a given source (vertex s) to each of the vertices. –Single-pair. Given two vertices, find a shortest path between them. Solution to single-source problem solves this problem efficiently, too. –All-pairs. Find shortest-paths for every pair of vertices. Dynamic programming algorithm.

5 Negative Weights and Cycles? Negative edges are OK, as long as there are no negative weight cycles (otherwise paths with arbitrary small “lengths” would be possible) Shortest-paths can have no cycles (otherwise we could improve them by removing cycles) –Any shortest-path in graph G can be no longer than n – 1 edges, where n is the number of vertices

6 Relaxation For each vertex v in the graph, we maintain v.d(), the estimate of the shortest path from s, initialized to  at the start Relaxing an edge (u,v) means testing whether we can improve the shortest path to v found so far by going through u  uv vu 2 2   Relax(u,v)  uv vu 2 2   Relax (u,v,G) if v.d() > u.d()+G.w(u,v) then v.setd(u.d()+G.w(u,v)) v.setparent(u)

7 Dijkstra's Algorithm Non-negative edge weights Greedy, similar to Prim's algorithm for MST Like breadth-first search (if all weights = 1, one can simply use BFS) Use Q, a priority queue ADT keyed by v.d() (BFS used FIFO queue, here we use a PQ, which is re-organized whenever some d decreases) Basic idea –maintain a set S of solved vertices –at each step select "closest" vertex u, add it to S, and relax all edges from u

8 Dijkstra’s Pseudo Code Input: Graph G, start vertex s relaxing edges Dijkstra(G,s) 01 for each vertex u  G.V() 02 u.setd(  03 u.setparent(NIL) 04 s.setd(0) 05 S  // Set S is used to explain the algorithm 06 Q.init(G.V()) // Q is a priority queue ADT 07 while not Q.isEmpty() 08 u  Q.extractMin() 09 S  S  {u} 10 for each v  u.adjacent() do 11 Relax(u, v, G) 12 Q.modifyKey(v)

9 Dijkstra’s Example    s uv yx    s uv yx Dijkstra(G,s) 01 for each vertex u  G.V() 02 u.setd(  03 u.setparent(NIL) 04 s.setd(0) 05 S  06 Q.init(G.V()) 07 while not Q.isEmpty() 08 u  Q.extractMin() 09 S  S  {u} 10 for each v  u.adjacent() do 11 Relax(u, v, G) 12 Q.modifyKey(v)

10 Dijkstra’s Example (2) uv    s yx    s uv yx Dijkstra(G,s) 01 for each vertex u  G.V() 02 u.setd(  03 u.setparent(NIL) 04 s.setd(0) 05 S  06 Q.init(G.V()) 07 while not Q.isEmpty() 08 u  Q.extractMin() 09 S  S  {u} 10 for each v  u.adjacent() do 11 Relax(u, v, G) 12 Q.modifyKey(v)

11 Dijkstra’s Example (3)    uv yx    uv yx Dijkstra(G,s) 01 for each vertex u  G.V() 02 u.setd(  03 u.setparent(NIL) 04 s.setd(0) 05 S  06 Q.init(G.V()) 07 while not Q.isEmpty() 08 u  Q.extractMin() 09 S  S  {u} 10 for each v  u.adjacent() do 11 Relax(u, v, G) 12 Q.modifyKey(v)

12 Dijkstra’s Running Time Extract-Min executed |V| time Decrease-Key executed |E| time Time = |V| T Extract-Min + |E| T Decrease-Key T depends on different Q implementations QT(Extract- Min) T(Decrease-Key)Total array (V)(V)  (1)  (V 2 ) binary heap  (lg V)  (E lg V) Fibonacci heap  (lg V)  (1) (amort.)  (V lgV + E)

13 Bellman-Ford Algorithm Dijkstra’s doesn’t work when there are negative edges: –Intuition – we can not be greedy any more on the assumption that the lengths of paths will only increase in the future Bellman-Ford algorithm detects negative cycles (returns false) or returns the shortest path-tree

14 Bellman-Ford Algorithm Bellman-Ford(G,s) 01 for each vertex u  G.V() 02 u.setd(  03 u.setparent(NIL) 04 s.setd(0) 05 for i  1 to |G.V()|-1 do 06 for each edge (u,v)  G.E() do 07 Relax (u,v,G) 08 for each edge (u,v)  G.E() do 09 if v.d() > u.d() + G.w(u,v) then 10 return false 11 return true

15 Bellman-Ford Example 5    s zy x t -4    s zy x t -4 5    s zy x t -4 5    s zy x t -4 5

16 Bellman-Ford Example    s zy x t -4 Bellman-Ford running time: –(|V|-1)|E| + |E| =  (VE) 5