3.7.18 · D4 · HinglishAlgorithm Paradigms

ExercisesBranch and bound

3,603 words16 min read↑ Read in English

3.7.18 · D4 · Coding › Algorithm Paradigms › Branch and bound

Do golden rules ka quick reminder (hum inhe baar baar use karte hain):

  • Minimization: bound ek lower bound hota hai (kisi bhi completion ka under-estimate). Ek node ko tab prune karo jab .
  • Maximization: bound ek upper bound hota hai (ek over-estimate). Ek node ko tab prune karo jab .

Yahan incumbent ka matlab hai abhi tak mili sabse achi complete solution ki value.


Level 1 — Recognition

L1.1

Inme se kaun si problem Branch and Bound ke liye design ki gayi hai: (a) "sabhi valid Sudoku boards print karo", (b) "sabse sasta delivery tour dhundho", (c) "check karo ki ek number prime hai ya nahi"?

Recall Solution

Answer: (b). B&B optimization problems solve karta hai — woh jo bahut saari feasible options mein se best (sabse sasta / sabse valuable) dhundhte hain. (a) ek find-all task hai jo Backtracking ke liye suitable hai; (c) ek decision/computation hai, ek search tree par optimization nahi.

L1.2

Ek maximization problem ke liye, ek node ka upper bound hai aur current incumbent value hai. Kya hum is node ko keep karein ya prune karein?

Recall Solution

Prune. Is subtree ka best possible result hai, jo hai, woh value jo hum already hold karte hain. Rule: max → prune when . ✅ toh poora subtree hopeless hai. Iske andar koi bhi path jo hum already hold karte hain use beat nahi kar sakta.

L1.3

Har live-node strategy ko uske data structure se match karo: FIFO B&B, LIFO B&B, Least-Cost B&B.

Recall Solution
  • FIFO B&B (breadth-first) → queue (sabse purana node pick karo).
  • LIFO B&B (depth-first) → stack (sabse naya node pick karo).
  • Least-Cost (LC) B&Bpriority queue (pehle best bound wala node pick karo).

Level 2 — Application

L2.1

Minimization. Ek node ka lower bound hai; incumbent cost hai. Prune karein ya keep karein?

Recall Solution

Prune. Rule: min → prune when . Yahan true hai. Best case mein bhi yeh subtree current best se tie karega — yeh strictly improve nahi kar sakta, toh hum ise discard karte hain. ( use karna ki jagah ek valid choice hai: ties ko rakhna sirf kaam barbaad karta hai.)

L2.2 (Knapsack root bound)

. Items (value, weight): A(40, 2), B(30, 5), C(50, 10), D(10, 5). Root upper bound compute karo fractional (greedy-density) relaxation use karke. (Upar definition box se yaad karo.)

Recall Solution

Step 1 — densities : A , B , C , D . Order: A, B, C, D. Step 2 — fill capacity :

  • A fully lo: profit , weight used , capacity left .
  • B fully lo: profit , weight used , capacity left .
  • C (weight ) tak pahuncho lekin sirf fit hoga → fraction .

Yeh true integer optimum ka ek over-estimate hai kyunki hum ne item C ko slice karke cheat kiya. Dekho 0-1 Knapsack.

L2.3 (TSP 2-min lower bound)

Cities with symmetric costs: Root lower bound compute karo .

Recall Solution

Neeche ka figure chaar cities ko blue circles ki tarah draw karta hai aur har edge ko uski cost se label karta hai. Green solid edges woh hain jo kisi city ke "two cheapest" ke roop mein chosen hain; gray dashed edges (yahan , cost ) kisi bhi city ke two cheapest mein kabhi nahi hote, toh woh is bound mein contribute nahi karte. Har vertex ke do sabse saste incident edges padhna:

  • City 1: edges → sum
  • City 2: edges → sum
  • City 3: edges → sum
  • City 4: edges → sum

Total . Halve karo (figure mein har edge dono endpoints se count hua tha): Koi bhi real tour kam se kam zaroor cost karega — pruning ke liye ek valid floor.

Figure — Branch and bound

Picture mein green edges "optimistic skeleton" banate hain: agar ek tour sirf har city ke do sabse saste edges se bana hota toh uski cost hoti, lekin ek real tour mehnga edge use karne par majboor hota hai, toh true optimum is floor ke upar hota hai. Gray dashed edge (cost ) notice karo: woh kisi ke cheapest pair mein nahi hai, toh yeh kabhi is bound mein nahi aata — figure exactly dikhata hai ki formula kaun si costs "dekhta" hai aur kaun si nahi.


Level 3 — Analysis

L3.1 (Kaun se nodes prune hote hain)

L2.2 ka knapsack continue karo. Branching ke baad, maan lo algorithm complete solution value ke saath dhundh leta hai (incumbent ). Live queue mein nodes hain bounds ke saath. Kaun se prune hote hain?

Recall Solution

Maximization → prune when .

  • keep.
  • prune.
  • prune.
  • prune (ties incumbent ko strictly beat nahi kar saktein).

Toh teen nodes mar jaate hain, ek survive karta hai. Yahi payoff hai: har prune ek single comparison se pura subtree erase kar deta hai.

L3.2 (Edge force karna TSP bound ko raise karta hai)

L2.3 mein, hum edge (cost ) ko tour mein force karke branch karte hain. City 2 aur city 3 ke contributions recompute karo, phir naya lower bound do. Change ki direction explain karo.

Recall Solution

force karne ka matlab hai ki cities 2 aur 3 dono ko apne do tour edges mein edge cost use karna hi hoga.

  • City 2: include karna zaroori hai; sabse sasta doosra edge () hai. Sum (pehle tha).
  • City 3: include karna zaroori hai; sabse sasta doosra edge () hai. Sum (pehle tha).
  • Cities 1 aur 4 unchanged: aur .

Total ; halve → . Direction: yeh se tak badh gaya. Ek edge force karna freedom remove karta hai, toh sabse sasta legal completion sirf aur mehenga ho sakta hai. Ek tighter (higher) lower bound aur aggressively prune karta hai — exactly wahi jo hum deeper jaate waqt chahte hain.

L3.3 (LC-B&B mein expansion ka order)

Live set mein nodes hain bounds (minimization) ke saath. Least-Cost B&B ke under, kaun sa node aage expand hoga, aur yeh yahan FIFO se zyada smart kyun hai?

Recall Solution

Bound wala node expand karo — sabse chota (best/most-promising) lower bound. LC-B&B hamesha us branch mein dive karta hai jiska optimistic estimate sabse best ho, kyunki woh true optimum contain karne aur jaldi ek strong incumbent produce karne ki sabse zyada possibility rakhta hai. Ek strong incumbent early aane se hum baaki nodes () ko jaldi prune kar sakte hain. FIFO blindly woh expand karta jo pehle enter hua, possibly -bound branch par effort waste karte hue.


Level 4 — Synthesis

L4.1 (Full mini knapsack trace)

. Items (value, weight): X(30, 3), Y(20, 2), Z(24, 4). Maximization B&B run karo: root bound compute karo, X par branch karo (include / exclude), aur winning set ke saath optimal value do. (Symbols upar box mein defined hain.)

Recall Solution

Densities: X , Y , Z . Order X, Y, Z. Tie-break rule: X aur Y ki equal density hai. Jab densities tie hoti hain, fractional relaxation value order se unaffected hoti hai — same total capacity same per-unit value se fill hoti hai chahe hum pehle kaun sa list karein, toh bound identical hota hai. Hum simply ek deterministic rule fix karte hain (e.g. lower item weight first, ya by input order) taaki trace reproducible ho; yahan hum input order ke hisaab se X before Y rakhte hain. Yeh choice sirf yeh affect karta hai ki hum pehle kaun se node par branch karte hain, kabhi correctness ya bound value nahi.

Root bound (capacity fill karo, use karke): X full (); Y full (); Z ko chahiye, fraction . . Incumbent .

Branch on X.

  • Include X (): Y full (); Z frac . .
  • Exclude X (): Y full (); Z full (). (already integer — ek complete candidate!). Incumbent with .

Expand include-X (bound , promising). Branch on Y:

  • Include Y (): Z ko chahiye lekin , fit nahi hoga → Z exclude force karo → complete solution value . Incumbent .
  • Exclude Y (): Z ko chahiye, , fit nahi hoga → complete value , no update.

Baaki saare live nodes ke hain aur prune ho jaate hain. (Brute force se confirm karo: w=5 ✅; w=7 ✗; ; . Max feasible .)

L4.2 (Ek valid bound design karo)

Tumhe jobs ko identical machines par assign karne ka makespan minimize karna hai (total load balanced). Ek partial assignment ke liye ek valid lower bound propose karo aur justify karo ki yeh under-estimate kyun hai.

Recall Solution

= sabhi jobs ka total processing time, aur = heavier machine par already placed current load. job ka processing time hai, toh sabse lamba single job ka time hai. Final makespan par ek valid lower bound hai: Valid kyun hai (under-estimate):

  • Makespan : ek machine already carry karti hai; jobs add karna load kabhi remove nahi karta.
  • Makespan : even ek perfectly balanced split mein ek machine ko kam se kam aadha total milta hai.
  • Makespan : kisi machine ko sabse lamba single job ek uninterrupted chunk mein run karna hi hoga.

Har ek ek floor hai jiske neeche kuch nahi ja sakta, toh floors ka max abhi bhi ek floor hai — ek legitimate lower bound. Yeh ek relaxation hai: hum ek easy, optimistic estimate pane ke liye integrality/assignment constraints ignore karte hain (same trick jaise fractional 0-1 Knapsack bound). Compare karo ki Greedy Algorithms aur Dynamic Programming ise exactly solve karne ke liye kya karte.


Level 5 — Mastery

L5.1 (Complexity truth)

Tumhara teammate claim karta hai: "Humne B&B pruning add ki, toh hamara TSP solver worst case mein ab polynomial hai." Big-O Notation reference karte hue precisely rebut karo.

Recall Solution

False. Pruning tabhi kaam remove karta hai jab ek node ka bound prove kare ki woh hopeless hai. Worst case mein, kuch bhi prune nahi hota — incumbent kabhi itna achha nahi hota itni jaldi, aur B&B (asymptotically) poora tree expand kar leta hai. TSP ke liye us tree mein leaves hote hain; knapsack ke liye . Toh worst-case complexity unchanged hai — abhi bhi exponential. B&B average/practical running time ko pruning ke zariye improve karta hai, worst-case big-O ko nahi. Yeh ek smarter exhaustive search hai, kabhi polynomial-time trick nahi. (Agar yeh TSP ke liye guaranteed polynomial method hota, toh P vs NP resolve ho jaata — ek hint ki claim sahi nahi ho sakta.)

L5.2 (Invalid-bound diagnosis)

Ek student ka knapsack solver kuch inputs par true optimum se choti value return karta hai. Bounding step use karta hai: "bound (sabse zyada valuable remaining item ki value jo abhi bhi fit hoti hai)." Bug diagnose karo aur prove karo ki yeh wrong answer cause kar sakta hai.

Recall Solution

Bug: yeh "bound" valid upper bound nahi hai. Sirf ek remaining item add karna true best completion ko under-estimate kar sakta hai, kyunki ek real integer completion kai remaining items pack kar sakti hai jinki combined value kisi bhi single item se kaafi zyada ho. Ek valid maximization bound har completion ka over-estimate hona chahiye; yeh quantity kuch completions se neeche ho sakti hai, toh yeh definition fail karti hai. Galat answer kyun aata hai: kyunki "bound" bahut chota ho sakta hai, test us node par fire ho sakta hai jiske real completions actually incumbent ko beat karte hain. Woh node — aur uska poora subtree, jisme true optimum ho sakta hai — wrongly prune ho jaata hai, toh algorithm true max se neeche value return karta hai. Yeh exactly reported symptom se match karta hai. Concrete counterexample (proof ki yeh under-estimate karta hai): maan lo kisi node par hai aur remaining capacity hai, remaining items P(10, 2) aur Q(10, 2) ke saath.

  • True best completion: dono P aur Q lo (total weight ) → completion value , toh node ki real reachable value hai.
  • Buggy bound: .

Kyunki , bound genuinely ek integer completion se strictly neeche hai — yeh upper bound nahi hai. Agar incumbent, hota, toh buggy test is node ko prune kar deta even though yeh tak pahunchta hai. Optimum kho jaata hai. ∎ Fix: 0-1 Knapsack ka fractional relaxation bound use karo — density-sorted greedy fill jisme overflow item ka fraction shamil ho — jo provably har integer completion hota hai. Counterexample mein yeh deta hai (dono exactly fit hote hain), correctly , toh node prune nahi hota. Ek valid bound optimistic hona chahiye: maximization ke liye over-estimate.

L5.3 (B&B ko ek naye objective ke liye adapt karo)

Tumhe woh tour dhundhna hai jo maximum total edge cost rakhta ho (TSP ka ek "most scenic route" variant). Explain karo ki B&B machinery exactly kaise flip hoti hai: kis type ka bound, prune test, aur bound kaise build karein.

Recall Solution

Ab objective maximization hai, toh:

  • Bound type: ek upper bound (optimistic = ek completion sabse zyada cost tak pahunch sakti hai).
  • Prune test: ek node discard karo jab (incumbent = abhi tak ka best complete scenic tour).
  • Bound build karna: 2-min idea mirror karo lekin har city ke do sabse mehenga incident edges lo aur halve karo: Yeh valid over-estimate kyun hai: har real tour mein har city ke exactly do incident edges hote hain (ek in, ek out). Kisi city ke liye, woh do edges uske do sabse mehenga incident edges se zyada cost nahi kar sakti — tum available sabse mehenga se bhi mehenga edge nahi pick kar sakte. Woh per-city maximum ko sabhi cities par sum karna har tour edge ko do baar count karta hai (ek baar har endpoint par), toh halving ek aise quantity ko recover karta hai jo kisi bhi real tour ki cost se hai. Isliye ek legitimate ceiling hai: koi completion ise exceed nahi kar sakti, exactly woh optimism jo ek maximization bound ko chahiye. Tour mein ek edge force karna is bound ko sirf lower kar sakta hai (woh us city ke pricier edge choose karne ki freedom remove karta hai), descend karte waqt pruning tight hoti hai. Core loop A* Search-style best-first expansion ke identical hai — sirf inequalities ki direction aur "best" edge choice flip hoti hai. Travelling Salesman Problem ke standard minimizing version se compare karo.

Recall Ek-line self-test

Woh single question jo upar ki har problem unlock karta hai ::: "Kya mera bound ek proven best-case hai (max ke liye over-estimate, min ke liye under-estimate), aur kya mein sirf tabhi prune kar raha hoon jab woh best-case jo mein already hold karta hoon use beat nahi kar sakta?"