kruskal算法基本思路:先对边按权重从小到大排序,先选取权重最小的一条边,如果该边的两个节点均为不同的分量,则加入到最小生成树,否则计算下一条边,直到遍历完所有的边。
prim算法基本思路:所有节点分成两个group,一个为已经选取的selected_node(为list类型),一个为candidate_node,首先任取一个节点加入到selected_node,然后遍历头节点在selected_node,尾节点在candidate_node的边,选取符合这个条件的边里面权重最小的边,加入到最小生成树,选出的边的尾节点加入到selected_node,并从candidate_node删除。直到candidate_node中没有备选节点(这个循环条件要求所有节点都有边连接,即边数要大于等于节点数-1,循环开始前要加入这个条件判断,否则可能会有节点一直在candidate中,导致死循环)。
python" id="highlighter_27402">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#coding=utf-8 class graph( object ): def __init__( self , maps): self .maps = maps self .nodenum = self .get_nodenum() self .edgenum = self .get_edgenum() def get_nodenum( self ): return len ( self .maps) def get_edgenum( self ): count = 0 for i in range ( self .nodenum): for j in range (i): if self .maps[i][j] > 0 and self .maps[i][j] < 9999 : count + = 1 return count def kruskal( self ): res = [] if self .nodenum < = 0 or self .edgenum < self .nodenum - 1 : return res edge_list = [] for i in range ( self .nodenum): for j in range (i, self .nodenum): if self .maps[i][j] < 9999 : edge_list.append([i, j, self .maps[i][j]]) #按[begin, end, weight]形式加入 edge_list.sort(key = lambda a:a[ 2 ]) #已经排好序的边集合 group = [[i] for i in range ( self .nodenum)] for edge in edge_list: for i in range ( len (group)): if edge[ 0 ] in group[i]: m = i if edge[ 1 ] in group[i]: n = i if m ! = n: res.append(edge) group[m] = group[m] + group[n] group[n] = [] return res def prim( self ): res = [] if self .nodenum < = 0 or self .edgenum < self .nodenum - 1 : return res res = [] seleted_node = [ 0 ] candidate_node = [i for i in range ( 1 , self .nodenum)] while len (candidate_node) > 0 : begin, end, minweight = 0 , 0 , 9999 for i in seleted_node: for j in candidate_node: if self .maps[i][j] < minweight: minweight = self .maps[i][j] begin = i end = j res.append([begin, end, minweight]) seleted_node.append(end) candidate_node.remove(end) return res max_value = 9999 row0 = [ 0 , 7 ,max_value,max_value,max_value, 5 ] row1 = [ 7 , 0 , 9 ,max_value, 3 ,max_value] row2 = [max_value, 9 , 0 , 6 ,max_value,max_value] row3 = [max_value,max_value, 6 , 0 , 8 , 10 ] row4 = [max_value, 3 ,max_value, 8 , 0 , 4 ] row5 = [ 5 ,max_value,max_value, 10 , 4 , 0 ] maps = [row0, row1, row2,row3, row4, row5] graph = graph(maps) print ( '邻接矩阵为\n%s' % graph.maps) print ( '节点数据为%d,边数为%d\n' % (graph.nodenum, graph.edgenum)) print ( '------最小生成树kruskal算法------' ) print (graph.kruskal()) print ( '------最小生成树prim算法' ) print (graph.prim()) |
初始的图如下。
运行结果如下。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/mashijia986/article/details/79100925