|
| enum | nodeStates : uint8_t { not_visited = 0,
in_stack,
visited
} |
| |
◆ isCyclicBFS()
| static bool CycleCheck::isCyclicBFS |
( |
Graph const & |
graph | ) |
|
|
inlinestatic |
Check if a graph has cycle or not.
This function uses BFS to check if a graph is cyclic or not.
- Parameters
-
| graph | which needs to be evaluated for the presence of cycle. |
- Returns
- true if a cycle is detected, else false.
246 for (
auto const& [parent, children] : graphAjdList) {
247 for (
auto const& child : children) {
256 if (!indegree[
node]) {
262 auto remain =
graph.getVertices();
264 while (!can_be_solved.
empty()) {
265 auto front = can_be_solved.
front();
272 if (
auto it = graphAjdList.find(front); it != graphAjdList.end()) {
273 for (
auto child : it->second) {
275 if (--indegree[child] == 0) {
286 return !(remain == 0);
◆ isCyclicDFS()
| static bool CycleCheck::isCyclicDFS |
( |
Graph const & |
graph | ) |
|
|
inlinestatic |
Driver function to check if a graph has a cycle.
This function uses DFS to check for cycle in the graph.
- Parameters
-
| graph | which needs to be evaluated for the presence of cycle. |
- Returns
- true if a cycle is detected, else false.
State of the node.
It is a vector of "nodeStates" which represents the state node is in. It can take only 3 values: "not_visited", "in_stack", and "visited".
Initially, all nodes are in "not_visited" state.
221 if (state[
node] == not_visited) {
◆ isCyclicDFSHelper()
| static bool CycleCheck::isCyclicDFSHelper |
( |
AdjList const & |
adjList, |
|
|
std::vector< nodeStates > * |
state, |
|
|
unsigned int |
node |
|
) |
| |
|
inlinestaticprivate |
Helper function of "isCyclicDFS".
- Parameters
-
| adjList | is the adjacency list representation of some graph. |
| state | is the state of the nodes of the graph. |
| node | is the node being evaluated. |
- Returns
- true if graph has a cycle, else false.
169 (*state)[
node] = in_stack;
173 if (
auto const& it = adjList.find(
node); it != adjList.end()) {
174 for (
auto child : it->second) {
177 if (
auto state_of_child = (*state)[child];
178 state_of_child == not_visited) {
182 }
else if (state_of_child == in_stack) {
193 (*state)[
node] = visited;
The documentation for this class was generated from the following file:
- graph/cycle_check_directed_graph.cpp