Graph Theory Dijkstra's Algorithm.

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
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
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.
Introduction to Algorithms 6.046J/18.401J/SMA5503
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Shortest Paths Definitions Single Source Algorithms
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
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.
Dijkstra's algorithm.
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,
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
Graph Theory Chapter 8. Varying Applications (examples) Computer networks Distinguish between two chemical compounds with the same molecular formula but.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Introduction to Algorithms Jiafen Liu Sept
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
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.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Graphs – Breadth First Search
Data Structures and Algorithms
Shortest Paths.
Minimum Spanning Trees
CSC317 Shortest path algorithms
Single-Source Shortest Paths
Graph Theory.
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
CS200: Algorithm Analysis
Graph Search Algorithms
Minimum Spanning Trees
Minimum Spanning Trees
SINGLE-SOURCE SHORTEST PATHS
Shortest Paths.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
2018/12/27 chapter25.
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Minimum Spanning Tree Algorithms
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Advanced Algorithms Analysis and Design
Shortest Paths.
CSE 417: Algorithms and Computational Complexity
Graph Theory J. Mercy Arokia Rani Assistant Professor
CS 3013: DS & Algorithms Shortest Paths.
Single-Source Shortest Path & Minimum Spanning Trees
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Graph Theory Dijkstra's Algorithm

Shortest-Path Problems Single-source (single-destination). Find a shortest path from a given source (vertex s) to each of the vertices. The topic of this lecture. 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. Unweighted shortest-paths – BFS.

Optimal Substructure Theorem: subpaths of shortest paths are shortest paths Proof (”cut and paste”) if some subpath were not the shortest path, one could substitute the shorter subpath and create a shorter total path

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

Shortest Path Tree The result of the algorithms – a shortest path tree. For each vertex v, it records a shortest path from the start vertex s to v. v.parent() gives a predecessor of v in this shortest path gives a shortest path length from s to v, which is recorded in v.d(). The same pseudo-code assumptions are used. Vertex ADT with operations: adjacent():VertexSet d():int and setd(k:int) parent():Vertex and setparent(p:Vertex)

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 u v u v 2 2 5 5 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) 9 6 Relax(u,v) Relax(u,v) 5 7 5 6 2 2 u v u v

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

Dijkstra’s ALgorithm Solution to Single-source (single-destination). Input: Graph G, start vertex s 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) relaxing edges

Dijkstra’s Example ¥ 10 ¥ 5 s u v y x 10 5 1 2 3 9 4 6 7 Dijkstra(G,s) s u v y x 10 5 1 2 3 9 4 6 7 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 ¥ 5 s u v y x 1 2 3 9 4 6 7

Dijkstra’s Example 8 14 5 7 8 13 5 7 u v s y x 10 1 2 3 9 4 6 s y x 10 1 2 3 9 4 6 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) 8 13 5 7 s u v y x 10 1 2 3 9 4 6

Dijkstra’s Example 8 9 5 7 8 9 5 7 u v 1 10 Dijkstra(G,s) 9 2 3 4 6 7 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) 9 2 3 4 6 7 5 5 7 2 x y 8 9 5 7 u v y x 10 1 2 3 4 6

Dijkstra’s Algorithm O(n2) operations (n-1) iterations: 1 for each vertex added to the distinguished set S. (n-1) iterations: for each adjacent vertex of the one added to the distinguished set. Note: it is single source – single destination algorithm