Download presentation
Presentation is loading. Please wait.
Published byAugust Long Modified over 8 years ago
1
CSE 326: Data Structures Lecture #23 Dijkstra 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 DE C F HG JI
6
Directed Acyclic Graphs (DAGs) DAGs are directed graphs with no cycles. main() add() access() mult() read() 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 AB CD E 2 10 1 -5 2 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 … A C B D FH G 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
Intuition in Action A C B D FH G E 2 2 3 2 1 1 4 10 8 1 1 9 4 2 7
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 this just as we did for mazes!
15
Dijkstra’s Algorithm in Action A C B D FH G E 2 2 3 2 1 1 4 10 8 1 1 9 4 2 7
16
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
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 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:
19
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
20
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 47 15 9 2
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) A C B D FH G E 2 2 3 2 1 4 10 8 1 9 4 2 7
23
Kruskal’s Algorithm in Action (2/5) A C B D FH G E 2 2 3 2 1 4 10 8 1 9 4 2 7
24
Kruskal’s Algorithm in Action (3/5) A C B D FH G E 2 2 3 2 1 4 10 8 1 9 4 2 7
25
Kruskal’s Algorithm in Action (4/5) A C B D FH G E 2 2 3 2 1 4 10 8 1 9 4 2 7
26
Kruskal’s Algorithm Completed (5/5) A C B D FH G E 2 2 3 2 1 4 10 8 1 9 4 2 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 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.
28
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:
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 7 th ; that’s only four days!) Final Exam (March 13 th )
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.