DFS goes as deep as possible before going back.
A graph is just a collection of nodes (also called vertices) and connections between them (called edges). Example:
Graphs can represent:
DFS (Depth-First Search) is a way to explore a graph by going as deep as possible down one path before backtracking.
Think of it like exploring a maze:
Imagine each node has neighbors. DFS will:
It goes deep, not wide.
This is different from BFS (Breadth-First Search), which explores layer by layer.
Use DFS when: ✅ You want to explore deep paths first ✅ You need to detect cycles ✅ You need to check if nodes are connected ✅ You’re searching possible solutions (like puzzles)
link: https://leetcode.com/problems/keys-and-rooms/
var canVisitAllRooms = function(rooms) {const visited = new Set();function dfs(room) {if (visited.has(room)) return;visited.add(room);for (const key of rooms[room]) {dfs(key);}}dfs(0);return visited.size === rooms.length;};