Minimum Spanning Tree - Kruskal algorithm

Minimum Spanning Tree - Kruskal algorithm

ALGORITHM

1. FIG all cables removed, leaving only vertex

2. Find the minimum weight set from the side edge of the array of FIG, connecting two vertices of the side

3. continue to look for minimum weight edge connecting vertex between the two, such that if the selected minimum spanning tree edge loop appears, the edge is discarded, the next smallest value option edge

4 until all of the vertices are connected together and there is no loop, the minimum spanning tree is generated.

 

Kruskal algorithm code

// the value of minimum spanning tree in the communication network

#include <stdio.h>

#include <stdlib.h>

 

#define MAXEDGE 20

#define MAXVEX  20

#define INFINITY 65535

 

typedef struct

{

    int arc[MAXVEX][MAXVEX];

    int numVertexes, numEdges; // number of vertices, the number of edges

} Ngrf;

 

typedef struct

{

    int begin;

    int end;

    int weight;

} Edge; // definition of the edge of arrays Edge Structure

 

// Create the adjacency matrix

void CreateMGraph(MGraph *G) {

    int i, j;

 

    G->numEdges=11;

    G->numVertexes=7;

 

    for (i = 0; i < G->numVertexes; i++) {

        for ( j = 0; j < G->numVertexes; j++) {

            if (i==j)

                G->arc[i][j]=0;

            else

                G->arc[i][j] = G->arc[j][i] = INFINITY;

        }

    }

    G->arc[0][1]=7;

    G->arc[0][3]=5;

    G->arc[1][2]=8;

    G->arc[1][3]=9;

    G->arc[1][4]=7;

    G->arc[2][4]=5;

    G->arc[3][4]=15;

    G->arc[3][5]=6;

    G->arc[4][5]=8;

    G->arc[4][6]=9;

    G->arc[5][6]=11;

 

    for(i = 0; i < G->numVertexes; i++) {

        for(j = i; j < G->numVertexes; j++) {

            G->arc[j][i] =G->arc[i][j];

        }

    }

 

}

 

// quick sort of condition

int cmp(const void* a, const void* b) {

    return (*(Edge*)a).weight - (*(Edge*)b).weight;

}

 

// find the root

int Find(int *parent, int f) {

    while ( parent[f] > 0) {

        f = parent[f];

    }

    return f;

}

 

// generate the minimum spanning tree

void MiniSpanTree_Kruskal(MGraph G) {

    int i, j, n, m;

    int k = 0;

    int parent [MAXVEX]; // for finding the root node of the array

 

    Edge edges [MAXEDGE]; // define the edge of arrays, edge structure as begin, end, weight, are integer

 

    // array and used to construct ordered set of edges (the diagonal portion of the right side of the adjacency matrix set into the array)

    for ( i = 0; i < G.numVertexes-1; i++) {

        for (j = i + 1; j < G.numVertexes; j++) {

            if (G.arc[i][j] < INFINITY) {

                edges [k] .begin = i; // number of smaller nodes headed

                edges [k] .end = j; // number of nodes is large tail

                edges[k].weight = G.arc[i][j];

                k++;

            }

        }

    }

 

    // Sort the array is the set of edges Edge

    qsort(edges, G.numEdges, sizeof(Edge), cmp);

 

    for (i = 0; i < G.numVertexes; i++)

        parent[i] = 0;

 

    printf ( "Printing minimum spanning tree: \ n");

    for (i = 0; i < G.numEdges; i++) {

        n = Find (parent, edges [i] .begin); // Find where the side of the tree edge [i] is the "first node" roots

        m = Find (parent, edges [i] .end); // Find where the side of the tree edge [i] "tail nodes" root

 

        // If n and m ranging from two vertices are not described in a tree, so this does not cause side edge sets have been added to the selected generating circuit

        if (n != m) {

            parent[n] = m;

            printf("(%d, %d) %d\n", edges[i].begin, edges[i].end, edges[i].weight);

        }

    }

}

 

int main(void)

{

    MGraph G;

    CreateMGraph(&G);

    MiniSpanTree_Kruskal(G);

 

    return 0;

}

Original link: https: //blog.csdn.net/junya_zhang/article/details/83584592

Original link: https://blog.csdn.net/hhu1506010220/article/details/51971717

Guess you like

Origin www.cnblogs.com/yuanch2019/p/11578715.html