首页 > 编程语言 > 详细

拓扑排序

时间:2020-03-09 19:31:25      阅读:54      评论:0      收藏:0      [点我收藏+]

技术分享图片

技术分享图片

    // 更新入度
    void updataIndegree( )
    {
        for (auto node : vexs)
            for (auto x : node.medges)
                ++x.mit->mindegree;
    }

技术分享图片

    // 拓扑排序
    void topsort()
    {
        int counter = 0;
        updataIndegree();
        std::queue< Iterator > q;
        for (auto it = vexs.begin(); it != vexs.end(); ++it)
            if (it->mindegree == 0)
                q.push(it);                       // 入队

        while (!q.empty())
        {
            Iterator v = q.front();
            q.pop();                               // 出队
            v->mtopNum = ++counter;
            for (auto& w : v->medges)
                if (--w.mit->mindegree == 0)
                    q.push(w.mit);                // 入队
        }

        if (counter != vexs.size())
            throw CycleFoundException{ };
    }
技术分享图片图类 

拓扑排序

原文:https://www.cnblogs.com/lsyy2020/p/12450138.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!