Transcript lecture22
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
C
D
E
F
G
I
H
J
Directed Acyclic Graphs (DAGs)
DAGs are directed
graphs with no
cycles.
main()
mult()
add()
access()
Trees DAGs Graphs
read()
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
A
2
-5
C
B
1
2
10
E
D
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…
B
A
F
H
G
C
D
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 e-mail;
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
2
A
1
4
D
2
B
1
2
10
9
4
C
2
7
3
F
1
G
8
E
H
1
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
2
A
1
4
D
2
B
1
2
10
9
4
C
2
E
known
cost
H
1
G
8
7
vertex
A
B
C
D
E
F
G
H
3
F
1
The Cloud Proof
Next shortest path from
inside the known cloud
G
Better path
to the same node
P
THE KNOWN
CLOUD
Source
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?
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
|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:
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:
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)
4
7
9
1
2
5
Minimum spanning tree: the spanning tree with the
least total edge cost.
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)
2
2
B
A
F
1
4
3
2
1
10
9
G
C
2
D
7
4
8
E
H
Kruskal’s Algorithm in Action (2/5)
2
2
B
A
F
1
4
3
2
1
10
9
G
C
2
D
7
4
8
E
H
Kruskal’s Algorithm in Action (3/5)
2
2
B
A
F
1
4
3
2
1
10
9
G
C
2
D
7
4
8
E
H
Kruskal’s Algorithm in Action (4/5)
2
2
B
A
F
1
4
3
2
1
10
9
G
C
2
D
7
4
8
E
H
Kruskal’s Algorithm Completed (5/5)
2
2
B
A
F
1
4
3
2
1
10
9
G
C
2
D
7
4
8
E
H
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.
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:
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 7th; that’s only four days!)
• Final Exam (March 13th)