十八、最小生成树(克鲁斯卡尔算法)

最小生成树

在含有n个顶点的连通图中选择n-1条边,构成一棵极小连通子图,并使该连通子图中n-1条边上权值之和达到最小,则称其为连通网的最小生成树。 

例如,对于如上图G4所示的连通网可以有多棵权值总和不相同的生成树。

克鲁斯卡尔算法介绍

克鲁斯卡尔(Kruskal)算法,是用来求加权连通图的最小生成树的算法。

基本思想:按照权值从小到大的顺序选择n-1条边,并保证这n-1条边不构成回路。 
具体做法:首先构造一个只含n个顶点的森林,然后依权值从小到大从连通网中选择边加入到森林中,并使森林中不产生回路,直至森林变成一棵树为止。

克鲁斯卡尔算法图解

以上图G4为例,来对克鲁斯卡尔进行演示(假设,用数组R保存最小生成树结果)。

第1步:将边<E,F>加入R中。 
    边<E,F>的权值最小,因此将它加入到最小生成树结果R中。 
第2步:将边<C,D>加入R中。 
    上一步操作之后,边<C,D>的权值最小,因此将它加入到最小生成树结果R中。 
第3步:将边<D,E>加入R中。 
    上一步操作之后,边<D,E>的权值最小,因此将它加入到最小生成树结果R中。 
第4步:将边<B,F>加入R中。 
    上一步操作之后,边<C,E>的权值最小,但<C,E>会和已有的边构成回路;因此,跳过边<C,E>。同理,跳过边<C,F>。将边<B,F>加入到最小生成树结果R中。 
第5步:将边<E,G>加入R中。 
    上一步操作之后,边<E,G>的权值最小,因此将它加入到最小生成树结果R中。 
第6步:将边<A,B>加入R中。 
    上一步操作之后,边<F,G>的权值最小,但<F,G>会和已有的边构成回路;因此,跳过边<F,G>。同理,跳过边<B,C>。将边<A,B>加入到最小生成树结果R中。

此时,最小生成树构造完成!它包括的边依次是:<E,F> <C,D> <D,E> <B,F> <E,G> <A,B>

克鲁斯卡尔算法分析

根据前面介绍的克鲁斯卡尔算法的基本思想和做法,我们能够了解到,克鲁斯卡尔算法重点需要解决的以下两个问题: 
问题一 对图的所有边按照权值大小进行排序。 
问题二 将边添加到最小生成树中时,怎么样判断是否形成了回路。

问题一很好解决,采用排序算法进行排序即可。

问题二,处理方式是:记录顶点在"最小生成树"中的终点,顶点的终点是"在最小生成树中与它连通的最大顶点"(关于这一点,后面会通过图片给出说明)。然后每次需要将一条边添加到最小生存树时,判断该边的两个顶点的终点是否重合,重合的话则会构成回路。 以下图来进行说明:

在将<E,F> <C,D> <D,E>加入到最小生成树R中之后,这几条边的顶点就都有了终点:

(01) C的终点是F。 
(02) D的终点是F。 
(03) E的终点是F。 
(04) F的终点是F。

关于终点,就是将所有顶点按照从小到大的顺序排列好之后;某个顶点的终点就是"与它连通的最大顶点"。 因此,接下来,虽然<C,E>是权值最小的边。但是C和E的重点都是F,即它们的终点相同,因此,将<C,E>加入最小生成树的话,会形成回路。这就是判断回路的方式。

邻接矩阵:

import java.util.ArrayList;
class Vertex
{
public char label; public int index;//在顶点数组中的下标索引 public Vertex(char label,int index)
{
this.label = label; this.index = index; } } class Edge
{
public Vertex start; public Vertex end; public int weight; public Edge(Vertex start, Vertex end, int weight)
{
this.start = start; this.end = end; this.weight = weight; } } class UDGraph
{
private final int MAX_VERTS = 20; private Vertex vertexList[]; private int adjMat[][]; private int nVerts; public UDGraph()
{ vertexList
= new Vertex[MAX_VERTS]; adjMat = new int[MAX_VERTS][MAX_VERTS]; nVerts = 0; for(int i=0;i<MAX_VERTS;i++) for(int j=0;j<MAX_VERTS;j++) adjMat[i][j] = 0; } public void addVertex(char lab)
{ vertexList[nVerts]
= new Vertex(lab,nVerts); nVerts++; } public void addEdge(int start,int end,int weight)
{ adjMat[start][end]
= weight; adjMat[end][start] = weight; } public void kruskal()
{ Edge[] edge
= new Edge[MAX_VERTS*MAX_VERTS]; int nEdges=0; // 用于保存"已有最小生成树"中每个顶点在该最小树中的终点。 int[] end_point = new int[nVerts]; // 结果数组,保存kruskal最小生成树的边 ArrayList<Edge> resultList = new ArrayList<Edge>(); for(int row=0;row<nVerts;row++)//上三角 for(int col=row;col<nVerts;col++) if(adjMat[row][col]>0)
{ edge[nEdges]
= new Edge(vertexList[row],vertexList[col],adjMat[row][col]); nEdges++; } //对边数组排序 for(int out=nEdges-1;out>0;out--) for(int in=0;in<out;in++) if(edge[in].weight>edge[in+1].weight)
{ Edge temp
= edge[in]; edge[in] = edge[in+1]; edge[in+1] = temp; } for(int i=0;i<nEdges;i++)
{
int m = getEndPoint(end_point, edge[i].start.index); int n = getEndPoint(end_point, edge[i].end.index); System.out.println(" "+edge[i].start.label+edge[i].end.label+"m="+m+" n="+n);
       //想象成两棵树的根不同,把一棵树的根连接到另一个树的根
if(m!=n)
{ end_point[m]
= n; resultList.add(edge[i]); } } //打印显示 for(int i=0;i<resultList.size();i++) System.out.println(resultList.get(i).start.label+"---"+resultList.get(i).weight+"---"+resultList.get(i).end.label); }
//终点想象成树的根
public int getEndPoint(int[] end_point, int i)
{
while(end_point[i] != 0) i = end_point[i]; return i; } } public class MatrixUDG_Kruskal
{
public static void main(String[] args)
{ UDGraph theGraph
= new UDGraph(); theGraph.addVertex('A'); // 0 (start for mst) theGraph.addVertex('B'); // 1 theGraph.addVertex('C'); // 2 theGraph.addVertex('D'); // 3 theGraph.addVertex('E'); // 4 theGraph.addVertex('F'); // 5 theGraph.addEdge(0, 1,1); // AB theGraph.addEdge(0, 2,4); // AC theGraph.addEdge(0, 5, 6); // AF theGraph.addEdge(1, 3,8); // BD theGraph.addEdge(1, 4,3); // BE theGraph.addEdge(2, 4,9); // CE theGraph.addEdge(2, 5,5); // CF theGraph.addEdge(3, 4,7); // DE theGraph.addEdge(3, 5,10); // DF theGraph.addEdge(4, 5,2); // EF theGraph.kruskal(); } }

邻接链表:

import java.util.ArrayList;
class Vertex
{
public char label; public int index;//在顶点数组中的下标索引 public Edge firstEdge; public Vertex(char label,int index)
{
this.label = label; this.index = index; firstEdge = null; } } class Edge
{
public int sour; public int dest;//边指向的顶点在数组列表中的位置 public Edge nextEdge;//指向的下一条边 public int weight;//权重 public Edge(int sour,int dest,int weight)
{
this.sour = sour; this.dest= dest; this.weight = weight; nextEdge = null; } } class UDGraph
{
private final int MAX_VERTS = 20; private Vertex vertexList[]; private int nVerts; public UDGraph()
{ vertexList
= new Vertex[MAX_VERTS]; nVerts = 0; } public void addVertex(char lab)
{ vertexList[nVerts]
= new Vertex(lab,nVerts); nVerts++; } public void addEdge(int start,int end,int weight)
{ Edge s_T_eEdge
= new Edge(start,end,weight); Edge e_T_sEdge = new Edge(end,start,weight); Edge edge2 = vertexList[start].firstEdge; if(edge2==null)
{ vertexList[start].firstEdge
= s_T_eEdge; }else
{ while(edge2.nextEdge!=null) edge2 = edge2.nextEdge; edge2.nextEdge = s_T_eEdge; } Edge edge3 = vertexList[end].firstEdge; if(edge3==null)
{ vertexList[end].firstEdge
= e_T_sEdge; }else
{ while(edge3.nextEdge!=null) edge3 = edge3.nextEdge; edge3.nextEdge = e_T_sEdge; } } public void kruskal()
{ Edge[] edge
= new Edge[MAX_VERTS*MAX_VERTS]; int nEdges=0; // 用于保存"已有最小生成树"中每个顶点在该最小树中的终点。 int[] end_point = new int[nVerts]; // 结果数组,保存kruskal最小生成树的边 ArrayList<Edge> resultList = new ArrayList<Edge>(); //把所有的边放入Edge[] edge中 for(int i=0;i<nVerts;i++){ Edge currentEdge = vertexList[i].firstEdge; while(currentEdge!= null )
{
//vertexList[i].index<currentEdge.dest,使所有的边起点小于重点,
//保证放入的边不重复,如果没有设置,则放入两倍的边
if( vertexList[i].index<currentEdge.dest)
{ edge[nEdges
++] = currentEdge; } currentEdge = currentEdge.nextEdge; } } //对边数组排序 for(int out=nEdges-1;out>0;out--) for(int in=0;in<out;in++) if(edge[in].weight>edge[in+1].weight)
{ Edge temp
= edge[in]; edge[in] = edge[in+1]; edge[in+1] = temp; } for(int i=0;i<nEdges;i++)
{
int m = getEndPoint(end_point, edge[i].sour); int n = getEndPoint(end_point, edge[i].dest); if(m!=n){ end_point[m] = n; resultList.add(edge[i]); } } //打印显示 for(int i=0;i<resultList.size();i++) System.out.println(vertexList[resultList.get(i).sour].label+"-------"+resultList.get(i).weight+"-------"+vertexList[resultList.get(i).dest].label); } public int getEndPoint(int[] end_point, int i)
{
while(end_point[i] != 0) i = end_point[i]; return i; } } public class ListUDG_Kruskal
{
public static void main(String[] args)
{ UDGraph theGraph
= new UDGraph(); theGraph.addVertex('A');// 0 (start for mst) theGraph.addVertex('B');// 1 theGraph.addVertex('C');// 2 theGraph.addVertex('D');// 3 theGraph.addVertex('E');// 4 theGraph.addVertex('F');// 5 theGraph.addEdge(0, 1,1);//AB theGraph.addEdge(0, 2,4);//AC theGraph.addEdge(0, 5,6);//AF theGraph.addEdge(1, 3,8);//BD theGraph.addEdge(1, 4,3);//BE theGraph.addEdge(2, 4,9);//CE theGraph.addEdge(2, 5,5);//CF theGraph.addEdge(3, 4,7);//DE theGraph.addEdge(3, 5,10);//DF theGraph.addEdge(4, 5,2);//EF theGraph.kruskal(); } }
原文地址:https://www.cnblogs.com/xxlong/p/5025642.html