The idea of Prim algorithm is as follows:
Firstly, the points of the graph are divided into two parts: one is the visited u, the other is the UN visited v
1: first, find an edge with the lowest weight from u to v among the visited vertices
2: then add the vertex in v in this edge to u,
Until the number of edges = the number of vertices - 1
As shown in the figure below, the figure below shows the prim algorithm
(original)
image.png
(a -1)
image.png
(a -2)
image.png
(a -3)
image.png
(a -4)
image.png
(a -5)
image.png
(a -6)
<h3>The flow chart of the algorithm is as follows:</h3>
image.png
The code is as follows:
// // main.cpp // Prim // // Created by orange and banana on December 2, 2018 // Copyright © 2018 orange and banana. All rights reserved // /* Adjacency table is used to store graphs The idea of the algorithm is to select the smallest edge from the set u that has been found and the set v that has not been found This indicates which nodes have been visited and which have not; Also maintain an array, the purpose of which is to record the set of weights of edges from u to v; How to record? 1: First, the starting point is taken as the base point, and the vertex is set as visited to initialize the array. The subscript of each array is a node, so that each data item = = the corresponding distance from the starting point to each node; 2:Cycle condition: the number of edges up to the spanning tree = the number of vertices - 1; Loop body: "find the smallest edge in the distance array. Note that this vertex has not been accessed; Find the vertex and mark it as visited; Update the distance array with this vertex; if the distance between the new vertex and the subscript vertex is less than the previous distance, update it; if the updated vertex has not been accessed !!!!!!!!!!!!Note that the following is not an exercise: Distance array; subscript is vertex; data item is the corresponding distance, that is, the weight of the edge from the vertex to the point not found, that is, the distance from u to the subscript vertex; 」 First of all, a random point is used as the starting point of traversal; before */ #include <iostream> using namespace std; typedef struct node{ char data;//Data domain int isAccess;//Used to mark whether it has been accessed }node; #define VERTEXNUM 100 class Graph{ private: node vertex[VERTEXNUM];//Vertex table int edge[VERTEXNUM][VERTEXNUM];//Side table int vertexNum;//Vertex number int edgeNum;//Number of edges int locate(char data);//Find the location of data in the vertex table void initEdge(); public: Graph(int vertexNum,int edgeNum);//Constructor, initializing vertexNUm and edgeNum void create();//Create a picture int Prim(char data);//prim algorithm void printGraph();//output }; void Graph::printGraph(){ cout<<endl; cout<<endl; cout<<"Vertex edge:\n"; cout<<"vertexNum:"<<vertexNum<<" edgeNum:"<<edgeNum<<endl; for (int i = 0; i<vertexNum; i++) { cout<<vertex[i].data<<"\t"; } cout<<endl; cout<<"The side table is as follows:\n"; for (int j = 0; j<vertexNum; j++) { for (int k = 0; k<vertexNum ; k++) { cout<<edge[j][k]<<"\t"; } cout<<endl; } } int Graph::locate(char data){ for (int i = 0; i<vertexNum;i++) { if(vertex[i].data == data){ return I; } } return -1; } Graph::Graph(int vertexNum,int edgeNum){ this->vertexNum = vertexNum; this->edgeNum = edgeNum; initEdge(); } void Graph::create(){ cout<<"input Graph data\n"; for (int i = 0; i<vertexNum; i++) { cin>>vertex[i].data; vertex[i].isAccess = false; } char start ,end; int wieght = -1; for (int j = 0; j<edgeNum; j++) { cout<<"input start and end of edge:\n"; cin>>start>>end>>wieght; int startPosition = locate(start); int endPosition = locate(end); edge[startPosition][endPosition] = wieght; edge[endPosition][startPosition] = wieght; } } void Graph:: initEdge(){//Initialize edge array for (int i = 0; i<vertexNum; i++) { for (int j =0 ; j<=i; j++) { edge[i][j] = INT_MAX;//Each item is set to the maximum edge[j][i] = INT_MAX; } } for (int i = 0; i<vertexNum; i++) { for (int j = 0; j<vertexNum; j++) { cout<<edge[i][j]<<"\t"; } cout<<endl; } } int Graph::Prim(char data){ int numWeight = -0;//Define weight, minimum weight of graph int distince[vertexNum];//Data defining distance int position = locate(data); vertex[position].isAccess = true;//Set to visited int minNodePostion = position;//Define the minimum node for (int i =0; i<vertexNum; i++) {//Initialize distance array if(edge[minNodePostion][i] < INT_MAX){ distince[i] = edge[minNodePostion][I]; }else{ distance[I] = INT_MAX } } int treeEdgeNum = 0; while (treeEdgeNum < vertexNum -1) { int min = INT_MAX; for (int i =0 ; i<vertexNum; i++) { if( vertex[i].isAccess == false && distince[i] < min){ min = distince[I]; minNodePostion = i; } } vertex[minNodePostion].isAccess = true; numWeight += distince[minNodePostion]; for (int i = 0; i<vertexNum; i++) { if(vertex[i].isAccess == false && edge[minNodePostion][i] < distince[I]){ distince[i] = edge[minNodePostion][I]; } } for (int i = 0; i<vertexNum; i++) { cout<<distince[i]<<"\t"; } cout<<endl; treeEdgeNum++; } return numWeight; } int main(){ Graph a(6,8); a.create(); a.printGraph(); int num = a.Prim('1'); cout<<"num: "<<num<<endl; return 1; }
The figure of the test is the figure a above
The operation results are as follows:
image.png