Minimum Spanning Tree CS 583 Analysis of Algorithms 7/1/2016

advertisement
Minimum Spanning Tree
CS 583
Analysis of Algorithms
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
1
Outline
• Breadth-First Tree
– Predecessor subgraph
– Printing paths
• Minimum Spanning Trees
– Definitions
– Generic algorithm
– Correctness
• Self-test
– 23.1-1, 23.1-4
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
2
Breadth-First Trees
• BFS builds a breadth-first tree as it searches the
graph.
– It is represented by the  field in each vertex.
– Define the predecessor subgraph of G=(V, E) as G =
(V, E):
• V = {v  V : [v]  NIL}  {s}
• E = {([v],v) : v  V - {s}}
– The predecessor graph is a breadth-first tree if:
• V consists of the vertices reachable from s, and
• For all v  V there is a unique path from s to v in G that is also
a shortest path in G.
– The breadth-first tree is a tree since (see Theorem B.2):
• It is connected (there’s a path between any two vertices), and
• | E | = | V | - 1
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
3
Breadth-First Trees in BFS
Lemma 22.6
When applied to a directed or undirected graph G=(V,E), BFS constructs  so
that the predecessor subgraph G = (V, E) is a breadth-first tree.
Proof.
Line 16 of BFS sets [v] = u if and only if (u,v)  E and (s,v) < . Hence V
consists of the vertices in V reachable from s.
Since G forms a tree, it contains a unique path from s to v. Using Theorem
22.5 we can show by induction that every such path is a shortest path. 
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
4
Printing Shortest Paths
The following procedure prints out the vertices on a shortest path from s to v,
assuming that BFS has already run.
Print-Path(G,s,v)
1 if v = s
2
print s
3 else
4
if [v] = NIL
5
print “No path from s to v exists!”
6
else
7
Print-Path(G,s,[v])
8
print v
The procedure runs in O(|V|) time since each recursive call is for a path one
vertex shorter.
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
5
The Networking Problem
• Design a network that connects n sites at a minimum
total cost.
– A weighted graph represents the cost (an edge’s weight)
of connecting any two sites.
– An optimal network will be a subgraph of such a graph
containing the minimum number of edges (n-1) at a total
minimum sum of the weights of these edges.
– Such a subgraph forms a spanning tree.
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
6
The Minimum Spanning Tree Problem
• Assume we have a connected, undirected graph G =
(V,E) with the weight function w : E  R.
– For each edge (u,v)  E, we have weight w(u,v).
• We wish to find an acyclic subset T  E that
connects all of the vertices, and whose total weight
is minimized.
– w(T) = (u,v)  T w(u,v)
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
7
Generic MST
• A “generic” algorithm uses a greedy approach in
that it grows the minimum spanning tree one edge at
a time.
• The algorithm manages a set of edges A and
maintains the following loop invariant:
– Prior to each iteration, A is a subset of some minimum
spanning tree.
• At each step we determine an edge that can be added
to A without violating the invariant.
– Such edge is called a safe edge.
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
8
Generic MST: Pseudocode
The following procedure sketches an algorithm to find a subset of edges of a
weighted graph that forms a minimum spanning tree.
Generic-MST(G,w)
1 A = 
2 while <A does not form a spanning tree>
3
<find an edge (u,v) that is safe for A>
4
A = A  {(u,v)}
5 return A
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
9
Generic MST: Loop Invariant
• Initialization:
– After line 1, the set A is empty, which is a subset of any
spanning tree,
• Maintenance:
– The while loop 2-4 maintains the invariant by adding only
safe edges.
• Termination:
– All edges added to A are in a minimum spanning tree.
– Once the spanning tree is formed it is the minimum
spanning tree.
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
10
Finding Safe Edges
• The key is to find safe edges (line 3)
– Such edge always exists according to the loop invariant.
• A cut (S, V-S) of an undirected graph G = (V,E) is a
partition of V.
– An edge (u,v) crosses the cut, if one of its endpoints is in
S, and the other is in V-S.
– A cut respects a set A of edges if no edge in A crosses the
cut.
– An edge is a light edge crossing a cut if its weight is the
minimum of any edge crossing the cut.
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
11
Recognizing Safe Edges
Theorem 23.1
• Let G = (V,E) be a connected, undirected graph with a real valued function w
defined on E.
• Let A be a subset of E that is included in some minimum spanning tree for G.
• Let (S, V – S) be any cut of G that respects A.
• Let (u,v) be a light edge crossing (S, V – S).
• Then edge (u, v) is safe for A.
Proof
Let T be a minimum spanning tree that includes A. If it contains (u,v), then it
can be safely added to A. If it does not, we construct another minimum
spanning tree T1 that includes A  (u,v).
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
12
Recognizing Safe Edges (cont.)
• The edge (u,v) forms a cycle with edges on the path
from u to v in T.
• Since u and v are on the opposite sides of the cut (S,
V – S), there’s at least one edge in T on this path that
also crosses the cut.
– Let this edge be (x,y).
• Since (x,y) is on unique path from u to v in T,
removing (x,y) breaks T.
– Adding (u,v) reconnects it to form a new spanning tree:
T1 = T – {(x,y)}  {(u,v)}
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
13
Recognizing Safe Edges (cont.)
• Show that T1 is a minimum spanning tree:
– Since (u,v) is a light edge and (x,y) also crosses the cut,
we have:
• w(u,v)  w(x,y)
• w(T1) = w(T) – w(x,y) + w(u,v)  w(T)
– Since T is a minimum spanning tree, T1 is also a
minimum spanning tree.
• Show that (u,v) is a safe edge for A:
–
–
–
–
7/1/2016
AT
(x,y)  A
A  {(u,v)}  T1
Since T1 is a minimum spanning tree, (u,v) is safe for
A.
CS583 Fall'06: Minimum Spanning Tree
14
Generic-MST: Framework
• As the algorithm proceeds, the set A is always
acyclic.
– MST cannot contain a cycle.
• At any step of the algorithm the graph GA = (V,A) is
a forest.
– Each of the connected components is a tree.
• Any safe edge for A connects distinct components of
GA.
– A  {(u,v)} must be acyclic.
• The loop 2-4 is executed |V| - 1 times to add each
edge of an MST.
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
15
Generic-MST: Framework (cont.)
Corollary 23.2
• Let G = (V,E) be a connected, undirected graph with a real valued function w
defined on E.
• Let A be a subset of E that is included in some minimum spanning tree for G.
• Let C = (VC ,EC) be a connected component (tree) in the forest GA.
• Let (u,v) be a light edge connecting C to some other component in GA.
• Then edge (u, v) is safe for A.
Proof
The cut (VC , V - VC) respects A, and edge (u,v) is a light edge for this cut.
Therefore, (u,v) is safe for A. 
7/1/2016
CS583 Fall'06: Minimum Spanning Tree
16
Download