Introduction to Algorithms

Similar documents
Introduction to Algorithms

Introduction to Algorithms

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

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

Single Source Shortest Paths

Introduction to Algorithms

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Single Source Shortest Paths

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Design and Analysis of Algorithms

Introduction to Algorithms

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

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

Algorithm Design and Analysis

Shortest Path Algorithms

Lecture 11. Single-Source Shortest Paths All-Pairs Shortest Paths

Data Structures and and Algorithm Xiaoqing Zheng

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

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Algorithm Design and Analysis

BFS Dijkstra. Oct abhi shelat

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

COMP251: Bipartite graphs

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

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

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

Discrete Optimization 2010 Lecture 3 Maximum Flows

COMP251: Bipartite graphs

Myriad of applications

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Shortest Paths. CS 320, Fall Dr. Geri Georg, Instructor 320 ShortestPaths 3

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

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

Algorithms: Lecture 12. Chalmers University of Technology

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

Query Processing in Spatial Network Databases

Design and Analysis of Algorithms

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

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

Breadth-First Search of Graphs

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Lecture Notes Discrete Optimization

ICS 252 Introduction to Computer Design

Partha Sarathi Mandal

Algorithm Design and Analysis

L feb abhi shelat

Algorithm Design and Analysis

Matching Residents to Hospitals

Discrete Mathematics, Spring 2004 Homework 9 Sample Solutions

CS 580: Algorithm Design and Analysis

CS781 Lecture 3 January 27, 2011

1. The graph of a function f is given above. Answer the question: a. Find the value(s) of x where f is not differentiable. Ans: x = 4, x = 3, x = 2,

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

Worksheets for GCSE Mathematics. Quadratics. mr-mathematics.com Maths Resources for Teachers. Algebra

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

Dynamic Programming: Shortest Paths and DFA to Reg Exps

16.1 Min-Cut as an LP

Dynamic Programming: Shortest Paths and DFA to Reg Exps

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

Solutions to Exercises

CS 410/584, Algorithm Design & Analysis, Lecture Notes 4

CSE 591 Foundations of Algorithms Homework 4 Sample Solution Outlines. Problem 1

Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case.

Analysis of Algorithms I: All-Pairs Shortest Paths

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

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

Examination paper for TDT4120 Algorithms and Data Structures

The Matching Polytope: General graphs

INVERSE SPANNING TREE PROBLEMS: FORMULATIONS AND ALGORITHMS

All-Pairs Shortest Paths

Algorithms and Data Structures (COMP 251) Midterm Solutions

Part IA Algorithms Notes

arxiv: v1 [cs.cg] 29 Jun 2012

arxiv: v1 [cs.ds] 20 Nov 2016

Discrete Mathematics

Terms of Use. Copyright Embark on the Journey

Question Paper Code :

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

25. Minimum Spanning Trees

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

25. Minimum Spanning Trees

Observation 4.1 G has a proper separation of order 0 if and only if G is disconnected.

Observation 4.1 G has a proper separation of order 0 if and only if G is disconnected.

CMPSCI 611: Advanced Algorithms

Connectivity of addable graph classes

Lecture 7: Shortest Paths in Graphs with Negative Arc Lengths. Reading: AM&O Chapter 5

from notes written mostly by Dr. Matt Stallmann: All Rights Reserved

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

Introduction to Algorithms

Greedy Alg: Huffman abhi shelat

Impossibility of Distributed Consensus with One Faulty Process

MA015: Graph Algorithms

Chapter 6. Properties of Regular Languages

22 Max-Flow Algorithms

6.889 Lecture 4: Single-Source Shortest Paths

Uncertain Compression & Graph Coloring. Madhu Sudan Harvard

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

CMPUT 675: Approximation Algorithms Fall 2014

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

Transcription:

Introduction to Algorithms 6.046J/18.401J LECTURE 17 Shortest Paths I Properties of shortest paths Dijkstra s algorithm Correctness Analysis Breadth-first search Prof. Erik Demaine November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.1

Paths in graphs Consider a digraph G = (V, E) with edge-weight function w : E R. The weight of path p = v 1 v L v k is defined to be k 1 i= 1 w ( p) = w( v i, v i + 1). November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.

Paths in graphs Consider a digraph G = (V, E) with edge-weight function w : E R. The weight of path p = v 1 v L v k is defined to be Example: k 1 i= 1 w ( p) = w( v i, v i + 1). v 1 v v 3 v 5 v v 4 4 5 1 w(p) = November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.3

Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortestpath weight from u to v is defined as δ(u, v) = min{w(p) : p is a path from u to v}. Note: δ(u, v) = if no path from u to v exists. November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.4

Optimal substructure Theorem. A subpath of a shortest path is a shortest path. November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.5

Optimal substructure Theorem. A subpath of a shortest path is a shortest path. Proof. Cut and paste: November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.6

Optimal substructure Theorem. A subpath of a shortest path is a shortest path. Proof. Cut and paste: November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.7

Triangle inequality Theorem. For all u, v, x V, we have δ(u, v) δ(u, x) + δ(x, v). November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.8

Triangle inequality Theorem. For all u, v, x V, we have δ(u, v) δ(u, x) + δ(x, v). Proof. uu δ(u, v) vv δ(u, x) δ(x, v) xx November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.9

Well-definedness of shortest paths If a graph G contains a negative-weight cycle, then some shortest paths may not exist. November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.10

Well-definedness of shortest paths If a graph G contains a negative-weight cycle, then some shortest paths may not exist. Example: < 0 uu vv November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.11

Single-source shortest paths Problem. From a given source vertex s V, find the shortest-path weights δ(s, v) for all v V. If all edge weights w(u, v) are nonnegative, all shortest-path weights must exist. IDEA: Greedy. 1. Maintain a set S of vertices whose shortestpath distances from s are known.. At each step add to S the vertex v V S whose distance estimate from s is minimal. 3. Update the distance estimates of vertices adjacent to v. November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.1

Dijkstra s algorithm d[s] 0 for each v V {s} do d[v] S Q V Q is a priority queue maintaining V S November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.13

Dijkstra s algorithm d[s] 0 for each v V {s} do d[v] S Q V Q is a priority queue maintaining V S while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.14

Dijkstra s algorithm d[s] 0 for each v V {s} do d[v] S Q V while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) Q is a priority queue maintaining V S relaxation step Implicit DECREASE-KEY November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.15

Example of Dijkstra s algorithm Graph with nonnegative edge weights: AA 10 BB D 8 1 4 7 9 3 CC EE November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.16

Initialize: Example of Dijkstra s algorithm 0 Q: A B C D E AA 10 3 BB CC D 8 1 4 7 9 0 S: {} EE November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.17

Example of Dijkstra s algorithm A EXTRACT-MIN(Q): 0 Q: A B C D E AA 10 3 BB CC D 8 1 4 7 9 0 S: { A } EE November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.18

Example of Dijkstra s algorithm Relax all edges leaving A: 0 Q: A B C D E 0 10 3 AA 10 3 10 BB D 8 1 4 7 9 CC S: { A } EE 3 November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.19

Example of Dijkstra s algorithm C EXTRACT-MIN(Q): Q: 0 A B C D E 0 10 3 AA 10 3 10 BB D 8 1 4 7 9 CC S: { A, C } EE 3 November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.0

Example of Dijkstra s algorithm Relax all edges leaving C: Q: 0 A B C D E 0 10 3 7 11 5 AA 10 3 7 11 BB D 8 1 4 7 9 CC S: { A, C } EE 3 5 November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.1

Example of Dijkstra s algorithm E EXTRACT-MIN(Q): Q: 0 A B C D E 0 10 3 7 11 5 AA 10 3 7 11 BB D 8 1 4 7 9 CC EE 3 5 S: { A, C, E } November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.

Example of Dijkstra s algorithm Relax all edges leaving E: Q: 0 A B C D E 0 10 3 7 11 5 7 11 AA 10 3 7 11 BB D 8 1 4 7 9 CC S: { A, C, E } EE 3 5 November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.3

Example of Dijkstra s algorithm B EXTRACT-MIN(Q): Q: 0 A B C D E 0 10 3 7 11 5 7 11 AA 10 3 7 11 BB D 8 1 4 7 9 CC EE 3 5 S: { A, C, E, B } November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.4

Example of Dijkstra s algorithm Relax all edges leaving B: Q: 0 A B C D E 0 10 3 7 11 5 7 11 9 AA 7 9 BB D November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.5 10 3 8 1 4 7 9 CC S: { A, C, E, B } EE 3 5

Example of Dijkstra s algorithm D EXTRACT-MIN(Q): Q: 0 A B C D E 0 10 3 7 11 5 7 11 9 AA 7 9 BB D November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.6 10 3 8 1 4 7 9 CC EE 3 5 S: { A, C, E, B, D }

Correctness Part I Lemma. Initializing d[s] 0 and d[v] for all v V {s} establishes d[v] δ(s, v) for all v V, and this invariant is maintained over any sequence of relaxation steps. November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.7

Correctness Part I Lemma. Initializing d[s] 0 and d[v] for all v V {s} establishes d[v] δ(s, v) for all v V, and this invariant is maintained over any sequence of relaxation steps. Proof. Suppose not. Let v be the first vertex for which d[v] < δ(s, v), and let u be the vertex that caused d[v] to change: d[v] = d[u] + w(u, v). Then, d[v] < δ(s, v) supposition δ(s, u) + δ(u, v) triangle inequality δ(s,u) + w(u, v) sh. path specific path d[u] + w(u, v) v is first violation Contradiction. November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.8

Correctness Part II Lemma. Let u be v s predecessor on a shortest path from s to v. Then, if d[u] = δ(s, u) and edge (u, v) is relaxed, we have d[v] = δ(s, v) after the relaxation. November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.9

Correctness Part II Lemma. Let u be v s predecessor on a shortest path from s to v. Then, if d[u] = δ(s, u) and edge (u, v) is relaxed, we have d[v] = δ(s, v) after the relaxation. Proof. Observe that δ(s, v) = δ(s, u)+ w(u, v). Suppose that d[v] > δ(s, v) before the relaxation. (Otherwise, we re done.) Then, the test d[v] > d[u] + w(u, v) succeeds, because d[v] > δ(s, v) = δ(s, u)+ w(u, v) = d[u] + w(u, v), and the algorithm sets d[v] = d[u] + w(u, v) = δ(s, v). November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.30

Correctness Part III Theorem. Dijkstra s algorithm terminates with d[v] = δ(s, v) for all v V. November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.31

Correctness Part III Theorem. Dijkstra s algorithm terminates with d[v] = δ(s, v) for all v V. Proof. It suffices to show that d[v] = δ(s, v) for every v V when v is added to S. Suppose u is the first vertex added to S for which d[u] >δ(s, u). Let y be the first vertex in V S along a shortest path from s to u, and let x be its predecessor: S, just before adding u. ss xx yy uu November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.3

Correctness Part III (continued) ss S xx yy uu Since u is the first vertex violating the claimed invariant, we have d[x] = δ(s, x). When x was added to S, the edge (x, y) was relaxed, which implies that d[y] =δ(s, y) δ(s, u) < d[u]. But, d[u] d[y] by our choice of u. Contradiction. November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.33

Analysis of Dijkstra while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.34

Analysis of Dijkstra V times while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.35

Analysis of Dijkstra V times degree(u) times while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.36

Analysis of Dijkstra V times degree(u) times while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) Handshaking Lemma Θ(E) implicit DECREASE-KEY s. November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.37

Analysis of Dijkstra V times degree(u) times while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) Handshaking Lemma Θ(E) implicit DECREASE-KEY s. Time = Θ(V T EXTRACT -MIN + E T DECREASE-KEY ) Note: Same formula as in the analysis of Prim s minimum spanning tree algorithm. November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.38

Analysis of Dijkstra (continued) Time = Θ(V) T EXTRACT -MIN + Θ(E) T DECREASE-KEY Q TEXTRACT-MIN T DECREASE -KEY Total November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.39

Analysis of Dijkstra (continued) Time = Θ(V) T EXTRACT -MIN + Θ(E) T DECREASE-KEY Q TEXTRACT-MIN T DECREASE -KEY Total array O(V) O(1) O(V ) November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.40

Analysis of Dijkstra (continued) Time = Θ(V) T EXTRACT -MIN + Θ(E) T DECREASE-KEY Q TEXTRACT-MIN T DECREASE -KEY Total array O(V) O(1) O(V ) binary heap O(lg V) O(lg V) O(E lg V) November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.41

Analysis of Dijkstra (continued) Time = Θ(V) T EXTRACT -MIN + Θ(E) T DECREASE-KEY Q TEXTRACT-MIN T DECREASE -KEY Total array O(V) O(1) O(V ) binary heap Fibonacci heap O(lg V) O(lg V) O(E lg V) O(lg V) amortized O(1) amortized O(E + V lg V) worst case November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.4

Unweighted graphs Suppose that w(u, v) = 1 for all (u, v) E. Can Dijkstra s algorithm be improved? November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.43

Unweighted graphs Suppose that w(u, v) = 1 for all (u, v) E. Can Dijkstra s algorithm be improved? Use a simple FIFO queue instead of a priority queue. November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.44

Unweighted graphs Suppose that w(u, v) = 1 for all (u, v) E. Can Dijkstra s algorithm be improved? Use a simple FIFO queue instead of a priority queue. Breadth-first search while Q do u DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] d[u] + 1 ENQUEUE(Q, v) November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.45

Unweighted graphs Suppose that w(u, v) = 1 for all (u, v) E. Can Dijkstra s algorithm be improved? Use a simple FIFO queue instead of a priority queue. Breadth-first search while Q do u DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] d[u] + 1 ENQUEUE(Q, v) Analysis: Time = O(V + E). November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.46

Example of breadth-first search aa bb cc dd ee f gg hh ii Q: November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.47

Example of breadth-first search 0 aa bb cc dd ee f gg hh ii 0 Q: a November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.48

Example of breadth-first search 0 1 1 aa bb cc dd ee 1 1 Q: a b d f gg hh ii November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.49

Example of breadth-first search 0 1 1 aa bb cc dd ee 1 Q: a b d c e f gg hh ii November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.50

Example of breadth-first search 0 1 1 aa bb cc dd ee Q: a bdc e f gg hh ii November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.51

Example of breadth-first search 0 1 1 aa bb cc dd ee Q: a bdce f gg hh ii November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.5

Example of breadth-first search 0 1 1 aa bb cc dd ee 3 f gg 3 3 3 Q: a b d c e g i hh ii November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.53

Example of breadth-first search 0 1 aa bb cc 1 dd ee 3 4 f gg 3 3 4 Q: a b d c e g i f hh ii November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.54

Example of breadth-first search 0 1 aa bb cc 1 dd ee 3 4 4 f gg 3 4 4 Q: a b d c e g i f h hh ii November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.55

Example of breadth-first search 0 1 aa bb cc 1 dd ee 3 4 4 f gg 3 4 Q: a b d c e g i f h hh ii November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.56

Example of breadth-first search 0 1 aa bb cc 1 dd ee 3 4 4 f gg 3 hh ii Q: a b d c e g i f h November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.57

Example of breadth-first search 0 1 aa bb cc 1 dd ee 3 4 4 f gg 3 hh ii Q: a b d c e g i f h November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.58

Correctness of BFS while Q do u DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] d[u] + 1 ENQUEUE(Q, v) Key idea: The FIFO Q in breadth-first search mimics the priority queue Q in Dijkstra. Invariant: v comes after u in Q implies that d[v] = d[u] or d[v] = d[u] + 1. November 14, 005 Copyright 001-5 by Erik D. Demaine and Charles E. Leiserson L17.59