Minimum Graph Generation tree

Source: Internet
Author: User


# Include <stdio. h>
# Include <stdlib. h>
# Include <conio. h>
# Include <string. h>
# Define ING 9999.
Int visited [10];/* access flag array */
Typedef struct ArcCell {
Int adj;/* vertex link type. 1 indicates adjacent, 0 indicates not adjacent */
} ArcCell, ** AdjMatrix;/* adjacent matrix */
Typedef struct type {
Char data [3];/* vertex value */
} VertexType;
Typedef struct {
VertexType * vexs;/* vertex vector */
AdjMatrix arcs;/* adjacent matrix */
Int vexnum, arcnum;/* Number of vertices and edges of the graph */
} MGraph;
Struct clos {
VertexType adjvex;
Int lowcost;
} * Closedge;/* Secondary array. When low .. is 0, it indicates that the access has been made */
Void InitGraph (MGraph * G)/* Initial graph */
{Int I, nu, mu;
Printf ("n number of vertex input and number of (edge) arcs :");
Scanf ("% d", & nu, & mu );
G-> arcs = (ArcCell **) malloc (nu * sizeof (ArcCell *));
For (I = 0; I <nu; I ++)/* allocate the adjacent matrix space */
G-> arcs [I] = (ArcCell *) malloc (nu * sizeof (ArcCell ));
G-> vexs = (VertexType *) malloc (nu * sizeof (VertexType);/* allocate vertex space */
G-> vexnum = nu; G-> arcnum = mu;/* Number of vertices and edges */
}
Void InsertGraph (MGraph * G, int I, VertexType e)/* Add a value to the graph vertex vector */
{If (I <0 | I> G-> vexnum) return;
Strcpy (G-> vexs [I]. data, e. data );
}
Int Locate (MGraph G, VertexType v1)/* determine the position of v1 in the graph vertex */
{Int I;
For (I = 0; I <G. vexnum; I ++)
If (strcmp (v1.data, G. vexs [I]. data) = 0) return I;
Return-1;
}
Void CreateUND (MGraph * G)/* array (Adjacent matrix )*/
{Int I, j, k, * p, d, w;
VertexType v1, v2;
P = (int *) malloc (G-> vexnum * sizeof (int ));
For (I = 0; I <10; I ++) p [I] = 0;
For (I = 0; I <G-> vexnum; ++ I)/* Initial adjacent table */
{For (j = 0; j <G-> vexnum; ++ j) G-> arcs [I] [j]. adj = ING ;}
Printf ("input vertices in sequence and their weights and then vertices, for example, 'v1 3 v2': n ");
For (k = 0; k <G-> arcnum; ++ k)
{Printf ("Enter the % d arc (edge): n", k + 1 );
Scanf ("% s % d % s", v1.data, & w, v2.data);/* enter two adjacent vertex values and weights */
I = Locate (* G, v1); j = Locate (* G, v2);/* use I and j to determine their locations */
G-> arcs [I] [j]. adj = w;
G-> arcs [j] [I]. adj = G-> arcs [I] [j]. adj;/* This is not used when Directed Graphs */
}
}
Int minimum (MGraph G, struct clos * closedge)/* Find the vertex position of the minimum weight except 0 in the auxiliary array */
{Int I, min, j, k;
For (I = 0; I <G. vexnum; I ++)/* Find the first weight not 0 to min */
If (closedge [I]. lowcost! = 0)
{Min = closedge [I]. lowcost; k = I; break ;}

Related Article

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.