### send_nodes's blog

By send_nodes, history, 4 years ago, ,

I was working on this problem: http://codeforces.com/contest/510/problem/C

I spent a fair bit of time on it, and I knew while solving it that it was a topological sorting problem. However, I have gone through the USACO training pages to learn my algorithms, which doesn't have a section on topological sorting. I know standard graph algorithms like bfs,dfs,warshall,dijkstra, etc. but I don't know how to solve these topological sorting problems. The editorial mentions that this is a classic topological sort problem.

My question is, how can dfs be applied to solve this problem, and where can I find more theory/practice problems to practice topological sorting.

Thanks!

• -3

 » 4 years ago, # | ← Rev. 2 →   0 Comparing a pair of adjacent, distinct names on the list gives us the relative order of a pair of characters. If "tourist" directly preceded "toosimple," for example, we could determine that 'u' precedes 'o'. Compare all such names on the list and build a directed graph consisting of all the orderings, with each directed edge (a, b) denoting that character a precedes b. The graph should contain at most n — 1 edges (less if the list contains adjacent, lexicographically equal names) and at most 26 vertices (1 vertex for each letter in the alphabet). The final alphabet is simply the topologically sorted graph, with unused characters inserted anywhere in any order. Check out this link for an explanation of what topological sorting is: http://www.geeksforgeeks.org/topological-sorting/
 » 3 years ago, # |   +1 You might try these two problems on Spoj
 » 3 years ago, # | ← Rev. 2 →   +5 There are a couple of algorithms for Toposort. The easiest to think about (in my opinion) being along the lines of: Pick any vertex with in-degree 0. Remove it from the graph and update in-degrees of outgoing vertices, then push it into some vector. Repeat 1 while there are still vertices in the graph. It's like you're picking fruits off a tree. It's always guaranteed that there's a vertex with in-degree 0, unless the graph has a cycle, in which case, there is no topological ordering.
•  » » 3 years ago, # ^ |   0 Befor do topsort on a graph,graph have to be DAG.Could you please help me how i check whether it is DAG or not? Thanks in Advance
•  » » » 3 years ago, # ^ |   0 You would apply the topological sort algorithm I mentioned using a queue to keep all the in-degree 0 vertices. At the end of the algorithm, if your vector has a size less than the number of vertices, then there was a cycle somewhere! This happens when your queue is empty but not all vertices in the graph have been pushed to it at some time.This way, there's no need to check that it's a DAG beforehand!
•  » » » 16 months ago, # ^ |   0 When there are still nodes remaining, but none of them as IN-degree as ZERO, you can be sure that a cycle exists in the graph.Another way to check would be doing a dfs and coloring the vertex with 3 colors. 1. WHITE — Unprocessed 2. GREY — In Process 3. BLACK — ProcessedIf you encounter GREY node while doing DFS traversal ==> CYCLE.
 » 3 years ago, # | ← Rev. 2 →   0 For topological sort problems,easiest approach is: 1.Store each vertex indegree in an array. 2.Initialize a queue with indegree zero vertices. 3. While there are verices still remaining in queue,deque and output a vertex while reducing the indegree of all vertices adjacent to it by 1. 4.Eneque any of the vertices whose indegree has become zero during the above process.(Indegree of a vertex is defined as number of edges pointing to it)
 » 11 months ago, # |   0 vector topo, adjlist[200005]; bool vis[200005]; void dfs(int x) { if (vis[x]) return; else vis[x] = 1; for (int i = 0; i < adjlist[x].size(); i++) dfs(adjlist[x][i]); topo.push_back(x); }