Skip to content

Commit 0f693cf

Browse files
authored
Update backtracking.md
1 parent 9b98aa5 commit 0f693cf

File tree

1 file changed

+38
-46
lines changed

1 file changed

+38
-46
lines changed

notes/backtracking.md

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Backtracking is a systematic method for solving problems that incrementally buil
66

77
Recursive functions are functions that call themselves directly or indirectly to solve a problem by breaking it down into smaller, more manageable subproblems. This concept is fundamental in computer science and mathematics, as it allows for elegant solutions to complex problems through repeated application of a simple process.
88

9-
### Key Concepts:
9+
Main idea:
1010

1111
1. **Base Case (Termination Condition)** is the condition under which the recursion stops. It prevents infinite recursion by providing an explicit solution for the simplest instance of the problem.
1212
2. **Recursive Case** is the part of the function where it calls itself with a modified parameter, moving towards the base case.
@@ -40,7 +40,7 @@ $$ n! =
4040
\end{cases}
4141
$$
4242

43-
#### Python Implementation:
43+
Python Implementation:
4444

4545
```python
4646
def factorial(n):
@@ -97,32 +97,29 @@ factorial(5)
9797

9898
The leaves represent the base case, and the tree unwinds as each recursive call returns.
9999

100-
### Important Considerations:
100+
**Important Considerations:**
101101

102-
- **Termination:** Ensure that all recursive paths eventually reach a base case.
103-
- **Stack Depth:** Each recursive call adds a new frame to the call stack. Deep recursion can lead to stack overflow.
104-
- **Efficiency:** Recursive solutions can be elegant but may not always be the most efficient in terms of time and space.
102+
- When using recursion, ensure **termination** by designing the recursive function such that all possible paths eventually reach a base case. This prevents infinite recursion.
103+
- Be mindful of **stack depth**, as each recursive call adds a new frame to the call stack. Too many recursive calls, especially in deep recursion, can result in a stack overflow error.
104+
- Consider **efficiency** when choosing a recursive approach. While recursive solutions can be elegant and clean, they may not always be optimal in terms of time and space, particularly when dealing with large input sizes or deep recursive calls.
105105

106106
## Depth-First Search (DFS)
107107

108108
Depth-First Search is an algorithm for traversing or searching tree or graph data structures. It starts at a selected node and explores as far as possible along each branch before backtracking.
109109

110-
### Key Concepts:
110+
Main idea:
111111

112-
- **Traversal Strategy:** DFS explores a branch to its deepest point before moving to another branch.
113-
- **Implementation:** Can be implemented using recursion or an explicit stack data structure.
114-
- **Applications:** Used in topological sorting, finding connected components, solving puzzles, and more.
112+
- The **traversal strategy** of Depth-First Search (DFS) involves exploring each branch of a graph or tree to its deepest point before backtracking to explore other branches.
113+
- **Implementation** of DFS can be achieved either through recursion, which implicitly uses the call stack, or by using an explicit stack data structure to manage the nodes.
114+
- **Applications** of DFS include tasks such as topological sorting, identifying connected components in a graph, solving puzzles like mazes, and finding paths in trees or graphs.
115115

116116
### Algorithm Steps:
117117

118-
1. **Start at the Root Node:**
119-
- Mark the node as visited.
120-
2. **Explore Each Branch:**
121-
- For each unvisited neighbor, recursively perform DFS.
122-
3. **Backtrack:**
123-
- When all neighbors are visited, return to the previous node.
118+
- **Start at the root node** by marking it as visited to prevent revisiting it during the traversal.
119+
- **Explore each branch** by recursively performing DFS on each unvisited neighbor, diving deeper into the graph or tree structure.
120+
- **Backtrack** once all neighbors of a node are visited, returning to the previous node to continue exploring other branches.
124121

125-
### Pseudocode:
122+
Pseudocode:
126123

127124
```pseudo
128125
DFS(node):
@@ -146,18 +143,15 @@ Consider the following tree:
146143

147144
Traversal using DFS starting from node 'A':
148145

149-
1. Visit 'A'.
150-
2. Move to 'B'.
151-
- 'B' has no unvisited neighbors; backtrack to 'A'.
152-
3. Move to 'C'.
153-
4. Move to 'D'.
154-
- 'D' has no unvisited neighbors; backtrack to 'C'.
155-
5. Move to 'E'.
156-
- 'E' has no unvisited neighbors; backtrack to 'C', then 'A'.
146+
- **Visit 'A'** to begin the traversal, marking it as visited.
147+
- **Move to 'B'**, but since 'B' has no unvisited neighbors, **backtrack to 'A'** to explore other branches.
148+
- **Move to 'C'**, continuing the traversal to the next unvisited node.
149+
- **Move to 'D'**, but as 'D' has no unvisited neighbors, **backtrack to 'C'**.
150+
- **Move to 'E'**, but since 'E' also has no unvisited neighbors, **backtrack to 'C'**, and then further **backtrack to 'A'** to complete the exploration.
157151

158152
Traversal order: A → B → C → D → E
159153

160-
### Implementation in Python:
154+
Implementation in Python:
161155

162156
```python
163157
class Node:
@@ -188,17 +182,17 @@ node_c.children = [node_d, node_e]
188182
dfs(node_a)
189183
```
190184

191-
### Analysis:
185+
Analysis:
192186

193-
- **Time Complexity:** $O(V + E)$, where $V$ is the number of vertices and $E$ is the number of edges.
194-
- **Space Complexity:** $O(V)$, due to the recursion stack and the visited flag.
187+
- The **time complexity** of Depth-First Search (DFS) is $O(V + E)$, where $V$ represents the number of vertices and $E$ represents the number of edges in the graph.
188+
- The **space complexity** is $O(V)$, primarily due to the space used by the recursion stack or an explicit stack, as well as the memory required for tracking visited nodes.
195189

196-
### Applications:
190+
### Applications
197191

198-
- **Cycle Detection in Graphs**
199-
- **Topological Sorting**
200-
- **Solving Mazes and Puzzles**
201-
- **Connected Components in Networks**
192+
- **Cycle detection** in directed and undirected graphs.
193+
- **Topological sorting** in directed acyclic graphs (DAGs).
194+
- **Solving mazes and puzzles** by exploring all possible paths.
195+
- Identifying **connected components** in a network or graph.
202196

203197
## Backtracking
204198

@@ -409,28 +403,26 @@ Q . . .
409403

410404
The algorithm explores the solution space as a tree, where each node represents a partial solution (queens placed up to a certain row). The branches represent the possible positions for the next queen.
411405

412-
- **Nodes:** Partial solutions with queens placed in certain rows.
413-
- **Branches:** Possible positions for the next queen in the next row.
414-
- **Leaves:** Complete solutions when $N$ queens are placed.
406+
- **Nodes** represent partial solutions where a certain number of queens have already been placed in specific rows.
407+
- **Branches** correspond to the possible positions for placing the next queen in the following row, exploring each valid option.
408+
- **Leaves** are the complete solutions when all $N$ queens have been successfully placed on the board without conflicts.
415409

416410
The backtracking occurs when a node has no valid branches (no safe positions in the next row), prompting the algorithm to return to the previous node and try other options.
417411

418412
#### Analysis:
419413

420-
**Time Complexity:** $O(N!)$
421-
422-
- The algorithm explores permutations of queen placements.
414+
I. The **time complexity** of the N-Queens problem is $O(N!)$ as the algorithm explores permutations of queen placements across rows.
423415

424-
**Space Complexity:** $O(N)$
416+
II. The **space complexity** is $O(N)$, where:
425417

426-
- The `board` array stores $N$ positions.
427-
- The recursion stack can go up to $N$ levels deep.
418+
- The `board` array stores the positions of the $N$ queens.
419+
- The recursion stack can go as deep as $N$ levels during the backtracking process.
428420

429421
#### Applications:
430422

431-
- **Constraint Satisfaction Problems:** The N-Queens problem is a classic example used to study algorithms in this domain.
432-
- **Algorithm Design:** Understanding backtracking and recursive problem-solving techniques.
433-
- **Artificial Intelligence:** Search algorithms and optimization.
423+
- **Constraint satisfaction problems** often use the N-Queens problem as a classic example to study and develop solutions for placing constraints on variable assignments.
424+
- In **algorithm design**, the N-Queens problem helps illustrate the principles of backtracking and recursive problem-solving.
425+
- In **artificial intelligence**, it serves as a foundational example for search algorithms and optimization techniques.
434426

435427
#### Potential Improvements:
436428

0 commit comments

Comments
 (0)