最小生成树-Kruskal算法-Prim算法

【最小生成树定义】

对于图G = (V,E),边附带权值w[],能找到一棵树T,顶点数为(V-1),使所有的边权值加起来最小...


【Kruskal算法-From 算法导论】

1.  A <- 空集                           // initially A is empty
2.  for each vertex v ∈ V[G]          // line 2-3 takes O(V) time
3.        do Create-Set(v)             // create set for each vertex
4.  sort the edges of E by nondecreasing weight w
5.  for each edge (u,v)∈E, in order bynondecreasing weight
6.        do if Find-Set(u) != Find-Set(v) // u & v on different trees
7.                 then  A <- A ∪ {(u,v)}
8.                          Union(u,v)
9.  return A

Total running time is O(E lg E).

【Kruskal算法-时间复杂度】

Lines 1-3 (initialization):  O(V)
Line 4 (sorting):  O(E lg E)
Lines 6-8 (set operations):  O(E log E)
Total: O(E log E)


【Kruskal算法-问题】

问题1:lg和log是怎么一回事呢? 实际上这两个的底数可能不一样,但是数量级别是一致的,为了方便,都是正确....

问题2:我感觉编程好难实现这个Union操作...Create-Set,Find-Set...用链表吗?

问题3:给edge根据权值排序就不太顺手....

问题4:Line 6~8 怎么要用到 O(E log E)呢,不是每个边找一次,O(E)吗?


【问题的解答】

Union 和 Create-Set Find-Set 用一个数组root[] 就简单解决了,root[i] 放着 i 顶点的根

Create-Set的时候: root[i] = i ;

Find-Set 的时候:return root[i] ;

Union(a,b)的时候, 把b所在树的所有顶点都移植给a所在树:root[i] = root[a];


【Kruskal算法-C++代码】

#include <iostream>
#include <algorithm>

using namespace std;

struct CEdge
{
    int u;
    int v;
    int weight;

    CEdge(){}
    CEdge(int u,int v,int w):u(u),v(v),weight(w){}
};


int *root;


bool compare(CEdge a,CEdge b)
{
    return  a.weight < b.weight;//ÉýÐò..
}

int Find(int x)
{
    return root[x];
}

void Union(int a,int b,int V)
{
    int root_a = Find(a);
    int root_b = Find(b);
    if(root_a != root_b)
    {
        root[b] = root_a;
        //如果a,b不在同一棵树,要把b所在树的所有顶点都移植过去给a...
        for(int i = 1 ; i <= V;i++)
            if(root[i] == root_b)
                root[i] = root_a;
    }
}

void Kruskal(int V,int E,CEdge *e)
{
    for(int i = 1 ; i <= V;i++)
        root[i] = i;

    //order by weight in edge
    sort(e,e+E,compare);

    for(int i = 0 ; i < E;i++)
        if(Find(e[i].u) != Find(e[i].v))
        {
            cout<<e[i].u<<"---"<<e[i].v<<" ";
            Union(e[i].u,e[i].v,V);
        }
    cout<<endl;
}

int main()
{
    int V = 9,E = 14;

    // make a graph
    CEdge edges[E];
    edges[0].v = 1;edges[0].u=2;edges[0].weight=4;
    edges[1].v = 2;edges[1].u=3;edges[1].weight=8;
    edges[2].v = 3;edges[2].u=4;edges[2].weight=7;
    edges[3].v = 4;edges[3].u=5;edges[3].weight=9;
    edges[4].v = 5;edges[4].u=6;edges[4].weight=10;
    edges[5].v = 6;edges[5].u=7;edges[5].weight=2;
    edges[6].v = 7;edges[6].u=8;edges[6].weight=1;
    edges[7].v = 1;edges[7].u=8;edges[7].weight=8;
    edges[8].v = 2;edges[8].u=8;edges[8].weight=11;
    edges[9].v = 3;edges[9].u=9;edges[9].weight=2;
    edges[10].v = 9;edges[10].u=8;edges[10].weight=7;
    edges[11].v = 9;edges[11].u=7;edges[11].weight=6;
    edges[12].v = 3;edges[12].u=6;edges[12].weight=4;
    edges[13].v = 4;edges[13].u=6;edges[13].weight=14;

    //init
    root = new int[V+1];

    Kruskal(V,E,edges);

    return 0;
}


【Kruskal-实验结果】

8---7 7---6 9---3 2---1 6---3 4---3 3---2 5---4


【Prim算法】

设图G =(V,E),其生成树的顶点集合为U。
1、把v0放入U。
2、在所有u∈U,v∈V-U的边(u,v)∈E中找一条最小权值的边,加入生成树。
3、把②找到的边的v加入U集合。如果U集合已有n个元素,则结束,否则继续执行。



【Prim算法-时间复杂度】

O((n+e) lg n) 


【Prim算法-C++代码】

#define MAXN 1000
#define INF 1<<30
int closest[MAXN],lowcost[MAXN],m;// m为节点的个数
int G[MAXN][MAXN];//邻接矩阵
int prim()
{
    for(int i=0;i<m;i++) 
    { 
        lowcost[i]=INF;
    }
    for(int i=0;i<m;i++) 
    { 
        closest[i]=0;
    }
    closest[0]=-1;//加入第一个点,-1表示该点在集合U中,否则在集合V中
    int num=0,ans=0,e=0;//e为最新加入集合的点
    while(num<m-1)//加入m-1条边
    {
        int micost=INF,miedge=-1;
        for(int i=0;i<m;i++)
            if(closest[i]!=-1)
            {
                int temp=G[e][i];
                if(temp<lowcost[i])
                {
                    lowcost[i]=temp;
                    closest[i]=e;
                }
                if(lowcost[i]<micost)
                micost=lowcost[miedge=i];
            }
        ans+=micost;
        closest[e=miedge]=-1;
        num++;
    }
    return ans;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值