Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Similar documents
Single Source Shortest Paths

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Single Source Shortest Paths

BFS Dijkstra. Oct abhi shelat

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk. 10/8/12 CMPS 2200 Intro.

CS 4407 Algorithms Lecture: Shortest Path Algorithms

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Design and Analysis of Algorithms

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Introduction to Algorithms

Introduction to Algorithms

Algorithm Design and Analysis

Algorithms: Lecture 12. Chalmers University of Technology

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

CS 161: Design and Analysis of Algorithms

Introduction to Algorithms

ne a priority queue for the nodes of G; ile (! PQ.is empty( )) select u PQ with d(u) minimal and remove it; I Informatik 1 Kurt Mehlhorn

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

1 Matchings in Non-Bipartite Graphs

Shortest Path Algorithms

directed weighted graphs as flow networks the Ford-Fulkerson algorithm termination and running time

Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

Introduction to Algorithms

Algorithm Design and Analysis

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

1 Some loose ends from last time

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

L feb abhi shelat

CSE 4502/5717 Big Data Analytics Spring 2018; Homework 1 Solutions

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Maximum flow problem (part I)

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

25. Minimum Spanning Trees

25. Minimum Spanning Trees

CS 253: Algorithms. Chapter 24. Shortest Paths. Credit: Dr. George Bebis

Breadth-First Search of Graphs

Discrete Optimization 2010 Lecture 3 Maximum Flows

Query Processing in Spatial Network Databases

CPSC 320 Sample Final Examination December 2013

CS781 Lecture 3 January 27, 2011

CS 6301 PROGRAMMING AND DATA STRUCTURE II Dept of CSE/IT UNIT V GRAPHS

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

NATIONAL UNIVERSITY OF SINGAPORE CS3230 DESIGN AND ANALYSIS OF ALGORITHMS SEMESTER II: Time Allowed 2 Hours

CS 138b Computer Algorithm Homework #14. Ling Li, February 23, Construct the level graph

PATH PROBLEMS IN SKEW-SYMMETRIC GRAPHS ANDREW V. GOLDBERG COMPUTER SCIENCE DEPARTMENT STANFORD UNIVERSITY STANFORD, CA

Preliminaries. Graphs. E : set of edges (arcs) (Undirected) Graph : (i, j) = (j, i) (edges) V = {1, 2, 3, 4, 5}, E = {(1, 3), (3, 2), (2, 4)}

Algorithms Theory. 08 Fibonacci Heaps

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

A faster algorithm for the single source shortest path problem with few distinct positive lengths

Linear-Time Algorithms for Finding Tucker Submatrices and Lekkerkerker-Boland Subgraphs

CMU Lecture 4: Informed Search. Teacher: Gianni A. Di Caro

Ngày 20 tháng 7 năm Discrete Optimization Graphs

Lecture 21 November 11, 2014

NP Completeness. CS 374: Algorithms & Models of Computation, Spring Lecture 23. November 19, 2015

Lecture 2: Network Flows 1

Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao

6.889 Lecture 4: Single-Source Shortest Paths

CS 374: Algorithms & Models of Computation, Spring 2017 Greedy Algorithms Lecture 19 April 4, 2017 Chandra Chekuri (UIUC) CS374 1 Spring / 1

Fundamental Algorithms 11

Priority queues implemented via heaps

Algorithms and Theory of Computation. Lecture 11: Network Flow

Assignment 5: Solutions

Divide-and-Conquer Algorithms Part Two

2-INF-237 Vybrané partie z dátových štruktúr 2-INF-237 Selected Topics in Data Structures

Fall 2017 November 10, Written Homework 5

Graph Search Howie Choset

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

IS 709/809: Computational Methods in IS Research Fall Exam Review

Scribes: Po-Hsuan Wei, William Kuzmaul Editor: Kevin Wu Date: October 18, 2016

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

CS 410/584, Algorithm Design & Analysis: Lecture Notes 3

2 GRAPH AND NETWORK OPTIMIZATION. E. Amaldi Introduction to Operations Research Politecnico Milano 1

Algorithm Design Strategies V

The min cost flow problem Course notes for Optimization Spring 2007

On shredders and vertex connectivity augmentation

Two Applications of Maximum Flow

15.082J & 6.855J & ESD.78J. Algorithm Analysis

Advanced Combinatorial Optimization September 24, Lecture 5

arxiv: v1 [cs.dm] 26 Apr 2010

Week 4. (1) 0 f ij u ij.

Approximation algorithms for cycle packing problems

Algorithms for pattern involvement in permutations

More Approximation Algorithms

Advanced Combinatorial Optimization Updated February 18, Lecture 5. Lecturer: Michel X. Goemans Scribe: Yehua Wei (2009)

Directed Graphs (Digraphs) and Graphs

On Powers of some Intersection Graphs

Part V. Matchings. Matching. 19 Augmenting Paths for Matchings. 18 Bipartite Matching via Flows

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

ICS 252 Introduction to Computer Design

Running Time. Assumption. All capacities are integers between 1 and C.

Introduction to Discrete Optimization

FIBONACCI HEAPS. preliminaries insert extract the minimum decrease key bounding the rank meld and delete. Copyright 2013 Kevin Wayne

Proof methods and greedy algorithms

Part IA Algorithms Notes

Lecture 10 September 27, 2016

CS 580: Algorithm Design and Analysis

Transcription:

CS 374: Algorithms & Models of Computation, Spring 2017 Breadth First Search, Dijkstra s Algorithm for Shortest Paths Lecture 17 March 1, 2017 Chandra Chekuri (UIUC) CS374 1 Spring 2017 1 / 42

Part I Breadth First Search Chandra Chekuri (UIUC) CS374 2 Spring 2017 2 / 42

Breadth First Search (BFS) Overview (A) BFS is obtained from BasicSearch by processing edges using a data structure called a queue. (B) It processes the vertices in the graph in the order of their shortest distance from the vertex s (the start vertex). As such... 1 DFS good for exploring graph structure 2 BFS good for exploring distances Chandra Chekuri (UIUC) CS374 3 Spring 2017 3 / 42

xkcd take on DFS Chandra Chekuri (UIUC) CS374 4 Spring 2017 4 / 42

Queue Data Structure Queues A queue is a list of elements which supports the operations: 1 enqueue: Adds an element to the end of the list 2 dequeue: Removes an element from the front of the list Elements are extracted in first-in first-out (FIFO) order, i.e., elements are picked in the order in which they were inserted. Chandra Chekuri (UIUC) CS374 5 Spring 2017 5 / 42

BFS Algorithm Given (undirected or directed) graph G = (V, E) and node s V Proposition BFS(s) Mark all vertices as unvisited Initialize search tree T to be empty Mark vertex s as visited set Q to be the empty queue enq(s) while Q is nonempty do u = deq(q) for each vertex v Adj(u) if v is not visited then add edge (u, v) to T Mark v as visited and enq(v) BFS(s) runs in O(n + m) time. Chandra Chekuri (UIUC) CS374 Spring 2017 / 42

BFS: An Example in Undirected Graphs 1 7 1 2 3 4 5 8 1. [1] Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 42

BFS: An Example in Undirected Graphs 1 7 1 2 3 2 3 4 5 8 1. [1] 2. [2,3] Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 42

BFS: An Example in Undirected Graphs 1 7 1 2 3 4 5 8 2 3 4 5 1. [1] 2. [2,3] 3. [3,4,5] Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 42

BFS: An Example in Undirected Graphs 1 7 1 2 3 4 5 8 2 3 4 5 8 7 1. [1] 4. [4,5,7,8] 2. [2,3] 3. [3,4,5] Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 42

BFS: An Example in Undirected Graphs 1 7 1 2 3 4 5 8 2 3 4 5 8 7 1. [1] 4. [4,5,7,8] 2. [2,3] 5. [5,7,8] 3. [3,4,5] Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 42

BFS: An Example in Undirected Graphs 1 7 1 2 3 2 3 4 5 8 4 5 8 7 1. [1] 4. [4,5,7,8] 2. [2,3] 5. [5,7,8] 3. [3,4,5]. [7,8,] Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 42

BFS: An Example in Undirected Graphs 1 7 1 2 3 2 3 4 5 8 4 5 8 7 1. [1] 4. [4,5,7,8] 7. [8,] 2. [2,3] 5. [5,7,8] 3. [3,4,5]. [7,8,] Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 42

BFS: An Example in Undirected Graphs 1 7 1 2 3 2 3 4 5 8 4 5 8 7 1. [1] 4. [4,5,7,8] 7. [8,] 2. [2,3] 5. [5,7,8] 8. [] 3. [3,4,5]. [7,8,] Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 42

BFS: An Example in Undirected Graphs 1 7 1 2 3 2 3 4 5 8 4 5 8 7 1. [1] 4. [4,5,7,8] 7. [8,] 2. [2,3] 5. [5,7,8] 8. [] 3. [3,4,5]. [7,8,] 9. [] Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 42

BFS: An Example in Undirected Graphs 1 7 1 2 3 2 3 4 5 8 4 5 8 7 1. [1] 4. [4,5,7,8] 7. [8,] 2. [2,3] 5. [5,7,8] 8. [] 3. [3,4,5]. [7,8,] 9. [] BFS tree is the set of black edges. Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 42

BFS: An Example in Directed Graphs B A C E F D G H lso called a digraph) is G = (V, E), where rtices or nodes set of ordered pairs of vertices called edges Chandra Chekuri (UIUC) CS374 8 Spring 2017 8 / 42

BFS with Distance BFS(s) Mark all vertices as unvisited; for each v set dist(v) = Initialize search tree T to be empty Mark vertex s as visited and set dist(s) = 0 set Q to be the empty queue enq(s) while Q is nonempty do u = deq(q) for each vertex v Adj(u) do if v is not visited do add edge (u, v) to T Mark v as visited, enq(v) and set dist(v) = dist(u) + 1 Chandra Chekuri (UIUC) CS374 9 Spring 2017 9 / 42

Properties of BFS: Undirected Graphs Theorem The following properties hold upon termination of BFS(s) (A) The search tree contains exactly the set of vertices in the connected component of s. (B) If dist(u) < dist(v) then u is visited before v. (C) For every vertex u, dist(u) is the length of a shortest path (in terms of number of edges) from s to u. (D) If u, v are in connected component of s and e = {u, v} is an edge of G, then dist(u) dist(v) 1. Chandra Chekuri (UIUC) CS374 10 Spring 2017 10 / 42

Properties of BFS: Directed Graphs Theorem The following properties hold upon termination of BFS(s): (A) The search tree contains exactly the set of vertices reachable from s (B) If dist(u) < dist(v) then u is visited before v (C) For every vertex u, dist(u) is indeed the length of shortest path from s to u (D) If u is reachable from s and e = (u, v) is an edge of G, then dist(v) dist(u) 1. Not necessarily the case that dist(u) dist(v) 1. Chandra Chekuri (UIUC) CS374 11 Spring 2017 11 / 42

BFS with Layers BFSLayers(s): Mark all vertices as unvisited and initialize T to be empty Mark s as visited and set L 0 = {s} i = 0 while L i is not empty do initialize L i +1 to be an empty list for each u in L i do for each edge (u, v) Adj(u) do if v is not visited mark v as visited add (u, v) to tree T add v to L i +1 i = i + 1 Chandra Chekuri (UIUC) CS374 12 Spring 2017 12 / 42

BFS with Layers BFSLayers(s): Mark all vertices as unvisited and initialize T to be empty Mark s as visited and set L 0 = {s} i = 0 while L i is not empty do initialize L i +1 to be an empty list for each u in L i do for each edge (u, v) Adj(u) do if v is not visited mark v as visited add (u, v) to tree T add v to L i +1 i = i + 1 Running time: O(n + m) Chandra Chekuri (UIUC) CS374 12 Spring 2017 12 / 42

Example 1 7 2 3 4 5 8 Chandra Chekuri (UIUC) CS374 13 Spring 2017 13 / 42

BFS with Layers: Properties Proposition The following properties hold on termination of BFSLayers(s). 1 BFSLayers(s) outputs a BFS tree 2 L i is the set of vertices at distance exactly i from s 3 If G is undirected, each edge e = {u, v} is one of three types: 1 tree edge between two consecutive layers 2 non-tree forward/backward edge between two consecutive layers 3 non-tree cross-edge with both u, v in same layer 4 = Every edge in the graph is either between two vertices that are either (i) in the same layer, or (ii) in two consecutive layers. Chandra Chekuri (UIUC) CS374 14 Spring 2017 14 / 42

Example B A C E F D G H lso called a digraph) is G = (V, E), where rtices or nodes set of ordered pairs of vertices called edges Chandra Chekuri (UIUC) CS374 15 Spring 2017 15 / 42

BFS with Layers: Properties For directed graphs Proposition The following properties hold on termination of BFSLayers(s), if G is directed. For each edge e = (u, v) is one of four types: 1 a tree edge between consecutive layers, u L i, v L i +1 for some i 0 2 a non-tree forward edge between consecutive layers 3 a non-tree backward edge 4 a cross-edge with both u, v in same layer Chandra Chekuri (UIUC) CS374 1 Spring 2017 1 / 42

Part II Shortest Paths and Dijkstra s Algorithm Chandra Chekuri (UIUC) CS374 17 Spring 2017 17 / 42

Shortest Path Problems Shortest Path Problems Input A (undirected or directed) graph G = (V, E) with edge lengths (or costs). For edge e = (u, v), l(e) = l(u, v) is its length. 1 Given nodes s, t find shortest path from s to t. 2 Given node s find shortest path from s to all other nodes. 3 Find shortest paths for all pairs of nodes. Chandra Chekuri (UIUC) CS374 18 Spring 2017 18 / 42

Shortest Path Problems Shortest Path Problems Input A (undirected or directed) graph G = (V, E) with edge lengths (or costs). For edge e = (u, v), l(e) = l(u, v) is its length. 1 Given nodes s, t find shortest path from s to t. 2 Given node s find shortest path from s to all other nodes. 3 Find shortest paths for all pairs of nodes. Many applications! Chandra Chekuri (UIUC) CS374 18 Spring 2017 18 / 42

Single-Source Shortest Paths: Non-Negative Edge Lengths Single-Source Shortest Path Problems 1 Input: A (undirected or directed) graph G = (V, E) with non-negative edge lengths. For edge e = (u, v), l(e) = l(u, v) is its length. 2 Given nodes s, t find shortest path from s to t. 3 Given node s find shortest path from s to all other nodes. Chandra Chekuri (UIUC) CS374 19 Spring 2017 19 / 42

Single-Source Shortest Paths: Non-Negative Edge Lengths Single-Source Shortest Path Problems 1 Input: A (undirected or directed) graph G = (V, E) with non-negative edge lengths. For edge e = (u, v), l(e) = l(u, v) is its length. 2 Given nodes s, t find shortest path from s to t. 3 Given node s find shortest path from s to all other nodes. 1 Restrict attention to directed graphs 2 Undirected graph problem can be reduced to directed graph problem - how? Chandra Chekuri (UIUC) CS374 19 Spring 2017 19 / 42

Single-Source Shortest Paths: Non-Negative Edge Lengths Single-Source Shortest Path Problems 1 Input: A (undirected or directed) graph G = (V, E) with non-negative edge lengths. For edge e = (u, v), l(e) = l(u, v) is its length. 2 Given nodes s, t find shortest path from s to t. 3 Given node s find shortest path from s to all other nodes. 1 Restrict attention to directed graphs 2 Undirected graph problem can be reduced to directed graph problem - how? 1 Given undirected graph G, create a new directed graph G by replacing each edge {u, v} in G by (u, v) and (v, u) in G. 2 set l(u, v) = l(v, u) = l({u, v}) 3 Exercise: show reduction works. Relies on non-negativity! Chandra Chekuri (UIUC) CS374 19 Spring 2017 19 / 42

Single-Source Shortest Paths via BFS Special case: All edge lengths are 1. Chandra Chekuri (UIUC) CS374 20 Spring 2017 20 / 42

Single-Source Shortest Paths via BFS Special case: All edge lengths are 1. 1 Run BFS(s) to get shortest path distances from s to all other nodes. 2 O(m + n) time algorithm. Chandra Chekuri (UIUC) CS374 20 Spring 2017 20 / 42

Single-Source Shortest Paths via BFS Special case: All edge lengths are 1. 1 Run BFS(s) to get shortest path distances from s to all other nodes. 2 O(m + n) time algorithm. Special case: Suppose l(e) is an integer for all e? Can we use BFS? Chandra Chekuri (UIUC) CS374 20 Spring 2017 20 / 42

Single-Source Shortest Paths via BFS Special case: All edge lengths are 1. 1 Run BFS(s) to get shortest path distances from s to all other nodes. 2 O(m + n) time algorithm. Special case: Suppose l(e) is an integer for all e? Can we use BFS? Reduce to unit edge-length problem by placing l(e) 1 dummy nodes on e Chandra Chekuri (UIUC) CS374 20 Spring 2017 20 / 42

Single-Source Shortest Paths via BFS Special case: All edge lengths are 1. 1 Run BFS(s) to get shortest path distances from s to all other nodes. 2 O(m + n) time algorithm. Special case: Suppose l(e) is an integer for all e? Can we use BFS? Reduce to unit edge-length problem by placing l(e) 1 dummy nodes on e Let L = max e l(e). New graph has O(mL) edges and O(mL + n) nodes. BFS takes O(mL + n) time. Not efficient if L is large. Chandra Chekuri (UIUC) CS374 20 Spring 2017 20 / 42

Towards an algorithm Why does BFS work? Chandra Chekuri (UIUC) CS374 21 Spring 2017 21 / 42

Towards an algorithm Why does BFS work? BFS(s) explores nodes in increasing distance from s Chandra Chekuri (UIUC) CS374 21 Spring 2017 21 / 42

Towards an algorithm Why does BFS work? BFS(s) explores nodes in increasing distance from s Lemma Let G be a directed graph with non-negative edge lengths. Let dist(s, v) denote the shortest path length from s to v. If s = v 0 v 1 v 2... v k is a shortest path from s to v k then for 1 i < k: 1 s = v 0 v 1 v 2... v i is a shortest path from s to v i 2 dist(s, v i ) dist(s, v k ). Relies on non-neg edge lengths. Chandra Chekuri (UIUC) CS374 21 Spring 2017 21 / 42

Towards an algorithm Proof. Suppose not. Then for some i < k there is a path P from s to v i of length strictly less than that of s = v 0 v 1... v i. Then P concatenated with v i v i +1... v k contains a strictly shorter path to v k than s = v 0 v 1... v k. For the second part, Chandra observe Chekuri (UIUC) that edge lengths CS374 are non-negative. 21 Spring 2017 21 / 42 Lemma Let G be a directed graph with non-negative edge lengths. Let dist(s, v) denote the shortest path length from s to v. If s = v 0 v 1 v 2... v k is a shortest path from s to v k then for 1 i < k: 1 s = v 0 v 1 v 2... v i is a shortest path from s to v i 2 dist(s, v i ) dist(s, v k ). Relies on non-neg edge lengths.

A proof by picture s = v 0 v 2 v 4 v 5 v v 1 v 3 Shortest path from v 0 to v Chandra Chekuri (UIUC) CS374 22 Spring 2017 22 / 42

A proof by picture Shorter path from v 0 to v 4 s = v 0 v 2 v 4 v 5 v v 1 v 3 Shortest path from v 0 to v Chandra Chekuri (UIUC) CS374 22 Spring 2017 22 / 42

A proof by picture A shorter path from v 0 to v. A contradiction. s = v 0 v 2 v 4 v 5 v v 1 v 3 Shortest path from v 0 to v Chandra Chekuri (UIUC) CS374 22 Spring 2017 22 / 42

A Basic Strategy Explore vertices in increasing order of distance from s: (For simplicity assume that nodes are at different distances from s and that no edge has zero length) Initialize for each node v, dist(s, v) = Initialize X = {s}, for i = 2 to V do (* Invariant: X contains the i 1 closest nodes to s *) Among nodes in V X, find the node v that is the i th closest to s Update dist(s, v) X = X {v} Chandra Chekuri (UIUC) CS374 23 Spring 2017 23 / 42

A Basic Strategy Explore vertices in increasing order of distance from s: (For simplicity assume that nodes are at different distances from s and that no edge has zero length) Initialize for each node v, dist(s, v) = Initialize X = {s}, for i = 2 to V do (* Invariant: X contains the i 1 closest nodes to s *) Among nodes in V X, find the node v that is the i th closest to s Update dist(s, v) X = X {v} How can we implement the step in the for loop? Chandra Chekuri (UIUC) CS374 23 Spring 2017 23 / 42

Finding the ith closest node 1 X contains the i 1 closest nodes to s 2 Want to find the ith closest node from V X. What do we know about the ith closest node? Chandra Chekuri (UIUC) CS374 24 Spring 2017 24 / 42

Finding the ith closest node 1 X contains the i 1 closest nodes to s 2 Want to find the ith closest node from V X. What do we know about the ith closest node? Claim Let P be a shortest path from s to v where v is the ith closest node. Then, all intermediate nodes in P belong to X. Chandra Chekuri (UIUC) CS374 24 Spring 2017 24 / 42

Finding the ith closest node 1 X contains the i 1 closest nodes to s 2 Want to find the ith closest node from V X. What do we know about the ith closest node? Claim Let P be a shortest path from s to v where v is the ith closest node. Then, all intermediate nodes in P belong to X. Proof. If P had an intermediate node u not in X then u will be closer to s than v. Implies v is not the i th closest node to s - recall that X already has the i 1 closest nodes. Chandra Chekuri (UIUC) CS374 24 Spring 2017 24 / 42

Finding the ith closest node repeatedly An example a 9 0 13 8 10 18 30 20 11 1 19 25 Chandra Chekuri (UIUC) CS374 25 Spring 2017 25 / 42

Finding the ith closest node repeatedly An example a 9 0 13 e b 8 c 10 18 30 20 25 d 11 1 f 19 g h Chandra Chekuri (UIUC) CS374 25 Spring 2017 25 / 42

Finding the ith closest node repeatedly An example a 9 0 13 e b 8 c 10 18 30 20 25 d 11 1 f 19 g h Chandra Chekuri (UIUC) CS374 25 Spring 2017 25 / 42

Finding the ith closest node repeatedly An example a 9 0 13 e b 9 8 c 10 18 30 20 25 d 11 1 f 19 g h Chandra Chekuri (UIUC) CS374 25 Spring 2017 25 / 42

Finding the ith closest node repeatedly An example a 9 0 13 e 13 b 9 8 c 10 18 30 20 25 d 11 1 f 19 g h Chandra Chekuri (UIUC) CS374 25 Spring 2017 25 / 42

Finding the ith closest node repeatedly An example a 9 0 13 e 13 b 9 8 c 10 18 30 20 25 d f 19 11 1 19 g h Chandra Chekuri (UIUC) CS374 25 Spring 2017 25 / 42

Finding the ith closest node repeatedly An example a 9 0 13 e 13 b 9 8 c 20 10 18 30 25 d 25 f 19 11 1 19 g h Chandra Chekuri (UIUC) CS374 25 Spring 2017 25 / 42

Finding the ith closest node repeatedly An example a 9 0 13 e 13 b 9 8 c 20 10 18 30 25 d 25 f 19 11 1 g 3 19 h Chandra Chekuri (UIUC) CS374 25 Spring 2017 25 / 42

Finding the ith closest node repeatedly An example a 9 0 13 e 13 b 9 8 c 20 10 18 30 25 d 25 f 19 11 1 g 3 19 38 h Chandra Chekuri (UIUC) CS374 25 Spring 2017 25 / 42

Finding the ith closest node a 9 0 13 8 10 18 30 20 11 1 19 25 Corollary The ith closest node is adjacent to X. Chandra Chekuri (UIUC) CS374 2 Spring 2017 2 / 42

Finding the ith closest node 1 X contains the i 1 closest nodes to s 2 Want to find the ith closest node from V X. 1 For each u V X let P(s, u, X ) be a shortest path from s to u using only nodes in X as intermediate vertices. 2 Let d (s, u) be the length of P(s, u, X ) Chandra Chekuri (UIUC) CS374 27 Spring 2017 27 / 42

Finding the ith closest node 1 X contains the i 1 closest nodes to s 2 Want to find the ith closest node from V X. 1 For each u V X let P(s, u, X ) be a shortest path from s to u using only nodes in X as intermediate vertices. 2 Let d (s, u) be the length of P(s, u, X ) Observations: for each u V X, 1 dist(s, u) d (s, u) since we are constraining the paths 2 d (s, u) = min t X (dist(s, t) + l(t, u)) - Why? Chandra Chekuri (UIUC) CS374 27 Spring 2017 27 / 42

Finding the ith closest node 1 X contains the i 1 closest nodes to s 2 Want to find the ith closest node from V X. 1 For each u V X let P(s, u, X ) be a shortest path from s to u using only nodes in X as intermediate vertices. 2 Let d (s, u) be the length of P(s, u, X ) Observations: for each u V X, 1 dist(s, u) d (s, u) since we are constraining the paths 2 d (s, u) = min t X (dist(s, t) + l(t, u)) - Why? Lemma If v is the ith closest node to s, then d (s, v) = dist(s, v). Chandra Chekuri (UIUC) CS374 27 Spring 2017 27 / 42

Finding the ith closest node Lemma Given: 1 X : Set of i 1 closest nodes to s. 2 d (s, u) = min t X (dist(s, t) + l(t, u)) If v is an ith closest node to s, then d (s, v) = dist(s, v). Proof. Let v be the ith closest node to s. Then there is a shortest path P from s to v that contains only nodes in X as intermediate nodes (see previous claim). Therefore d (s, v) = dist(s, v). Chandra Chekuri (UIUC) CS374 28 Spring 2017 28 / 42

Finding the ith closest node Lemma If v is an ith closest node to s, then d (s, v) = dist(s, v). Corollary The ith closest node to s is the node v V X such that d (s, v) = min u V X d (s, u). Proof. For every node u V X, dist(s, u) d (s, u) and for the ith closest node v, dist(s, v) = d (s, v). Moreover, dist(s, u) dist(s, v) for each u V S. Chandra Chekuri (UIUC) CS374 29 Spring 2017 29 / 42

Algorithm Initialize for each node v: dist(s, v) = Initialize X =, d (s, s) = 0 for i = 1 to V do (* Invariant: X contains the i 1 closest nodes to s *) (* Invariant: d (s, u) is shortest path distance from u to s using only X as intermediate nodes*) Let v be such that d (s, v) = min u V X d (s, u) dist(s, v) = d (s, v) X = X {v} for each node u in V X do ) d (s, u) = min t X (dist(s, t) + l(t, u) Chandra Chekuri (UIUC) CS374 30 Spring 2017 30 / 42

Algorithm Initialize for each node v: dist(s, v) = Initialize X =, d (s, s) = 0 for i = 1 to V do (* Invariant: X contains the i 1 closest nodes to s *) (* Invariant: d (s, u) is shortest path distance from u to s using only X as intermediate nodes*) Let v be such that d (s, v) = min u V X d (s, u) dist(s, v) = d (s, v) X = X {v} for each node u in V X do ) d (s, u) = min t X (dist(s, t) + l(t, u) Correctness: By induction on i using previous lemmas. Chandra Chekuri (UIUC) CS374 30 Spring 2017 30 / 42

Algorithm Initialize for each node v: dist(s, v) = Initialize X =, d (s, s) = 0 for i = 1 to V do (* Invariant: X contains the i 1 closest nodes to s *) (* Invariant: d (s, u) is shortest path distance from u to s using only X as intermediate nodes*) Let v be such that d (s, v) = min u V X d (s, u) dist(s, v) = d (s, v) X = X {v} for each node u in V X do ) d (s, u) = min t X (dist(s, t) + l(t, u) Correctness: By induction on i using previous lemmas. Running time: Chandra Chekuri (UIUC) CS374 30 Spring 2017 30 / 42

Algorithm Initialize for each node v: dist(s, v) = Initialize X =, d (s, s) = 0 for i = 1 to V do (* Invariant: X contains the i 1 closest nodes to s *) (* Invariant: d (s, u) is shortest path distance from u to s using only X as intermediate nodes*) Let v be such that d (s, v) = min u V X d (s, u) dist(s, v) = d (s, v) X = X {v} for each node u in V X do ) d (s, u) = min t X (dist(s, t) + l(t, u) Correctness: By induction on i using previous lemmas. Running time: O(n (n + m)) time. 1 n outer iterations. In each iteration, d (s, u) for each u by scanning all edges out of nodes in X ; O(m + n) time/iteration. Chandra Chekuri (UIUC) CS374 30 Spring 2017 30 / 42

Example s 9 13 8 10 18 30 20 11 1 19 25 0 Chandra Chekuri (UIUC) CS374 31 Spring 2017 31 / 42

Example s 9 13 8 10 18 30 20 11 1 19 25 0 Chandra Chekuri (UIUC) CS374 31 Spring 2017 31 / 42

Example s 9 13 8 10 18 30 20 11 1 19 25 9 9 0 Chandra Chekuri (UIUC) CS374 31 Spring 2017 31 / 42

Example s 9 13 8 10 18 30 20 11 1 19 25 9 9 0 13 13 Chandra Chekuri (UIUC) CS374 31 Spring 2017 31 / 42

Example s 9 13 8 10 18 30 20 11 1 19 25 9 10 9 19 0 13 13 Chandra Chekuri (UIUC) CS374 31 Spring 2017 31 / 42

Example s 9 13 8 10 18 30 20 11 1 19 25 9 10 9 19 0 13 25 13 Chandra Chekuri (UIUC) CS374 31 Spring 2017 31 / 42

Example s 9 13 8 10 18 30 20 11 1 19 25 9 10 9 19 0 13 25 11 3 13 Chandra Chekuri (UIUC) CS374 31 Spring 2017 31 / 42

Example s 9 13 8 10 18 30 20 11 1 19 25 9 10 9 19 0 13 25 11 3 25 13 38 Chandra Chekuri (UIUC) CS374 31 Spring 2017 31 / 42

Improved Algorithm 1 Main work is to compute the d (s, u) values in each iteration 2 d (s, u) changes from iteration i to i + 1 only because of the node v that is added to X in iteration i. Chandra Chekuri (UIUC) CS374 32 Spring 2017 32 / 42

Improved Algorithm 1 Main work is to compute the d (s, u) values in each iteration 2 d (s, u) changes from iteration i to i + 1 only because of the node v that is added to X in iteration i. Initialize for each node v, dist(s, v) = d (s, v) = Initialize X =, d (s, s) = 0 for i = 1 to V do // X contains the i 1 closest nodes to s, // and the values of d (s, u) are current Let v be node realizing d (s, v) = min u V X d (s, u) dist(s, v) = d (s, v) X = X {v} Update d (s, u) for( each u in V X as follows: ) d (s, u) = min d (s, u), dist(s, v) + l(v, u) Running time: Chandra Chekuri (UIUC) CS374 32 Spring 2017 32 / 42

Improved Algorithm Initialize for each node v, dist(s, v) = d (s, v) = Initialize X =, d (s, s) = 0 for i = 1 to V do // X contains the i 1 closest nodes to s, // and the values of d (s, u) are current Let v be node realizing d (s, v) = min u V X d (s, u) dist(s, v) = d (s, v) X = X {v} Update d (s, u) for( each u in V X as follows: ) d (s, u) = min d (s, u), dist(s, v) + l(v, u) Running time: O(m + n 2 ) time. 1 n outer iterations and in each iteration following steps 2 updating d (s, u) after v is added takes O(deg(v)) time so total work is O(m) since a node enters X only once 3 Finding v from d (s, u) values is O(n) time Chandra Chekuri (UIUC) CS374 32 Spring 2017 32 / 42

Dijkstra s Algorithm 1 eliminate d (s, u) and let dist(s, u) maintain it 2 update dist values after adding v by scanning edges out of v Initialize for each node v, dist(s, v) = Initialize X =, dist(s, s) = 0 for i = 1 to V do Let v be such that dist(s, v) = min u V X dist(s, u) X = X {v} for each u in Adj(v) ( do ) dist(s, u) = min dist(s, u), dist(s, v) + l(v, u) Priority Queues to maintain dist values for faster running time Chandra Chekuri (UIUC) CS374 33 Spring 2017 33 / 42

Dijkstra s Algorithm 1 eliminate d (s, u) and let dist(s, u) maintain it 2 update dist values after adding v by scanning edges out of v Initialize for each node v, dist(s, v) = Initialize X =, dist(s, s) = 0 for i = 1 to V do Let v be such that dist(s, v) = min u V X dist(s, u) X = X {v} for each u in Adj(v) ( do ) dist(s, u) = min dist(s, u), dist(s, v) + l(v, u) Priority Queues to maintain dist values for faster running time 1 Using heaps and standard priority queues: O((m + n) log n) 2 Using Fibonacci heaps: O(m + n log n). Chandra Chekuri (UIUC) CS374 33 Spring 2017 33 / 42

Priority Queues Data structure to store a set S of n elements where each element v S has an associated real/integer key k(v) such that the following operations: 1 makepq: create an empty queue. 2 findmin: find the minimum key in S. 3 extractmin: Remove v S with smallest key and return it. 4 insert(v, k(v)): Add new element v with key k(v) to S. 5 delete(v): Remove element v from S. Chandra Chekuri (UIUC) CS374 34 Spring 2017 34 / 42

Priority Queues Data structure to store a set S of n elements where each element v S has an associated real/integer key k(v) such that the following operations: 1 makepq: create an empty queue. 2 findmin: find the minimum key in S. 3 extractmin: Remove v S with smallest key and return it. 4 insert(v, k(v)): Add new element v with key k(v) to S. 5 delete(v): Remove element v from S. decreasekey(v, k (v)): decrease key of v from k(v) (current key) to k (v) (new key). Assumption: k (v) k(v). 7 meld: merge two separate priority queues into one. Chandra Chekuri (UIUC) CS374 34 Spring 2017 34 / 42

Priority Queues Data structure to store a set S of n elements where each element v S has an associated real/integer key k(v) such that the following operations: 1 makepq: create an empty queue. 2 findmin: find the minimum key in S. 3 extractmin: Remove v S with smallest key and return it. 4 insert(v, k(v)): Add new element v with key k(v) to S. 5 delete(v): Remove element v from S. decreasekey(v, k (v)): decrease key of v from k(v) (current key) to k (v) (new key). Assumption: k (v) k(v). 7 meld: merge two separate priority queues into one. All operations can be performed in O(log n) time. decreasekey is implemented via delete and insert. Chandra Chekuri (UIUC) CS374 34 Spring 2017 34 / 42

Dijkstra s Algorithm using Priority Queues Q makepq() insert(q, (s, 0)) for each node u s do insert(q, (u, )) X for i = 1 to V do (v, dist(s, v)) = extractmin(q) X = X {v} for each u in Adj(v) ( do decreasekey Q, ( u, min ( dist(s, u), dist(s, v) + l(v, u) ))). Priority Queue operations: 1 O(n) insert operations 2 O(n) extractmin operations 3 O(m) decreasekey operations Chandra Chekuri (UIUC) CS374 35 Spring 2017 35 / 42

Implementing Priority Queues via Heaps Using Heaps Store elements in a heap based on the key value 1 All operations can be done in O(log n) time Chandra Chekuri (UIUC) CS374 3 Spring 2017 3 / 42

Implementing Priority Queues via Heaps Using Heaps Store elements in a heap based on the key value 1 All operations can be done in O(log n) time Dijkstra s algorithm can be implemented in O((n + m) log n) time. Chandra Chekuri (UIUC) CS374 3 Spring 2017 3 / 42

Priority Queues: Fibonacci Heaps/Relaxed Heaps Fibonacci Heaps 1 extractmin, insert, delete, meld in O(log n) time 2 decreasekey in O(1) amortized time: Chandra Chekuri (UIUC) CS374 37 Spring 2017 37 / 42

Priority Queues: Fibonacci Heaps/Relaxed Heaps Fibonacci Heaps 1 extractmin, insert, delete, meld in O(log n) time 2 decreasekey in O(1) amortized time: l decreasekey operations for l n take together O(l) time 3 Relaxed Heaps: decreasekey in O(1) worst case time but at the expense of meld (not necessary for Dijkstra s algorithm) Chandra Chekuri (UIUC) CS374 37 Spring 2017 37 / 42

Priority Queues: Fibonacci Heaps/Relaxed Heaps Fibonacci Heaps 1 extractmin, insert, delete, meld in O(log n) time 2 decreasekey in O(1) amortized time: l decreasekey operations for l n take together O(l) time 3 Relaxed Heaps: decreasekey in O(1) worst case time but at the expense of meld (not necessary for Dijkstra s algorithm) 1 Dijkstra s algorithm can be implemented in O(n log n + m) time. If m = Ω(n log n), running time is linear in input size. Chandra Chekuri (UIUC) CS374 37 Spring 2017 37 / 42

Priority Queues: Fibonacci Heaps/Relaxed Heaps Fibonacci Heaps 1 extractmin, insert, delete, meld in O(log n) time 2 decreasekey in O(1) amortized time: l decreasekey operations for l n take together O(l) time 3 Relaxed Heaps: decreasekey in O(1) worst case time but at the expense of meld (not necessary for Dijkstra s algorithm) 1 Dijkstra s algorithm can be implemented in O(n log n + m) time. If m = Ω(n log n), running time is linear in input size. 2 Data structures are complicated to analyze/implement. Recent work has obtained data structures that are easier to analyze and implement, and perform well in practice. Rank-Pairing Heaps (European Symposium on Algorithms, September 2009!) Chandra Chekuri (UIUC) CS374 37 Spring 2017 37 / 42

Shortest Path Tree Dijkstra s algorithm finds the shortest path distances from s to V. Question: How do we find the paths themselves? Chandra Chekuri (UIUC) CS374 38 Spring 2017 38 / 42

Shortest Path Tree Dijkstra s algorithm finds the shortest path distances from s to V. Question: How do we find the paths themselves? Q = makepq() insert(q, (s, 0)) prev(s) null for each node u s do insert(q, (u, ) ) prev(u) null X = for i = 1 to V do (v, dist(s, v)) = extractmin(q) X = X {v} for each u in Adj(v) do if (dist(s, v) + l(v, u) < dist(s, u)) then decreasekey(q, (u, dist(s, v) + l(v, u))) prev(u) = v Chandra Chekuri (UIUC) CS374 38 Spring 2017 38 / 42

Shortest Path Tree Lemma The edge set (u, prev(u)) is the reverse of a shortest path tree rooted at s. For each u, the reverse of the path from u to s in the tree is a shortest path from s to u. Proof Sketch. 1 The edge set {(u, prev(u)) u V } induces a directed in-tree rooted at s (Why?) 2 Use induction on X to argue that the tree is a shortest path tree for nodes in V. Chandra Chekuri (UIUC) CS374 39 Spring 2017 39 / 42

Shortest paths to s Dijkstra s algorithm gives shortest paths from s to all nodes in V. How do we find shortest paths from all of V to s? Chandra Chekuri (UIUC) CS374 40 Spring 2017 40 / 42

Shortest paths to s Dijkstra s algorithm gives shortest paths from s to all nodes in V. How do we find shortest paths from all of V to s? 1 In undirected graphs shortest path from s to u is a shortest path from u to s so there is no need to distinguish. 2 In directed graphs, use Dijkstra s algorithm in G rev! Chandra Chekuri (UIUC) CS374 40 Spring 2017 40 / 42

Shortest paths between sets of nodes Suppose we are given S V and T V. Want to find shortest path from S to T defined as: dist(s, T ) = min dist(s, t) s S,t T How do we find dist(s, T )? Chandra Chekuri (UIUC) CS374 41 Spring 2017 41 / 42

Example Problem You want to go from your house to a friend s house. Need to pick up some dessert along the way and hence need to stop at one of the many potential stores along the way. How do you calculate the shortest trip if you include this stop? Chandra Chekuri (UIUC) CS374 42 Spring 2017 42 / 42

Example Problem You want to go from your house to a friend s house. Need to pick up some dessert along the way and hence need to stop at one of the many potential stores along the way. How do you calculate the shortest trip if you include this stop? Given G = (V, E) and edge lengths l(e), e E. Want to go from s to t. A subset X V that corresponds to stores. Want to find min x X d(s, x) + d(x, t). Chandra Chekuri (UIUC) CS374 42 Spring 2017 42 / 42

Example Problem You want to go from your house to a friend s house. Need to pick up some dessert along the way and hence need to stop at one of the many potential stores along the way. How do you calculate the shortest trip if you include this stop? Given G = (V, E) and edge lengths l(e), e E. Want to go from s to t. A subset X V that corresponds to stores. Want to find min x X d(s, x) + d(x, t). Basic solution: Compute for each x X, d(s, x) and d(x, t) and take minimum. 2 X shortest path computations. O( X (m + n log n)). Chandra Chekuri (UIUC) CS374 42 Spring 2017 42 / 42

Example Problem You want to go from your house to a friend s house. Need to pick up some dessert along the way and hence need to stop at one of the many potential stores along the way. How do you calculate the shortest trip if you include this stop? Given G = (V, E) and edge lengths l(e), e E. Want to go from s to t. A subset X V that corresponds to stores. Want to find min x X d(s, x) + d(x, t). Basic solution: Compute for each x X, d(s, x) and d(x, t) and take minimum. 2 X shortest path computations. O( X (m + n log n)). Better solution: Compute shortest path distances from s to every node v V with one Dijkstra. Compute from every node v V shortest path distance to t with one Dijkstra. Chandra Chekuri (UIUC) CS374 42 Spring 2017 42 / 42