拓扑排序 C 代码


拓扑排序 C 代码

/*  topsort.c
    Topologically sort a directed acyclic graph (DAG)
    by: Steven Skiena
    begun: March 26, 2002
*/
/*
Copyright 2003 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
This program appears in my book:
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information.
This book can be ordered from Amazon.com at
https://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include "bool.h"
#include "graph.h"
#include "queue.h"
compute_indegrees(graph *g, int in[])
{
    int i,j;            /* counters */



}
topsort(graph *g, int sorted[])
{
    int indegree[MAXV];     /* indegree of each vertex */
    queue zeroin;           /* vertices of indegree 0 */
    int x, y;           /* current and next vertex */
    int i, j;           /* counters */
    compute_indegrees(g,indegree);
    init_queue(&zeroin);

        if (indegree[i] == 0) enqueue(&zeroin,i);
    j=0;
    while (empty(&zeroin) == FALSE) {
        j = j+1;
        x = dequeue(&zeroin);
        sorted[j] = x;

            y = g->edges[x][i];
            indegree[y] --;
            if (indegree[y] == 0) enqueue(&zeroin,y);
        }
    }
    if (j != g->nvertices)
        printf("Not a DAG -- only %d vertices found\n",j);
}
main()
{
    graph g;
    int out[MAXV];
    int i;
    read_graph(&g,TRUE);
    print_graph(&g);
    topsort(&g,out);

        printf(" %d",out[i]);
    printf("\n");
}

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《拓扑排序 C 代码
本文地址:https://www.zhiletu.com/archives-7851.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微