Detecting Cycles
Understand the different techniques used to detect cycles in a graph.
We'll cover the following...
Directed acyclic graph
A directed acyclic graph or dag is a directed graph with no directed cycles. Any vertex in a dag that has no incoming vertices is called a source; any vertex with no outgoing edges is called a sink. An isolated vertex with no incident edges at all is both a source and a sink. Every dag has at least one source and one sink but may have more than one of each. For example, in the graph with vertices but no edges, every vertex is a source, and every vertex is a sink.
Algorithm for detecting directed acyclic graph
Recall from our earlier case analysis that if for any edge , the graph contains a directed path from to and, therefore, contains a directed cycle through the edge . Thus, we can determine whether a given directed graph is a dag in time by computing a postordering of the vertices and then checking each edge by brute force.
Alternatively, instead of numbering the vertices, we can explicitly maintain the status of each ...
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy