Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Algorithms/DetectCycleInDirectedGraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <bits/stdc++.h>
using namespace std;

// Function to check if a cycle exists using DFS
bool dfs(int node, vector<int> adj[], vector<int> &visited, vector<int> &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<int> adj[]) {
vector<int> visited(V, 0);
vector<int> 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<int> 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;
}
2 changes: 1 addition & 1 deletion Algorithms/Quick_sort.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <iostream>
TT#include <iostream>
#include <vector>
#include <algorithm> // Required for std::swap

Expand Down