这个算法主要用在网络节点的分割。他的思想是将一个网络节点图分割成两个相等的节点集合。为了连接两个社区的边权最小。
step1:随机产生两个节点的集合A和B.
step2:计算A和B中的每个节点的internal cost and external cost
step3:将A中的节点与B中的节点进行交换,计算两个损失权重的差值,也就是原先图的内外权重差,减去转表后的内外权重差。使这个值最后为负为止。
1 function Kernighan-Lin(G(V,E)): 2 determine a balanced initial partition of the nodes into sets A and B 3 A1 := A; B1 := B 4 do 5 compute D values for all a in A1 and b in B1 6 for (n := 1 to |V|/2) 7 find a[i] from A1 and b[j] from B1, such that g[n] = D[a[i]] + D[b[j]] - 2*c[a[i]][b[j]] is maximal 8 move a[i] to B1 and b[j] to A1 9 remove a[i] and b[j] from further consideration in this pass 10 update D values for the elements of A1 = A1 a[i] and B1 = B1 b[j] 11 end for 12 find k which maximizes g_max, the sum of g[1],...,g[k] 13 if (g_max > 0) then 14 Exchange a[1],a[2],...,a[k] with b[1],b[2],...,b[k] 15 until (g_max <= 0) 16 return G(V,E)