Implementation Code of the C program for the minimal spanning tree algorithm of undirected graphs (prim algorithm)

Source: Internet
Author: User

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>

// This struct is used to represent other vertices that can be reached from a vertex.
Struct Enode
{
Int secpoint; // vertex number
Int weight;
Enode * Next; // pointer to the next Vertex
};

// This struct indicates the information of each vertex.
Struct pnode
{
Char value; // vertex Value
Enode * Next; // pointer to the other first vertex that can be reached by the vertex
};

// Structure of the graph, which has a maximum of 100 vertices
Struct Map
{
Pnode point [100]; // The subscript of the array is the vertex number of the vertex.
Int numpoint, numedge;
};

// Graph creation Function
Struct map * createmap ()
{
Struct map * MP = (struct map *) malloc (sizeof (struct map ));
Int I, J;
Int firp, SECP, weight;
Int nump, nume;
Char INFOP;

Memset (MP, 0, sizeof (struct map ));

Printf ("Enter the number of vertices and edges in the format of 'number of vertices, edges ': \ n ");
Scanf ("% d, % d", & nump, & nume );
MP-> numpoint = nump;
MP-> numedge = nume;

Printf ("enter the information of each vertex. continuous input without separators: \ n ");
Fflush (stdin );
For (I = 0; I <MP-> numpoint; I ++)
{
Scanf ("% C", & INFOP );
MP-> point [I]. value = INFOP;
}

Printf ("Enter the edge and weight in the format of 'vertex-vertex, weighting '\ n ");
Fflush (stdin );
For (j = 0; j <MP-> numedge; j ++)
{
Scanf ("% d-% d, % d", & firp, & SECP, & weight );
Struct Enode * newnode = (struct Enode *) malloc (sizeof (struct Enode ));
Newnode-> secpoint = SECP;
Newnode-> Weight = weight;
Newnode-> next = MP-> point [firp]. Next;
MP-> point [firp]. Next = newnode;
}
Return MP;
}

Void prim (struct map * MP)
{
Int pointarray [20], I, j, min;
Int curpoint1, curpoint2;
Struct Enode * pnode;
Memset (pointarray, 0, sizeof (pointarray ));
Pointarray [0] = 1;
I = 1;
Printf ("\ n each side of the Minimum Spanning Tree: \ n ");

// Cycle when vertex is not added

While (I <MP-> numpoint)
{

Min = 65537;

// Cyclically traverse the vertices of the entire Graph

For (j = 0; j <MP-> numpoint; j ++)
{
For (pnode = MP-> point [J]. Next; pnode! = NULL; pnode = pnode-> next)

{

// If one vertex has been found and another vertex has not been found and the weight is smaller than the current minimum value, it is recorded

If (pointarray [J] = 0 & pointarray [pnode-> secpoint] = 1 & pnode-> weight <min)
{
Curpoint2 = J;
Curpoint1 = pnode-> secpoint;
Min = pnode-> weight;
}
Else if (pointarray [J] = 1 & pointarray [pnode-> secpoint] = 0 & pnode-> weight <min)
{
Curpoint1 = J;
Curpoint2 = pnode-> secpoint;
Min = pnode-> weight;
}
}

}

// Curpoint2 is the vertex found this time.

Pointarray [curpoint2] = 1;
I ++;
Printf ("% d-% d \ n", curpoint1, curpoint2 );
}
}

Int main ()
{
Struct map * MP = createmap ();
Prim (MP );

Return 1;
}

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.