Lecture 22: Implementing Dijkstra’s

Slides:



Advertisements
Similar presentations
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Advertisements

Greed is good. (Some of the time)
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
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.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
SPANNING TREES Lecture 21 CS2110 – Spring
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
COSC 2007 Data Structures II Chapter 14 Graphs III.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
WEEK 12 Graphs IV Minimum Spanning Tree Algorithms.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Graph Search Applications, Minimum Spanning Tree
Minimum Spanning Trees
Instructor: Lilian de Greef Quarter: Summer 2017
COMP108 Algorithmic Foundations Greedy methods
May 12th – Minimum Spanning Trees
Fundamentals, Terminology, Traversal, Algorithms
Minimum Spanning Trees
CSE373: Data Structures & Algorithms
Spanning Trees.
Spanning Trees Lecture 21 CS2110 – Fall 2016.
Minimum Spanning Trees and Shortest Paths
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Three Graph Algorithms
Short paths and spanning trees
Minimum Spanning Trees
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Minimum-Cost Spanning Tree
CSCE350 Algorithms and Data Structure
Dijkstra’s Algorithm Vertex Distance Predecessor Processed s w x u v t
Minimum Spanning Trees
Minimum Spanning Tree.
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
CSE 373 Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
Minimum-Cost Spanning Tree
CSE 373: Data Structures and Algorithms
Minimum-Cost Spanning Tree
Autumn 2015 Lecture 10 Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Minimum Spanning Tree Algorithms
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Minimum Spanning Tree.
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
CSE 373: Data Structures and Algorithms
Lecture 19: Minimum Spanning Trees
Weighted Graphs & Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
Lecture 20: Disjoint Sets
CSE 373 Data Structures and Algorithms
Lecture 18: Implementing Graphs
CSE 373: Data Structures and Algorithms
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Winter 2019 Lecture 10 Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Lecture 14 Minimum Spanning Tree (cont’d)
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Lecture 23: Minimum Spanning Trees
Minimum-Cost Spanning Tree
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:

Lecture 22: Implementing Dijkstra’s CSE 373: Data Structures and Algorithms CSE 373 19 Sp - Kasey Champion

Administrivia HW 5 Part 2 due Wednesday Wednesday Review Session – How to Ace the Technical Interview CSE 373 SP 18 - Kasey Champion

Dijkstra’s Algorithm Basic idea: Greedily pick the vertex with smallest distance, update other vertices distance based on choice, repeat until all vertices have been processed (Greedy algorithms pick the locally optimal choice at each step and repeat to achieve a global solution) Algorithm Initialize all vertices initial distance from source. Set source’s distance to 0 and all others to “∞” For all unprocessed vertices Get the closest unvisited vertex, “current” Look at each of current’s directly connected neighbors, “next” Calculate “newDistance” from current to next If newDistance is shorter than next’s currently stored distance, update next’s distance and predecessor Mark current as visited Pseudocode Dijkstra(Graph G, Vertex source) initialize distances to ∞ mark all vertices unprocessed mark source as distance 0 while(there are unprocessed vertices){ let u be the closest unprocessed vertex for each(edge (u,v) leaving u){ if(u.dist+weight(u,v) < v.dist){ v.dist = u.dist+weight(u,v) v.predecessor = u } mark u as processed 1. 2. A. B. I. II. C. CSE 373 19 wi - Kasey Champion

Dijkstra’s Run Through Vertex Distance Predecessor Processed S C B T E Pseudocode Dijkstra(Graph G, Vertex source) initialize distances to ∞ mark all vertices unprocessed mark source as distance 0 while(there are unprocessed vertices){ let u be the closest unprocessed vertex for each(edge (u,v) leaving u){ if(u.dist+weight(u,v) < v.dist){ v.dist = u.dist+weight(u,v) v.predecessor = u } mark u as processed CSE 373 SP 19 - Kasey Champion

Dijkstra’s Run Through Vertex Distance Predecessor Processed S No C ∞ B T E Pseudocode Dijkstra(Graph G, Vertex source) initialize distances to ∞ mark all vertices unprocessed mark source as distance 0 while(there are unprocessed vertices){ let u be the closest unprocessed vertex for each(edge (u,v) leaving u){ if(u.dist+weight(u,v) < v.dist){ v.dist = u.dist+weight(u,v) v.predecessor = u } mark u as processed CSE 373 SP 19 - Kasey Champion

Dijkstra’s Run Through Vertex Distance Predecessor Processed S -- No C 6 B 1 T ∞ E Pseudocode Dijkstra(Graph G, Vertex source) initialize distances to ∞ mark all vertices unprocessed mark source as distance 0 while(there are unprocessed vertices){ let u be the closest unprocessed vertex for each(edge (u,v) leaving u){ if(u.dist+weight(u,v) < v.dist){ v.dist = u.dist+weight(u,v) v.predecessor = u } mark u as processed CSE 373 SP 19 - Kasey Champion

Dijkstra’s Run Through Vertex Distance Predecessor Processed S -- Yes C 6 No B 1 T ∞ E Pseudocode Dijkstra(Graph G, Vertex source) initialize distances to ∞ mark all vertices unprocessed mark source as distance 0 while(there are unprocessed vertices){ let u be the closest unprocessed vertex for each(edge (u,v) leaving u){ if(u.dist+weight(u,v) < v.dist){ v.dist = u.dist+weight(u,v) v.predecessor = u } mark u as processed CSE 373 SP 19 - Kasey Champion

Dijkstra’s Run Through Vertex Distance Predecessor Processed S -- Yes C 6 No B 1 T E 2 Pseudocode Dijkstra(Graph G, Vertex source) initialize distances to ∞ mark all vertices unprocessed mark source as distance 0 while(there are unprocessed vertices){ let u be the closest unprocessed vertex for each(edge (u,v) leaving u){ if(u.dist+weight(u,v) < v.dist){ v.dist = u.dist+weight(u,v) v.predecessor = u } mark u as processed CSE 373 SP 19 - Kasey Champion

Dijkstra’s Run Through Vertex Distance Predecessor Processed S -- Yes C 6 No B 1 T 6 3 E 2 Pseudocode Dijkstra(Graph G, Vertex source) initialize distances to ∞ mark all vertices unprocessed mark source as distance 0 while(there are unprocessed vertices){ let u be the closest unprocessed vertex for each(edge (u,v) leaving u){ if(u.dist+weight(u,v) < v.dist){ v.dist = u.dist+weight(u,v) v.predecessor = u } mark u as processed CSE 373 SP 19 - Kasey Champion

Dijkstra’s Run Through Vertex Distance Predecessor Processed S -- Yes C 6 No B 1 T 6 3 E 2 Pseudocode Dijkstra(Graph G, Vertex source) initialize distances to ∞ mark all vertices unprocessed mark source as distance 0 while(there are unprocessed vertices){ let u be the closest unprocessed vertex for each(edge (u,v) leaving u){ if(u.dist+weight(u,v) < v.dist){ v.dist = u.dist+weight(u,v) v.predecessor = u } mark u as processed CSE 373 SP 19 - Kasey Champion

Dijkstra’s Pseuodocode Dijkstra(Graph G, Vertex source) initialize distances to ∞ mark source as distance 0 mark all vertices unprocessed while(there are unprocessed vertices){ let u be the closest unprocessed vertex foreach(edge (u,v) leaving u){ if(u.dist+weight(u,v) < v.dist){ v.dist = u.dist+weight(u,v) v.predecessor = u } mark u as processed Min Priority Queue ADT removeMin() – returns and removes element with the smallest priority state behavior Set of comparable values - Ordered by “priority” peek() – find the element with the smallest priority insert(value) – add new element to collection Wut? CSE 373 19 wi - Kasey Champion

Dijkstra’s Pseuodocode Dijkstra(Graph G, Vertex source) initialize distances to ∞ mark source as distance 0 mark all vertices unprocessed initialize MPQ as a Min Priority Queue, add source while(there are unprocessed vertices){ u = MPQ.removeMin(); foreach(edge (u,v) leaving u){ if(u.dist+weight(u,v) < v.dist){ v.dist = u.dist+weight(u,v) v.predecessor = u } mark u as processed Min Priority Queue ADT removeMin() – returns and removes element with the smallest priority state behavior Set of comparable values - Ordered by “priority” peek() – find the element with the smallest priority insert(value) – add new element to collection How? CSE 373 19 wi - Kasey Champion

Dijkstra’s Pseuodocode Dijkstra(Graph G, Vertex source) initialize distances to ∞ mark source as distance 0 initialize MPQ as a Min Priority Queue, add source while(MPQ is not empty){ u = MPQ.removeMin(); foreach(edge (u,v) leaving u){ oldDist = v.dist; newDist = u.dist+weight(u,v) if(newDist < oldDist){ v.dist = newDist v.predecessor = u if(oldDist == INFINITY) { MPQ.insert(v) } else { MPQ.updatePriority(v, newDist) } } Min Priority Queue ADT removeMin() – returns and removes element with the smallest priority state behavior Set of comparable values - Ordered by “priority” peek() – find the element with the smallest priority insert(value) – add new element to collection How? Wut? Huh? decreaseKey(e, p) – decreases priority of element e down to p CSE 373 19 wi - Kasey Champion

Dijkstra’s Pseuodocode Vertex<E> state behavior data dist predecessor … Edge<E> state behavior vertex1 vertex2 cost … Dijkstra’s Pseuodocode Dijkstra(Graph G, Vertex source) for (Vertex v : G.getVertices()) { v.dist = INFINITY; } G.getVertex(source).dist = 0; initialize MPQ as a Min Priority Queue, add source while(MPQ is not empty){ u = MPQ.removeMin(); for (Edge e : u.getEdges(u)){ oldDist = v.dist; newDist = u.dist+weight(u,v) if(newDist < oldDist){ v.dist = newDist v.predecessor = u if(oldDist == INFINITY) { MPQ.insert(v) } else { MPQ.updatePriority(v, newDist) } } getVertices() – return keyset of graph AdjacencyListGraph<V, E> state behavior Dictionary<V, Set<E>> graph getEdges(v) – return set of outgoing edges from given vertex … getVertex(value) – return Vertex with given value stored CSE 373 19 wi - Kasey Champion

Dijkstra’s Runtime +V +C1 +logV Dijkstra(Graph G, Vertex source) for (Vertex v : G.getVertices()) { v.dist = INFINITY; } G.getVertex(source).dist = 0; initialize MPQ as a Min Priority Queue, add source while(MPQ is not empty){ u = MPQ.removeMin(); for (Edge e : u.getEdges(u)){ oldDist = v.dist; newDist = u.dist+weight(u,v) if(newDist < oldDist){ v.dist = newDist v.predecessor = u if(oldDist == INFINITY) { MPQ.insert(v) } else { MPQ.updatePriority(v, newDist) } } Code Model = C1 + V + V(logV + E(C2 + 2logV)) = C1 + V + VlogV + VEC2 + VEC3logV O Bound = O(VElogV) +V +C1 +logV This actually doesn’t run all E times – for every iteration of the outer loop. It actually will run E times in total; if every vertex is only removed from the priority queue (processed) once, then we will examine each edge once. So each line inside this foreach gets multiplied by a single E instead of E * V. Tight O Bound = O(VlogV + ElogV) +V +C2 +E of 1 V +logV +? (assume logV) CSE 373 19 wi - Kasey Champion

More Dijkstra’s Implementation How do we keep track of vertex costs? Create a vertex object with a cost field Store a dictionary that maps vertices to costs How do we find vertex with smallest distance? Loop over dictionary of costs to find smallest Use a min heap with priority based on distance How do we keep track of shortest paths? Create a vertex object with a predecessor field, update while running Dijkstra’s update fields While running Dijkstra’s build dictionary of vertix to edge backpointers Find shortest path from A to B Run Dijkstra’s, navigate backpointers from B to A CSE 373 SP 18 - Kasey Champion

Minimum Spanning Trees CSE 373 SP 18 - Kasey Champion

Minimum Spanning Trees It’s the 1920’s. Your friend at the electric company needs to choose where to build wires to connect all these cities to the plant. A B D E C 3 6 2 1 4 5 8 9 10 7 She knows how much it would cost to lay electric wires between any pair of locations, and wants the cheapest way to make sure electricity from the plant to every city. CSE 373 SP 18 – Robbie Webber

Minimum Spanning Trees What do we need? A set of edges such that: Every vertex touches at least one of the edges. (the edges span the graph) The graph on just those edges is connected. The minimum weight set of edges that meet those conditions. Assume all edge weights are positive. Claim: The set of edges we pick never has a cycle. Why? Notice we do not need a directed graph! A B D E C 3 2 1 4 5 7 A B D E C 3 2 1 4 5 7 A B D E C 3 2 1 4 A B D E C 3 2 1 4 5 7 CSE 373 19 wi – Kasey Champion

Aside: Trees Our BSTs had: Our heaps had: On graphs our tees: C 3 2 1 4 Our BSTs had: A root Left and/or right children Connected and no cycles Our heaps had: Varying numbers of children On graphs our tees: Don’t need a root (the vertices aren’t ordered, and we can start BFS from anywhere) An undirected, connected acyclic graph. Tree (when talking about graphs) CSE 373 SP 18 – Robbie Webber

MST Problem What do we need? A set of edges such that: Every vertex touches at least one of the edges. (the edges span the graph) The graph on just those edges is connected. The minimum weight set of edges that meet those conditions. Our goal is a tree! We’ll go through two different algorithms for this problem today. Minimum Spanning Tree Problem Given: an undirected, weighted graph G Find: A minimum-weight set of edges such that you can get from any vertex of G to any other on only those edges. CSE 373 SP 18 – Robbie Webber

Graph Algorithm Toolbox Example Try to find an MST of this graph: BFS/DFS Pick an arbitrary starting point Queue up unprocessed neighbors Process next neighbor in queue Repeat until all vertices in queue have been processed Dijkstra’s Start at source Update distance from current to unprocessed neighbors Process optimal neighbor Repeat until all vertices have been marked processed Graph Algorithm Toolbox A B D F E C 3 6 2 1 4 5 8 9 10 7 A B D F E C 3 6 2 1 4 5 8 9 10 7 A B D F E C 3 6 2 1 4 5 8 9 10 7 A B D F E C 3 6 2 1 4 5 8 9 10 7 A B D F E C 3 6 2 1 4 5 8 9 10 7 A B D F E C 3 6 2 1 4 5 8 9 10 7 CSE 373 19 wi – Kasey Champion

Prim’s Algorithm Algorithm idea: choose an arbitrary starting point Dijkstra’s Start at source Update distance from current to unprocessed neighbors Process optimal neighbor Repeat until all vertices have been marked processed Algorithm idea: choose an arbitrary starting point Investigate edges that connect unprocessed vertices Add the lightest edge to solution (be greedy) Repeat until solution connects all vertices PrimMST(Graph G) initialize distances to ∞ mark source as distance 0 mark all vertices unprocessed foreach(edge (source, v) ) { v.dist = weight(source,v) v.bestEdge = (source,v) } while(there are unprocessed vertices){ let u be the closest unprocessed vertex add u.bestEdge to spanning tree foreach(edge (u,v) leaving u){ if(weight(u,v) < v.dist && v unprocessed ){ v.dist = weight(u,v) v.bestEdge = (u,v) mark u as processed Dijkstra(Graph G, Vertex source) initialize distances to ∞ mark source as distance 0 mark all vertices unprocessed while(there are unprocessed vertices){ let u be the closest unprocessed vertex foreach(edge (u,v) leaving u){ if(u.dist+weight(u,v) < v.dist){ v.dist = u.dist+weight(u,v) v.predecessor = u } mark u as processed CSE 373 SP 18 - Kasey Champion

Try it Out G 50 6 B 2 3 E 4 C 5 A 9 2 7 7 F D 8 Vertex Distance PrimMST(Graph G) initialize distances to ∞ mark source as distance 0 mark all vertices unprocessed foreach(edge (source, v) ) { v.dist = weight(source,v) v.bestEdge = (source,v) } while(there are unprocessed vertices){ let u be the closest unprocessed vertex add u.bestEdge to spanning tree foreach(edge (u,v) leaving u){ if(weight(u,v) < v.dist && v unprocessed ){ v.dist = weight(u,v) v.bestEdge = (u,v) mark u as processed 4 C 5 A 9 2 7 7 F D 8 Vertex Distance Best Edge Processed A B C D E F G CSE 373 SP 18 - Kasey Champion

Try it Out G 50 6 B 2 3 E 4 C 5 A 9 2 7 7 F D 8 Vertex Distance PrimMST(Graph G) initialize distances to ∞ mark source as distance 0 mark all vertices unprocessed foreach(edge (source, v) ) { v.dist = weight(source,v) v.bestEdge = (source,v) } while(there are unprocessed vertices){ let u be the closest unprocessed vertex add u.bestEdge to spanning tree foreach(edge (u,v) leaving u){ if(weight(u,v) < v.dist && v unprocessed ){ v.dist = weight(u,v) v.bestEdge = (u,v) mark u as processed 4 C 5 A 9 2 7 7 F D 8 Vertex Distance Best Edge Processed A B C D E F G - X ✓ 2 (A, B) ✓ 4 (A, C) ✓ ---2 --------(C, D) (A, D) 7 ✓ ---5 6 --------(C, E) (B, E) ✓ 3 (B, F) ✓ 50 (B, G) ✓ CSE 373 SP 18 - Kasey Champion

A different Approach Prim’s Algorithm started from a single vertex and reached more and more other vertices. Prim’s thinks vertex by vertex (add the closest vertex to the currently reachable set). What if you think edge by edge instead? Start from the lightest edge; add it if it connects new things to each other (don’t add it if it would create a cycle) This is Kruskal’s Algorithm.

Kruskal’s Algorithm KruskalMST(Graph G) initialize each vertex to be a connected component sort the edges by weight foreach(edge (u, v) in sorted order){ if(u and v are in different components){ add (u,v) to the MST Update u and v to be in the same component }

Try It Out B 6 3 E 2 1 C A 10 9 5 7 4 F D 8 Edge Include? Reason (A,C) KruskalMST(Graph G) initialize each vertex to be a connected component sort the edges by weight foreach(edge (u, v) in sorted order){ if(u and v are in different components){ add (u,v) to the MST Update u and v to be in the same component } 1 C A 10 9 5 7 4 F D 8 Edge Include? Reason (A,C) (C,E) (A,B) (A,D) (C,D) Edge (cont.) Inc? Reason (B,F) (D,E) (D,F) (E,F) (C,F)

Try It Out B 6 3 E 2 1 C A 10 9 5 7 4 F D 8 Edge Include? Reason (A,C) KruskalMST(Graph G) initialize each vertex to be a connected component sort the edges by weight foreach(edge (u, v) in sorted order){ if(u and v are in different components){ add (u,v) to the MST Update u and v to be in the same component } 1 C A 10 9 5 7 4 F D 8 Edge Include? Reason (A,C) Yes (C,E) (A,B) (A,D) (C,D) No Cycle A,C,D,A Edge (cont.) Inc? Reason (B,F) Yes (D,E) No Cycle A,C,E,D,A (D,F) Cycle A,D,F,B,A (E,F) Cycle A,C,E,F,D,A (C,F) Cycle C,A,B,F,C

Kruskal’s Algorithm: Running Time KruskalMST(Graph G) initialize each vertex to be a connected component sort the edges by weight foreach(edge (u, v) in sorted order){ if(u and v are in different components){ add (u,v) to the MST Update u and v to be in the same component }

Kruskal’s Algorithm: Running Time Running a new BFS in the partial MST, at every step seems inefficient. Do we have an ADT that will work here? Not yet…

CSE 373 SP 18 - Kasey Champion