Finding Cycles in an Undirected Graphs (using DFS) |
Using Depth-First Search algorithm to traverse the graph and detect cycles. The graph is represented as an adjacency list, where each node is a vector of integers representing its neighbors. The algorithm starts at node 0 and keeps track of the visited nodes and their parent nodes. If it encounters a node that has already been partially visited, it means that a cycle has been detected. The algorithm then backtracks from the current node to its parent nodes to form the cycle and stores it in a vector of cycles. Finally, the program prints out all the cycles found in the graph.
Time Complexity: O(n+m)
vector<vector<int> cycles;
void DFS_Cycles_Finder(vector<vector<int> &graph, vector<int> &visited, vector<int> &parents, int node, int p)
{
// visited status 0: unvisited
// visited status 1: partially visited
// visited status 2: fully visited
if (visited.at(node) == 0)
{
parents.at(node) = p;
visited.at(node) = 1;
for (auto neighbor : graph.at(node))
{
if (neighbor != parents.at(node))
{
DFS_Cycles_Finder(graph, visited, parents, neighbor, node);
}
}
visited.at(node) = 2;
}
else if (visited.at(node) == 1)
{
vector<int> newCycle;
int currentNode = p;
newCycle.push_back(currentNode);
while (currentNode != node)
{
currentNode = parents.at(currentNode);
newCycle.push_back(currentNode);
}
cycles.push_back(newCycle);
}
}
int main()
{
vector<vector<int>> graph = {
{1, 2},
{0, 3},
{0, 3},
{1, 2, 4},
{3, 5, 6},
{4, 6},
{4, 5}};
vector<int> visited;
visited.resize(graph.size(), 0);
vector<int> parents;
parents.resize(graph.size(), -1);
DFS_Cycles_Finder(graph, visited, parents, 0, 0);
for (int i = 0; i < cycles.size(); i++)
{
for (int j = 0; j < cycles.at(i).size(); j++)
{
cout << cycles.at(i).at(j) << " ";
}
cout << endl;
}
}