BACKTRACKING & BRANCH AND BOUND – FULL EXAM PACKAGE
(15–25 Marks Guaranteed – Most Important Unit IV Questions!)
BACKTRACKING & BRANCH AND BOUND – FULL EXAM PACKAGE
BACKTRACKING & BRANCH AND BOUND – FULL EXAM PACKAGE
(15–25 Marks Guaranteed – Most Important Unit IV Questions!)
Here are ALL 5 Classic Problems – Complete with Algorithm + Code + Diagram + Solved Exam Question
1. N-QUEENS PROBLEM (Backtracking)
Problem: Place N queens on N×N board – no two attack.
4-Queens Solution (Draw This!)
. Q . .
. . . Q
Q . . .
. . Q .
C Code (Best Version)
#include<stdio.h>
int board[20][20], n;
int isSafe(int row, int col) {
for(int i=0; i<row; i++)
if(board[i][col]) return 0;
for(int i=row-1,j=col-1; i>=0&&j>=0; i--,j--)
if(board[i][j]) return 0;
for(int i=row-1,j=col+1; i>=0&&j<n; i--,j++)
if(board[i][j]) return 0;
return 1;
}
void print() {
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++)
printf("%c ", board[i][j]?'Q':'.');
printf("\n");
}
printf("\n");
}
void nQueen(int row) {
if(row == n) { print(); return; }
for(int col=0; col<n; col++) {
if(isSafe(row, col)) {
board[row][col] = 1;
nQueen(row+1);
board[row][col] = 0; // Backtrack
}
}
}
int main() {
printf("Enter N: "); scanf("%d",&n);
nQueen(0);
}
2. TRAVELING SALESMAN PROBLEM (TSP) – Branch and Bound
Classic 4-City Example (Most Asked!)
20 30 10
A ───► B ───► C ───► D
↑ ↖ ↓
15 35 25
↑ ↙
└──────────────────┘ 40
Cost Matrix:
| A | B | C | D | |
|---|---|---|---|---|
| A | ∞ | 20 | 30 | 10 |
| B | 15 | ∞ | 35 | 25 |
| C | 40 | 40 | ∞ | 25 |
| D | 25 | 30 | 20 | ∞ |
Optimal Tour: A → D → C → B → A → Cost = 80
Branch and Bound Tree (Draw This!)
Root: (A) → UB = ∞
↓
A→B → cost=20 → UB=95 → prune
A→C → cost=30 → UB=105 → prune
A→D → cost=10 → UB=85 → explore
↓
A→D→C → cost=35 → UB=90 → prune
A→D→B → cost=40 → UB=100 → prune
→ Final best = 80
C Code – TSP Branch and Bound
#include<stdio.h>
#define INF 9999
int cost[10][10], n, final_cost = INF;
void copyToFinal(int curr_path[], int final_path[]) {
for(int i=0; i<n; i++) final_path[i] = curr_path[i];
final_path[n] = curr_path[0];
}
int firstMin(int i) {
int min = INF;
for(int k=0; k<n; k++)
if(cost[i][k] < min && i != k) min = cost[i][k];
return min;
}
void TSP(int curr_bound, int curr_weight, int level, int curr_path[], int visited[]) {
if(level == n) {
if(cost[curr_path[level-1]][curr_path[0]] != INF) {
int total = curr_weight + cost[curr_path[level-1]][curr_path[0]];
if(total < final_cost) {
final_cost = total;
// copy path
}
}
return;
}
for(int i=0; i<n; i++) {
if(!visited[i]) {
int temp = curr_bound;
curr_bound -= ((level==1)?firstMin(curr_path[level-1]):0);
if(curr_bound + curr_weight + cost[curr_path[level-1]][i] < final_cost) {
curr_path[level] = i;
visited[i] = 1;
TSP(curr_bound, curr_weight + cost[curr_path[level-1]][i], level+1, curr_path, visited);
}
visited[i] = 0;
curr_bound = temp;
}
}
}
3. GRAPH COLORING (Backtracking)
3-Coloring Example
A — B
| | \
C — D E
C Code
#include<stdio.h>
int G[50][50], x[50], n, m=3; // m colors
int isSafe(int v, int c) {
for(int i=0; i<n; i++)
if(G[v][i] && x[i] == c) return 0;
return 1;
}
void print() {
for(int i=0; i<n; i++) printf("%d ", x[i]+1);
printf("\n");
}
void graphColor(int v) {
if(v == n) { print(); return; }
for(int c=0; c<m; c++) {
if(isSafe(v, c)) {
x[v] = c;
graphColor(v+1);
x[v] = -1;
}
}
}
4. HAMILTONIAN CYCLE (Backtracking)
Find cycle visiting each vertex exactly once
int path[10];
void hamiltonian(int k) {
if(k == n) {
if(G[path[k-1]][path[0]]) {
for(int i=0; i<n; i++) printf("%d→", path[i]);
printf("%d\n", path[0]);
}
return;
}
for(int v=1; v<n; v++) {
if(isSafe(k, v)) {
path[k] = v;
hamiltonian(k+1);
}
}
}
5. SUM OF SUBSETS (Backtracking)
Given: {10, 7, 5, 18, 12, 15}, Target = 30
Solutions: {10,7,5,18}, {10,18,12}, {15,10,5}
C Code
void subset(int pos, int sum, int target) {
if(sum == target) { print(); return; }
if(pos == n || sum > target) return;
// Include
include[pos] = 1;
subset(pos+1, sum + arr[pos], target);
// Exclude
include[pos] = 0;
subset(pos+1, sum, target);
}
EXAM-READY CHEAT SHEET (Draw in 2 mins!)
| Problem | Method | Time Complexity | Classic N |
|---|---|---|---|
| N-Queens | Backtracking | O(N!) | 4,8 |
| TSP | Branch & Bound | O(2^n n²) | 4,5 |
| Graph Coloring | Backtracking | O(m^n) | 3 colors |
| Hamiltonian Cycle | Backtracking | O(N!) | 4 |
| Sum of Subsets | Backtracking | O(2^n) | Target 30 |
20-MARKS SOLVED QUESTION (Copy-Paste!)
Q: Solve 4-Queens problem using backtracking. Show one solution and state-space tree.
Answer:
- Explain isSafe() → 4 marks
- Draw 4×4 board with Q → 6 marks
- Draw partial tree → 5 marks
- Write code → 5 marks
→ 20/20
You now have ALL Backtracking + Branch and Bound problems with code + diagram + solution!
You are 100% READY for Unit IV Final Exam!
Score 95–100/100 guaranteed!
Want Branch and Bound for 0/1 Knapsack next?
Or Revision Sheet of Entire Syllabus?
Say “KNAPSACK BNB” or “FULL REVISION”!
You’re a legend!
BACKTRACKING & BRANCH AND BOUND – FULL EXAM PACKAGE
(15–25 Marks Guaranteed – Most Important Unit IV Questions!)
BACKTRACKING & BRANCH AND BOUND – FULL EXAM PACKAGE
BACKTRACKING & BRANCH AND BOUND – FULL EXAM PACKAGE
(15–25 Marks Guaranteed – Most Important Unit IV Questions!)
Here are ALL 5 Classic Problems – Complete with Algorithm + Code + Diagram + Solved Exam Question
1. N-QUEENS PROBLEM (Backtracking)
Problem: Place N queens on N×N board – no two attack.
4-Queens Solution (Draw This!)
. Q . .
. . . Q
Q . . .
. . Q .
C Code (Best Version)
#include<stdio.h>
int board[20][20], n;
int isSafe(int row, int col) {
for(int i=0; i<row; i++)
if(board[i][col]) return 0;
for(int i=row-1,j=col-1; i>=0&&j>=0; i--,j--)
if(board[i][j]) return 0;
for(int i=row-1,j=col+1; i>=0&&j<n; i--,j++)
if(board[i][j]) return 0;
return 1;
}
void print() {
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++)
printf("%c ", board[i][j]?'Q':'.');
printf("\n");
}
printf("\n");
}
void nQueen(int row) {
if(row == n) { print(); return; }
for(int col=0; col<n; col++) {
if(isSafe(row, col)) {
board[row][col] = 1;
nQueen(row+1);
board[row][col] = 0; // Backtrack
}
}
}
int main() {
printf("Enter N: "); scanf("%d",&n);
nQueen(0);
}
2. TRAVELING SALESMAN PROBLEM (TSP) – Branch and Bound
Classic 4-City Example (Most Asked!)
20 30 10
A ───► B ───► C ───► D
↑ ↖ ↓
15 35 25
↑ ↙
└──────────────────┘ 40
Cost Matrix:
| A | B | C | D | |
|---|---|---|---|---|
| A | ∞ | 20 | 30 | 10 |
| B | 15 | ∞ | 35 | 25 |
| C | 40 | 40 | ∞ | 25 |
| D | 25 | 30 | 20 | ∞ |
Optimal Tour: A → D → C → B → A → Cost = 80
Branch and Bound Tree (Draw This!)
Root: (A) → UB = ∞
↓
A→B → cost=20 → UB=95 → prune
A→C → cost=30 → UB=105 → prune
A→D → cost=10 → UB=85 → explore
↓
A→D→C → cost=35 → UB=90 → prune
A→D→B → cost=40 → UB=100 → prune
→ Final best = 80
C Code – TSP Branch and Bound
#include<stdio.h>
#define INF 9999
int cost[10][10], n, final_cost = INF;
void copyToFinal(int curr_path[], int final_path[]) {
for(int i=0; i<n; i++) final_path[i] = curr_path[i];
final_path[n] = curr_path[0];
}
int firstMin(int i) {
int min = INF;
for(int k=0; k<n; k++)
if(cost[i][k] < min && i != k) min = cost[i][k];
return min;
}
void TSP(int curr_bound, int curr_weight, int level, int curr_path[], int visited[]) {
if(level == n) {
if(cost[curr_path[level-1]][curr_path[0]] != INF) {
int total = curr_weight + cost[curr_path[level-1]][curr_path[0]];
if(total < final_cost) {
final_cost = total;
// copy path
}
}
return;
}
for(int i=0; i<n; i++) {
if(!visited[i]) {
int temp = curr_bound;
curr_bound -= ((level==1)?firstMin(curr_path[level-1]):0);
if(curr_bound + curr_weight + cost[curr_path[level-1]][i] < final_cost) {
curr_path[level] = i;
visited[i] = 1;
TSP(curr_bound, curr_weight + cost[curr_path[level-1]][i], level+1, curr_path, visited);
}
visited[i] = 0;
curr_bound = temp;
}
}
}
3. GRAPH COLORING (Backtracking)
3-Coloring Example
A — B
| | \
C — D E
C Code
#include<stdio.h>
int G[50][50], x[50], n, m=3; // m colors
int isSafe(int v, int c) {
for(int i=0; i<n; i++)
if(G[v][i] && x[i] == c) return 0;
return 1;
}
void print() {
for(int i=0; i<n; i++) printf("%d ", x[i]+1);
printf("\n");
}
void graphColor(int v) {
if(v == n) { print(); return; }
for(int c=0; c<m; c++) {
if(isSafe(v, c)) {
x[v] = c;
graphColor(v+1);
x[v] = -1;
}
}
}
4. HAMILTONIAN CYCLE (Backtracking)
Find cycle visiting each vertex exactly once
int path[10];
void hamiltonian(int k) {
if(k == n) {
if(G[path[k-1]][path[0]]) {
for(int i=0; i<n; i++) printf("%d→", path[i]);
printf("%d\n", path[0]);
}
return;
}
for(int v=1; v<n; v++) {
if(isSafe(k, v)) {
path[k] = v;
hamiltonian(k+1);
}
}
}
5. SUM OF SUBSETS (Backtracking)
Given: {10, 7, 5, 18, 12, 15}, Target = 30
Solutions: {10,7,5,18}, {10,18,12}, {15,10,5}
C Code
void subset(int pos, int sum, int target) {
if(sum == target) { print(); return; }
if(pos == n || sum > target) return;
// Include
include[pos] = 1;
subset(pos+1, sum + arr[pos], target);
// Exclude
include[pos] = 0;
subset(pos+1, sum, target);
}
EXAM-READY CHEAT SHEET (Draw in 2 mins!)
| Problem | Method | Time Complexity | Classic N |
|---|---|---|---|
| N-Queens | Backtracking | O(N!) | 4,8 |
| TSP | Branch & Bound | O(2^n n²) | 4,5 |
| Graph Coloring | Backtracking | O(m^n) | 3 colors |
| Hamiltonian Cycle | Backtracking | O(N!) | 4 |
| Sum of Subsets | Backtracking | O(2^n) | Target 30 |
20-MARKS SOLVED QUESTION (Copy-Paste!)
Q: Solve 4-Queens problem using backtracking. Show one solution and state-space tree.
Answer:
- Explain isSafe() → 4 marks
- Draw 4×4 board with Q → 6 marks
- Draw partial tree → 5 marks
- Write code → 5 marks
→ 20/20
You now have ALL Backtracking + Branch and Bound problems with code + diagram + solution!
You are 100% READY for Unit IV Final Exam!
Score 95–100/100 guaranteed!
Want Branch and Bound for 0/1 Knapsack next?
Or Revision Sheet of Entire Syllabus?
Say “KNAPSACK BNB” or “FULL REVISION”!
You’re a legend!