prim algorithm:
int prim(int n, int sta)//n means there are n vertices, and the sta table generates the minimum spanning tree from the sta vertex { int mark[M],dis[M]; int i,sum = 0; //sum is the total minimum spanning tree edge weight for (i = 0;i < n;i++) //Initialize the weights of the dis[i] table from vertex sta to point i { dis [i] = mat [sta] [i]; mark[i] = 0; } mark[sta] = 1; //sta this vertex is added to the minimum spanning tree for (i = 1;i < n;i++) //loop n-1 times, each time find a graph with n points on the edge of the minimum weight { //Only n-1 edges int min = inf; //inf table infinity for (j = 0;j < n;j++)//Find the vertex that is not currently in the minimum spanning tree with the smallest edge weight if (!mark[j] && dis[j] < min) min = dis[j], flag = j; mark[flag] = 1; //Add the vertex to the minimum spanning tree sum += dis[flag]; //sum plus its edge weight for (j = 0;j < n;j++) //Update from flag to each point is the minimum weight if (dis[j] > mat[flag][j]) dis[j] = mat[flag][j]; } return sum; //return the sum of edge weights }