# Minimum Spanning Trees and Prim's Algorithm

1. Introduction:

Describe the problem: for the undirected graph weighted G = (V, E) find the way so that all the top things that way together and the total weight of the smallest way.
The idea of ​​the algorithm:
Step 1: starting from any vertex k (usually selected first peak) has selected a minimum weight edge adjacent to the vertex k (min {A [k] [j]} j = 1..n) we 2 top passing mark and the next addition is found to be 1 to Step 2.
Step 2: Find the smallest edge of the graph with edge conditions found to have 1 top and 1 top mark not marked. That is, we find min {A [i] [j] j = 1..n}, i = 1..n such that i and j marks not marked to avoid the forming cycle. We increased the number of edges found on 1 and go to Step 3.
Step 3: If the edges found by n-1 end of the algorithm, reverse back to Step 2.

2. Source code:

 #include #include #define max 100#define FileInt "File7.inp"#define FileOut "File7.out"typedef struct Egde{ int x,y;}; //Read data from Filevoid Read_File(int A[max][max],int &n) { FILE*f = fopen(FileInt,"rb"); fscanf(f,"%d",&n); for(int i =0;iA[0][j] && A[0][j]!=0){ min = A[0][j]; L[0].y = j; } L[0].x = 0; D[0] = 1; D[L[0].y] = 1; Sum+=min; Count++; do { min = MAXINT; for( i=0; i0 && min>A[i][j] && D[j]==0){ min = A[i][j]; L[Count].x = i; L[Count].y = j; } Sum+=min; D[L[Count].y] = 1; Count++; } while(Count

3. Result: