// ================================================ ======================================
// Use adjacency matrix and prim algorithm spanning minispantree
// By: chlaws
// Time: 08-8-6
// PS: reprinted. Please do not delete this note
// ================================================ ======================================
# Include <stdio. h>
# Include <stdlib. h>
# Define max_vertex_num 20 // maximum number of vertices
Typedef int adjmatrix [max_vertex_num] [max_vertex_num]; // type of the adjacent matrix
Typedef char vertextype;
Typedef struct {
Vertextype vexs [max_vertex_num]; // vertex table
Adjmatrix arcs; // The adjacent matrix.
Int vexnum, arcnum; // Number of vertices and arcs in the graph
} Mgraph;
Struct dnode {
Vertextype adjvex;
Int lowcost;
} Closedge [max_vertex_num];
Int locatevex (mgraph g, char U );
Void minispantree_prim (mgraph g, vertextype U );
Void createmgraph (mgraph & G );
Int mininum (struct dnode closedge [], int vexnum );
Void main ()
{
Char U;
Mgraph g;
Printf ("text/N ");
Createmgraph (g );
Printf ("input you will start find first vertex :");
Fflush (stdin );
Scanf ("% C", & U );
Minispantree_prim (G, U );
}
Void minispantree_prim (mgraph g, vertextype U)
{
Int I, J, K;
K = locatevex (G, U );
For (j = 0; j <G. vexnum; ++ J)
{
If (J! = K)
{
Closedge [J]. adjvex = u;
Closedge [J]. lowcost = G. arcs [k] [J];
}
}
Closedge [K]. lowcost = 0;
For (I = 1; I <G. vexnum; ++ I)
{
K = mininum (closedge, G. vexnum );
Printf ("(% C, % C)/n", closedge [K]. adjvex, G. vexs [k]);
Closedge [K]. lowcost = 0;
For (j = 0; j <G. vexnum; ++ J)
{
// The two if statements are critical.
// Ensure that there is a path between the two points and the tree is not selected
If (G. ARCs [k] [J]> 0 & closedge [J]. lowcost> 0) & (g. ARCs [k] [J] <closedge [J]. lowcost ))
{
Closedge [J]. adjvex = G. vexs [k];
Closedge [J]. lowcost = G. arcs [k] [J];
} // Determine if there is no direct path between two points
Else if (closedge [J]. lowcost <0)
{
Closedge [J]. adjvex = G. vexs [k];
Closedge [J]. lowcost = G. arcs [k] [J];
}
}
}
}
Int mininum (struct dnode closedge [], int vexnum)
{
Int min = 10000;
Int Pos =-1;
While (vexnum --)
{
If (closedge [vexnum]. lowcost> 0) & (min> closedge [vexnum]. lowcost ))
{
Min = closedge [vexnum]. lowcost;
Pos = vexnum;
}
}
If (-1 = POS) {printf ("closedge. lowcost find error! "); Exit (-1 );}
Return Pos;
}
Void createmgraph (mgraph & G)
{
Int I, J, K, W;
Char V1, V2;
Printf ("input vexnum & arcnum :");
Scanf ("% d, % d", & G. vexnum, & G. arcnum );
Printf ("input vertices :");
Scanf ("% s", G. vexs );
For (I = 0; I <G. vexnum; I ++)
For (j = 0; j <G. vexnum; j ++)
G. arcs [I] [J] =-1;
For (k = 0; k <G. arcnum; k ++)
{
Printf ("input arcs (V1-> V2 & W):/N ");
Fflush (stdin );
Scanf ("% C, % C, % d", & V1, & V2, & W );
I = locatevex (G, V1 );
J = locatevex (G, V2 );
G. arcs [I] [J] = W;
G. arcs [J] [I] = W;
}
Return;
}
Int locatevex (mgraph g, char U)
{
Int I;
For (I = 0; I <G. vexnum; I ++)
{
If (u = G. vexs [I]) return I;
}
If (I = G. vexnum)
{
Printf ("error u! /N ");
Exit (1 );
}
Return 0;
}
// Test Result:
/*
Text
Input vexnum & arcnum: 6, 10
Input vertices: abcdef
Input arcs (V1-> V2 & W ):
A, B, 6
Input arcs (V1-> V2 & W ):
A, C, 1
Input arcs (V1-> V2 & W ):
A, D, 5
Input arcs (V1-> V2 & W ):
B, C, 5
Input arcs (V1-> V2 & W ):
B, E, 3
Input arcs (V1-> V2 & W ):
C, E, 6
Input arcs (V1-> V2 & W ):
C, F, 4
Input arcs (V1-> V2 & W ):
C, d, 5
Input arcs (V1-> V2 & W ):
D, F, 2
Input arcs (V1-> V2 & W ):
E, F, 6
Input you will start find first vertex:
(A, c)
(C, F)
(F, d)
(C, B)
(B, E)
Press any key to continue...
*/