Professional Documents
Culture Documents
Submitted by:
Abdullah Al Mamun (Oronno)
Roll: 55
Year: 2nd
12th Batch
Session: 2006-07
What is Minimum Spanning Tree?
• Given a connected, undirected graph, a
spanning tree of that graph is a subgraph
which is a tree and connects all the
vertices together.
• A single graph can have many different
spanning trees.
• A minimum spanning tree is then
a spanning tree with weight less
than or equal to the weight of
every other spanning tree.
graph G
4 3 4 5
1 1 1
Algorithm for finding
Minimum Spanning Tree
Open List: d
Close List:
Total Cost: 0
Open List: a, f, e, b
Close List: d
Total Cost: 5
Open List: f, e, b
Close List: d, a
Total Cost: 11
Open List: b, e, g
Close List: d, a, f
Total Cost: 18
Open List: e, g, c
Close List: d, a, f, b
Total Cost: 25
Open List: c, g
Close List: d, a, f, b, e
Total Cost: 30
Open List: g
Close List: d, a, f, b, e, c
Total Cost: 39
Open List:
Close List: d, a, f, b, e, c
PSEUDO-CODE FOR PRIM'S
ALGORITHM
Designate one node as the start node
Add the start node to the priority queue of open nodes.
WHILE (there are still nodes to be added to the closed list)
{
Remove a node from priority queue of open nodes, designate it as current
node.
IF (the current node is not already in the closed list)
{
IF the node is not the first node removed from the priority queue, add the
minimal edge connecting it with a closed node to the minimal spanning
tree.
Add the current node to the closed list.
FOR each successor of current node
IF (the successor is not already in the closed list OR the successor is
now connected to a closed node by an edge of lesser weight than
before)
Add that successor to the priority queue of open nodes;
}
}
Sample C++ Implementation
• void prim(graph \&g, vert s) { • int minvertex(graph \&g, int *d) {
• int v;
• int dist[g.num_nodes];
• int vert[g.num_nodes]; • for (i = 0; i < g.num_nodes; i++)
• if (g.is_marked(i, UNVISITED)) {
• for (int i = 0; i < g.num_nodes; i++) { • v = i; break;
• dist[i] = INFINITY; • }