prims algorithm on mst
TRANSCRIPT
![Page 1: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/1.jpg)
Prim's Algorithmon minimum spanning tree
Submitted to:Ms. Shaily KabirLecturer,Department of C.S.E.University of Dhaka.
Submitted by:Abdullah Al Mamun (Oronno)Roll: 55Year: 2nd
12th BatchSession: 2006-07
![Page 2: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/2.jpg)
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.
![Page 3: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/3.jpg)
graph G
Spanning Tree from Graph G
2
4
1
2
3
1
4 5
1
![Page 4: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/4.jpg)
Algorithm for finding Minimum Spanning Tree
• The Prim's Algorithm• Kruskal's Algorithm• Baruvka's Algorithm
![Page 5: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/5.jpg)
About Prim’s AlgorithmThe algorithm was discovered in 1930 by mathematician Vojtech Jarnik and later independently by computer scientist Robert C. Prim in 1957.
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 densegraphs.
Prim's algorithm runs in O(n*n)
But the running time can be reduceusing a simple binary heap data structureand an adjacency list representation
![Page 6: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/6.jpg)
• Prim's algorithm for finding a minimal spanning tree parallels closely the depth-and breadth-first traversal algorithms. Just as these algorithms maintained a closed list of nodes and the paths leading to them, 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, Prim's uses a priority queue.
![Page 7: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/7.jpg)
Let’s see an example to understand
Prim’s Algorithm.
![Page 8: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/8.jpg)
Lets….� At first we declare an array named: closed list .
� And consider the open list as a priority queue with min-heap.
� Adding a node and its edge to the closed list indicates that we have found an edge that links the node into the minimal spanningtree. As a node is added to theclosed list, its successors(immediately adjacent nodes)are examined and added to apriority queue of open nodes.
![Page 9: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/9.jpg)
Open List: dClose List:
Total Cost: 0
![Page 10: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/10.jpg)
Open List: a, f, e, bClose List: d
Total Cost: 0
![Page 11: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/11.jpg)
Open List: f, e, bClose List: d, a
Total Cost: 5
![Page 12: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/12.jpg)
Open List: b, e, gClose List: d, a, f
Total Cost: 11
![Page 13: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/13.jpg)
Open List: e, g, cClose List: d, a, f, b
Total Cost: 18
![Page 14: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/14.jpg)
Open List: c, gClose List: d, a, f, b, e
Total Cost: 25
![Page 15: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/15.jpg)
Open List: gClose List: d, a, f, b, e, c
Total Cost: 30
![Page 16: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/16.jpg)
Open List: Close List: d, a, f, b, e, c
Total Cost: 39
![Page 17: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/17.jpg)
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;}
}
![Page 18: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/18.jpg)
Sample C++ Implementation
• void prim(graph \&g, vert s) {
• int dist[g.num_nodes];• int vert[g.num_nodes];
• for (int i = 0; i < g.num_nodes; i++) {• dist[i] = INFINITY;
• dist[s.number()] = 0;
• for (i = 0; i < g.num_nodes; i++) {• vert v = minvertex(g, dist);
• g.mark(v, VISITED);• if (v != s) add_edge_to_MST(vert[v], v);• if (dist[v] == INFINITY) return;
• for (edge w = g.first_edge; g.is_edge(w), w = g.next_edge(w)) {• if (dist[g.first_vert(w)] = g.weight(w)) {• dist[g.second_vert(w)] = g.weight(w);• vert[g.second_vert(w)] = v;• }• }• }• }
• int minvertex(graph \&g, int *d) {• int v;
• for (i = 0; i < g.num_nodes; i++)• if (g.is_marked(i, UNVISITED)) {• v = i; break;• }
• for (i = 0; i < g.num_nodes; i++)• if ((g.is_marked(i, UNVISITED)) && (dist[i] <
dist[v])) v = i;
• return (v);• }
![Page 19: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/19.jpg)
Complexity Analysis
O(E + V log(V))Fibonacci heap and adjacency list
O((V + E) log(V)) = O(E log(V))
binary heap and adjacency list
O(V*V)adjacency matrix, searching
Time complexity (total)Minimum edge weight data structure
![Page 20: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/20.jpg)
Application� One practical application of a MST would be in the design of a
network. For instance, a group of individuals, who are separated by varying distances, wish to be connected together in a telephone network. Because the cost between two terminal is different, if we want to reduce our expenses, Prim's Algorithm is a way to solve it
� Connect all computers in a computer science building using least amount of cable.
� A less obvious application is that the minimum spanning tree can be used to approximately solve the traveling salesman problem. A convenient formal way of defining this problem is to find the shortest path that visits each point at least once.
� Another useful application of MST would be finding airline routes. The vertices of the graph would represent cities, and the edges would represent routes between the cities. Obviously, the further one has to travel, the more it will cost,so MST can be applied to optimize airline routes by finding the least costly paths with no cycles.
![Page 21: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/21.jpg)
Reference
Book:Introduction to AlgorithmBy: Thomas H. Cormen
Website:• mathworld.wolfram.com• www-b2.is.tokushima-u.ac.jp/suuri/Prim.shtml • www.cprogramming.com/tutorial/
computersciencetheory/mst.html• en.wikipedia.org/wiki/Prim's_algorithm• www.unf.edu/~wkloster/foundations/
PrimApplet/PrimApplet.htm
![Page 22: Prims Algorithm on MST](https://reader033.vdocument.in/reader033/viewer/2022051108/543d2fa5b1af9f310a8b459e/html5/thumbnails/22.jpg)
Thank you for being with me…