Implementation of the Minimum Spanning Tree Prim algorithm (using adjacent table Storage) C ++

Source: Internet
Author: User

 

// Implement the Prim algorithm (using the adjacent table Storage). cpp: Defines the entry point for the console application.

//

# Include "stdafx. h"

# Include "stdafx. h"

# Include <iostream>

# Deprecision MAX 100

# Define Infinity 65535

Typedef int WeiType;

Using namespace std;

Struct edgeNode

{

Int no; // edge serial number

Char info; // edge name

WeiType weight; // Edge weight

Struct edgeNode * next; // next

};

Struct vexNode

{

Char info; // node name

Struct edgeNode * link; // endpoint connected to it

};

// Store node Information

VexNode adjlist [MAX];

// Access flag

Bool visited [MAX];

// Lowcost [j] minimum cost of storage from Start Node to node j

WeiType lowcost [MAX];

// Parent [j] indicates the precursor node of node j.

Int parent [MAX];

// Create an adjacent table Storage

Void createGraph (vexNode * adjlist, const int n, const int e, const int v0)

{

 

Int I;

For (I = 1; I <= n; I ++)

{

Cout <"Enter the node" <I <"name :";

Cin> adjlist [I]. info;

Adjlist [I]. link = NULL;

// Initialization

Visited [I] = false;

Lowcost [I] = Infinity;

Parent [I] = v0;

}

EdgeNode * p1, * p2;

Int v1, v2;

WeiType weight;

For (I = 1; I <= e; I ++)

{

Cout <"enter the second-end node number of Edge" <I <:";

Cin> v1> v2;

Cout <"weight of this edge :";

Cin> weight;

P1 = (edgeNode *) malloc (sizeof (edgeNode ));

P2 = (edgeNode *) malloc (sizeof (edgeNode ));

P1-> no = v1;

P1-> weight = weight;

P1-> info = adjlist [v1]. info;

P1-> next = adjlist [v2]. link;

Adjlist [v2]. link = p1;

P2-> no = v2;

P2-> weight = weight;

P2-> info = adjlist [v2]. info;

P2-> next = adjlist [v1]. link;

Adjlist [v1]. link = p2;

}

}

 

Int _ tmain (int argc, _ TCHAR * argv [])

{

Int cases;

Cout <"Enter the number of cases :";

Cin> cases;

While (cases --)

{

Int n, e;

Cout <"Enter the number of nodes :";

Cin> n;

Cout <"Enter the number of sides :";

Cin> e;

Cout <"start from which node :";

Int v;

Cin> v;

Int I, j;

// Sum of the weights of the Minimum Spanning Tree

WeiType sum = 0;

CreateGraph (adjlist, n, e, v );

EdgeNode * p, * q;

P = adjlist [v]. link;

Visited [v] = true;

While (p! = NULL)

{

Lowcost [p-> no] = p-> weight;

P = p-> next;

}

WeiType minCost;

For (I = 1; I <n; I ++)

{

MinCost = Infinity;

Int k;

For (j = 1; j <= n; j ++)

{

If (minCost> lowcost [j] &! Visited [j])

{

MinCost = lowcost [j];

K = j;

}

}

Sum + = minCost;

Visited [k] = true;

Q = adjlist [k]. link;

While (q! = NULL)

{

If (! Visited [q-> no] & q-> weight <lowcost [q-> no])

{

Lowcost [q-> no] = q-> weight;

Parent [q-> no] = k;

}

Q = q-> next;

}

}

Cout <"the edge set of the Minimum Spanning Tree is:" <endl;

For (I = 1; I <= n; I ++)

If (I! = V)

Cout <"(" <adjlist [parent [I]. info <"," <adjlist [I]. info <")" <"";

Cout <endl;

Cout <"Minimum Spanning Tree weight:" <sum <endl;

}

System ("pause ");

Return 0;

}

----------------------------------------------------- Program test --------------------------------------------------

 

Enter the number of cases: 1

Enter the number of nodes: 9

Enter the number of edges: 14

Enter the Start Node: 2

Enter the name of Node 1:

Enter Node 2 Name: B

Enter Node 3 name: c

Enter node 4 name: d

Enter node 5 Name: e

Enter the name of node 6: f

Enter the name of node 7: g

Enter node 8 name: h

Enter node 9 name: I

Enter the node number at the second end of edge 1: 1 2

Edge Weight: 4

Enter the node number at the second end of edge 2: 1 8

Edge Weight: 8

Enter the node number at the second end of edge 3: 2 3

Edge Weight: 8

Enter the node number at the second end of edge 4: 2 8

Weight of this edge: 11

Enter the node number at the second end of edge 5: 3 4

Edge Weight: 7

Enter the node number at the second end of edge 6: 3 6

Edge Weight: 4

Enter the node number at the second end of edge 7: 3 9

Weight of this edge: 2

Enter the node number at the second end of edge 8: 4 5

Weight of this edge: 9

Enter the node number at the second end of edge 9: 4 6

Weight of this edge: 14

Enter the node number at the second end of edge 10: 5 6

Weight of this edge: 10

Enter the node number of the second end of edge 11: 6 7

Weight of this edge: 2

Enter the node number at the second end of edge 12: 7 8

Weight of this edge: 1

Enter the node number of the second end of edge 13: 7 9

Weight of this edge: 6

Enter the node number at the second end of edge 14: 8 9

Edge Weight: 7

The edge set of the Minimum Spanning Tree is:

(B, a) (B, c) (c, d) (d, e) (c, f) (f, g) (g, h) (c, I)

Minimum Spanning Tree weight: 37

Press any key to continue...

 

 

Author heyongluoyao8

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.