Single-Source Shortest Path & Minimum Spanning Trees

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

CS138A Single Source Shortest Paths Peter Schröder.
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,
Introduction to Algorithms 6.046J/18.401J/SMA5503
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
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.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.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
Topological Sorting and Least-cost Path Algorithms.
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
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/10/2015 ITCS 6114 Single-Source Shortest Path.
CISC 235: Topic 11 Shortest Paths Algorithms. CISC 235 Topic 112 Outline Single-Source Shortest Paths Algorithm for Unweighted Graphs Algorithm for Weighted,
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
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.
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.
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)
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,
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Minimum Spanning Trees
Lecture 13 Shortest Path.
Single-Source Shortest Path
Graph Algorithms BFS, DFS, Dijkstra’s.
Single-Source Shortest Paths
Minimum Spanning Tree Chapter 13.6.
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree Neil Tang 3/25/2010
SINGLE-SOURCE SHORTEST PATHS
Algorithms and Data Structures Lecture XII
Page 620 Single-Source Shortest Paths
Lecture 24 CSE 331 Oct 29, 2012.
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
Dijkstra Algorithm.
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Minimum Spanning Tree Neil Tang 4/3/2008
Lecture 13 Algorithm Analysis
The Bellman-Ford Shortest Path Algorithm Neil Tang 03/27/2008
Algorithms Searching in a Graph.
Minimum spanning tree Shortest path algorithms
Single-Source Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
Advanced Algorithms Analysis and Design
Dijkstra’s Shortest Path Algorithm
Bellman Ford.
CS 3013: DS & Algorithms Shortest Paths.
Prim’s algorithm for minimum spanning trees
Winter 2019 Lecture 10 Minimum Spanning Trees
Topological Sorting Minimum Spanning Trees Shortest Path
Advanced Algorithms Analysis and Design
Presentation transcript:

Single-Source Shortest Path & Minimum Spanning Trees Dijkstra’s, Prim’s, and Kruskal’s Algorithms

Single-Source Shortest Path Defined Given G = (V, E) Source vertex s  V Compute {SP} = set of shortest paths from s to all vertices in V

Path Weight Defined Weight of path p = {v1, v2, … vk} Shortest-path weight from u to v

Determining Path Lengths 1 3 s b 5 2 4 c

Determining Path Lengths 3 1 3 s b 5 5 2 4 c 2

Determining Path Lengths 3 1 3 s b 5 5 2 4 c d[b] <= d[c] + w(c,b) 5 <= 2 + 4 2

Determining Path Lengths d[b] > d[a] + w(a,b) 5 > 3 + 1 3 1 3 s b 5 5 2 4 c 2

Determining Path Lengths d[b] > d[a] + w(a,b) 5 > 3 + 1 3 1 3 s b 5 4 2 4 c 2 Reduce

Path Lengths - FIN a All vertices are in S 3 1 3 s b 5 4 2 4 c 2

Cycles in Paths a b -4 3 -1 3 4 s 6 c d g 5 8 5 11 - -3 2 3 7 e f - 5 11 - -3 2 3 7 e f - - -6

Properties of Shortest Paths Cannot contain a negative-weight cycle No need to contain 0-weight cycle No need to contain positive-weight cycle Thus contains no cycles Will contain at most V-1 edges Assumes no cycles (as stated above) Maintain predecessor information E & V

Shortest Paths Not Unique x 6 3 9 3 s 4 2 1 2 7 3 5 5 11 6 y z t x t x 3 6 9 3 6 9 3 3 s 4 s 4 2 1 2 7 2 1 2 7 3 3 5 11 5 11 5 5 6 6 y y z z

Shortest Path Distance Is Unique x s u z v y [z] must be either x or y, but not both This is true even if d[x] + w(x,z) = d[y] + w(y,z) establishing that if s is source, d[z] is well-defined and unique

Setting up the Solution For each vertex v in V[G] Predecessor of v ([v]) = NIL Distance to v from source (d[v]) =  RELAX: as we process the graph, we’ll “Improve” [v] & d[v] by “relaxing” if (d[v] > d[u] + w(u,v)) d[v] = d[u] + w(u,v) [v] = u

Init & Relax Initialize(G, s) for each v in V[G] d[v] = INFINITY [v] = NULL d[s] = 0 Relax(u, v, w) if (d[v] > d[u] + w(u,v)) d[v] = d[u] + w(u,v) [v] = u

Dijkstra’s Algorithm (1959) Assume G = (V, E), source s, & weight function w Also assume all edges are non-negative Dijkstra(G, w, s) Initialize(G, s) S = {} Q = V[G] while Q != {} u = Extract_Min(Q) S.Add(u) for each vertex v in u.Adjacency() Relax(u, v, w)

Example: Dijkstra’s t x 1   10 s 9 2 3 4 6 7 5   y 2 z

Example: Dijkstra’s t x 1 10  10 s 9 2 3 4 6 7 5 5  y 2 z

Example: Dijkstra’s t x 1 8 14 10 s 9 2 3 4 6 7 5 5 7 y 2 z

Example: Dijkstra’s t x 1 8 13 10 s 9 2 3 4 6 7 5 5 7 y 2 z

Example: Dijkstra’s t x 1 8 9 10 s 9 2 3 4 6 7 5 5 7 y 2 z

Dijkstra’s - FIN t x 1 8 9 10 s 9 2 3 4 6 7 5 5 7 y 2 z

Runtime of Dijkstra’s O(V lgV + E lgV) O(E lgV) O(V lgV + E) If G is connected O(V lgV + E) If we use a Fibonacci heap Better if V << E (i.e. sparsely connected)

MST Defined

Minimum Spanning Tree Connect all nodes at minimum cost. Can start anywhere? Why? One solution. i.e. There is just one minimum Two algorithms Prim’s Kruskal’s

A Graph 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Prim's start 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Prim's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Prim's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Prim's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Prim's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Prim's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Prim's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Prim's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Prim's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Prim's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Prim's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Prim's 5 4 4 2 5 2 5 6 6 44 5

The Same Graph 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Kruskal's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Kruskal's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Kruskal's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Kruskal's 5 4 9 4 2 5 12 6 8 7 2 5 8 6 8 7 9 12 6 5 11

Kruskal's 5 4 4 2 5 2 5 6 6 44 5