CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.

Slides:



Advertisements
Similar presentations
CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2011W2.
Advertisements

Lecture 15. Graph Algorithms
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CSC 2300 Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
CSE332: Data Abstractions Lecture 17: Shortest Paths Dan Grossman Spring 2010.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Minimum Spanning Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
CSE 326: Data Structures Lecture #19 Graphs I Alon Halevy Spring Quarter 2001.
CSE 326 Graphs David Kaplan Dept of Computer Science & Engineering Autumn 2001.
CSE 326: Data Structures Lecture #21 Graphs II.5 Alon Halevy Spring Quarter 2001.
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
CSE 326: Data Structures Lecture #17 Trees and DAGs and Graphs, Oh MY! Bart Niswonger Summer Quarter 2001.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CSE 326: Data Structures Spanning Trees Ben Lerner Summer 2007.
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
CSE 326: Data Structures Lecture #19 More Fun with Graphs Henry Kautz Winter Quarter 2002.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.
CS 146: Data Structures and Algorithms July 21 Class Meeting
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
SPANNING TREES Lecture 21 CS2110 – Spring
CSE 326: Data Structures First Post-Schism Lecture Lecture #22 SteganoGRAPHy Steve Wolfman Winter Quarter 2000.
CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1.
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)
CSE373: Data Structures & Algorithms Lecture 17: Dijkstra’s Algorithm Lauren Milne Summer 2015.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
WEEK 12 Graphs IV Minimum Spanning Tree Algorithms.
CSE373: Data Structures & Algorithms Lecture 19: Dijkstra’s algorithm and Spanning Trees Catie Baker Spring 2015.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Instructor: Lilian de Greef Quarter: Summer 2017
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
May 12th – Minimum Spanning Trees
Minimum Spanning Trees
CSE373: Data Structures & Algorithms
Minimum Spanning Trees and Shortest Paths
Cinda Heeren / Geoffrey Tien
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.
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
CSE 373: Data Structures and Algorithms
Hannah Tang and Brian Tjaden Summer Quarter 2002
Based on slides of Dan Suciu
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Slide Courtesy: Uwash, UT
CSE 373: Data Structures and Algorithms
Minimum Spanning Trees
Autumn 2016 Lecture 10 Minimum Spanning Trees
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
Richard Anderson Spring 2016
CSE 332: Minimum Spanning Trees
Directed Graphs (Part II)
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000

Today’s Outline Wave Hi to the Camera (and Sign a Release Form) What Steve Didn’t Finish on Wednesday Dijkstra’s Algorithm Spanning Trees Kruskal’s Algorithm

Connectivity Undirected graphs are connected if there is a path between any two vertices Directed graphs are strongly connected if there is a path from any one vertex to any other Di-graphs are weakly connected if there is a path between any two vertices, ignoring direction A complete graph has an edge between every pair of vertices

Graph Density A sparse graph has O(|V|) edges A dense graph has  (|V| 2 ) edges Anything in between is either sparsish or densy depending on the context.

Trees as Graphs Every tree is a graph with some restrictions: –the tree is directed –there are no cycles (directed or undirected) –there is a directed path from the root to every node A B DE C F HG JI

Directed Acyclic Graphs (DAGs) DAGs are directed graphs with no cycles. main() add() access() mult() read() Trees  DAGs  Graphs

Begin Friday

Single Source, Shortest Path Given a graph G = (V, E) and a vertex s  V, find the shortest path from s to every vertex in V Many variations: –weighted vs. unweighted –cyclic vs. acyclic –positive weights only vs. negative weights allowed –multiple weight types to optimize

The Trouble with Negative Weighted Cycles AB CD E What’s the shortest path from A to E? (or to B, C, or D, for that matter)

Unweighted Shortest Path Problem Assume source vertex is C … A C B D FH G E Distance to: A B C D E F G H

Dijkstra Legendary figure in computer science; now a professor at University of Texas. Supports teaching introductory computer courses without computers (pencil and paper programming) Supposedly wouldn’t (until recently) read his ; so, his staff had to print out messages and put them in his box.

Dijkstra’s Algorithm for Single Source Shortest Path Classic algorithm for solving shortest path in weighted graphs without negative weights A greedy algorithm (irrevocably makes decisions without considering future consequences) Intuition: –shortest path from source vertex to itself is 0 –cost of going to adjacent nodes is at most edge weights –cheapest of these must be shortest path to that node –update paths for new node and continue picking cheapest path

Intuition in Action A C B D FH G E

Dijkstra’s Pseudocode (actually, our pseudocode for Dijkstra’s algorithm) Initialize the cost of each node to  Initialize the cost of the source to 0 While there are unknown nodes left in the graph Select the unknown node with the lowest cost: n Mark n as known For each node a which is adjacent to n a’s cost = min(a’s old cost, n’s cost + cost of (n, a)) We can get the path from this just as we did for mazes!

Dijkstra’s Algorithm in Action A C B D FH G E

THE KNOWN CLOUD G Next shortest path from inside the known cloud P Better path to the same node The Cloud Proof But, if the path to G is the next shortest path, the path to P must be at least as long. So, how can the path through P to G be shorter? Source

Inside the Cloud (Proof) Everything inside the cloud has the correct shortest path Proof is by induction on the # of nodes in the cloud: –initial cloud is just the source with shortest path 0 –inductive step: once we prove the shortest path to G is correct, we add it to the cloud Negative weights blow this proof away!

Data Structures for Dijkstra’s Algorithm Select the unknown node with the lowest cost findMin/deleteMin a’s cost = min(a’s old cost, …) decreaseKey find by name |V| times: |E| times: runtime:

Dijkstra’s uses |V| deleteMins and |E| decreaseKeys runtime with Fibonacci heaps: Fibonacci Heaps Somewhat like Binomial Queues with lazy merges Amortized O(1) time bound for decreaseKey O(log n) time for deleteMin

Spanning tree: a subset of the edges from a connected graph that… …touches all vertices in the graph (spans the graph) …forms a tree (is connected and contains no cycles) Minimum spanning tree: the spanning tree with the least total edge cost. Spanning Tree

Kruskal’s Algorithm for Minimum Spanning Trees Yet another greedy algorithm: Initialize all vertices to unconnected While there are still unmarked edges Pick the lowest cost edge e = (u, v) and mark it If u and v are not already connected, add e to the minimum spanning tree and connect u and v Sound familiar? (Think maze generation.)

Kruskal’s Algorithm in Action (1/5) A C B D FH G E

Kruskal’s Algorithm in Action (2/5) A C B D FH G E

Kruskal’s Algorithm in Action (3/5) A C B D FH G E

Kruskal’s Algorithm in Action (4/5) A C B D FH G E

Kruskal’s Algorithm Completed (5/5) A C B D FH G E

Proof of Correctness We already showed this finds a spanning tree: that was part of our definition of a good maze. Proof by contradiction that Kruskal’s finds the minimum: Assume another spanning tree has lower cost than Kruskal’s Pick an edge e 1 = (u, v) in that tree that’s not in Kruskal’s Kruskal’s tree connects u ’s and v ’s sets with another edge e 2 But, e 2 must have at most the same cost as e 1 ! So, swap e 2 for e 1 (at worst keeping the cost the same) Repeat until the tree is identical to Kruskal’s: contradiction! QED: Kruskal’s algorithm finds a minimum spanning tree.

Data Structures for Kruskal’s Algorithm Pick the lowest cost edge… findMin/deleteMin If u and v are not already connected… …connect u and v. find representative union |E| times: runtime:

To Do Finish Project IV Read Chapter 9 (graphs) Read Chapter 10 (algorithmic techniques) Work on final review (remember you can and should work with others)

Coming Up Algorithmic Techniques Project IV due (March 7 th ; that’s only four days!) Final Exam (March 13 th )