# 1 minimum spanning trees. minimum- spanning trees 1. concrete example: computer connection 2....

Post on 03-Jan-2016

224 views

Category:

## Documents

Embed Size (px)

TRANSCRIPT

Minimum Spanning Trees2. Definition of a Minimum- Spanning Tree
*
Imagine: You wish to connect all the computers in an
office building using the least amount of cable
Each vertex in a graph G represents a computer
Each edge represents the amount of cable needed to
connect all computers
is tree (acyclic)
1.bin
*
A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree.
A graph may have many spanning trees.
or
or
or
Graph A
Spanning Trees
Complete Graph
A complete graph with n vertices has
n (n-2) spanning trees.(Cayley's formula)
53 is 125 86 is 262144
108 is 100 million 10098 is 10196
Compare: there are
A nanosecond is one billionth (10-9) of a second.
(An electrical signal can travel about 30cm in a nanosecond.) There are
31.5576*1015 nanoseconds in a year.
*
Input:
Undirected connected graph G = (V, E) and weight function w : E→R,
Output:
A Minimum spanning tree T : tree that connects all the vertices and minimizes
Greedy Algorithms
Theorem. Let T be the MST of G = (V, E),
and let A V. Suppose that (u, v) ∈ E is the
least-weight edge connecting A to V – A.
Then, (u, v) ∈ T.
Generic algorithm
Grow MST one edge at a time
Manage a set of edges A, maintaining the following loop invariant:
Prior to each iteration, A is a subset of some MST
At each iteration, we determine an edge (u, v) that can be added to A without violate this invariant
A {(u, v)} is also a subset of a MST
(u, v) is called a safe edge for A
*
*
Any MST tree contains |V| - 1 edges
*
How to Find A Safe Edge?
Theorem 23.1. Let A be a subset of E that is included in some MST, let (S, V-S) be any cut of G that respects A, and let (u, v) be a light edge crossing (S, V-S). Then edge (u, v) is safe for A
Cut (S, V-S): a partition of V
Crossing edge: one endpoint in S and the other in V-S
A cut respects a set of A of edges if no edges in A crosses the cut
*
*
*
*
A={(a,b}, (c, i}, (h, g}, {g, f}}
S={a, b, c, i, e}; V-S = {h, g, f, d} many kinds of cuts satisfying the requirements of Theorem 23.1
(c, f) is the light edges crossing S and V-S and will be a safe edge
Illustration of Theorem 23.1
Greedy strategy:
From the remaining edges, select a least-cost edge that does not result in a cycle when added to the set of already selected edges
Repeat |V|-1 times
OUTPUT:
touches all vertices,
has n-1 edges
Algorithm:
Add the edges one at a time, in increasing weight order
An edge is accepted it if connects vertices of distinct trees (if the edge does not form a cycle in A)
until A contains n-1 edges
*
3 Make-Set(v) //creates set containing v (for initialization)
4 sort the edges of E
5 for each (u,v)ÎE do
6 if Find-Set(u) ¹ Find-Set(v) then // different component
7 A ¬ A È {(u,v)}
8 Union(Set(u),Set(v)) // merge
9 return A
Data Structures For Kruskal’s Algorithm
Does the addition of an edge (u, v) to T result in a cycle?
Each component of T is a tree.
When u and v are in the
same component, the addition of the edge (u, v) creates a cycle.
different components, the addition of the edge (u, v) does not create a cycle.
1
3
2
4
2
5
7
6
8
4
7
3
6
Data Structures For Kruskal’s Algorithm
Each component of T is defined by the vertices in the component.
Represent each component as a set of vertices.
{1, 2, 3, 4}, {5, 6}, {7, 8}
Two vertices are in the same component iff they are in the same set of vertices.
3
4
6
7
1
3
5
7
2
4
6
8
2
Data Structures For Kruskal’s Algorithm
When an edge (u, v) is added to T, the two components that have vertices u and v combine to become a single component
In our set representation of components, the set that has vertex u and the set that has vertex v are united.
{1, 2, 3, 4} + {5, 6} {1, 2, 3, 4, 5, 6}
1
3
5
7
2
4
6
8
2
3
4
6
7
1 A ¬ Æ
2 for each vertex v Î V[G] do // takes O(V)
3 Make-Set(v)
4 sort the edges of E // takes O(E lg E)
// takes O(E)
6 if Find-Set(u) ¹ Find-Set(v) then
7 A ¬ A È {(u,v)}
8 Union(Set(u),Set(v))
Kruskal’s Algorithm consists of two stages.
Initializing the set A in line 1 takes O(1) time.
Sorting the edges by weight in line 4.
takes O(E lg E)
|E| FindSet(), for loop in lines 5-8.
|V| - 1 Union(), for loop in lines 5-8.
which takes O(V + E)
O(E lg E)
We have lg E = O(lg V) because # of E = V-1
So total running time becomes O(E lg V).
*
Prim’s Algorithm
The tree starts from an arbitrary root vertex r and grows until the tree spans all the vertices in V.
At each step,
Vertex based algorithm.
*
MST-Prim(G,w,r) //G: graph with weight w and a root vertex r
1 for each u Î V[G]{
2 key[u] ¬ ¥
}
6 while Q ¹ Æ do
7 u ¬ ExtractMin(Q) // making u part of T
8 for each v Î Adj[u] do
9 if v Î Q and w(u,v) key[v] then
10 p[v] ¬ u
11 key[v] ¬ w(u,v)
updating keys
For each vertex v, key [v] is min weight of any edge connecting v to a vertex in tree.
key [v]= ∞ if there is no edge and p [v] names parent of v in tree.
When algorithm terminates the min-priority queue Q is empty.
*
Prim’s Algorithm
Lines 1-5 set the key of each vertex to ∞ (except root r, whose key is set to 0 first vertex processed). Also, set parent of each vertex to NULL, and initialize min-priority queue Q to contain all vertices.
Line 7 identifies a vertex u Q
Removing u from set Q adds it to set Q-V of vertices in tree, thus adding (u, p[ u]) to A.
*
MST-Prim(G,w,r)
2 key[u] ¬ ¥
6 while Q ¹ Æ do
7 u ¬ ExtractMin(Q) // making u part of T
8 for each v Î Adj[u] do
9 if v Î Q and w(u,v) < key[v] then
10 p[v] ¬ u
11 key[v] ¬ w(u,v)
Extract-Min is executed |V| times
*
Binary heap: O(E lg V):
BuildHeap takes O(log V) time
number of “while” iterations: V
ExtractMin takes O(lg V) time
total number of “for” iterations: E
DecreaseKey takes O(lg V) time
Hence,
Time = O(V lg V + E lg V) = O(E lg V)
Since E V – 1 (because G is connected)
*
Minimum bottleneck spanning tree
A bottleneck edge is the highest weighted edge in a spanning tree.
(,)
()(,)
r
u
u
14