Minimum Spanning Tree Algorithm

Source: Internet
Author: User

I only know two Minimum Spanning Tree algorithms: prim and kruscal.

Like the Dijkstra algorithm, the prim algorithm is a typical greedy algorithm (that is, obtaining the global optimal solution from the local optimal solution ).

The following figure shows how the prim algorithm is used to store graphs in an adjacent matrix.

#define N 1000int edge[N][N],closedge[N],n;bool vis[N];void prim(int s){    int i,j,k;    for(i=1;i<=n;i++)        closedge[i]=edge[s][i];    memset(vis,0,sizeof(vis));    vis[s]=true;    for(j=1;j<=n-1;j++)    {        k=1;        while(vis[k])k++;        int beg=k;        for(i=k+1;i<=n;i++)            if(!vis[i]&&closedge[i]<closedge[k])                k=i;        vis[k]=true;        for(i=beg;i<=n;i++)            if(!vis[i]&&edge[k][i]<closedge[i])                closedge[i]=edge[k][i];    }}

The following figure shows how the prim algorithm saves graphs in a static adjacent table.

#define N 1000#define Inf 9999999struct Edge{    int to,w,next;}e[100000];int pp[N],n,closedge[N];bool vis[N];void prim(int s){    int i,j,k;    for(i=1;i<=n;i++)        closedge[i]=Inf;    closedge[s]=0;    memset(vis,0,sizeof(vis));    for(j=1;j<=n;j++)    {        k=1;        while(vis[k])k++;        for(i=k+1;i<=n;i++)            if(!vis[i]&&closedge[i]<closedge[k])                k=i;        vis[k]=true;        for(i=pp[k];i!=-1;i=e[i].next)            if(!vis[e[i].to]&&e[i].w<closedge[e[i].to])                closedge[e[i].to]=e[i].w;    }}

Similarly, the prim algorithm can be optimized using the priority queue.

#define N 1000#define Inf 9999999struct Edge{    int to,w,next;}e[100000];int ec,pp[N],closedge[N],n;bool vis[N];struct Node{    int u,d;    bool operator<(const struct Node a)const    {        return d>a.d;    }}t1;void prim(int s){    int i,j,k;    priority_queue<struct Node>que;    for(i=1;i<=n;i++)        closedge[i]=Inf;    memset(vis,0,sizeof(vis));    closedge[s]=0;    t1.d=0;    t1.u=s;    que.push(t1);    while(!que.empty())    {        t1=que.top();        que.pop();        k=t1.u;        if(vis[k])continue;        vis[k]=true;        for(i=pp[k];i!=-1;i=e[i].next)        {            if(!vis[e[i].to]&&e[i].w<closedge[e[i].to])            {                closedge[e[i].to]=e[i].w;                t1.d=e[i].w;                t1.u=e[i].to;                que.push(t1);            }        }    }}

The prim algorithm is generally used for dense graphs, while the kruscal algorithm is generally used for sparse graphs. We also use and query sets in the following kruscal algorithm.

#define N 1000struct Edge{    int u,v,w;}e[100000];int parent[N],cnt,n;int Find(int x){    if(parent[x]<0)        return x;    int r=Find(parent[x]);    parent[x]=r;    return r;}void Union(int x,int y){    int a=Find(x),b=Find(y);    if(a!=b)parent[a]=b;}int cmp(const void* a,const void* b){    return ((struct Edge*)a)->w-((struct Edge*)b)->w;}void kruscal(){    int i,j,k=0;    memset(parent,-1,sizeof(parent));    qsort(e,cnt,sizeof(struct Edge),cmp);    for(i=0;i<cnt;i++)        if(Find(e[i].u)!=Find(e[i].v))        {            Union(e[i].u,e[i].v);            k++;            if(k==n-1)                break;        }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.