3.7.17 · D3 · HinglishAlgorithm Paradigms

Worked examplesBacktracking problems — N-Queens, Sudoku solver, all permutations - subsets

3,400 words15 min read↑ Read in English

3.7.17 · D3 · Coding › Algorithm Paradigms › Backtracking problems — N-Queens, Sudoku solver, all permuta

Yeh page ek "no surprises" drill hai. Parent ne tumhe chaar templates diye the; yahan hum unhe har tarah ke input pe run karte hain taaki interviewer chahe kuch bhi throw kare — ek tiny array, ek empty array, ek board jiska koi solution nahi, ya "list ki jagah count karo" wala twist — uska shape tumne pehle hi dekha hua ho.

Neeche sab kuch plain recursion hai jo partial choices ke ek tree pe DFS kar rahi hai, aur "un-choose" step shared state ko restore karta hai. Agar koi word unfamiliar lage, to woh word parent mein build kiya gaya tha — ek baar wahan jhaanko, phir wapas aao.


Is page pe hum jo names use karte hain

Kisi bhi example se pehle, aao un muthi bhar variables ko fix kar lein jo har jagah appear hote hain, taaki kuch bhi assumed na ho:

Neeche har worked example exactly inhi names ko reuse karta hai. Jab bhi tum res.append(path[:]) dekho, socho: "ek finished path growing res mein copy ho gayi."


Scenario matrix

Backtracking problems kuch axes pe vary karti hain. Examples karne se pehle, aao har cell list kar lein jahan ek question land kar sakta hai, taaki koi bhi example random na lage.

Cell Axis Kya badalta hai Example jo cover karta hai
A. Empty input size n = 0 — degenerate Ex 1
B. Singleton size n = 1 — sabse chhota non-trivial Ex 2
C. Exponential output branching subsets, leaves Ex 3
D. Factorial output branching permutations, leaves Ex 4
E. Heavy pruning, all solutions pruning N-Queens, har answer collect karo Ex 5
F. No solution exists feasibility woh board jahan recursion har jagah dead-end ho Ex 6
G. First solution only short-circuit stack ke upar True return karo Ex 7
H. Count, don't list output twist ek integer return karo, koi path[:] copy nahi Ex 8
I. Duplicates in input constraint duplicate branches ko skip karna zaroori Ex 9
J. Real-world word problem modelling ek story ko backtracking ke roop mein frame karo Ex 10

Aao us matrix ke diagonal pe chalo, ek figure per geometric case. Figure 1 neeche matrix ko ek grid ke roop mein draw karta hai: columns teen output shapes hain (list all / first only / count), rows chaar input shapes hain, aur amber cells dikhate hain ki kaun sa example kahan land karta hai — ise page ke map ki tarah padho.

Figure — Backtracking problems — N-Queens, Sudoku solver, all permutations - subsets

Example 1 — Empty input (Cell A)


Example 2 — Singleton (Cell B)


Example 3 — Exponential output (Cell C)

Figure — Backtracking problems — N-Queens, Sudoku solver, all permutations - subsets

Example 4 — Factorial output (Cell D)


Example 5 — Heavy pruning, all solutions (Cell E)

Figure — Backtracking problems — N-Queens, Sudoku solver, all permutations - subsets

Example 6 — No solution exists (Cell F)


Example 7 — First solution only (Cell G)


Example 8 — Count, don't list (Cell H)


Example 9 — Duplicates in input (Cell I)


Example 10 — Real-world word problem (Cell J)

Recall Har example kaun sa cell tha?

Empty input ::: Ex 1 Singleton ::: Ex 2 Exponential subsets ::: Ex 3 Factorial permutations ::: Ex 4 All-solutions pruning ::: Ex 5 No solution exists ::: Ex 6 First-solution short-circuit ::: Ex 7 Count not list ::: Ex 8 Duplicates ::: Ex 9 Word problem ::: Ex 10


Yeh bhi dekho: kyun $2^n$ aur $n!$ blow up karte hain, Branch-and-Bound (bounds ke saath pruning), aur Dynamic Programming (jab subproblems overlap karte hain).