Single-Source Shortest Paths

Slides:



Advertisements
Similar presentations
CS138A Single Source Shortest Paths Peter Schröder.
Advertisements

More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
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.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Dijkstra's algorithm.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Paths CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
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.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington These slides are.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Minimum Spanning Trees
Minimum Spanning Trees
Chapter 22 Elementary Graph Algorithms
Data Structures and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs
Shortest Paths and Minimum Spanning Trees
CSC317 Shortest path algorithms
Single-Source Shortest Paths
Minimum Spanning Trees
C.Eng 213 Data Structures Graphs Fall Section 3.
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
Introduction to Graphs
Minimum Spanning Trees
Short paths and spanning trees
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Minimum Spanning Trees
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
SINGLE-SOURCE SHORTEST PATHS
CSE 373 Data Structures and 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.
2018/12/27 chapter25.
Minimum Spanning Trees
Advanced Algorithms Analysis and Design
Lecture 11 Topics Application of BFS Shortest Path
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Autumn 2015 Lecture 10 Minimum Spanning Trees
Dijkstra Algorithm.
Minimum Spanning Tree Algorithms
Shortest Paths and Minimum Spanning Trees
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Fundamental Structures of Computer Science II
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Algorithms: Design and Analysis
Algorithms Searching in a Graph.
Minimum spanning tree Shortest path algorithms
CS 584 Project Write up Poster session for final Due on day of final
Autumn 2016 Lecture 10 Minimum Spanning Trees
Dijkstra’s Shortest Path Algorithm
Graph Algorithms: Shortest Path
CS 3013: DS & Algorithms Shortest Paths.
Winter 2019 Lecture 10 Minimum Spanning Trees
Single-Source Shortest Path & Minimum Spanning Trees
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Chapter 9 Graph algorithms
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Single-Source Shortest Paths CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Terminology A network is a directed graph. We will use both terms interchangeably. The weight of a path is the sum of weights of the edges that make up the path. The shortest path between two vertices s and t in a directed graph is a directed path from s to t with the property that no other such path has a lower weight.

Shortest Paths We will consider two problems: Assumptions: Single-source: find the shortest path from the source vertex s to all other vertices in the graph. These shortest paths will form a tree, with s as the root. All-pairs: find the shortest paths for all pairs of vertices in the graph. Assumptions: Directed graphs Edges cannot have non-negative weights.

Assumptions For directed graphs. In all our shortest path algorithms, we will allow graphs to be directed. Obviously, any algorithm that works on directed graphs will also work on undirected graphs. Why? Negative edge weights are not allowed. Why?

Assumptions Negative edge weights are not allowed. Why? Obviously, any algorithm that works on directed graphs will also work on undirected graphs. Why? Undirected graphs are a special case of directed graphs. Negative edge weights are not allowed. Why? With negative weights, "shortest paths" may not be defined. If a cycle has negative weight, then repeating that cycle infinitely on a path make it "shorter" and "shorter" Note that the weight of the whole path needs to be negative (not just an edge) for this to happen. If all weights are nonnegative, a shortest path never needs to include a cycle.

Shortest-Paths Spanning Tree Given a network G and a designated vertex s, a shortest-paths spanning tree (SPST) for s is a tree that contains s and all vertices reachable from s, such that: Vertex s is the root of this tree. Each tree path is a shortest path in G.

Dijkstra’s Algorithm Dijkstra(G,w,s) // N = |V| int d[N], p[N] For v =0 -> N-1 d[v]=inf //total weight from s to v p[v]=-1 //predecessor of v on path from s to v d[s]=0 Q = PriorityQueue(d) While notEmpty(Q) u = removeMin(Q,w) for each v adjacent to u if v in Q and (d[u]+w(u,v))<d[v] p[v]=u d[v] = d[u]+w(u,v); //total weight of path from s to v through u decreasedKeyFix(Q,v,d) //v is neither index nor key Add to the MST the vertex, u, with the shortest distance. For each vertex, v, record the shortest distance from s to it and the edge that connects it (like Prim).

Dijkstra’s Algorithm: Runtime Time complexity: O(ElgV) O(V + VlgV + E lgV) = O(ElgV) Assuming V=O(E) Dijkstra(G,w,s) // N = |V| int d[N], p[N] For v =0 -> N-1 ---------------------> Θ(V) d[v]=inf //total weight from s to v p[v]=-1 //predecessor of v on path from s to v d[s]=0 Q = PriorityQueue(d) ---------------------> Θ(V) While notEmpty(Q) ---------------------> O(V) u = removeMin(Q,w) ---------------------> O(lgV) --> O(VlgV) (lines 7 & 8) for each v adjacent to u -----------------> O(E) (from lines 7 & 9) if v in Q and (d[u]+w(u,v))<d[v] p[v]=u d[v] = d[u]+w(u,v); //total weight of path from s to v through u decreasedKeyFix(Q,v,d) //v is neither index nor key ---> O(lgV) --> O(ElgV) (aggregate from both for-loop and while-loop Lines: 7,9,13)

Dijkstra's Algorithm Computes an SPST for a graph G and a source s. Very similar to Prim's algorithm, but: First vertex to add is the source, s. Works with directed graphs as well, whereas Prim's only works with undirected graphs. Requires edge weights to be non-negative. It looks at the total path weight, not just the weight of the current edge. Time complexity: O(E lg V) using a heap for the priority-queue and adjacency list for edges.

Dijkstra’s Algorithm: SPST(G,0) Dijkstra(G,w,s) // N = |V| int d[N], p[N] For v =0 -> N-1 d[v]=inf //total weight from s to v p[v]=-1 //v’s predecessor on path s to v d[s]=0 Q = PriorityQueue(d) While notEmpty(Q) u = removeMin(Q,w) for each v adjacent to u if v in Q and (d[u]+w(u,v))<d[v] p[v]=u d[v] = d[u]+w(u,v); decreasedKeyFix(Q,v,d) Added Vertex, v Edge Dis-tance from s to v Vertex Work Dist (parent)

Dijkstra’s Algorithm 6 2 1 7 3 5 4 Find the SPST(G,5). 20 4 30 20 10 9 Dijkstra(G,w,s) // N = |V| int d[N], p[N] For v =0 -> N-1 d[v]=inf //total weight from s to v p[v]=-1 //v’s predecessor on path s to v d[s]=0 Q = PriorityQueue(d) While notEmpty(Q) u = removeMin(Q,w) for each v adjacent to u if v in Q and (d[u]+w(u,v))<d[v] p[v]=u d[v] = d[u]+w(u,v); decreasedKeyFix(Q,v,d) Dijkstra’s Algorithm Find the SPST(G,5). Show the distance and the parent for each vertex. 1 7 2 5 3 4 6 20 4 30 20 10 18 15 9 11 30 15 25 5 12

Dijkstra(G,w,s) // N = |V| int d[N], p[N] For v =0 -> N-1 d[v]=inf //total weight from s to v p[v]=-1 //v’s predecessor on path s to v d[s]=0 Q = PriorityQueue(d) While notEmpty(Q) u = removeMin(Q,w) for each v adjacent to u if v in Q and (d[u]+w(u,v))<d[v] p[v]=u d[v] = d[u]+w(u,v); decreasedKeyFix(Q,v,d) Dijkstra’s Algorithm 1 7 2 5 3 4 6 20 4 20 30 10 18 15 9 10 28 15 25 5 12

Worked-out Dijkstra example Note that this example is for an undirected graph. The same algorithm will be applied to a directed graph (going in the direction of the arrows). When moving to a new page, the last state of the graph (bottom right) is copied first (top left). Purple edges – edges that change the distance from source to vertex (best edge to take to get to that vertex). Gray edges – edges discovered that do not provide a shorter path to the vertex (discovered, but not used). Red edges and vertices – shortest path spanning tree (SPST) built with Dijkstra.

1 7 2 5 3 4 6 10 20 30 15 28 12 25 18 9 inf (-) 1 7 2 5 3 4 6 10 20 30 15 28 12 25 18 9 (5) (-) inf Distance from source (5) to this vertex 1 7 2 5 3 4 6 10 20 30 15 28 12 25 18 9 (5) (-) (0) 40 1 7 2 5 3 4 6 10 20 30 15 28 12 25 18 9 (5) (-) (0) 17 (4) 40 Parent

1 7 2 5 3 4 6 10 20 30 15 28 12 25 18 9 (5) (-) (0) 17 (4) 40 1 7 2 5 3 4 6 10 20 30 15 28 12 25 18 9 (5) (-) (0) 17 (4) 40 (3) 1 7 2 5 3 4 6 10 20 30 15 28 12 25 18 9 (5) (-) (0) 17 (4) 26 (7) 1 7 2 5 3 4 6 10 20 30 15 28 12 25 18 9 (5) (-) (0) 17 (4) 26 (7)

1 7 2 5 3 4 6 10 20 30 15 28 12 25 18 9 (5) (-) (0) 17 (4) 26 (7) 1 7 2 5 3 4 6 10 20 30 15 28 12 25 18 9 (5) (-) (0) 17 (4) 26 (7) Vertex, as added Edge (parent,vertex) Distance from source to vertex 5 - (5,0) 10 4 (5,4) 12 3 (5,3) 15 7 (4,7) 17 1 (3,1) 25 2 (7,2) 26 6 (0,6) 30 1 7 2 5 3 4 6 10 20 30 15 28 12 25 18 9 (5) (-) (0) 17 (4) 26 (7)

All-Pairs Shortest Paths Run Dijkstra to compute the Shortest Path Spanning Tree (SPST) for each vertex used as source. Note that the array of predecessors completely specifies the SPST. Trick to get the successor (rather than the predecessor/parent) on a path: reverse direction of arrows, solve problem in reversed graph. That solution gives the predecessor in the reversed graph which correspond to successors in the original graph. A 2D table will be needed to store the all pair shortest paths (one row for each SPTS).