diff --git a/Algorithms/DetectCycleInDirectedGraph.cpp b/Algorithms/DetectCycleInDirectedGraph.cpp new file mode 100644 index 0000000..66aefa3 --- /dev/null +++ b/Algorithms/DetectCycleInDirectedGraph.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; + +// Function to check if a cycle exists using DFS +bool dfs(int node, vector adj[], vector &visited, vector &recStack) { + visited[node] = 1; // Mark the current node as visited + recStack[node] = 1; // Add this node to the recursion stack + + // Visit all adjacent vertices + for (auto neighbor : adj[node]) { + // If the neighbor is not visited, do a DFS on it + if (!visited[neighbor]) { + if (dfs(neighbor, adj, visited, recStack)) { + return true; // Found a cycle + } + } + // If the neighbor is already in recursion stack → cycle found + else if (recStack[neighbor]) { + return true; + } + } + + // Remove the node from recursion stack before returning + recStack[node] = 0; + return false; +} + +// Function to check if the directed graph has a cycle +bool isCyclic(int V, vector adj[]) { + vector visited(V, 0); + vector recStack(V, 0); + + // Check each node (for disconnected graphs) + for (int i = 0; i < V; i++) { + if (!visited[i]) { + if (dfs(i, adj, visited, recStack)) { + return true; // If cycle found + } + } + } + return false; // No cycle +} + +int main() { + int V, E; + cout << "Enter number of vertices and edges: "; + cin >> V >> E; + + vector adj[V]; + cout << "Enter edges (u -> v):\n"; + for (int i = 0; i < E; i++) { + int u, v; + cin >> u >> v; + adj[u].push_back(v); // Directed edge + } + + if (isCyclic(V, adj)) + cout << "Cycle detected in the graph.\n"; + else + cout << "No cycle found in the graph.\n"; + + return 0; +} diff --git a/Algorithms/Quick_sort.cpp b/Algorithms/Quick_sort.cpp index 7fc46f2..3fad718 100644 --- a/Algorithms/Quick_sort.cpp +++ b/Algorithms/Quick_sort.cpp @@ -1,4 +1,4 @@ -#include +TT#include #include #include // Required for std::swap