Data Structure(Spanning Tree and Minimam spanning tree)
Spanning Tree :
Properties of Spanning Tree
- A connected graph can have multiple spanning trees. A graph with n vertices can have an n^(n-2) number of spanning trees.
- Spanning trees does not have any loop or cycle.
- Spanning trees have n vertices and n-1 number of edges.
- All spanning tree of a graph has equivalent vertices.
- Removing a single edge from the spanning tree will make the graph disconnected as the spanning tree is minimal connected.
- Adding any edge can create a loop or cycle in the spanning tree.
- Spanning trees can be formed on connected graphs only, disconnected graphs cannot form spanning trees.
Example of Spanning Tree
Consider the following original graph:
The following Spanning trees are possible from the above graph:
Minimum Spanning Tree
A minimum spanning tree or minimum cost spanning tree is that spanning tree, which covers all the vertices of the graph with minimum edges and the sum of the weight of those edges is minimum among other spanning trees of that graph.
Example of a Spanning Tree
The initial graph is:
The possible spanning trees from the above graph are:
The minimum spanning tree from the above spanning trees is:
The minimum spanning trees are mainly of two types:
- Prim’s MST
- Kruskal’s MST
Prim’s MST
Prim’s algorithm starts with a single node/vertex and moves on to the adjacent vertices to explore all the connected edges. The idea behind prim’s algorithm is that it maintains two sets of vertices. The first set comprises vertices that are already included in the minimum spanning tree and the other set consists of all the vertices which are not included yet.
How Prim's algorithm works
It falls under a class of algorithms called greedy algorithms that find the local optimum in the hopes of finding a global optimum.
We start from one vertex and keep adding edges with the lowest weight until we reach our goal.
The steps for implementing Prim's algorithm are as follows:
- Initialize the minimum spanning tree with a vertex chosen at random.
- Find all the edges that connect the tree to new vertices, find the minimum and add it to the tree
- Keep repeating step 2 until we get a minimum spanning tree
Example of Prim's algorithm
Kruskal’s MST
Kruskal’s algorithm is a greedy algorithm used to find out the shortest path in a minimum-spanning tree. The algorithm aims to traverse the graph and detect the subset of edges with minimal value and cover all the vertices of the graph. At every step of the algorithm and analysis, it follows a greedy approach for an overall optimized result.
Kruskal’s algorithm can be summed up as a minimum spanning tree algorithm taking a graph as input and forms a subset of the edges of the graph,
- which has a minimum sum of edge weight among all the trees that can be formed from that graph.
- that form a tree including each vertex of the graph, without forming any cycle between the vertex.
How Kruskal's algorithm works
It falls under a class of algorithms called greedy algorithms that find the local optimum in the hopes of finding a global optimum.
We start from the edges with the lowest weight and keep adding edges until we reach our goal.
The steps for implementing Kruskal's algorithm are as follows:
- Sort all the edges from low weight to high
- Take the edge with the lowest weight and add it to the spanning tree. If adding the edge created a cycle, then reject this edge.
- Keep adding edges until we reach all vertices.
Example of Kruskal's algorithm
Applications of Spanning Tree:
- In routing protocols
- Cluster mapping and analysis
- Network Planning
- Explore the path/ route in the maps.
Comments
Post a Comment