码迷,mamicode.com
首页 > 编程语言 > 详细

贪婪算法

时间:2017-03-22 21:12:04      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:follow   system   算法   const   cal   include   typedef   mini   min   

最小生成树(Kruskal’s Minimum Spanning Tree)

#include <cstdio>
#include <cstdlib>
#include <cstring>

typedef struct
{
    int src, dest, weight;
}Edge;

typedef struct
{
    int vertices, edges_n;
    Edge *edges;
}Graph;

typedef struct
{
    int parent;
    int rank;
}Subset;

Graph * createGraph(int vertices, int edges);
int compare(const void *a, const void *b);
int find(Subset *subsets, int n);
void Union(Subset *subsets, int x, int y);
void KruskalMST(Graph *graph);

int main(void)
{
    /* Let us create following weighted graph
        10
    0--------1
    |  \     |
   6|   5\   |15
    |      \ |
    2--------3
        4
    */
    int vertices = 4;
    int edges_n = 5;
    Graph *graph = createGraph(vertices, edges_n);

    // add edge 0-1
    graph->edges[0].src = 0;
    graph->edges[0].dest = 1;
    graph->edges[0].weight = 10;

    // add edge 0-2
    graph->edges[1].src = 0;
    graph->edges[1].dest = 2;
    graph->edges[1].weight = 6;

    // add edge 0-3
    graph->edges[2].src = 0;
    graph->edges[2].dest = 3;
    graph->edges[2].weight = 5;

    // add edge 1-3
    graph->edges[3].src = 1;
    graph->edges[3].dest = 3;
    graph->edges[3].weight = 15;

    // add edge 2-3
    graph->edges[4].src = 2;
    graph->edges[4].dest = 3;
    graph->edges[4].weight = 4;

    KruskalMST(graph);

    system("pause");
    return 0;
}

Graph * createGraph(int vertices, int edges)
{
    Graph *graph = (Graph *)calloc(1, sizeof(Graph));
    graph->vertices = vertices;
    graph->edges_n = edges;
    graph->edges = (Edge *)calloc(edges, sizeof(Edge));

    return graph;
}

int compare(const void * a, const void * b)
{
    return ((Edge *)a)->weight > ((Edge *)b)->weight;
}

/*
    Find the root of which subset a particular element is in.
    Using path compression for optimization.
*/
int find(Subset * subsets, int n)
{
    if (subsets[n].parent != n)
    {
        subsets[n].parent = find(subsets, subsets[n].parent);
    }

    return subsets[n].parent;
}

/*
    Join two subsets into a single subset.
    Uion by rank for optimization.
*/
void Union(Subset * subsets, int x, int y)
{
    int x_root = find(subsets, x);
    int y_root = find(subsets, y);

    if (subsets[x_root].rank < subsets[y_root].rank)
        subsets[x_root].parent = y_root;
    else if (subsets[x_root].rank > subsets[y_root].rank)
        subsets[y_root].parent = x_root;
    else
    {
        subsets[y_root].parent = x_root;
        subsets[x_root].rank++;
    }
}

/*
    Using Kruskal‘s algorithm to find MST in the connected and undirected graph
*/
void KruskalMST(Graph * graph)
{
    int vertices = graph->vertices;
    Subset *subsets;

    subsets = (Subset *)calloc(vertices, sizeof(Subset));
    for (int i = 0; i < vertices; i++)
    {
        subsets[i].parent = i;
    }

    qsort(graph->edges, graph->edges_n, sizeof(Edge), compare);

    int e = 0, i = 0;
    Edge *result;

    result = (Edge *)calloc(vertices - 1, sizeof(Edge));
    while (e < vertices - 1)
    {
        Edge next_edge = graph->edges[i++];

        int x = find(subsets, next_edge.src);
        int y = find(subsets, next_edge.dest);

        if (x != y)
        {
            result[e++] = next_edge;
            Union(subsets, x, y);
        }
    }

    printf("Edges in the constructed MST\n");
    for (int i = 0; i < vertices - 1; i++)
    {
        printf("%d -- %d == %d\n", result[i].src, result[i].dest, result[i].weight);
    }

    return;
}

 

贪婪算法

标签:follow   system   算法   const   cal   include   typedef   mini   min   

原文地址:http://www.cnblogs.com/Lao-zi/p/6601826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!