CSE373: Data Structures & Algorithms Lecture 19: Dijkstra’s algorithm and Spanning Trees Catie Baker Spring 2015.

Slides:



Advertisements
Similar presentations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advertisements

The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CSE332: Data Abstractions Lecture 17: Shortest Paths Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 26: Minimum Spanning Trees Dan Grossman Spring 2010.
1 7-MST Minimal Spanning Trees Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
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.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CSE373: Data Structures & Algorithms Lecture 17: Shortest Paths Nicki Dell Spring 2014.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
CSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees Kate Deibel Summer 2012 August 13, 2012 CSE 332 Data Abstractions, Summer.
CSE332: Data Abstractions Lecture 24.5: Interlude on Intractability Dan Grossman Spring 2012.
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
CSE332: Data Abstractions Lecture 17: Shortest Paths Dan Grossman Spring 2012.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
CSE373: Data Structures & Algorithms Lecture 17: Dijkstra’s Algorithm Lauren Milne Summer 2015.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Section 7: Dijkstra’s Justin Bare and deric Pang
Instructor: Lilian de Greef Quarter: Summer 2017
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
May 12th – Minimum Spanning Trees
Shortest Paths and Minimum Spanning Trees
CSE 373: Data Structures & Algorithms Graph Traversals: Dijkstra’s
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Cse 373 May 8th – Dijkstras.
Instructor: Lilian de Greef Quarter: Summer 2017
Instructor: Lilian de Greef Quarter: Summer 2017
CSE373: Data Structures & Algorithms Lecture 17: Shortest Paths
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
Outline This topic covers Prim’s algorithm:
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Section 7: Dijkstra’s & Midterm Postmortem
Shortest Paths and Minimum Spanning Trees
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Slide Courtesy: Uwash, UT
CSE 373: Data Structures and Algorithms
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
CSE332: Data Abstractions Lecture 17: Shortest Paths
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
Topological Sorting Minimum Spanning Trees Shortest Path
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:

CSE373: Data Structures & Algorithms Lecture 19: Dijkstra’s algorithm and Spanning Trees Catie Baker Spring 2015

Announcements Homework 4 due tonight at 11pm!! Homework 5 out tonight –Due May 27 th –As with HW4 you’re allowed to work with a partner Spring 20142CSE373: Data Structures & Algorithms

Dijkstra’s algorithm Dijkstra’s algorithm: Compute shortest paths in a weighted graph with no negative weights Spring 20143CSE373: Data Structures & Algorithms Initially, start node has cost 0 and all other nodes have cost  At each step: –Pick an unknown vertex v with the lowest “cost” –Add it to the “cloud” of known vertices –Update distances for nodes with edges from v A B D C FH E G 0 2 4∞ ∞

Correctness and Efficiency What should we do after learning an algorithm? –Prove it is correct Not obvious! We will sketch the key ideas –Analyze its efficiency Will do better by using a data structure we learned earlier! Spring 20144CSE373: Data Structures & Algorithms

Correctness: Intuition Rough intuition: All the “known” vertices have the correct shortest path –True initially: shortest path to start node has cost 0 –If it stays true every time we mark a node “known”, then by induction this holds and eventually everything is “known” Key fact we need: When we mark a vertex “known” we won’t discover a shorter path later! –This holds only because Dijkstra’s algorithm picks the node with the next shortest path-so-far –The proof is by contradiction… Spring 20145CSE373: Data Structures & Algorithms

Correctness: The Cloud (Rough Sketch) Spring 20146CSE373: Data Structures & Algorithms v Next shortest path from inside the known cloud w Better path to v? Source Suppose v is the next node to be marked known (“added to the cloud”) The best-known path to v must have only nodes “in the cloud” – Else we would have picked a node closer to the cloud than v Suppose the actual shortest path to v is different –It won’t use only cloud nodes, or we would know about it –So it must use non-cloud nodes. –Let w be the first non-cloud node on this path. –The part of the path up to w is already known and must be shorter than the best-known path to v. So v would not have been picked. –Contradiction. The known cloud No!

Efficiency, first approach Use pseudocode to determine asymptotic run-time –Notice each edge is processed only once Spring 20147CSE373: Data Structures & Algorithms dijkstra(Graph G, Node start) { for each node: x.cost=infinity, x.known=false start.cost = 0 while(not all nodes are known) { b = find unknown node with smallest cost b.known = true for each edge (b,a) in G if(!a.known) if(b.cost + weight((b,a)) < a.cost){ a.cost = b.cost + weight((b,a)) a.path = b }

Efficiency, first approach Use pseudocode to determine asymptotic run-time –Notice each edge is processed only once Spring 20148CSE373: Data Structures & Algorithms dijkstra(Graph G, Node start) { for each node: x.cost=infinity, x.known=false start.cost = 0 while(not all nodes are known) { b = find unknown node with smallest cost b.known = true for each edge (b,a) in G if(!a.known) if(b.cost + weight((b,a)) < a.cost){ a.cost = b.cost + weight((b,a)) a.path = b } O(|V|) O(|V| 2 ) O(|E|) O(|V| 2 )

Improving asymptotic running time So far: O(|V| 2 ) We had a similar “problem” with topological sort being O(|V| 2 ) due to each iteration looking for the node to process next –We solved it with a queue of zero-degree nodes –But here we need the lowest-cost node and costs can change as we process edges Solution? –A priority queue holding all unknown nodes, sorted by cost –But must support decreaseKey operation Must maintain a reference from each node to its current position in the priority queue Conceptually simple, but can be a pain to code up Spring 20149CSE373: Data Structures & Algorithms

Efficiency, second approach Use pseudocode to determine asymptotic run-time Spring CSE373: Data Structures & Algorithms dijkstra(Graph G, Node start) { for each node: x.cost=infinity, x.known=false start.cost = 0 build-heap with all nodes while(heap is not empty) { b = deleteMin() b.known = true for each edge (b,a) in G if(!a.known) if(b.cost + weight((b,a)) < a.cost){ decreaseKey(a,“new cost – old cost”) a.path = b }

Efficiency, second approach Use pseudocode to determine asymptotic run-time Spring CSE373: Data Structures & Algorithms dijkstra(Graph G, Node start) { for each node: x.cost=infinity, x.known=false start.cost = 0 build-heap with all nodes while(heap is not empty) { b = deleteMin() b.known = true for each edge (b,a) in G if(!a.known) if(b.cost + weight((b,a)) < a.cost){ decreaseKey(a,“new cost – old cost”) a.path = b } O(|V|) O(|V|log|V|) O(|E|log|V|) O(|V|log|V|+|E|log|V|)

Dense vs. sparse again First approach: O(|V| 2 ) Second approach: O(|V|log|V|+|E|log|V|) So which is better? –Sparse: O(|V|log|V|+|E|log|V|) (if |E| > |V|, then O(|E|log|V|)) –Dense: O(|V| 2 ) But, remember these are worst-case and asymptotic –Priority queue might have slightly worse constant factors –On the other hand, for “normal graphs”, we might call decreaseKey rarely (or not percolate far), making |E|log|V| more like |E| Spring CSE373: Data Structures & Algorithms

Done with Dijkstra’s You will implement Dijkstra’s algorithm in homework 5 Onward….. Spanning trees! Spring CSE373: Data Structures & Algorithms

Spanning Trees A simple problem: Given a connected undirected graph G=(V,E), find a minimal subset of edges such that G is still connected –A graph G2=(V,E2) such that G2 is connected and removing any edge from E2 makes G2 disconnected Spring CSE373: Data Structures & Algorithms

Observations 1.Any solution to this problem is a tree –Recall a tree does not need a root; just means acyclic –For any cycle, could remove an edge and still be connected 2.Solution not unique unless original graph was already a tree 3.Problem ill-defined if original graph not connected –So |E| ≥ |V|-1 4.A tree with |V| nodes has |V|-1 edges –So every solution to the spanning tree problem has |V|-1 edges Spring CSE373: Data Structures & Algorithms

Motivation A spanning tree connects all the nodes with as few edges as possible Example: A “phone tree” so everybody gets the message and no unnecessary calls get made In most compelling uses, we have a weighted undirected graph and we want a tree of least total cost Example: Electrical wiring for a house or clock wires on a chip Example: A road network if you cared about asphalt cost rather than travel time This is the minimum spanning tree problem –Will do that next, after intuition from the simpler case Spring CSE373: Data Structures & Algorithms

Two Approaches Different algorithmic approaches to the spanning-tree problem: 1.Do a graph traversal (e.g., depth-first search, but any traversal will do), keeping track of edges that form a tree 2.Iterate through edges; add to output any edge that does not create a cycle Spring CSE373: Data Structures & Algorithms

Spanning tree via DFS Spring CSE373: Data Structures & Algorithms spanning_tree(Graph G) { for each node i i.marked = false for some node i: f(i) } f(Node i) { i.marked = true for each j adjacent to i: if(!j.marked) { add(i,j) to output f(j) // DFS } Correctness: DFS reaches each node. We add one edge to connect it to the already visited nodes. Order affects result, not correctness. Time: O(|E|)

Example Stack f(1) Spring CSE373: Data Structures & Algorithms Output:

Example Stack f(1) f(2) Spring CSE373: Data Structures & Algorithms Output: (1,2)

Example Stack f(1) f(2) f(7) Spring CSE373: Data Structures & Algorithms Output: (1,2), (2,7)

Example Stack f(1) f(2) f(7) f(5) Spring CSE373: Data Structures & Algorithms Output: (1,2), (2,7), (7,5)

Example Stack f(1) f(2) f(7) f(5) f(4) Spring CSE373: Data Structures & Algorithms Output: (1,2), (2,7), (7,5), (5,4)

Example Stack f(1) f(2) f(7) f(5) f(4) f(3) Spring CSE373: Data Structures & Algorithms Output: (1,2), (2,7), (7,5), (5,4),(4,3)

Example Stack f(1) f(2) f(7) f(5) f(4) f(6) f(3) Spring CSE373: Data Structures & Algorithms Output: (1,2), (2,7), (7,5), (5,4), (4,3), (5,6)

Example Stack f(1) f(2) f(7) f(5) f(4) f(6) f(3) Spring CSE373: Data Structures & Algorithms Output: (1,2), (2,7), (7,5), (5,4), (4,3), (5,6)

Second Approach Iterate through edges; output any edge that does not create a cycle Correctness (hand-wavy): –Goal is to build an acyclic connected graph –When we add an edge, it adds a vertex to the tree Else it would have created a cycle –The graph is connected, so we reach all vertices Efficiency: –Depends on how quickly you can detect cycles –Reconsider after the example Spring CSE373: Data Structures & Algorithms

Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Spring CSE373: Data Structures & Algorithms Output:

Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Spring CSE373: Data Structures & Algorithms Output: (1,2)

Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Spring CSE373: Data Structures & Algorithms Output: (1,2), (3,4)

Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Spring CSE373: Data Structures & Algorithms Output: (1,2), (3,4), (5,6),

Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7),(1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Spring CSE373: Data Structures & Algorithms Output: (1,2), (3,4), (5,6), (5,7)

Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7), (1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Spring CSE373: Data Structures & Algorithms Output: (1,2), (3,4), (5,6), (5,7), (1,5)

Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7), (1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Spring CSE373: Data Structures & Algorithms Output: (1,2), (3,4), (5,6), (5,7), (1,5)

Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7), (1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Spring CSE373: Data Structures & Algorithms Output: (1,2), (3,4), (5,6), (5,7), (1,5)

Example Edges in some arbitrary order: (1,2), (3,4), (5,6), (5,7), (1,5), (1,6), (2,7), (2,3), (4,5), (4,7) Spring CSE373: Data Structures & Algorithms Output: (1,2), (3,4), (5,6), (5,7), (1,5), (2,3) Can stop once we have |V|-1 edges

Cycle Detection To decide if an edge could form a cycle is O(|V|) because we may need to traverse all edges already in the output So overall algorithm would be O(|V||E|) But there is a faster way we know Use union-find! –Initially, each item is in its own 1-element set –Union sets when we add an edge that connects them –Stop when we have one set Spring CSE373: Data Structures & Algorithms

Using Disjoint-Set Can use a disjoint-set implementation in our spanning-tree algorithm to detect cycles: Invariant: u and v are connected in output-so-far iff u and v in the same set Initially, each node is in its own set When processing edge (u,v) : –If find(u) equals find(v), then do not add the edge –Else add the edge and union(find(u),find(v)) –O(|E|) operations that are almost O(1) amortized Spring CSE373: Data Structures & Algorithms

Summary So Far The spanning-tree problem –Add nodes to partial tree approach is O(|E|) –Add acyclic edges approach is almost O(|E|) Using union-find “as a black box” But really want to solve the minimum-spanning-tree problem –Given a weighted undirected graph, give a spanning tree of minimum weight –Same two approaches will work with minor modifications –Both will be O(|E| log |V|) Spring CSE373: Data Structures & Algorithms

Minimum Spanning Tree Algorithms Algorithm #1 Shortest-path is to Dijkstra’s Algorithm as Minimum Spanning Tree is to Prim’s Algorithm (Both based on expanding cloud of known vertices, basically using a priority queue instead of a DFS stack) Algorithm #2 Kruskal’s Algorithm for Minimum Spanning Tree is Exactly our 2 nd approach to spanning tree but process edges in cost order Spring CSE373: Data Structures & Algorithms