_datastructure_c_impl: Minimum spanning tree of graphs

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h> #include <string.h>typedef char vertextype[4];typedef Char infoptr;typedef int vrtype; #define INFINITY 10000//defines an infinitely large value # define MAXSIZE 50//MAX vertex number typedef Enum{dg,dn,ug,un} Types of graphkind;//graphs: the graph, the direction net, the non-direction graph and the non-direction network typedef struct{vrtype ADJ;//for the right graph, with 1 for the adjacent, 0 for the non-adjacent; for the weighted graph, the storage weights infoptr *info;// Information related to arcs or edges the type of the}arcnode,adjmatrix[maxsize][maxsize];//diagram defines typedef struct{vertextype vex[maxsize];//for storing vertices adjmatrix arc;//adjacency Matrix, storing the information of an edge or arc int vexnum,arcnum;//the number of vertices and edges (arcs) Graphkind The kind;//diagram's type}mgraph;//record the least-cost edge from the vertex collection u to v-u definition typedef Struct{vertextype Adjvex; Vrtype Lowcost;} Closeedge[maxsize];//finds Vertex v in the vertex vector, finds the ordinal returned in the vector, otherwise returns -1int Locatevertex (Mgraph N,vertextype v) {int i;for (i=0;i< N.vexnum;++i) if (strcmp (n.vex[i],v) ==0) return i;return-1;} Using adjacency matrix notation to create a nvoid creategraph (mgraph *n) {int I,j,k,w,infoflag,len;char s[maxsize]; Vertextype v1,v2;printf ("Please enter the number of vertices with the net N, ARC number, ARC information (yes: 1, no: 0):"), scanf ("%d,%d,%d",& (*n) .vexnum,& (*n). Arcnum, &infoflag);p rintf ("Please enter a value of%d vertices (<%d characters): \ n ", n->vexnum,maxsize); for (i=0;i<n->vexnum;++i)//save each vertex of the net scanf ("%s ", N->vex[i]); for (i=0;i<n- >vexnum;i++)//Initialize adjacency matrix for (j=0;j<n->vexnum;j++) {n->arc[i][j].adj=infinity; The information for the n->arc[i][j].info=null;//arc is initialized to an empty}printf ("Enter the arc-tail-arc-head weights for%d arcs (with spaces as intervals): \ n", n->arcnum); for (k=0;k<n-> arcnum;k++) {scanf ("%s%s%d", v1,v2,&w);//Input two vertex and arc weights I=locatevertex (*N,V1); J=locatevertex (*N,V2); N->arc[i][j].adj=w;if (Infoflag) {//If the arc contains other information printf ("Please enter information about arcs:"); gets (s); Len=strlen (s); if (len) {n->arc[i][ j].info= (char *) malloc ((len+1) *sizeof (char)); strcpy (n->arc[i][j].info,s);}} The type of n->kind=dn;//diagram is the}//destruction network nvoid destroygraph (mgraph *n) {int i,j;for (i=0;i<n->vexnum;i++)//release arc information for (j =0;j<n->vexnum;j++) if (n->arc[i][j].adj!=infinity)//If there is an arc if (n->arc[i][j].info!=null) {//If the arc has relevant information, Free space occupied by this information (N->arc[i][j].info); N->arc[i][j].info=null;} n->vexnum=0;//set the number of vertices of the net to 0n->arcnum=0;//the number of arcs in the net to 0}//void Displaygraph (mgraph N) {int i,j;printf ("Have a forward net with%d vertices% D arc, Vertex in order: ", N.VExnum,n.arcnum); for (i=0;i<n.vexnum;++i)//Output mesh vertex printf ("%s", N.vex[i]);p rintf ("\ n" \ n "\ n");//Output Net N's Arc printf (" Serial number i= "), for (i=0;i<n.vexnum;i++) printf ("%8d ", i);p rintf (" \ n "), for (i=0;i<n.vexnum;i++) {printf ("%8d ", I), and for (j =0;j<n.vexnum;j++) printf ("%8d", N.arc[i][j].adj);p rintf ("\ n"); }}//returns the ordinal of the minimum value of lowcost int mininum (Closeedge edge,mgraph G) {int i=0,j,k,min;while (!edge[i].lowcost) i++;min=edge[i]. lowcost;//the first value not to be 0 k=i;for (j=i+1;j<g.vexnum;j++) if (edge[j].lowcost>0&&edge[j].lowcost<min) {// Assigns the ordinal of the minimum value to kmin=edge[j].lowcost;k=j;} return k;} The minimum spanning tree tvoid Prim (mgraph g,vertextype u) {int I,j,k;closeedge Closedge;k=locatevertex (g,u) of constructing net G from the first u vertex is obtained by using the PRIMM algorithm; K for vertex u corresponding to the ordinal for (j=0;j<g.vexnum;j++) {//array initialization strcpy (closedge[j].adjvex,u); Closedge[j].lowcost=g.arc[k][j].adj;} closedge[k].lowcost=0;//Initial set u includes only vertex uprintf ("The edges of the smallest spanning tree of the non-net are: \ n"); for (i=1;i<g.vexnum;++i) {// Select the remaining g.vexnum-1 vertex k=mininum (closedge,g),//k is the ordinal of the next vertex adjacent to the vertex in U, printf ("(%s-%s) \ n", Closedge[k].adjvex,g.vex[k]); Edge Closedge of the output spanning tree[k].lowcost=0;//k Vertex incorporates U-set for (J=0;J&LT;G.VEXNUM;++J) if (g.arc[k][j].adj<closedge[j].lowcost) {// The new vertex is added to the U-set and the minimum edge is re-deposited into the array strcpy (Closedge[j].adjvex,g.vex[k]); Closedge[j].lowcost=g.arc[k][j].adj;}} void Main () {Mgraph n;printf ("Create a non-net: \ n"); Creategraph (&n);D isplaygraph (N); Prim (N, "A");D estroygraph (&n), System ("pause");

Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint |copyright©2011-2015,supernatural, all rights Reserved.

_datastructure_c_impl: Minimum spanning tree of graphs

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.