Minimum spanning tree (adjacency matrix or adjacency list)

topic description

The minimum spanning tree problem is a very important class of problems in actual production and life. Assuming that a communication network needs to be established between n cities, only n-1 lines are needed to connect n cities. At this time, it is natural to consider such a problem, that is, how to establish this communication network under the premise of saving money the most.

A connected network can be used to represent n cities and the communication lines that may be set between n cities, where the vertices of the network represent the cities, the edges represent the lines between two cities, and the weights assigned to the edges represent the corresponding costs. For a connected network with n vertices, many different spanning trees can be established, and each spanning tree can be a communication network. Now, it is necessary to choose a spanning tree that minimizes the total cost. This problem is to construct the minimum cost spanning tree of the connected network, referred to as the minimum spanning tree. The cost of a spanning tree is the sum of the costs of the edges on the tree.

Among the commonly used minimum spanning tree construction algorithms, Prim's algorithm is a very commonly used algorithm.
In this question, read the data of an undirected graph and store it in the adjacency matrix, then use the Prim algorithm to build the minimum spanning tree, and output the cost of the minimum spanning tree.


This topic provides some reference programs

enter

Line 1: Enter the total number of vertices n and the total number of edges e, separated by spaces. Where n does not exceed 50, and e does not exceed 1000.
Line 2: Enter the names of n vertices (char type)
and then enter line e: enter the vertices and weights attached to one edge in each line, such as ab 5 

output

First output n-1 lines, corresponding to n-1 edges of the constructed minimum spanning tree, the format is "(edge ​​endpoint, edge endpoint): edge weight" and finally output the total cost value of the minimum spanning
tree, the format is "minCost= total value". Note the newline output at the end of the line.

#include <iostream>
#include <iomanip>
using namespace std;

#define Max 1000
#define MVNum 100         //最大顶点数
#define OK 1

typedef char VerTexType; //顶点信息
typedef int OtherInfo;    //和边相关的信息
typedef int ArcType;

//- - - - -图的邻接表存储表示- - - - -
typedef struct {
   int info;
   VerTexType vexs[MVNum];            //顶点表
   ArcType arcs[MVNum][MVNum];      //邻接矩阵
   int vexnum, arcnum;                //图的当前点数和边数
} Graph;

int LocateVex(const Graph &g, VerTexType v)
{
   //确定点v在G中的位置
   for(int i = 0; i < g.vexnum; ++i)
      if(g.vexs[i] == v)
         return i;
   return -1;
}//LocateVex


void CreateUDG(Graph &g)
{
   //采用邻接矩阵表示法,创建无向图G
   /****在此下面完成代码***************/
    int i,j,k,w;
    char v1,v2;
    cin>>g.vexnum>>g.arcnum;
    for(i=0;i<g.vexnum;i++)
    {
        cin>>g.vexs[i];
    }
    for(i=0;i<g.vexnum;i++)
    for(j=0;j<g.vexnum;j++)
    {
        if(i==j)g.arcs[i][j]=0;
        else g.arcs[i][j]=Max;
    }
    for(k=0;k<g.arcnum;k++)
    {
        cin>>v1>>v2>>w;
        i=LocateVex(g, v1);
        j=LocateVex(g, v2);
        g.arcs[i][j]=g.arcs[j][i]=w;
    }
   /***********************************/
}//CreateUDN

void mintree(Graph g,int ad[][MVNum],int n)
{
    int i,j,k,p,q,sum=0,m;
    p=0;q=0;
    ad[p][q]=Max;
    for( k=1; k<n; k++ )
    {     m = Max;
           for( i=0; i<n; i++ )
           {
                if( ad[i][i]==Max)
                {
                    for( j=1; j<n; j++ )
                    {
                        if( (ad[j][j]==0) && (ad[i][j]<m))
                        {
                            m = ad[i][j];   p = i;    q = j;
                        }
                    }
                }
            }
            cout<<"("<<g.vexs[p]<<","<<g.vexs[q]<<")"<<":"<<ad[p][q]<<endl;
            ad[q][q] = Max;
            sum+=ad[p][q];
    }
    cout<<"minCost="<<sum<<endl;
}

int main()
{
   Graph g;
   CreateUDG(g);
   mintree(g,g.arcs,g.vexnum);
   return 0;
}//main

Sample input Copy

6 10
a b c d e f
a b 6
a c 1
a d 5
b c 5
b e 3
c d 5
c e 6
c f 4
d f 2
e f 6

Sample output Copy

(a,c):1
(c,f):4
(f,d):2
(c,b):5
(b,e):3
minCost=15

Guess you like

Origin blog.csdn.net/qq_63306482/article/details/124855770