[算法与数据结构][图算法][拓扑排序]尝试2

Posted xcy6666

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[算法与数据结构][图算法][拓扑排序]尝试2相关的知识,希望对你有一定的参考价值。

#include<cassert>

#include<vector>
#include<unordered_set>
#include<set>
#include<map>
#include<iostream>
/*
e.g.1   1

e.g.2   2 -> 1

e.g.3
     /- 6
   4 -- 3 -- 1
   5 -- 2 -/
e.g.4
   2 -> 1 -> 3 -> 2

*/
using namespace std;
vector<vector<int>> adj_list(7);

//template<typename T> using adj=set<T>;
template<typename T> using adj=unordered_set<T>;
map<int, adj<int>> deg_ids; //indexed by degree
vector<int> in_d(7, 0); // in degree of id

bool update_in_d(int id, int new_in_d){
        int old_in_d = in_d[id];
        if(new_in_d == old_in_d) return true;

        auto m_it = deg_ids.find(old_in_d);
        if(m_it == deg_ids.end()) assert(false);// an id has in_d v, then deg_ids[v] must valid

        auto s_it = m_it->second.find(id);
        if(s_it == m_it->second.end()) assert(false);// an id has in_d v, then deg_ids[v] must valid
        m_it->second.erase(s_it);

        m_it = deg_ids.find(new_in_d);
        if( m_it == deg_ids.end() ){
                deg_ids.insert({ new_in_d, {id}});
        }else{
                m_it->second.insert(id);
        }
        in_d[id] = new_in_d;
        return true;
}

void construct(){
        //e.g.3
        adj_list[4].push_back(6);
        adj_list[4].push_back(3);
        adj_list[3].push_back(1);
        adj_list[5].push_back(2);
        adj_list[2].push_back(1);
        //time: O(E), space O(E)

        //look up; remove; insert

        deg_ids[0] = {};
        for(int i=1; i < 7; i++){ deg_ids[0].insert(i);}

        update_in_d(6, in_d[6]+1);
        update_in_d(3, in_d[3]+1);
        update_in_d(1, in_d[1]+1);
        update_in_d(2, in_d[2]+1);
        update_in_d(1, in_d[1]+1);
        //time:  O(V) + O(E*log_deg*1)
        //space: O(V+V+deg)

}
void print_deg_ids(){
    for(auto s: deg_ids){
        cout << "in_d : " << s.first << endl;
        for(auto e : s.second){
                cout << e << " ";
        }
        cout << endl;
    }
}

bool topo_sort(){
        vector<int> res;
        int id;
        while(deg_ids[0].size() > 0){
                id = *(deg_ids[0].begin());
                res.push_back(id);
                update_in_d(id, -1); //seperate to -1
                for(auto nbr : adj_list[id]){
                        update_in_d(nbr, in_d[nbr]-1);
                }
        }
        //time: O(V*log_deg)
        if(deg_ids[-1].size()+1 != in_d.size())
                return false;
        for(auto e : res) cout << e << " ";
        cout << endl;

        return true;
}

int main(){
    construct();
//    print_deg_ids();
    topo_sort();
}

/*
 * fail to construct a data structure that can:
 *      store {id, degree}
 *      quick find minimal degree
 *      quick update degree
 *
 *      deg_ids: store degree : set<id>, sorted on degree
 *      in_d:    store id     : degree
 *
 * reflection:
 *      deg_ids is a global structure to maintain all nodes degree info and updated all nodes order info
 *      on graph structure:
 *              bfs pattern or dfs pattern is common
 *              for the next degree 0 nodes, a bfs pattern can be used to simplify
 *
 * */

以上是关于[算法与数据结构][图算法][拓扑排序]尝试2的主要内容,如果未能解决你的问题,请参考以下文章

[算法与数据结构][图算法][拓扑排序]尝试3

数据结构与算法简记--拓扑排序

拓扑排序算法实现

数据结构与算法之深入解析图的拓扑排序

图相关算法

数据结构与算法:图