TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
CycleCheck Class Reference

Static Public Member Functions

static bool isCyclicDFS (Graph const &graph)
 
static bool isCyclicBFS (Graph const &graph)
 

Private Types

enum  nodeStates : uint8_t { not_visited = 0 , in_stack , visited }
 

Static Private Member Functions

static bool isCyclicDFSHelper (AdjList const &adjList, std::vector< nodeStates > *state, unsigned int node)
 

Detailed Description

Check if a directed graph has a cycle or not.

This class provides 2 methods to check for cycle in a directed graph: isCyclicDFS & isCyclicBFS.

  • isCyclicDFS uses DFS traversal method to check for cycle in a graph.
  • isCyclidBFS used BFS traversal method to check for cycle in a graph.

Definition at line 159 of file cycle_check_directed_graph.cpp.

Member Enumeration Documentation

◆ nodeStates

enum CycleCheck::nodeStates : uint8_t
private

Definition at line 161 of file cycle_check_directed_graph.cpp.

161: uint8_t { not_visited = 0, in_stack, visited };

Member Function Documentation

◆ 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
graphwhich needs to be evaluated for the presence of cycle.
Returns
true if a cycle is detected, else false.

Definition at line 250 of file cycle_check_directed_graph.cpp.

250 {
251 auto graphAjdList = graph.getAdjList();
252 auto vertices = graph.getVertices();
253
254 std::vector<unsigned int> indegree(vertices, 0);
255 // Calculate the indegree i.e. the number of incident edges to the node.
256 for (auto const& list : graphAjdList) {
257 auto children = list.second;
258 for (auto const& child : children) {
259 indegree[child]++;
260 }
261 }
262
263 std::queue<unsigned int> can_be_solved;
264 for (unsigned int node = 0; node < vertices; node++) {
265 // If a node doesn't have any input edges, then that node will
266 // definately not result in a cycle and can be visited safely.
267 if (!indegree[node]) {
268 can_be_solved.emplace(node);
269 }
270 }
271
272 // Vertices that need to be traversed.
273 auto remain = vertices;
274 // While there are safe nodes that we can visit.
275 while (!can_be_solved.empty()) {
276 auto solved = can_be_solved.front();
277 // Visit the node.
278 can_be_solved.pop();
279 // Decrease number of nodes that need to be traversed.
280 remain--;
281
282 // Visit all the children of the visited node.
283 auto it = graphAjdList.find(solved);
284 if (it != graphAjdList.end()) {
285 for (auto child : it->second) {
286 // Check if we can visited the node safely.
287 if (--indegree[child] == 0) {
288 // if node can be visited safely, then add that node to
289 // the visit queue.
290 can_be_solved.emplace(child);
291 }
292 }
293 }
294 }
295
296 // If there are still nodes that we can't visit, then it means that
297 // there is a cycle and return true, else return false.
298 return !(remain == 0);
299 }
Graph Algorithms.

◆ 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
graphwhich 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.

Definition at line 213 of file cycle_check_directed_graph.cpp.

213 {
214 auto vertices = graph.getVertices();
215
223 std::vector<nodeStates> state(vertices, not_visited);
224
225 // Start visiting each node.
226 for (unsigned int node = 0; node < vertices; node++) {
227 // If a node is not visited, only then check for presence of cycle.
228 // There is no need to check for presence of cycle for a visited
229 // node as it has already been checked for presence of cycle.
230 if (state[node] == not_visited) {
231 // Check for cycle.
232 if (isCyclicDFSHelper(graph.getAdjList(), &state, node)) {
233 return true;
234 }
235 }
236 }
237
238 // All nodes have been safely traversed, that means there is no cycle in
239 // the graph. Return false.
240 return false;
241 }
static bool isCyclicDFSHelper(AdjList const &adjList, std::vector< nodeStates > *state, unsigned int node)

◆ isCyclicDFSHelper()

static bool CycleCheck::isCyclicDFSHelper ( AdjList const & adjList,
std::vector< nodeStates > * state,
unsigned int node )
inlinestaticprivate

Helper function of "isCyclicDFS".

Parameters
adjListis the adjacency list representation of some graph.
stateis the state of the nodes of the graph.
nodeis the node being evaluated.
Returns
true if graph has a cycle, else false.

Definition at line 171 of file cycle_check_directed_graph.cpp.

173 {
174 // Add node "in_stack" state.
175 (*state)[node] = in_stack;
176
177 // If the node has children, then recursively visit all children of the
178 // node.
179 auto const it = adjList.find(node);
180 if (it != adjList.end()) {
181 for (auto child : it->second) {
182 // If state of child node is "not_visited", evaluate that child
183 // for presence of cycle.
184 auto state_of_child = (*state)[child];
185 if (state_of_child == not_visited) {
186 if (isCyclicDFSHelper(adjList, state, child)) {
187 return true;
188 }
189 } else if (state_of_child == in_stack) {
190 // If child node was "in_stack", then that means that there
191 // is a cycle in the graph. Return true for presence of the
192 // cycle.
193 return true;
194 }
195 }
196 }
197
198 // Current node has been evaluated for the presence of cycle and had no
199 // cycle. Mark current node as "visited".
200 (*state)[node] = visited;
201 // Return that current node didn't result in any cycles.
202 return false;
203 }
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13

The documentation for this class was generated from the following file: