Prim's Algorithm

on minimum spanning tree
Submitted to:
Ms. Shaily Kabir
Lecturer, Department of C.S.E. University of Dhaka.

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 Spanning Tree from Graph G 2 4 1 3 2 4 5 1 1 .

Algorithm for finding Minimum Spanning Tree • The Prim's Algorithm • Kruskal's Algorithm • Baruvka's Algorithm .

Prim in 1957. Prim's algorithm runs in O(n*n) But the running time can be reduce using a simple binary heap data structure and an adjacency list representation .About Prim’s Algorithm The algorithm was discovered in 1930 by mathematician Vojtech Jarnik and later independently by computer scientist Robert C. The algorithm continuously increases the size of a tree starting with a single vertex until it spans all the vertices. Prim's algorithm is faster on dense graphs.

. Prim's uses a priority queue. Just as these algorithms maintained a closed list of nodes and the paths leading to them.• Prim's algorithm for finding a minimal spanning tree parallels closely the depthand breadth-first traversal algorithms. Prim's algorithm maintains a closed list of nodes and the edges that link them into the minimal spanning tree. • Whereas the depth-first algorithm used a stack as its data structure to maintain the list of open nodes and the breadth-first traversal used a queue.

.Let’s see an example to understand Prim’s Algorithm.

As a node is added to the closed list. At first we declare an array named: closed list. its successors (immediately adjacent nodes) are examined and added to a priority queue of open nodes. Adding a node and its edge to the closed list indicates that we have found an edge that links the node into the minimal spanning tree.Lets…. And consider the open list as a priority queue with min-heap. .

Total Cost: 0 Open List: d Close List: .

Close List: d f. b .Total Cost: 0 Open List: a. e.

a . b Close List: d. e.Total Cost: 5 Open List: f.

f . e. a.Total Cost: 11 Open List: b. g Close List: d.

b . g.Total Cost: 18 Open List: e. f. a. c Close List: d.

b. g Close List: d. f. a. e .Total Cost: 25 Open List: c.

e. c . f.Total Cost: 30 Open List: g Close List: d. a. b.

b. f. e.Total Cost: 39 Open List: Close List: d. a. c .

WHILE (there are still nodes to be added to the closed list) { Remove a node from priority queue of open nodes. Add the current node to the closed list. IF (the current node is not already in the closed list) { IF the node is not the first node removed from the priority queue.PSEUDO-CODE FOR PRIM'S ALGORITHM Designate one node as the start node Add the start node to the priority queue of open nodes. 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. designate it as current node. add the minimal edge connecting it with a closed node to the minimal spanning tree. } } .

second_vert(w)] = v. for (int i = 0.Sample C++ Implementation • • • • • • • • • • • • • • • • • • • void prim(graph \&g. • • • • • • • • • • int minvertex(graph \&g. for (i = 0. i < g.number()] = 0. int vert[g.num_nodes]. i < g. i++) { vert v = minvertex(g. w = g. return (v). vert s) { int dist[g. UNVISITED)) { v = i. } for (edge w = g.is_edge(w).num_nodes.num_nodes. i++) if (g.num_nodes. UNVISITED)) && (dist[i] < dist[v])) v = i.weight(w). v).first_edge. break. g. if (v != s) add_edge_to_MST(vert[v]. for (i = 0. int *d) { int v.is_marked(i.weight(w)) { dist[g.next_edge(w)) { if (dist[g. vert[g. if (dist[v] == INFINITY) return.first_vert(w)] = g. i++) if ((g.num_nodes]. i < g.mark(v. } } } } . i < g. g.second_vert(w)] = g. } for (i = 0. dist).is_marked(i. i++) { dist[i] = INFINITY. dist[s. VISITED).num_nodes.

searching Time complexity (total) O(V*V) binary heap and adjacency list Fibonacci heap and adjacency list O((V + E) log(V)) = O(E log(V)) O(E + V log(V)) .Complexity Analysis Minimum edge weight data structure adjacency matrix.

Because the cost between two terminal is different. Obviously. Another useful application of MST would be finding airline routes.Application One practical application of a MST would be in the design of a network. if we want to reduce our expenses. . and the edges would represent routes between the cities. A less obvious application is that the minimum spanning tree can be used to approximately solve the traveling salesman problem. the more it will cost. who are separated by varying distances. A convenient formal way of defining this problem is to find the shortest path that visits each point at least once. so MST can be applied to optimize airline routes by finding the least costly paths with no cycles. a group of individuals. Prim's Algorithm is a way to solve it Connect all computers in a computer science building using least amount of cable. the further one has to travel. The vertices of the graph would represent cities. For instance. wish to be connected together in a telephone network.

tokushima-u.htm .is.unf.html • • www-b2. Cormen Website: • PrimApplet/ computersciencetheory/'s_algorithm • www.Reference Book: Introduction to Algorithm By: Thomas • www.wikipedia.

Thank you for being with me… .

Sign up to vote on this title
UsefulNot useful