📖 Scenario: Imagine you are working with a network of roads connecting cities. You want to check if there is any circular route that starts and ends at the same city without repeating roads. This is called cycle detection in an undirected graph.
🎯 Goal: You will build a program in Go to detect if an undirected graph has any cycle. The graph will be represented using an adjacency list.
📋 What You'll Learn
Create an adjacency list to represent the graph
Add a helper variable to track visited cities
Implement a depth-first search (DFS) function to detect cycles
Print whether the graph contains a cycle or not
💡 Why This Matters
🌍 Real World
Cycle detection helps in network design, detecting deadlocks, and ensuring no circular dependencies in systems.
💼 Career
Understanding graph traversal and cycle detection is important for software engineers working with networks, databases, and complex data relationships.
Progress0 / 4 steps
1
Create the graph adjacency list
Create a variable called graph as a map[int][]int with these exact edges: 0 connected to 1 and 2, 1 connected to 0 and 2, 2 connected to 0 and 1, 3 connected to 4, and 4 connected to 3.
DSA Go
Hint
Use a map where keys are city numbers and values are slices of connected cities.
2
Add a visited map to track visited cities
Add a variable called visited as a map[int]bool initialized as empty to keep track of visited cities.
DSA Go
Hint
Use a map[int]bool to mark cities as visited or not.
3
Implement DFS function to detect cycles
Write a function called dfs with parameters node int, parent int, graph map[int][]int, and visited map[int]bool. It returns a bool. Use DFS to check if a cycle exists starting from node. Return true if a cycle is found, otherwise false. Mark node as visited. For each neighbor, if not visited, recursively call dfs with neighbor and current node as parent. If neighbor is visited and not parent, return true for cycle.
DSA Go
Hint
Use recursion to explore neighbors. If you find a visited neighbor that is not the parent, a cycle exists.
4
Check all nodes and print if cycle exists
In main, use a for loop with variable node over graph keys. If node is not visited, call dfs(node, -1, graph, visited). If it returns true, print "Cycle detected" and return. After the loop, print "No cycle detected".
DSA Go
Hint
Check each city. If DFS finds a cycle, print and stop. Otherwise, print no cycle.
Practice
(1/5)
1. What is the main idea behind detecting a cycle in an undirected graph using DFS?
easy
A. If we visit a node that is already visited and is not the parent, a cycle exists.
B. If all nodes are visited exactly once, there is a cycle.
C. If the graph has more edges than nodes, it always has a cycle.
D. If the graph is disconnected, it contains a cycle.
Solution
Step 1: Understand DFS traversal in undirected graph
DFS visits nodes and keeps track of the parent node to avoid false cycle detection.
Step 2: Detect cycle condition
If a visited node is found again and it is not the parent, it means a cycle exists.
Final Answer:
If we visit a node that is already visited and is not the parent, a cycle exists. -> Option A
Quick Check:
Cycle detection in undirected graph = visit visited node not parent [OK]
Hint: Check visited nodes excluding parent during DFS [OK]
Common Mistakes:
Ignoring parent node when checking visited nodes
Assuming disconnected graph always has cycles
Confusing cycle detection with number of edges
2. Which of the following Go code snippets correctly initializes an adjacency list for an undirected graph with 3 nodes?