Purpose
Orders the vertices of a Directed Acyclic Graph (DAG) in a linear sequence.
Time Complexity
Depends
Usage
Declaration
auto res = topologicalSord(g);Get the vertices in a linear sequence using res.
Implementation
vector<int> topologicalSort(DirectedGraph &_g) { vector<int> d, ind(_g.n); for (int i = 0; i < _g.n; i++) for (auto e : _g.g[i]) ind[e.to]++; queue<int> que; for (int i = 0; i < _g.n; i++) if (ind[i] == 0) que.push(i); while (!que.empty()) { int now = que.front(); d.push_back(now); que.pop(); for (auto e : _g.g[now]) { ind[e.to]--; if (ind[e.to] == 0) que.push(e.to); } } return d;}