📖 Scenario: Imagine you are working with a network of friends where each friendship is a connection between two people. You want to find out if there is any circle of friends where you can start from one person and come back to the same person by following friendships without repeating any connection.
🎯 Goal: You will build a program to detect if there is a cycle (a closed loop) in an undirected graph representing friendships.
📋 What You'll Learn
Create an adjacency list to represent the undirected graph
Use a helper variable to track visited nodes
Implement a depth-first search (DFS) function to detect cycles
Print true if a cycle exists, otherwise false
💡 Why This Matters
🌍 Real World
Cycle detection helps find loops in networks like social connections, computer networks, or road maps to avoid repeated paths or deadlocks.
💼 Career
Understanding cycle detection is important for software engineers working on graph algorithms, network analysis, and debugging complex data structures.
Progress0 / 4 steps
1
Create the graph as an adjacency list
Create a constant called graph as an object representing the undirected graph with these exact edges: 0 connected to 1 and 2, 1 connected to 0 and 2, 2 connected to 0, 1, and 3, 3 connected to 2.
DSA Javascript
Hint
Use an object where keys are node numbers and values are arrays of connected nodes.
2
Create a visited set to track visited nodes
Create a constant called visited and set it to a new empty Set() to keep track of visited nodes.
DSA Javascript
Hint
Use new Set() to create an empty set for visited nodes.
3
Implement DFS function to detect cycle
Write a function called hasCycle that takes node and parent as parameters. Inside, add node to visited. Then, use a for loop with variable neighbor to iterate over graph[node]. If neighbor is not in visited, recursively call hasCycle(neighbor, node) and return true if it returns true. If neighbor is in visited and neighbor is not equal to parent, return true. After the loop, return false.
DSA Javascript
Hint
Use recursion and track the parent node to detect cycles in an undirected graph.
4
Check all nodes and print if cycle exists
Create a variable called cycleExists and set it to false. Use a for loop with variable node to iterate over Object.keys(graph). Inside the loop, convert node to a number. If visited does not have node, call hasCycle(node, -1) and if it returns true, set cycleExists to true and break the loop. Finally, print cycleExists.
DSA Javascript
Hint
Check each node to find cycles and print the result.
Practice
(1/5)
1. What is the main idea behind detecting a cycle in an undirected graph using DFS?
easy
A. If we find a node with no neighbors, a cycle exists.
B. If we visit a node that is already visited and is not the parent, a cycle exists.
C. If the graph has more edges than nodes, it always has a cycle.
D. If all nodes are visited exactly once, a cycle exists.
Solution
Step 1: Understand DFS traversal in undirected graph
DFS visits nodes and tracks the parent node to avoid false cycle detection.
Step 2: Detect cycle condition
If during DFS, a visited node is found that 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 B
Quick Check:
Cycle detection in undirected graph = check visited node not parent [OK]
Hint: Check visited neighbors excluding parent to find cycles [OK]
Common Mistakes:
Ignoring the parent node in cycle check
Assuming more edges than nodes always means cycle
Confusing cycle detection logic with directed graphs
2. Which of the following is the correct way to represent an undirected graph adjacency list in JavaScript?