Single Source Shortest Path Problem • Given a graph and a start vertex s – Determine distance of every vertex from s CSE 421 – Identify shortest paths to each vertex Algorithms • Express concisely as a “shortest paths tree” • Each vertex has a pointer to a predecessor on Richard Anderson shortest path 1 u u Dijkstra’s algorithm 1 2 3 5 s x s x

3 4 3 v v

Construct Shortest Path Tree Warmup from s • If P is a shortest path from s to v, and if t is 2 d d on the path P, the segment from s to t is a a 1 5 a 4 4 shortest path between s and t 4 e e -3 c s c v -2 s t 3 3 2 s 6 g g b b •WHY? 7 3 f f

Assume all edges have non-negative cost Simulate Dijkstra’s algorithm Dijkstra’s Algorithm (strarting from s) on the graph

S = {}; d[s] = 0; d[v] = infinity for v != s

Round Vertex sabcd While S != V Added

Choose v in V-S with minimum d[v] 1 a c 1 Add v to S 1 3 2 For each w in the neighborhood of v 2 s 4 1 d[w] = min(d[w], d[v] + c(v, w)) 4 6 3 4 1 3 y b d 4 1 3 1 u 0 1 1 4 5 s x 2 2 2 2 v 2 3 z 5 Dijkstra’s Algorithm as a greedy Correctness Proof algorithm • Elements committed to the solution by • Elements in S have the correct label order of minimum distance • Key to proof: when v is added to S, it has the correct distance label.

y

x

s u v

Proof Negative Cost Edges

• Let v be a vertex in V-S with minimum d[v] • Draw a small example a negative cost

• Let Pv be a path of length d[v], with an edge (u,v) edge and show that Dijkstra’s algorithm • Let P be some other path to v. Suppose P first fails on this example leaves S on the edge (x, y) y –P = Psx + c(x,y) + Pyv x –Len(Psx) + c(x,y) >= d[y]

–Len(Pyv) >= 0 – Len(P) >= d[y] + 0 >= d[v] s u v

Compute the bottleneck shortest Bottleneck Shortest Path paths • Define the bottleneck distance for a path to be the maximum cost edge along the 6 d d a 6 5 a path 4 4 4 e e -3 c c s -2 s 3 3 2 6 g g b b u 6 5 7 4 f f s 2 5 x

3 4 v How do you adapt Dijkstra’s algorithm to handle bottleneck distances Who was Dijkstra? • Does the correctness proof still apply? • What were his major contributions?

http://www.cs.utexas.edu/users/EWD/ Shortest Paths

• Edsger Wybe Dijkstra was one of the most • Negative Cost Edges influential members of computing science's founding generation. Among the domains in – Dijkstra’s algorithm assumes positive cost edges which his scientific contributions are – For some applications, negative cost edges make fundamental are sense – algorithm design – Shortest path not well defined if a graph has a – programming languages negative cost cycle – program design a 6 4 -4 – operating systems 4 e – distributed processing -3 c s -2 – formal specification and verification 3 3 – design of mathematical arguments 2 6 g b f 7 4

Dijkstra’s Algorithm Negative Cost Edge Preview Implementation and Runtime

• Topological Sort can be used for solving S = {}; d[s] = 0; d[v] = infinity for v != s the shortest path problem in directed While S != V acyclic graphs Choose v in V-S with minimum d[v] Add v to S • Bellman-Ford algorithm finds shortest For each w in the neighborhood of v paths in a graph with negative cost edges d[w] = min(d[w], d[v] + c(v, w)) (or reports the existence of a negative cost y cycle). u a HEAP OPERATIONS s x n Extract Mins v m Heap Updates b z Edge costs are assumed to be non-negative Compute the bottleneck shortest Bottleneck Shortest Path paths • Define the bottleneck distance for a path to be the maximum cost edge along the 6 d d a 6 5 a path 4 4 4 e e -3 c c s -2 s 3 3 2 5 g g b b u 6 5 7 7 f f s 2 5 x

3 4 v

Dijkstra’s Algorithm Minimum for Bottleneck Shortest Paths

S = {}; d[s] = negative infinity; d[v] = infinity for v != s • Introduce Problem While S != V • Demonstrate three different greedy Choose v in V-S with minimum d[v] algorithms Add v to S For each w in the neighborhood of v • Provide proofs that the algorithms work d[w] = min(d[w], max(d[v], c(v, w))) 3 4 y u a 1 3 2 4 s 1 x 1 2 v 5 3 b 4 z

Greedy Algorithms for Minimum Minimum Spanning Tree Spanning Tree • Extend a tree by including the 15 t cheapest out going 4 a 6 b c 9 14 edge 3 4 e • Add the cheapest 20 10 13 c 5 11 s 11 20 edge that joins 8 5 a 17 2 7 disjoint components g d b 8 7 • Delete the most e 22 u f 22 1 12 expensive edge that 16 does not disconnect v the graph 1 Greedy Algorithm 2 Prim’s Algorithm Kruskal’s Algorithm • Extend a tree by including the cheapest • Add the cheapest edge that joins disjoint out going edge components

15 15 t a 6 t a 6 14 14 3 9 4 e 3 9 4 e 10 13 c 10 13 c s 11 s 11 20 5 20 5 Construct the MST 17 17 2 7 2 7 with Prim’s g Construct the MST g b 8 b 8 algorithm starting u f 22 with Kruskal’s u f 22 from vertex a 1 12 algorithm 1 12 Label the edges in 16 Label the edges in 16 order of insertion v order of insertion v

Greedy Algorithm 3 Why do the greedy algorithms Reverse-Delete Algorithm work? • Delete the most expensive edge that does • For simplicity, assume all edge costs are not disconnect the graph distinct

15 • Let S be a subset of V, and suppose e = t a 6 (u, v) is the minimum cost edge of E, with 14 3 9 4 e u in S and v in V-S 10 13 c s 11 20 5 • e is in every minimum spanning tree 17 2 7 Construct the MST g b 8 with the reverse- u f 22 delete algorithm 1 12 Label the edges in 16 order of removal v

Proof Optimality Proofs

• Suppose T is a spanning tree that does not • Prim’s Algorithm computes a MST contain e • Add e to T, this creates a cycle

• The cycle must have some edge e1 = (u1, v1) with u1 in S and v1 in V-S

• Kruskal’s Algorithm computes a MST

•T1 = T – {e1} + {e} is a spanning tree with lower cost • Hence, T is not a minimum spanning tree Dealing with the assumption of no Reverse-Delete Algorithm equal weight edges • Lemma: The most expensive edge on a • Force the edge weights to be distinct cycle is never in a minimum spanning tree – Add small quantities to the weights – Give a tie breaking rule for equal weight edges

Dijkstra’s Algorithm Minimum Spanning Tree for Minimum Spanning Trees Undirected Graph S = {}; d[s] = 0; d[v] = infinity for v != s G=(V,E) with edge While S != V 15 weights t Choose v in V-S with minimum d[v] a 6 9 14 Add v to S 3 4 e For each w in the neighborhood of v 10 13 c s 11 d[w] = min(d[w], c(v, w)) 20 5 17 2 7 y 3 g 4 b 8 u a f 22 1 3 2 4 u 1 12 s 1 x 1 16 2 v 5 v 3 b 4 z

Greedy Algorithms for Minimum Why do the greedy algorithms Spanning Tree work? •[Prim]Extend a tree by • For simplicity, assume all edge costs are including the cheapest distinct out going edge 4 b c • [Kruskal] Add the cheapest edge that joins 20 disjoint components 5 11 8 • [ReverseDelete] Delete a d the most expensive edge 7 that does not disconnect e 22 the graph e is the minimum cost edge Edge inclusion lemma between S and V-S Proof

• Let S be a subset of V, and suppose e = • Suppose T is a spanning tree that does not contain e • Add e to T, this creates a cycle (u, v) is the minimum cost edge of E, with • The cycle must have some edge e1 = (u1, v1) with u1 in S u in S and v in V-S and v1 in V-S • e is in every minimum spanning tree of G – Or equivalently, if e is not in T, then T is not a SV -S minimum spanning tree e

•T1 = T – {e1} + {e} is a spanning tree with lower cost • Hence, T is not a minimum spanning tree e SV -S

Optimality Proofs Prim’s Algorithm

• Prim’s Algorithm computes a MST S = { }; T = { }; • Kruskal’s Algorithm computes a MST while S != V choose the minimum cost edge • Show that when an edge is added to the e = (u,v), with u in S, and v in V-S MST by Prim or Kruskal, the edge is the add e to T minimum cost edge between S and V-S add v to S for some set S.

Prove Prim’s algorithm computes Kruskal’s Algorithm an MST • Show an edge e is in the MST when it is Let C = {{v }, {v }, . . ., {v }}; T = { } added to T 1 2 n while |C| > 1

Let e = (u, v) with u in Ci and v in Cj be the minimum cost edge joining distinct sets in C

Replace Ci and Cj by Ci U Cj Add e to T Prove Kruskal’s algorithm Reverse-Delete Algorithm computes an MST • Show an edge e is in the MST when it is • Lemma: The most expensive edge on a added to T cycle is never in a minimum spanning tree

Dealing with the assumption of no Application: Clustering equal weight edges • Force the edge weights to be distinct • Given a collection of points in an r- – Add small quantities to the weights dimensional space, and an integer K, – Give a tie breaking rule for equal weight divide the points into K sets that are edges closest together

Distance clustering Divide into 2 clusters

• Divide the data set into K subsets to maximize the distance between any pair of sets

–dist (S1, S2) = min {dist(x, y) | x in S1, y in S2} Divide into 3 clusters Divide into 4 clusters

Distance Clustering Algorithm K-clustering

Let C = {{v1}, {v2},. . ., {vn}}; T = { } while |C| > K

Let e = (u, v) with u in Ci and v in Cj be the minimum cost edge joining distinct sets in C

Replace Ci and Cj by Ci U Cj