pptx - UTA.edu

Minimum Spanning Trees
CSE 2320 – Algorithms and Data Structures
Vassilis Athitsos and Alexandra Stefan
University of Texas at Arlington
These slides are based on “Algorithms in C” by R. Sedgewick
1
Weighted Graphs
• Each edge has a weight.
• Example: a transportation
network (roads, railroads,
subway). The weight of each
road can be:
– The length.
– The expected time to traverse.
– The expected cost to build.
20
0
6
10
15
5
30
20
2
1 15
7
3
25
18
30
10
4
• Example: in a computer network,
the weight of each edge (direct
link) can be:
– Latency.
– Expected cost to build.
2
Spanning Tree
• A spanning tree is a tree that
connects all vertices of the graph.
• The weight of a spanning tree is
the sum of weight of its edges.
• Minimum-cost spanning tree
(MST):
– Connects all vertices of the
graph (spanning tree).
– Has the smallest total weight of
edges.
20
0
20
0
6
10
15
5
30
20
6
2
10
1 15
7
3
25
18
30
20
1 15
30
15
10
4
Weight:20+15+30+20+30+25+18 = 158
5
2
7
3
25
18
30
10
4
Weight:10+20+15+30+20+10+15 = 120
3
Minimum-Cost
Spanning Tree (MST)
6
15
30
7
3
25
10
4
18
0
– Allow directed graphs.
– Not allow negative weights.
2
1 15
5
• Warning: later in the course
(when we discuss Dijkstra's
algorithm) we will make the
opposite assumptions:
30
20
10
• Assume that the graph is:
– connected
– undirected
– edges can have negative
weights.
20
0
20
6
20
10
15
5
30
2
1 15
3
7
10
4
4
Prim's Algorithm - Overview
• Prim's algorithm:
– Start from an tree that contains a single vertex.
– Keep growing that tree, by adding at each step the
‘shortest’ edge connecting a vertex in the tree to a vertex
outside the tree.
• We will cover the CLRS version.
– Sedgewick gives other implementations
5
Example
1. Start by adding a vertex, r,
10
(here r = 0) to the MST
(minimum-cost spanning tree).
3
2. Repeat until all
15
vertices have been
5
added to the tree:
20
0
6
30
20
2
1 15
7
25
18
30
10
4
3. From all edges connecting vertices from
the current tree to vertices outside the
current tree, select the smallest edge.
4. Add that edge to the tree, and also add to
the tree the non-tree vertex of that edge.
6
Example
2. Repeat until all vertices have been added to the tree:
a) Select the smallest edge from all edges connecting vertices from the
current tree to vertices outside the tree.
b) Add to the tree that edge, and non-tree vertex of it.
20
0
Red - current MST
Purple - potential edges
Blue – unprocessed edges and vertices.
6
10
15
5
30
20
2
1 15
7
3
25
18
30
10
4
7
Example
2. Repeat until all vertices have been added to the tree:
a) Select the smallest edge from all edges connecting vertices
from the current tree to vertices outside the tree.
b) Add to the tree that edge, and non-tree vertex of it.
20
0
20
0
6
10
15
5
30
20
2
1 15
7
3
25
18
6
10
15
5
2
1 15
30
10
4
30
20
7
3
25
18
30
10
4
8
Example
2. Repeat until all vertices have been added to the tree:
a) Select the smallest edge from all edges connecting vertices
from the current tree to vertices outside the tree.
b) Add to the tree that edge, and non-tree vertex of it.
20
0
6
10
15
5
30
20
2
1 15
7
3
25
18
30
10
4
9
Example
Note that (5,4) with weight 18 is not
picked because it connects vertices that
are already in the tree.
20
0
20
0
6
10
15
5
30
20
2
1 15
7
3
25
18
6
10
15
5
2
1 15
30
10
4
30
20
7
3
25
18
30
10
4
10
Example
20
0
20
0
6
10
15
5
30
20
2
1 15
7
3
25
18
6
10
15
5
2
1 15
30
10
4
30
20
7
3
25
18
30
10
4
11
Example 2
NOTE: This graph has some new edges
added and weights changed.
Transition to implementation:
1. Note that for each vertex that is not in the MST,
we need to know what is the shortest edge that
connects it to the MST.
2. As we add one vertex to the tree, update its
neighbors if:
- They are not in the tree
5
- This vertex ‘brings them closer’
20
0
10
2
1 15
18
15
30
20
9
7
3
25
12
6
4
30
5
4
12
Example 2
Transition to implementation:
1. Note that for each vertex that is not in the MST, we need to know what is the
shortest edge that connects it to the MST.
2. As we add one vertex to the tree, update its neighbors if:
- They are not in the tree
20
0
- This vertex ‘brings them closer’
4
30
20
2
10
1 15
9
18
Note that as we added 5, for vertex 3
We record that edge (5,3, 15) is the
‘shortest’ edge to connect 3 to the MST
( not (0,3,18) ).
Convention: (u, v, weight(u,w))
15
5
7
3
25
12
6
30
5
4
13
Example 2
Transition to implementation:
1. Note that for each vertex that is not in the MST, we need to know what is the
shortest edge that connects it to the MST.
2. As we add one vertex to the tree, update its neighbors if:
- They are not in the tree
20
0
- This vertex ‘brings them closer’
4
30
20
2
10
1 15
9
18
Note that as we added 4, edge (4, 3, 25) is not
a better connection for 3 and so we still keep
edge (5,3,15) for vertex 3).
5
Similar to vertex 3 before, for vertex 7 we mark
edge (4,7,10) as the best (instead of (0,7,15) ).
15
7
3
25
12
6
30
5
4
14
Example 2
10
5
2
1 15
18
15
30
20
25
12
6
4
15
5
4
Now (7,2,9) is recorded for vertex 2.
5
2
1 15
18
30
30
20
10
9
7
3
20
0
20
0
9
7
3
25
12
6
4
30
5
4
Now (2,6,4) is recorded for vertex 6.
15
20
0
30
20
10
5
25
4
20
15
2
1 15
18
25
12
6
4
15
5
4
5
2
1 15
18
30
30
20
10
9
7
3
20
0
30
20
10
30
5
12
0
5
9
7
3
15
2
1 15
18
6
4
9
7
3
25
12
6
4
30
5
4
16
Prim’s Algorithm
(CLRS)
• Q – is a priority queue
Time complexity:
17
Prim’s Algorithm
(CLRS)
• Q – is a priority queue
Time complexity:
O(V + VlgV + E lgV) =
O(ElgV)
O(V)
connected graph => |E| ≥ (|V|-1)
O(V) (build heap)
O(V)
O(lgV)
Lines 6 & 8 together: O(E)
(touch each edge twice)
O(V*lgV)
O(E*lgV)
Decrease-priority: O(lgV)
18
Prim’s Algorithm
(CLRS)
• See if v is in Q.
– ANS:
• Find index of v in Q
– Note difference between v and node
in heap corresponding to v.
– See heap slides : Index Heap Example
19
Kruskal's Algorithm
20
Kruskal's Algorithm: Overview
• Kruskal's algorithm works with a forest (a set of trees).
– Initially, each vertex in the graph is its own tree.
– Keep merging trees together, until end up with a single tree.
21
Kruskal's Algorithm: Overview
1. Initialize a forest (a collection of trees), by defining
each vertex to be its own separate tree.
2. Repeat until the forest contains a single tree:
3. Find the shortest edge F connecting two trees in the
forest.
4. Connect those two trees into a single tree using edge F.
• The abstract description is simple, but we need to
think carefully about how exactly to implement these
steps as it affects the runtime.
– Sort edges: Y/N?
– Put edges in a min-heap?
22
Kruskal’s Algorithm
• Example 2 graph with edge (3,1, 11) added.
• Algorithm (based on a forest of trees):
– Initially, each vertex in the graph is its own tree.
– Keep merging trees together, until end up with a single
tree. Order u, v, weight
20
0
1st
2nd
10
…
18
15
Last
5
30
20
1
11
3
25
12
2
6
4
9
15
7
30
5
4
23
Extra Page
20
0
10
18
15
5
30
20
3
1
11
25
2
6
4
9
15
7
30
5
4
24
Kruskal's Algorithm:
Worked Out Example 1
1. Initialize a forest (a
collection of trees), by
defining each vertex to be
its own separate tree.
2. Repeat until the forest
contains a single tree:
3.
4.
Find the shortest edge F
connecting two trees in the
forest.
Connect those two trees
into a single tree using
edge F.
(work-out example)
20
0
6
10
15
5
30
20
2
1 15
7
3
25
20
30
10
4
25
Kruskal's Algorithm: An Example
1. Initialize a forest (a
collection of trees), by
defining each vertex to be
its own separate tree.
2. Repeat until the forest
contains a single tree:
3.
4.
Find the shortest edge F
connecting two trees in the
forest.
Connect those two trees
into a single tree using
edge F.
20
0
6
15
5
30
20
10
2
1 15
7
3
25
20
30
10
4
26
Kruskal's Algorithm: An Example
1. Initialize a forest (a
collection of trees), by
defining each vertex to be
its own separate tree.
2. Repeat until the forest
contains a single tree:
3.
4.
Find the shortest edge F
connecting two trees in the
forest.
Connect those two trees
into a single tree using
edge F.
20
0
6
15
5
30
20
10
2
1 15
7
3
25
20
30
10
4
27
Kruskal's Algorithm: An Example
1. Initialize a forest (a
collection of trees), by
defining each vertex to be
its own separate tree.
2. Repeat until the forest
contains a single tree:
3.
4.
Find the shortest edge F
connecting two trees in the
forest.
Connect those two trees
into a single tree using
edge F.
20
0
6
15
5
30
20
10
2
1 15
7
3
25
20
30
10
4
28
Kruskal's Algorithm: An Example
1. Initialize a forest (a
collection of trees), by
defining each vertex to be
its own separate tree.
2. Repeat until the forest
contains a single tree:
3.
4.
Find the shortest edge F
connecting two trees in the
forest.
Connect those two trees
into a single tree using
edge F.
20
0
6
15
5
30
20
10
2
1 15
7
3
25
20
30
10
4
29
Kruskal's Algorithm: An Example
1. Initialize a forest (a
collection of trees), by
defining each vertex to be
its own separate tree.
2. Repeat until the forest
contains a single tree:
3.
4.
Find the shortest edge F
connecting two trees in the
forest.
Connect those two trees
into a single tree using
edge F.
20
0
6
15
5
30
20
10
2
1 15
7
3
25
20
30
10
4
30
Kruskal's Algorithm: An Example
1. Initialize a forest (a
collection of trees), by
defining each vertex to be
its own separate tree.
2. Repeat until the forest
contains a single tree:
3.
4.
Find the shortest edge F
connecting two trees in the
forest.
Connect those two trees
into a single tree using
edge F.
0
20
6
20
10
15
5
30
2
1 15
3
7
10
4
31
Definitions
(CLRS, pg 625)
• A cut (S, V-S) of an graph is a partition of its vertices, V.
• An edge (u,v) crosses the cut (S, V-S) if one of its
endpoints is in S and the other in V-S.
• Let A be a subset of a minimum spanning tree over G.
• An edge (u,v) is safe for A if A  {(u,v)} is still a subset of
a minimum spanning tree.
• A cut respects a set of edges, A, if no edge in A crosses
the cut.
• An edge is a light edge crossing a cut if its weight is the
minimum weight of any edge crossing the cut.
32
Correctness of Prim and Kruskall
(CLRS, pg 625)
• Let G = (V,E) be a connected, undirected, weighted
graph. Let A be a subset of some minimum spanning
tree, T, for G, let (S, V-S) be some cut of G that
respects A, and let (u,v) be a light edge crossing (S, VS). Then, edge (u,v) is safe for A.
• Proof:
– If (u,v) is part of T, done
– Else, in T, u and v must be connected through another
path, p. One of the edges of p, must connect a vertex x
from A and a vertex, y, from V-A. Adding edge(u,v) to T will
create a cycle with the path p. (x,y) also crosses (A, V-A)
and (u,v) is light => weight(u,v) <=
33
Note
• There may be several different MST in a graph, but
they will all have the same weight (smallest possible).
34