Primm algorithm to find the minimum spanning tree

Source: Internet
Author: User

#include "iostream" using namespace std;const int num = 9; Number of nodes # define Infinity 65535;//This example takes node 0 as the starting node of the spanning tree void minspantree_prime (int graphic[num][num]) {int lowcost[num]; Record the shortest distance from the node num to the spanning tree, or 0 to indicate that the node is already in the spanning tree int adjvex[num]; Records related nodes, such as: adjvex[1] = 5 indicates that nodes 1 and 5 in the smallest spanning tree have paths connected int sum = 0; Record the sum of the minimum spanning tree edge weights memset (adjvex, 0, sizeof (Adjvex));//Select 0 nodes as the starting point for the spanning tree for (int i = 0; i < num; i++) lowcost[i] = Graphic[0][i] ; for (int i = 1; i < num; i++) {int min = infinity;int index;for (int j = 1; j < num; J + +) {if (lowcost[j]! = 0 && Amp Lowcost[j] < min) {index = J;min = Lowcost[j];}} Sum + = Min;lowcost[index] = 0; Put the current node in the spanning tree cout << Adjvex[index] << "<< index << endl;//Fix the minimum distance for other nodes to the spanning tree for (int j = 1; J < num; J + +) {if (lowcost[j]! = 0 && Graphic[index][j] < Lowcost[j]) {lowcost[j] = graphic[index][j];adjvex[j] = index;} }}cout << "sum =" << sum << Endl;} int main () {int graphic[num][num];for (int i = 0; i < num; i++) for (int j = 0; J &Lt Num J + +) {if (i = = j) Graphic[i][j] = 0;elsegraphic[i][j] = Infinity;} GRAPHIC[0][1] = 1;graphic[0][2] = 5;graphic[1][0] = 1;graphic[1][2] = 3;graphic[1][3] = 7;graphic[1][4] = 5;graphic[2][0] = 5;graphic[2][1] = 3;graphic[2][4] = 1;graphic[2][5] = 7;graphic[3][1] = 7;graphic[3][4] = 2;graphic[3][6] = 3;graphic[4] [1] = 5;graphic[4][2] = 1;graphic[4][3] = 2;graphic[4][5] = 3;graphic[4][6] = 6;graphic[4][7] = 9;graphic[5][2] = 7;graphi C[5][4] = 3;graphic[5][7] = 5;graphic[6][3] = 3;graphic[6][4] = 6;graphic[6][7] = 2;graphic[6][8] = 7;graphic[7][4] = 9;gr APHIC[7][5] = 5;graphic[7][6] = 2;graphic[7][8] = 4;graphic[8][6] = 7;graphic[8][7] = 4; Minspantree_prime (graphic); return 0;}

Primm algorithm to find the minimum spanning tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.