Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions DFS/README.md
Original file line number Diff line number Diff line change
@@ -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`.
40 changes: 40 additions & 0 deletions DFS/word_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution {
public:
bool dfs(vector<vector<char>>& 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<vector<char>>& 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;
}
};