Summary:
This C++ code defines a Solution class with a method is Cyclic to detect cycles in a directed graph using Depth-First Search (DFS). The solve function is a recursive helper function for DFS traversal, maintaining two maps to track visited nodes and nodes in the current DFS traversal path. The algorithm explores each node, marking it visited and checking for cycles by detecting nodes already in the current DFS path. If a node is visited and is part of the current DFS path, a cycle is detected, and the function returns true. The main function iterates through all nodes, invoking the solve function on unvisited nodes, returning true if a cycle is found and false otherwise.
Solution:
class Solution { public: // Function to detect cycle in a directed graph. bool solve(int src , unordered_map<int ,bool>& visited, unordered_map<int , bool>& dfsTrack ,vector<int> adj[]){ visited[src] = true; dfsTrack[src] = true;
for(auto nbr : adj[src]){ if(!visited[nbr]){ bool ans = solve(nbr, visited,dfsTrack , adj ); if(ans == true) return true; } if(visited[nbr] == 1 && dfsTrack[nbr] == 1) return true; } dfsTrack[src] = false; return false; }
bool isCyclic(int V, vector<int> adj[]) { // code here unordered_map<int , bool>visited; unordered_map<int , bool>dfsTrack; for(int i =0;i<V;i++){ if(!visited[i]){
bool ans = solve(i, visited, dfsTrack, adj); if(ans == true) return true; } } return false;
} }; |
Code explanation:
The solve function performs a DFS traversal on the graph starting from the source node (src).
It uses two maps (visited and dfsTrack) to keep track of visited nodes and nodes in the current DFS path.
The loop iterates through the neighbors of the current node, recursively calling solve for unvisited nodes.
If a neighbor is already visited and is part of the current DFS path, a cycle is detected, and the function returns true.
After exploring all neighbors, the current node is marked as not in the DFS path (dfsTrack[src] = false) before returning false.
The isCyclic function iterates through all nodes, invoking the solve function on unvisited nodes. If any invocation returns true, indicating a cycle, the function returns true; otherwise, it returns false.
The algorithm leverages DFS to efficiently detect cycles in directed graphs, making use of visited and DFS path tracking to identify cycles during traversal.
Answered by: >aman63t28
Credit: >GeekforGeeks
Suggested blogs:
>Step by Step guide to Deploy Terraform in Azure using GitHub Actions
>Testing react components using Hooks and Mocks
>How to mock a constant value in Python?
>Creating a pivot table by 6 month interval rather than year
>How to do PHP Decryption from Node.js Encryption
>How to Set up the Android emulator?
>How to Set up the local environment for Angular development?
>How to solve encoding issue when writing to a text file, with Python?
>Use Firebase Realtime Database with ASP.NET MVC App
>How to Install mariaDB client efficiently inside docker?
>Can I call an API to find out the random seed in Python `hash()` function?
>How remove residual lines after merge two regions using Geopandas in python?
>How to configure python in jmeter?