Prim algorithm:
Suppose n = (v,{e}) is a connected network, TE is the set of edges in the smallest spanning tree on N. The algorithm starts with u={u0} (u0), te={}, and repeats the following: in all u belonging to the v-u side (U,V) belonging to E, find a cost-minimal edge (U0,V0) merged into the set TE, while V0 merged into U until U=v, There must be a n-1 bar edge in TE, then t= (V,{te}) is the smallest spanning tree of N.
To implement this algorithm, an auxiliary array Closedge should be attached to record edges with the lowest cost from u to v-u. For each vertex vi belonging to the v-u, there is a corresponding component in the secondary array closedge[i-1], which includes two domains, the Lowcost domain stores the Edge's right, and the Vex field stores the vertices of the side attached to the U.
Consider the following non-network:
Adjacency Matrix:
The minimum spanning tree is:
The prim algorithm process (the graph is represented by the adjacency matrix, assuming starting from vertex a):
1. Initialize the auxiliary array first, and the vertex u into the U set: