Data structure--painting--minimum spanning tree (prim algorithm)

Source: Internet
Author: User
Tags array definition

Minimum spanning tree configuration for a communication network, which is to make the build tree value on the right and minimize it. Prim and Kruskal algorithms are often used. Look at the prim algorithm: just in case n={v,{e}} It is the smallest spanning tree in the communication network, TE it is n set edge. From Algorithm u={u0} (UO belongs to V). te={} start, re-run the following operations: in all u belongs to U. V belongs to the v-u Edge (u,v) belongs to the least expensive edge found in E (u0,v0) into the set TE, at the same time V0 incorporates U, until u=v. At this point the TE must have N-1, t={v,{te}} as the smallest spanning tree of N.

To implement this algorithm, a second auxiliary array, Closedge, is required to record the edge with the minimum weight from u to v-u. Each time a new vertex is merged into U, the Closedge is updated.

Detailed code such as the following:

#include <iostream> #include <queue> #include <limits.h> #include ".  /header.h "using namespace std;//Primm algorithm constructs a minimum spanning tree const int max_vertex_num=20; Maximum vertex number typedef enum {DG,DN,UDG,UDN} graphkind;//(a map. Have to the net.    No direction graph, no net) typedef int VRTYPE;TYPEDEF Char infotype;typedef char vertextype; #define INFINITY int_maxtypedef struct arccell{  Vrtype adj; Vrtype is a vertex relationship type, for an unauthorized graph. Use 1 or 0 to indicate whether the vertices are adjacent or not. For the right graph.        The value type InfoType info;//the ARC-related information pointer Arccell () {adj=0;    info=0; }}arccell,adjmatrix[max_vertex_num][max_vertex_num];typedef struct mgraph{vertextype Vexs[MAX_VERTEX_NUM];//vertex vector A  Djmatrix arcs;  adjacency Matrix int vexnum,arcnum;  Figure current vertex number and arc number Graphkind kind;    The kind of graph flag}mgraph;//records from the vertex set U to v-u the least-cost edge of the auxiliary array definition typedef struct minedge{vertextype Adjvex; Vrtype Lowcost;}    Minedge,closedge[max_vertex_num];int minimum (mgraph g,closedge closedge) {int min=1;                for (int i=1;i<g.vexnum;++i) {if (closedge[i].lowcost!=0) {min=i;            Break }    } for (int i=min+1;i<g.vexnum;++i) {if (CLOSEDGE[I].LOWCOST&LT;CLOSEDGE[MIN].LOWCOST&AMP;&AMP;CLOSEDGE[I].L    owcost>0) min=i; } return min;}    int Locatevex (mgraph G,vertextype v1) {for (int i=0;i<max_vertex_num;++i) {if (G.VEXS[I]==V1) return i; } return max_vertex_num+1;} Status Createudn (mgraph &g) {//used array (adjacency matrix) notation.  Constructing the G.KIND=UDN network;    The manual assignment is an vexnumber=0,arcnumber=0 int;    char info;    cout<< "Please input the Vexnumber arcnumber and info:";    cin>>vexnumber>>arcnumber>>info;    G.vexnum=vexnumber;    G.arcnum=arcnumber; for (int i=0;i<g.vexnum;++i) {//construct vertex vector cout<< "Please input the vertex of number" <<i<< "(Type ch        AR) ";    cin>>g.vexs[i]; } for (int i=0;i<g.vexnum;++i)//Initialize adjacency matrix for (int j=0;j<g.vexnum;++j) {g.arcs[i][j].adj=infinity            ;        g.arcs[i][j].info=0;    } char V1,v2;    int weight=0,i=0,j=0; Char infOmation; for (int k=0;k<g.arcnum;++k) {//Initialize adjacency matrix cout<< "Please input the Vertexs of the arc and it ' s weight" &l        t;<k+1<< "";        cin>>v1>>v2>>weight;  I=locatevex (G,V1);        J=locatevex (G,V2);        G.arcs[i][j].adj=weight;        G.arcs[j][i].adj=weight;            if (info!=48) {//0 ASCII code is cout<< "please input infomation:";            cin>>infomation;            G.arcs[i][j].info=infomation;        G.arcs[j][i].info=infomation; }} return OK; void Dismgraph (mgraph m) {for (int. i=0;i<m.vexnum;++i) {for (int j=0;j<m.vexnum;++j) {cout<&lt        ;m.arcs[i][j].adj<< "";    } cout<<endl;    }}//Primm algorithm void Minispantree_prim (mgraph g,vertextype u) {int P=locatevex (G,U);    Closedge Closedge;            for (int j=0;j<g.vexnum;++j) {//auxiliary array initialization if (j!=p) closedge[j].adjvex=u;    Closedge[j].lowcost=g.arcs[p][j].adj; } Closedge[p].lowcost=0;    Closedge[p].adjvex=u;    int k=0;        for (int i=1;i<g.vexnum;++i) {k=minimum (G,closedge);        cout<<closedge[k].adjvex<< "--" <<G.vexs[k]<<endl;        closedge[k].lowcost=0; for (int j=0;j<g.vexnum;++j) {//update Closedge array if (G.arcs[k][j].adj<closedge[j].lowcost&&g.arcs[k][j]                . adj!=0) {closedge[j].adjvex=g.vexs[k];            Closedge[j].lowcost=g.arcs[k][j].adj;    }}}}int Main () {mgraph m;    CREATEUDN (m);    Dismgraph (m);    Minispantree_prim (M, ' a '); return 0;}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Data structure--painting--minimum spanning tree (prim algorithm)

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.