Backtracking
Backtracking एक algorithmic technique है, जिसका use combinatorial problems, constraint satisfaction problems (CSP) और puzzle solving में किया जाता है।यह method recursion पर based होती है, जिसमें solution को step-by-step build किया जाता है।
Table of Contents
ToggleKey Point
- Process partial solution से Start होती है।
- यदि current solution valid है, तो algorithm अगले step की ओर move करता है।
- यदि solution invalid हो, तो algorithm backtrack करता है और पिछले step पर लौटकर दूसरा option try करता है।
Characteristics of Backtracking
(Backtracking की विशेषताएँ)
1. Recursive Nature
- Backtracking problems को recursion की help से solve किया जाता है।
- Algorithm हर step को solve करके next step की ओर बढ़ता है।
- अगर कोई path invalid निकलता है, तो recursive call से वापस लौटकर दूसरा option try किया जाता है।
2. Solution Space Tree
- सभी possible solutions को एक tree structure के रूप में represent किया जाता है।
- Algorithm root node से start होकर branches के through candidate solutions explore करता है।
- Leaf nodes पर पहुँचने पर valid solutions को record किया जाता है।
3. Trial-and-Error Approach
- हर step में candidate solution try किया जाता है।
- Constraints violate होने पर backtrack करके alternative try किया जाता है।
4. Constraint Satisfaction
- वही paths आगे बढ़ते हैं, जो problem constraints satisfy करते हैं।
- Invalid paths जल्दी discard कर दिए जाते हैं।
5. Start with Partial Solutions
- Complete solution बनाने से पहले partial solution तैयार किया जाता है।
- Step-by-step solution construct होता है।
6. Exploration of All Solutions
- कुछ cases में algorithm केवल first valid solution खोजता है।
- कभी, all possible solutions को explore करता है।
7. Backtracking Mechanism
- जब कोई candidate solution आगे extend नहीं हो पाता, तो algorithm previous step पर लौटता है।और alternative path try करता है।
Backtracking Working Steps (कार्यप्रणाली)
Step 1: Start (Initial State)
- Problem को empty या partial solution से start किया जाता है।
Step 2: Select a Choice
- Algorithm सभी possible candidates को एक-एक करके select करता है।
Step 3: Constraint Check
- Selected choice problem constraints को satisfy करती है या नहीं, यह check किया जाता है।
- Conflict होने पर choice discard कर दी जाती है।
Step 4: Recursive Move
- choice valid है, उसे solution में add करके recursive call की जाती है।
Step 5: Backtrack (Dead End)
- जब आगे कोई valid choice नहीं मिलती, तो algorithm previous state पर लौटता है।
- Last added choice को remove किया जाता है।
Step 6: Solution Achieved
- जब सभी steps successfully complete हो जाते हैं, complete solution मिल जाता है।
- Solution को print / store किया जाता है।
Step 7: Find Next Solution
- यदि required हो, algorithm Again backtracking करके next possible solution search करता है।
Example (उदाहरण): 8-Queen Problem
8-Queen Problem एक classic Backtracking problem है, जिसमें 8×8 chessboard पर 8 queens को इस तरह place करना होता है, कि कोई भी queen दूसरी queen को attack न करे।
Queen chess में तीन directions में attack करती है :
- Same row
- Same column
- Same diagonal
Objective (उद्देश्य)
1. Safe Placement करना
- हर queen को ऐसी safe position पर place करना, जहाँ वह किसी दूसरी queen के साथ conflict में न हो।
2. Chess Rules Follow करना
- कोई भी दो queens :
- same row में न हों
- same column में न हों
- same diagonal में न हों
3. Backtracking Technique Apply करना
- Trial-and-error method का उपयोग करके solution step-by-step build किया जाता है।
- Invalid placement मिलने पर algorithm previous step पर लौटता है (backtrack करता है)।
4. All Possible Valid Solutions Find करना
- 8-Queen problem की सभी valid arrangements को systematically find किया जाता है।
5. Constraint Satisfaction समझना
- दिए गए constraints को follow करते हुए correct solution generate किया जाता है।
Why 8-Queen Problem is Important?
(8-Queen Problem क्यों महत्वपूर्ण है?)
1. Backtracking Concept को Clearly Explain करता है
- 8-Queen Problem Backtracking का सबसे classic और popular example है।
- Trial-and-error और backtrack process आसानी से समझ में आता है।
2. Constraint Satisfaction Problem (CSP) का Best Example
- इसमें multiple constraints (row, column, diagonal) होते हैं।
- Algorithm को हर step पर constraints check करने पड़ते हैं।
3. Strong Understanding of Recursion
- Row-by-row queen placement recursion concept को practical रूप में दिखाता है।
4. Algorithm Design Skills Improve करता है
- Solution space tree, pruning और decision making skills develop होती हैं।
5. Base for Other Problems
- Sudoku, Graph Coloring, Knight’s Tour जैसे problems इसी logic पर based होते हैं।
Solution Approach (Backtracking Method)
8-Queen Problem को solve करने के लिए Backtracking Algorithm का उपयोग किया जाता है। इस method में queens को row-by-row chessboard पर place किया जाता है।
Step-by-Step Solution Approach
Step 1: Starting from the first row
- Chessboard की पहली row में queen place की जाती है।
- एक समय पर एक ही queen place की जाती है।
Step 2: Column Selection (Choices Try करना)
- Current row के सभी columns को one-by-one try किया जाता है।
Step 3: Safety Check (isSafe Function)
- हर placement से पहले यह check किया जाता है कि:
- Same column में कोई queen न हो
- Left diagonal पर कोई queen न हो
- Right diagonal पर कोई queen न हो
Step 4: Valid Placement पर आगे बढ़ना
- यदि position safe है, तो queen को place करके next row के लिए recursive call की जाती है।
Step 5: Dead End मिलने पर Backtracking
- अगर किसी row में कोई safe column नहीं मिलता, तो
- Previous row पर वापस जाते हैं
- Last placed queen को remove करते हैं
- Next possible column try करते हैं
Step 6: Complete Solution मिलना
- जब सभी 8 rows में queens safely place हो जाती हैं, तब एक valid solution मिल जाता है।
Step 7: All Solutions Find करना
- Algorithm फिर से backtrack करके अन्य possible arrangements भी find करता है।
Core Logic : Try → Check → Place → Recurse → Fail → Remove → Try Next
Pseudocode
function solveNQueens(row):
if row == N:
print solution
return
for col in 0 to N-1:
if isSafe(row, col):
placeQueen(row, col)
solveNQueens(row + 1)
removeQueen(row, col) // backtrack
Advantages of 8-Queen Problem (लाभ)
1. Backtracking Concept को Strong बनाता है
- 8-Queen Problem Backtracking technique को समझने का सबसे अच्छा example है।
- Trial-and-error और backtrack process clearly समझ में आता है।
2. Constraint Satisfaction समझाता है
- Row, column और diagonal जैसे multiple constraints को handle करना सिखाता है।
3. Recursion की Practical Understanding देता है
- Row-by-row queen placement recursion concept को real problem से जोड़ता है।
4. Algorithm Design Skills Improve करता है
- Solution space tree, decision making और pruning techniques सीखने में मदद करता है।
5. Problem-Solving Ability बढ़ाता है
- Logical thinking, analytical skills और debugging ability develop होती है।
Applications of Backtracking (अनुप्रयोग)
1. N-Queen / 8-Queen Problem
- Chessboard पर queens को इस तरह place किया जाता है, कि कोई queen दूसरी queen को attack न करे।
- Backtrackin g हर row में possible columns try करता है, और conflict मिलने पर previous step लौटता है।
2. Sudoku Solver
- Sudoku grid की empty cells में numbers fill किए जाते हैं।
- Wrong number मिलने पर algorithm backtrack करके दूसरा number try करता है।
3. Graph Coloring Problem
- Graph के vertices को colors assign किए जाते हैं, जिससे adjacent vertices का color same न हो।
- Color conflict होने पर backtracking से previous vertex पर लौटते हैं।
4. Subset Sum Problem
- दिए गए numbers में से ऐसा subset find किया जाता है, जिसका sum target value के बराबर हो।
- Sum exceed होते ही current path discard कर दिया जाता है।
5. Crossword / Word Placement Problem
- Grid में words को given constraints के अनुसार place किया जाता है।
- Word place न होने पर backtracking से दूसरा position try किया जाता है।
6. Permutation and Combination Problems
- Elements की सभी possible arrangements generate की जाती हैं।
- Duplicate या invalid arrangement मिलने पर backtracking apply होती है।
7. Knight’s Tour Problem
- Chessboard पर knight को हर square पर exactly once move कराना होता है।
- Invalid move sequence मिलने पर algorithm previous move पर लौटता है।
Advantages of Backtracking (लाभ)
- Finds All Possible Solutions : Backtracking सभी valid solutions को systematically explore करता है।
- Best for Constraint Satisfaction : जिन problems में rules / constraints होते हैं, वहाँ यह technique बहुत effective होती है।
- Easy Recursive Implementation : Recursion के उपयोग से code structured और logical बनता है।
- Pruning Increases Efficiency : Invalid paths early discard हो जाते हैं, जिससे unnecessary work कम होता है।
- Improves Problem-Solving Skills : Backtracking से Logical thinking, recursion और DFS concept strong होते हैं।
Disadvantages of Backtracking (सीमाएँ)
- High Time Complexity : Worst case में algorithm सभी possible combinations check करता है।
- Large Search Space Problem : Input size बढ़ने पर solution space बहुत बड़ा हो जाता है।
- Not Suitable for Large-Scale Problems : Big data या real-time systems में slow हो सकता है।
- Stack Overflow Risk : Deep recursion की वजह से memory issue हो सकता है।
- Implementation Debugging is Difficult : Recursive calls और backtracking steps trace करना beginners के लिए difficult होता है।





