Biomimicry sea spray can save our Beaches

The Thwaites Glacier is rapidly calving off and when he is off, there will be no more Sand Beaches. Read my lips, no more Sand Beaches, anywhere in the world and many more dire consequences. We have…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Introduction to Spanning tree in Data Structure

A Spanning Tree of Graph G is a tree that includes all vertices of G, each and every edge belongs to G. In other words, spanning tree is an acyclic sub-graph of a connected and undirected graph G, that connects all the vertices. Spanning Trees are more like a sub-graphs of graph G that have some important properties. If T is a spanning tree of graph G then

· T must be a sub-graph of G or every vertex that appears in T must also be appeared in G.

· Every spanning tree T must contain every vertex in G.

· If all the edges of spanning tree T also exists in Graph G, then G is similar to T.

An example of Spanning tree is:

Properties of Spanning Tree

· Spanning trees are acyclic, it means they do not have any closed loops.

· A connected graph can have more than one spanning tree.

· All possible spanning trees for a same graph G, must have same number of vertices and edges.

· Adding or removing one edge to the spanning tree will create a closed loop or makes the graph disconnected, that makes spanning tree a maximally acyclic or minimally connected.

· All spanning trees must have |n|-1 edges, where n is number of vertices in graph.

Spanning trees are useful for path-finding algorithms such as Kruskal’s Algorithm and Prim’s Algorithm.

Minimum Spanning Tree

Minimum Spanning Tree is a form of spanning tree that has minimum weight that all other spanning trees of the graph. A minimum spanning tree for a weighted graph G is a type of spanning tree that reduces the weight of edges within the tree. Similarly, for un-weighted graph G, a minimum spanning tree that minimizes the number of edges.

A minimum spanning tree is not a special kind of tree, it is just a type of spanning tree with minimal total weight. Minimum spanning tree has direct application in the design of networks. There are also many minimum spanning tree as similar to spanning tree. An example of minimum spanning tree is shown as:

There are two famous algorithms that are used to find minimum spanning tree for an acyclic graph. These algorithms are described as:

· Kruskal’s Algorithm

Algorithm Steps:

· Sort the graph edges with respect to their weights.

· Create a priority queue Q that contains all the edges of the graph.

· Start adding edges with least weight until the edge of the largest weight.

· Only add edges which doesn’t form a cycle.

C program for Kruskal’s Algorithm

1. #include <iostream>

2. #include <vector>

3. #include <utility>

4. #include <algorithm>

5. using namespace std;

6. const int MAX = 1e4 + 5;

7. int id[MAX], nodes, edges;

8. pair <long long, pair<int, int> > p[MAX];

9.

10. void init()

11. {

12. for(int i = 0;i < MAX;++i)

13. id[i] = i;

14. }

15.

16. int root(int x)

17. {

18. while(id[x] != x)

19. {

20. id[x] = id[id[x]];

21. x = id[x];

22. }

23. return x;

24. }

25.

26. void union1(int x, int y)

27. {

28. int p = root(x);

29. int q = root(y);

30. id[p] = id[q];

31. }

32.

33. long long kruskal(pair<long long, pair<int, int> > p[])

34. {

35. int x, y;

36. long long cost, minimumCost = 0;

37. for(int i = 0;i < edges;++i)

38. {

39. x = p[i].second.first;

40. y = p[i].second.second;

41. cost = p[i].first;

42. if(root(x) != root(y))

43. {

44. minimumCost += cost;

45. union1(x, y);

46. }

47. }

48. return minimumCost;

49. }

50.

51. int main()

52. {

53. int x, y;

54. long long weight, cost, minimumCost;

55. init();

56. cout <<”Enter Nodes and edges”;

57. cin >> nodes >> edges;

58. for(int i = 0;i < edges;++i)

59. {

60. cout<<”Enter the value of X, Y and edges”;

61. cin >> x >> y >> weight;

63. }

64. sort(p, p + edges);

65. minimumCost = kruskal(p);

66. cout <<”Minimum cost is “<< minimumCost << endl;

67. return 0;

68. }

Time Complexity

In Kruskal’s Algorithm, most of the time is consumed in sorting since the total complexity of Disjoint set operation will be O(ElogV) which is the overall time complexity of the algorithm.

Algorithm Steps

· Choose a starting vertex.

· Check for outgoing edges and select the one with least weight.

· Add the selected edge and the vertex to the minimum spanning tree T.

· Avoid making loops while selecting edges and vertices.

1. #include <stdio.h>

2. #include <limits.h>

3. #define vertices 5

4.

6. {

8.

9. for (i = 0; i < vertices; i++)

10. if (mst[i] == 0 && k[i] < minimum )

11. minimum = k[i], min = i;

12.

13. return min;

14. }

15.

16.

17. void prim(int g[vertices][vertices])

18. {

19. int parent[vertices];

20. int k[vertices];

21. int mst[vertices];

22. int i, count,u,v;

23. for (i = 0; i < vertices; i++)

25.

26. k[0] = 0;

27. parent[0] = -1;

28.

29. for (count = 0; count < vertices-1; count++)

30. {

31.

33. mst[u] = 1;

34.

35. for (v = 0; v < vertices; v++)

36.

37. if (g[u][v] && mst[v] == 0 && g[u][v] < k[v])

38. parent[v] = u, k[v] = g[u][v];

39. }

40.

41. for (i = 1; i < vertices; i++)

42. printf(“%d %d %d \n”, parent[i], i, g[i][parent[i]]);

43. }

44.

45.

46. void main()

47. {

48. int g[vertices][vertices] = {{3, 2, 1, 9, 0},

49. {5, 1, 2, 10, 4},

50. {0, 4, 1, 0, 9},

51. {8, 10, 0, 2, 10},

52. {1, 6, 8, 11, 0},

53. };

54.

55. prim(g);

56. }

Time Complexity

The time complexity of Prim’s Algorithm is O((V+E)logV).

So, that’s all from Spanning tree and Minimum Spanning tree. The path-finding algorithms for Minimum Spanning tree are very useful in real life. Hope you liked it.

Give your queries and suggestion in the comment box if you are unable understand some concepts or might found some errors on the same. Good day.

Add a comment

Related posts:

Why Should You Opt for a Commercial Grade Shade Sail?

Shade sails have become increasingly popular as they help keep your outdoor spaces pleasant in the scorching summer heat. Both residential and commercial applications of sail shades make them worthy…

Surmounting her father!

Perhaps the hard run, and flung himself talk. He was not quite calmly. Tyler Garrett noticed that every little nose irritated him. But if some one saw a dream, but not get inside the air he…

How to Kill Boredom during Online Learning and Teaching

Students get bored in online zoom classes because they have to look at a single screen for a longer duration of time and less interaction kills their enthusiasm for the lesson. In the physical…