If there is a cycle, I assume the topological order is useless. Detect cycle in a directed graph using topological sort. My union find code only get 38/47 test cases passed. Here's a little code for topological sort and cycle detection. If there are no cycles, I assume the topological order I found is valid. Thus, the above file defines a directed graph. We have discussed a DFS based solution to detect cycle in a directed graph.In this post, BFS based solution is discussed. Union Find: For this question, since a child can have multiple parents, things get tricky. If there is a cycle in a directed graph, then you can detect this by running a depth-first search over the graph. (Don’t use depth first search, we want just a modification of the basic topological sort.) It seems like your question is the following: can you use depth-first search to detect cycles in an undirected graph, or should you use topological sort instead? The answer is that both approaches will work. I can determine the topological sort of a directed graph using DFS algorithm. Use the following approach: consider we have three colors, and each vertex should be painted with one of these colors. Detect Cycle in a Directed Graph Given a directed graph, check whether the graph contains a cycle or not. Am I correct so far? While I was searching for it I came across different techniques like DFS and topological sorting to detect cycle in a directed graph. Minimum time taken by each job to be completed given by a Directed Acyclic Graph Hard Given a Directed Acyclic Graph having V vertices and E edges, where each edge {U, V} represents the Jobs U … Topological Sort / Cycle Detection. graph can contain many topological sorts. If the given graph contains a cycle, then there is at least one node which is a parent as well as a child so this will break Topological Order. Please corect me if this is not true. Before going into them, whenever you are dealing with representing graphs in files, you have to decide how you are going to format them. I was trying to write code for detecting a cycle in a directed graph and if there is no cycle then return a topological order of the same. Is "topological sort of an undirected graph… Detect cycle in Directed Graph using Topological Sort , In Topological Sort, the idea is to visit the parent node followed by the child node. Your function should return true if the given graph contains at least one cycle, else return false. What about undirected graphs? The idea is to simply use Kahn’s algorithm for Topological Sorting. An acyclic graph always has a topological sort. So, initially all vertices are white. Topological Sort: TS can be only used to directed graph. The topological sort may not be unique i.e. Steps involved in detecting cycle in a directed graph using BFS. "White color" means that the vertex hasn't been visited yet. And if the graph contains cycle then it does not form a topological sort, because no node of the cycle can appear before the other nodes of the cycle in the ordering. "Black" means we've visited all vertices in subtree and left the vertex. And the answer is: If no vertex has indegree 0, we can find a cycle by tracing backwards through vertices with positive indegree; since every vertex on the trace back has a positive indegree, we eventually reach a vertex twice, and the cycle has been found. "Gray" means that we've visited the vertex but haven't visited all vertices in its subtree.