[算法与数据结构][图算法][拓扑排序]尝试1
Posted xcy6666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[算法与数据结构][图算法][拓扑排序]尝试1相关的知识,希望对你有一定的参考价值。
#include<utility> //pair
#include<vector>
#include<set> // multiset
#include<iostream>
/*
1
2 -> 1
/- 6
4 -- 3 -- 1
5 -- 2 -/
2 -> 1 -> 3 -> 2
*/
using namespace std;
vector<vector<int>> adj_list;
typedef pair<int, int> id_in_d; //id, in degree
//typedef custom_comp function<bool(id_in_d, id_in_d)>
auto custom_cmp = [](id_in_d a, id_in_d b){ return a.second < b.second; };
multiset<id_in_d, decltype(custom_cmp)> nodes(custom_cmp);
void construct(){
adj_list.resize(6+1);
//adj_list[2].push_back(1);
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
// time: O(E*log_V) + O(E*log_V)
// space: O(V)
nodes.insert({6,1});
nodes.insert({3,1});
nodes.insert({1,2});
nodes.insert({2,1});
nodes.insert({4,0});
nodes.insert({5,0});
auto it = nodes.find({3,1});
cout << it->first << " " << it->second << endl;
}
void topo_sort(){
vector<int> res;
int id;
while(nodes.size() > 0 && nodes.begin()->second == 0){
id = nodes.begin()->first;
res.push_back(id);
nodes.erase(nodes.begin());
for(auto nbr : adj_list[id]){
}
}
}
int main(){
construct();
return 0;
}
/* fail to construct a data structure that can:
* store {id, degree}
* quick find minimal degree
* quick update degree
*
* vector<int> degree; //offset as id
* map<int, set<int>> deg_ids; //indexed by degree
*/
以上是关于[算法与数据结构][图算法][拓扑排序]尝试1的主要内容,如果未能解决你的问题,请参考以下文章