[Graph Theory] [Matlab] Kruskal Algorithm of Minimum Spanning Tree [Greedy Thought Ultra-detailed Explanation and Application of Kruskal Algorithm]

Kruskal algorithm of minimum spanning tree
insert image description here
Note: Content learning comes from: B station CleverFrank digital-analog algorithm


foreword

What the blogger brings to you today are the two classic algorithms in the minimum spanning tree, the Kruskal algorithm and the Kruskal algorithm in the Prim algorithm.

Today's content has a certain inspection on the basic knowledge of graph theory and graphs. Before you eat this article, you need to know a little about how Matlab generates graphs and some operations on graph-related data structures.


Then the bloggers here will first write some columns full of dry goods!

Data Structure Column: Data Structure This contains a lot of bloggers' summaries of data structure learning. Each article is written with great care. Interested partners can support it!
Algorithm Column: Algorithm This can be said to be the blogger's writing process. It summarizes some classic questions and the algorithm implementation, which is very helpful for exams and competitions!
Likou Brush Question Column: Leetcode wants to hit the partners of ACM, Blue Bridge Cup or college student programming competition. Here are the blogger's brush questions record, I hope it will help you!
C's deep anatomy column: C language deep anatomy For beginners who want to deeply learn the wisdom contained in C language and the underlying implementation of various functions, I believe this column will be helpful to you!


introduction of practical problems

insert image description here
In fact, the answer to this question is actually to find the minimum spanning tree of this graph.

Kruskal's algorithm

This algorithm can be called "edge addition method". The initial minimum spanning tree has 0 edges, and each iteration selects an edge that satisfies the condition with the minimum cost and adds it to the set of minimum spanning tree edges.
In fact, the core idea is the greedy idea: to achieve the overall optimum through local optimum

  • Sort all edge weights
  • Keep iterating over the edge with the least weight until all points are connected (number of edges = number of nodes - 1).
  • During iteration, if an edge forms a cycle, the edge is discarded because there are no cycles in the tree!

Overall code display

In matlab, the generation of the minimum spanning tree can be done directly with the minspantree()function.

s=[1,1,1,1,2,2,3,3,4,4,5,5,6];
t=[2,3,4,5,3,6,5,7,5,6,6,7,7];
w=[35,24,10,25,25,20,15,11,12,30,15,25,18];
names={
    
    '1','2','3','4','5','6','7'};
G=graph(s,t,w,names);
p=plot(G,"EdgeLabel",G.Edges.Weight);
% 求解最小生成树
T=minspantree(G,"Method","sparse");
% sparse代表的是Kruskal算法
% dense代表的是Prim算法

% sparse:Kruskal算法
% 算法按权重对所有的边排序,然后将不构成循环的边添加到树中
p=plot(G,"EdgeLabel",G.Edges.Weight);
highlight(p,T,"NodeColor","red","EdgeColor","red"); 
% 将最小生成树的边设置为红色!

Generated graph:
insert image description here
Generated minimum spanning tree:
insert image description here
We can also print out the edges and nodes of the minimum spanning tree, or add up the weights of the entire road:
insert image description here

end

Seeing this, I believe that we have learned the process of Kruskal algorithm to find the minimum spanning tree. Of course, this is far from the requirements of mathematical modeling and our goal. In the process of continuous learning, bloggers also hope to share The way to learn diary drives everyone! If you find this article helpful, don't forget to like it! focus on! Favorite Oh!

Guess you like

Origin blog.csdn.net/Yu_Cblog/article/details/126393842