1 / 30

CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph)

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.

bettycgray
Download Presentation

CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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

  4. 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.

  5. 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 C D E F G H I J

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

  7. Begin Friday

  8. 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

  9. The Trouble with Negative Weighted Cycles 2 A B 10 -5 1 E 2 C D What’s the shortest path from A to E? (or to B, C, or D, for that matter)

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

  11. 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 e-mail; so, his staff had to print out messages and put them in his box.

  12. 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

  13. 2 2 3 B A F H 1 1 2 1 4 10 9 4 G C 8 2 D 1 E 7 Intuition in Action

  14. 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 thisjust as we did for mazes!

  15. 2 2 3 B A F H 1 1 2 1 4 10 9 4 G C 8 2 D 1 E 7 Dijkstra’s Algorithm in Action

  16. The Cloud Proof Next shortest path from inside the known cloud 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? G Better path to the same node THE KNOWN CLOUD P Source

  17. 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!

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

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

  20. Spanning Tree 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. 4 7 9 2 1 5

  21. 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.)

  22. Kruskal’s Algorithm in Action (1/5) 2 2 3 B A F H 1 2 1 4 10 9 G 4 C 8 2 D E 7

  23. Kruskal’s Algorithm in Action (2/5) 2 2 3 B A F H 1 2 1 4 10 9 G 4 C 8 2 D E 7

  24. Kruskal’s Algorithm in Action (3/5) 2 2 3 B A F H 1 2 1 4 10 9 G 4 C 8 2 D E 7

  25. Kruskal’s Algorithm in Action (4/5) 2 2 3 B A F H 1 2 1 4 10 9 G 4 C 8 2 D E 7

  26. Kruskal’s Algorithm Completed (5/5) 2 2 3 B A F H 1 2 1 4 10 9 G 4 C 8 2 D E 7

  27. 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 e1 = (u, v) in that tree that’s not in Kruskal’s Kruskal’s tree connects u’s and v’s sets with another edge e2 But, e2 must have at most the same cost as e1! So, swap e2 for e1 (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.

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

  29. 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)

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

More Related