Topological Sorting Gfg Code Example


Example: topological sort cp algorithms

int n; // number of vertices vector<vector<int>> adj; // adjacency list of graph vector<bool> visited; vector<int> ans;  void dfs(int v) {     visited[v] = true;     for (int u : adj[v]) {         if (!visited[u])             dfs(u);     }     ans.push_back(v); }  void topological_sort() {     visited.assign(n, false);     ans.clear();     for (int i = 0; i < n; ++i) {         if (!visited[i])             dfs(i);     }     reverse(ans.begin(), ans.end()); }

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable