minimum spanning trees (msts) and minimum cost spanning trees (mcsts)

Click here to load reader

Post on 15-Jan-2016

68 views

Category:

Documents

2 download

Embed Size (px)

DESCRIPTION

Minimum Spanning Trees (MSTs) and Minimum Cost Spanning Trees (MCSTs). What is a Minimum Spanning Tree? Constructing Minimum Spanning Trees. What is a Minimum-Cost Spanning Tree (MCST) ? Applications of Minimum Cost Spanning Trees. MCST Algorithms: Kruskal’s Algorithm. - PowerPoint PPT Presentation

TRANSCRIPT

Animated Example for Prim’ÿ Algorithm.Minimum Spanning Trees (MSTs) and Minimum Cost Spanning Trees (MCSTs)
What is a Minimum Spanning Tree?
Constructing Minimum Spanning Trees.
Applications of Minimum Cost Spanning Trees.
MCST Algorithms:
What is a Minimum Spanning Tree?
Let G = (V, E) be a simple, connected, undirected graph that is not edge-weighted.
A spanning tree of G is a free tree (i.e., a tree with no root) with | V | - 1 edges that connects all the vertices of the graph.
Thus a minimum spanning tree T = (V’, E’) for G is a subgraph of G with the following properties:
V’ = V
T is acyclic ( |E’| = |V| - 1)
*
Constructing Minimum Spanning Trees
Any traversal of a connected, undirected graph visits all the vertices in that graph. The set of edges which are traversed during a traversal forms a spanning tree.
For example, Fig (b) shows a spanning tree of G obtained from a breadth-first traversal starting at vertex a.
Similarly, Fig (c) shows a spanning tree of G obtained from a depth-first traversal starting at vertex c.
(a) Graph G
*
What is a Minimum-Cost Spanning Tree?
For an edge-weighted , connected, undirected graph, G, the total cost of G is the sum of the weights on all its edges.
A minimum-cost spanning tree for G is a minimum spanning tree of G that has the least total cost.
Example: The graph
Has 16 spanning trees. Some are:
*
Minimum-cost spanning trees have many applications. Some are:
Building cable networks that join n locations with minimum cost.
Building a road network that joins n cities with minimum cost.
Obtaining an independent set of circuit equations for an electrical network.
*
Kruskal’s algorithm finds the minimum cost spanning
tree of a graph by adding edges one-by-one to an initially empty free
tree
enqueue edges of G in a queue in increasing order of cost.
T = ;
if(e does not create a cycle with edges in T)
add e to T;
*
Tracing Kruskal’s Algorithm
Trace Kruskal's algorithm in finding a minimum-cost spanning tree for the undirected, weighted graph given below:
The minimum cost is: 24
*
Graph result = new GraphAsLists(false);
queue.enqueue(e);
while (!queue.isEmpty()){ // test if there is a path from vertex
// from to vertex to before adding the edge
Edge e = (Edge) queue.dequeueMin();
}
*
Vertex fromVertex = getVertex(from);
Vertex toVertex = getVertex(to);
throw new IllegalArgumentException("Vertex not in the graph");
PathVisitor visitor = new PathVisitor(toVertex);
boolean reached = false;
boolean isReached(){return reached;}
MCST Algorithms: Prim’s Algorithm
The algorithm continuously increases the size of a tree starting with a
single vertex until it spans all the vertices of G:
Consider a weighted, connected, undirected graph G=(V, E) and a free tree
T = (VT, ET) that is a subgraph of G
Initialize: VT = {x}; // where x is an arbitrary vertex from V
Initialize: ET = ;
repeat{
Choose an edge (v,w) with minimal weight such that v is in VT and w is not
(if there are multiple edges with the same weight, choose arbitrarily but
consistently); // add an minimal weight edge that will not form a cycle
Add vertex v to VT;
add edge (v, w) to ET;
}until (VT == V);
Graph and Minimum edge weight data structure
Time complexity (total)
O(E + V log(V))
Tracing Prim’s Algorithm (Example adopted from Wikipedia)
We want to find a Minimum Cost Spanning Tree for the graph on the left. The numbers near the edges indicate the edge weights.
Vertex D has been arbitrarily chosen as a starting point. Vertices A, B, E and F are connected to D through a single edge. A is the vertex nearest to D and will be chosen as the second vertex along with the edge AD.
*
Tracing Prim’s Algorithm (Cont’d)
The algorithm carries on as above. Vertex B, which is 7 away from A, is highlighted.
In this case, we can choose between C, E, and G. C is 8 away from B, E is 7 away from B, and G is 11 away from F. E is nearest, so we highlight the vertex E and the arc BE.
*
Tracing Prim’s Algorithm (Cont’d)
Vertex G is the only remaining vertex. It is 11 away from F, and 9 away from E. E is nearer, so we highlight it and the arc EG.
*
Trace Prim’s algorithm starting at vertex a:
The resulting minimum-cost spanning tree is:
*
Implementation of Prim’s Algorithm using MinHeap
The implementation of Prim's algorithm uses an Entry class, which contains the following three fields:
know: a boolean variable indicating whether the weight of the edge from this vertex to an active vertex v1 is known, initially false for all vertices.
weight : the minimum known weight from this vertex to an active vertex v1, initially infinity for all vertices except that of the starting vertex which is 0.
predecessor : the predecessor of an active vertex v1 in the MCST, initially unknown for all vertices.
public class Algorithms{
Implementation of Prim’s Algorithm using MinHeap (Cont’d)
make a table of values (known, weight(edge), predecessor) initially (FALSE,,null) for each vertex except the start vertex table entry is set to (FALSE, 0, null) ;
MinHeap = ;
// let tree T be empty;
while (MinHeap is not empty) { // T has fewer than n vertices
dequeue an Association (weight(e), v) from the MinHeap;
Update table entry for v to (TRUE, weight(e), v); // add v and e to T
for( each emanating edge f = (v, u) of v){
if( table[u].known == FALSE ) // u is not already in T
if (weight(f) < table[u].weight) { // find the current min edge weight
table[u].weight = weight(f);
table[u].predecessor = v;
}
}
}
}
*
public static Graph primsAlgorithm(Graph g, Vertex start){
int n = g.getNumberOfVertices();
for(int v = 0; v < n; v++)
table[v] = new Entry();
if(!table[n2].known && d < table[n2].weight){
table[n2].weight = d; table[n2].predecessor = v1;
queue.enqueue(new Association(new Integer(d), v2));
Iterator it = g.getVertices(); //add vertices of this graph to result
while (it.hasNext()){
// add edges to result using each vertex predecessor in table
it = g.getVertices();
if (v != start){
int index = g.getIndex(v);
String from = v.getLabel();
result.addEdge(from, to, new Integer(table[index].distance));
}
}
Tracing Prim’s Algorithm from the MinHeap implementation
Trace Prim’s algorithm in finding a MCST starting from vertex A:
Enqueue edge weights for edges emanating from A
Enqueue edge weights for edges emanating from C
23.bin
26.bin
25.bin
27.bin
Tracing Prim’s Algorithm from the MinHeap implementation (Cont’d)
The MCST:
*
Note on MCSTs generated by Prim’s and Kruskal’s Algorithms
Note: It is not necessary that Prim's and Kruskal's algorithm generate the same minimum-cost spanning tree.
For example for the graph:
Kruskal's algorithm (that imposes an ordering on edges with equal weights) results in the following minimum cost spanning tree:
*
Review Questions
Find the breadth-first spanning tree and depth-first spanning tree of the graph GA shown above.
For the graph GB shown above, trace the execution of Prim's algorithm as it finds the minimum-cost spanning tree of the graph starting from vertex a.
Repeat question 2 above using Kruskal's algorithm.
GB
A
D
E
B
C
30
5
20
6
9
2
12
6
T
0
null
F
5
C
T
2
A
F
6
C
F
6
C
table
queue
front
rear
6
D
5
B
6
E
30
B
9
D
F
0
null
F
T
0
null
F
30
A
F
2
A
F
9
A
F
T
0
null
T
5
C
T
2
A
F
6
C
F
6
C
table
queue
front
rear
6
E
6
D
30
B
9
D
T
0
null
T
5
C
T
2
A
T
6
C
F
6
C
table
queue
front
rear
9
D
6
E
30
B
T
0
null
T
5
C
T
2
A
T
6
C
T
6
C
table
queue
front
rear
A
D
E
B
C
5
6
2
6