Primm algorithm (prim algorithm), an algorithm in graph theory, can search for the smallest spanning tree in weighted connected graphs. It means that the subset of edges found by this algorithm not only includes all the vertices in the connected graph, but also the sum of the weights of all the edges is minimized.
The definition of borrowing Wikipedia is:
starting with a single vertex, the Primm algorithm incrementally expands the number of vertices contained in the tree by following these steps until all vertices of the connected graph are spread.
Input: A weighted connected graph, where the vertex set is V and the edge set is E;
Initialize: vnew = {x}, where x is any node in the Set V (starting point), enew = {};
Repeat the following operations until Vnew = V:
In set E, select the edge with the least weight (U, v), where u is the element in the collection vnew, and V is the vertex without vnew in V (if there are multiple edges that have the same weight as the aforementioned conditions, You can choose any one of them);
the V is added to the set vnew, and (U, v) is added to the set enew;
Output: Use the collection vnew and enew to describe the resulting minimum spanning tree.
in fact, the basic idea of the algorithm is a greedy concept, select a node as the source node to expand, the extension method is to select the source node and other nodes The least-weighted node, because if you construct a minimum spanning tree, it must contain the source node, and the source node will certainly be selected to contain the nearest edge from the source. After selecting a node, it forms a minimum spanning tree collection, dividing all the nodes into two parts. The minimum spanning tree collection each time the loop is incremented adds a node, so you can get the final result by looping verNum-1 times.
One of the most constant maintenance is the array lowcost of the smallest side, that is, the minimum edge length from the set to the other nodes, so that when the node is filtered, the node I that has been selected into the set Lowcost[i] will be assigned a value of 0. The process of selecting min in each cycle can actually be achieved by using a priority queue, which is automatically sorted, and can only be taken out of the team at a time.
typedef struct {int Arc[maxvex][maxvex]; int numvertexes;} Mgraph;void Prim (mgraph G) {int i,j;//loop variable int min;//holds the edge minimum value of each loop int k;//record the next node to expand int adjvex[maxvex];// Another node that holds the smallest edge of the full-time, such as adjvex[2] = 4; The connection minimum edge is node 2 to node 4.int lowcost[maxvex];//holds the minimum weight of the node to each node in the minimum spanning tree collection. lowcost[0] = 0;// Assigning the minimum weight to 0 indicates that the node is already in the minimum spanning tree collection adjvex[0] = 0;//node 0 to node 0 has the lowest weight, so adjvex[0] = 0.for (i = 0; i < g.numvertexes; i++)//Initialize, node 0 all sides The value is assigned to the smallest complete array {lowcost[i] = g.arc[0][i];adjvex[i] = 0;} for (i = 1; i < g.numvertexes; i++)//Loop through 1 nodes, and each loop increments a node in the minimum spanning tree set {min = Infinity;j = 1;k = 0;while (J < G.numverte XES)//loop find set outward extending the smallest edge {if (lowcost[j]!=0&&lowcost[j]<min)//lowcost=0 represents the node already in the collection {min = lowcost[j];// Record the value of the minimum edge k = j;//record the next node to be expanded}j++;} cout << "(" << Adjvex[k] << "," << K << ")" << ""; Prints the edge of the current vertex with the least weight lowcost[k] = 0;//Sets the weight of the current vertex to 0, indicating that the vertex has completed the task for (j = 1; j < G.numvertexes; J + +)//Expand the edge in the collection, update the Lowcos T array. {if (Lowcost[j]!=0&&g.arc[k][j] < lowcost[j]) {Lowcost[j] = G.arcK [J];adjvex[j] = k;}}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Minimum spanning Tree prim algorithm