In this video, I show you how Kruskal's algorithm finds a minimum spanning tree in a weighted
undirected graph.
A spanning tree is a connected and acyclic subgraph that contains all vertices.
So for example, the vertices and edges in orange form a spanning tree.
The weight of the spanning tree is the sum of all edge weights, in this case the weight
is 47.
The goal of Kruskal's algorithm is now to find a spanning tree that has the smallest
possible weight.
Let's go through Kruskal's algorithm step-by-step.
The algorithm simple adds an edge with lowest weight in every iteration such that no cycle
is created.
The smallest weight is -4, so we add edge "d-e".
Now, the second-smallest weight is -1 and we add edge "f-g".
So we now just continue in the same way, we add edge "a-c" with weight 2 and edge "b-c"
with weight 3.
The next edge that we would like to add is edge "a-b" with a weight of 6.
But if we add this edge we would create a green cycle on vertices "a", "b" and "c".
So that's why we do not insert this edge, instead we colour it red and ignore this edge
from now on.
Instead we add edge "c-e" which has a weight of 7.
Now among all remaining edges, there are three edges which have the same lowest edge weight
of 8.
We are not allowed to pick this edge "b-e", because if we do so then we would create a
green cycle again.
The other edges with weight 8, namely "d-f" and "e-f", are fine.
For those edges we have free choice, I pick edge "e-f".
Now we can't add any more edges without creating a cycle.
That means that Kruskal's algorithm is now finished and has found this minimum spanning
tree with a weight of 15.
So finally here is some information about Kruskal's algorithm.
Kruskal's algorithm is also a so-called greedy algorithm.
The graph must be a connected, weighted and undirected graph.
Kruskal's algorithm works also if the graph contains some edges with a negative weight.
In general, the minimum spanning tree is not unique, so there can be different minimum
spanning trees for a graph.
But, if you have a graph were all edge weights are different then you can be sure that there
is exactly one minimum spanning tree.
The time complexity depends mainly on two components: there is the component for sorting
the edges and there is also the component for checking if adding an edge would create
a cycle.
Sorting edges is the dominant component so that the total time complexity is number of
edges times the logarithm of the number of edges.
This also means that Kruskal's algorithm is rather slow if the graph has many edges.
But on the positive side, Kruskal's algorithm is quite easy to implement.
That is all the key information about Kruskal's algorithm.
I hope you could take away some bits and pieces from this video!
If you did, please subscribe to this channel and if you want to find out more about algorithms
on minimum spanning trees, Prim's algorithm here is the right thing for you!
Thanks, and bye bye.






For more infomation >> Pourquoi ne pas faire quelque chose d'aussi simple que sain ? - Duration: 6:54. 


Không có nhận xét nào:
Đăng nhận xét