From 9a128dd4936f4fe445122b906b35694d67b058c7 Mon Sep 17 00:00:00 2001 From: Leanne Jacob Date: Thu, 17 Sep 2026 22:25:36 +0530 Subject: [PATCH] Add DFS implementation with README --- DFS/README.md | 111 ++++++++++++++++++++++++++++++++++++++++++++ DFS/word_search.cpp | 40 ++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 DFS/README.md create mode 100644 DFS/word_search.cpp diff --git a/DFS/README.md b/DFS/README.md new file mode 100644 index 00000000..4c28079d --- /dev/null +++ b/DFS/README.md @@ -0,0 +1,111 @@ +# Depth-First Search (DFS) + +## What is DFS? + +**Depth-First Search (DFS)** is a graph and tree traversal algorithm that explores as far as possible along one path before backtracking and exploring another path. + +DFS can be implemented using: + +* **Recursion** +* **Stack** + +## How DFS Works + +1. Start from a node. +2. Mark the node as visited. +3. Visit one of its unvisited neighbors. +4. Continue going deeper until there are no unvisited neighbors. +5. Backtrack and explore other paths. + +### Example + +For the graph: + +```text + A + / \ + B C + / \ +D E +``` + +A possible DFS traversal is: + +```text +A → B → D → E → C +``` + +## DFS in Word Search + +In the **Word Search** problem, DFS is used to explore neighboring cells to find the given word. + +From each cell, we can move in four directions: + +```text + Up + ↑ +Left ← Cell → Right + ↓ + Down +``` + +A cell is temporarily marked as visited so that it is not used twice in the same path. After exploring the path, the cell is restored. This process is called **backtracking**. + +## Algorithm + +```text +For every cell in the board: + Start DFS + +DFS(row, col, index): + If the complete word is found: + return true + + If the cell is invalid or doesn't match: + return false + + Mark the cell as visited + + Search: + Up + Down + Left + Right + + Restore the cell + + Return whether any direction found the word +``` + +## Complexity + +For a graph with `V` vertices and `E` edges: + +* **Time:** `O(V + E)` +* **Space:** `O(V)` + +For the Word Search problem, the worst-case time complexity is approximately: + +`O(m × n × 4^L)` + +where: + +* `m × n` = board size +* `L` = length of the word + +## Applications + +DFS is commonly used for: + +* Graph and tree traversal +* Finding connected components +* Cycle detection +* Maze solving +* Path finding +* Topological sorting +* Backtracking problems +* Word Search + +## Implementation + +The C++ implementation for Word Search is provided in `word_search.cpp`. diff --git a/DFS/word_search.cpp b/DFS/word_search.cpp new file mode 100644 index 00000000..9d26c6e2 --- /dev/null +++ b/DFS/word_search.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + bool dfs(vector>& board, string word, int i, int j, int k) { + // All characters matched + if (k == word.size()) + return true; + + // Out of bounds or character doesn't match + if (i < 0 || i >= board.size() || + j < 0 || j >= board[0].size() || + board[i][j] != word[k]) + return false; + + // Mark as visited + char temp = board[i][j]; + board[i][j] = '#'; + + // Explore 4 directions + bool found = dfs(board, word, i + 1, j, k + 1) || + dfs(board, word, i - 1, j, k + 1) || + dfs(board, word, i, j + 1, k + 1) || + dfs(board, word, i, j - 1, k + 1); + + // Backtrack + board[i][j] = temp; + + return found; + } + + bool exist(vector>& board, string word) { + for (int i = 0; i < board.size(); i++) { + for (int j = 0; j < board[0].size(); j++) { + if (dfs(board, word, i, j, 0)) + return true; + } + } + + return false; + } +}; \ No newline at end of file