PRIM algorithm code example

xiaoxiao2021-03-06  49

#include #include #include

#define infinity INT_MAX #define max_vertex_num 20

Typedef Int vrtype; typedef int infint; typedef char vertexType;

Typedef struct arccell {vrtype adj; infotype * info;} arccell, adjmaMatrix [max_vertex_num] [max_vertex_num];

TypedEf struct {vertextype vexs [max_vertex_num]; Adjmatrix Arcs; int VexNum, ArcNum;} mgraph;

Typedef struct {vertextype adjgEX; vrtype lowcost;} closedge [max_vertex_num];

Void CreateGraph (MGraph & g); Void MinisPantree_Prim (Mgraph G, Vertextype U); int Locatevex (Mgraph G, Vertextype U); int minimum (closed

Void main (void) {INT I, J; MGRAPH G; CreateGraph (g); for (i = 0; i

} void creategraph (mgraph & g) {Int weigh; int i, j = 0, k = 0; char hand, TIDE; COUT << "Input the number for vexnum and arcnum:"; cin >> g.vexnum >> G .arcnum; for (i = 0; i > g.vexs [i]; cout << Endl COUT << "INPUT" << g.arcnum << "arc (char, char, weigh):" << end1; j = 0; k = 0; for (i = 0; i >; cin >> Tide; cin >> weigh; while (hand! = G.vexs [j]) j ; while (Tide! = G.vexs [ K]) K ; g.arcs [j] [k] .adj = weigh; g.ARCS [k] [j] .adj = weigh; j = 0; k = 0; cout << endl;}}

Void MinisPantree_Prim (Mgraph G, Vertextype U) {INT I, J, K = 0; Closed CLOSE; K = Locatevex (G, U); for (j = 0; j "; cout << g.vexs [k] << "; cout << close [k] .lowcost << endl; close [ K] .Lowcost = 0; for (j = 0; j

While (g.vexs [k ] == u) Return K-1; Return 0;}

INT minimum (CloseDge Close) {INT J1 = 0, Client = 88, J2; While (Close [J1] .adjvex! = '/ 0') {IF (Client> Close [J1] .Lowcost && Close [J1]. Lowcost! = 0) {Client = Close [J1] .Lowcost; J2 = J1;} J1 ;} Return J2;}

Input and output example: Input the number for vexnum and arcnum: 6 10

Input6char for vexs: abcdef

INPUT10ARC (Char, Char, Weigh): 0: AB6

1: BC5

2: AC1

3: AD5

4: CD5

5: BE3

6: CE6

7: CF4

8: EF6

9: DF2

88 6 1 5 88 886 88 5 88 3 881 5 88 5 6 45 88 5 88 88 288 3 6 88 88 688 88 4 2 6 88A ----> C 1c ----> f 4F ----> D 2C ----> B 5B ----> E 3press any key to continue

转载请注明原文地址:https://www.9cbs.com/read-82339.html

New Post(0)