Module IV

Backtracking & Maximum Flow Networks

Master backtracking techniques (N-Queens, Sum of Subsets, Graph Coloring, Hamiltonian Cycles) and flow network algorithms (Ford-Fulkerson, Augmenting Paths).

1. Introduction to Backtracking

Backtracking
Backtracking is an algorithmic technique that builds a solution incrementally, abandoning solutions ("backtracks") as soon as it determines that the current path cannot lead to a valid solution. It's essentially a refined brute-force approach.
🎯
Exam Tip (100% Frequency): N-Queen Problem appears in EVERY exam paper! You must know the algorithm, be able to draw the state-space tree for N=4, and show the two solutions. Sum of Subsets with M=17, W={2,7,8,9,15} also appears frequently with the EXACT same values across multiple papers.

1.1 Key Concepts

1
State Space Tree
A tree representing all possible states (configurations) of the problem. Root is initial state, leaves are complete configurations.
2
Promising Function
A function that determines if the current partial solution can potentially lead to a complete solution. Returns false if constraints are violated.
3
Pruning
Cutting off branches of the state-space tree that cannot lead to valid solutions. This is what makes backtracking more efficient than brute force.

1.2 General Backtracking Template

Pseudocode
function BACKTRACK(state):
    if IS_SOLUTION(state):
        PROCESS_SOLUTION(state)
        return

    for each choice in GET_CHOICES(state):
        if IS_PROMISING(state, choice):
            MAKE_CHOICE(state, choice)
            BACKTRACK(state)        // Recurse
            UNDO_CHOICE(state, choice)  // Backtrack

2. N-Queens Problem

N-Queens Problem
Place N queens on an N×N chessboard such that no two queens attack each other. Queens attack horizontally, vertically, and diagonally.

2.1 Constraints

  • Row constraint: Only one queen per row
  • Column constraint: Only one queen per column
  • Diagonal constraint: No two queens on the same diagonal

2.2 Promising Function

For placing queen in row k at column j, check if it conflicts with queens in rows 1 to k-1:

Pseudocode
function IS_PROMISING(k, col[], j):
    for i = 1 to k-1:
        // Check column conflict
        if col[i] == j:
            return false

        // Check diagonal conflict
        // |row difference| == |column difference| means same diagonal
        if |i - k| == |col[i] - j|:
            return false

    return true

2.3 N-Queens Algorithm

Pseudocode
function N_QUEENS(k, n, col[]):
    // k = current row, n = board size, col[i] = column of queen in row i

    if k > n:
        // All queens placed successfully
        PRINT_SOLUTION(col)
        return

    for j = 1 to n:
        if IS_PROMISING(k, col, j):
            col[k] = j          // Place queen in row k, column j
            N_QUEENS(k+1, n, col)  // Try to place in next row
            // Backtracking happens automatically on return
Solved Example: 4-Queens (100% Exam Frequency) 10 Marks
Give the algorithm to solve the N-Queen Problem using backtracking. Give any 2 solutions for the 4-Queen Problem.
Solution

State Space Tree for 4-Queens

State Space Tree (Partial)
                                    Root (place Q in row 1)
                        /          |          |          \
                    col=1       col=2       col=3       col=4
                      |           |           |           |
                  Row 2:      Row 2:      Row 2:      Row 2:
                 /|\          /|\         /|\         /|\
            (✗col 1,2)    (✗col 1,2,3) (✗col 2,3,4) (✗col 3,4)
               col=3,4      col=4         col=1       col=1,2

Legend: ✗ = pruned (conflict), continues where promising

Path to Solution 1: Row1=col2 → Row2=col4 → Row3=col1 → Row4=col3
Path to Solution 2: Row1=col3 → Row2=col1 → Row3=col4 → Row4=col2

Solution 1: col[] = {2, 4, 1, 3}

4-Queens Solution 1

Queens at: (1,2), (2,4), (3,1), (4,3)

Solution 2: col[] = {3, 1, 4, 2}

4-Queens Solution 2

Queens at: (1,3), (2,1), (3,4), (4,2)

💡
The 4-Queens problem has exactly 2 distinct solutions (which are mirror images of each other).

2.4 Time Complexity

Aspect Complexity
Worst Case (no pruning) O(n!)
With effective pruning Much better in practice
Space O(n) for recursion stack + O(n) for col array

3. Sum of Subsets Problem

Sum of Subsets Problem
Given a set W = {w₁, w₂, ..., wₙ} of n positive integers and a target sum M, find all subsets of W whose elements sum to exactly M.

3.1 Promising Function

A node is promising if:

  1. Current sum + remaining elements ≥ M (can still reach M)
  2. Current sum + next element ≤ M (won't exceed M)
Pseudocode
function SUM_OF_SUBSETS(i, weight, total):
    // i = current index, weight = current sum, total = sum of remaining elements
    // Global: W[] = weights, n = size, M = target sum, x[] = solution vector

    if IS_PROMISING(i, weight, total):
        if weight == M:
            PRINT_SOLUTION(x)
        else:
            // Include W[i+1]
            x[i+1] = 1
            SUM_OF_SUBSETS(i+1, weight + W[i+1], total - W[i+1])

            // Exclude W[i+1]
            x[i+1] = 0
            SUM_OF_SUBSETS(i+1, weight, total - W[i+1])

function IS_PROMISING(i, weight, total):
    return (weight + total >= M) AND
           (weight == M OR weight + W[i+1] <= M)
Solved Example (67% Frequency - SAME in 3 papers!) 10 Marks
Write and explain sum of subset algorithm for n=5, W={2, 7, 8, 9, 15}, M=17
Solution

Given: W = {2, 7, 8, 9, 15}, M = 17, Total = 41

State Space Tree
                                        (0, 0, 41)
                                    Include 2? / \ Exclude 2?
                                             /   \
                                    (1,2,39)     (1,0,39)
                                   / \           / \
                           Include 7  Excl 7   Inc 7  Excl 7
                               /      \         /      \
                        (2,9,32)  (2,2,32)  (2,7,32)  (2,0,32)
                          /  \      /  \      /  \      / \
                      Inc 8  Exc  Inc 8 Exc Inc 8 Exc Inc 8 Exc
                      (17!✓) ...  (10) ...  (15) ...  (8)  ...
                                    ↓         ↓
                              (3,10,24)   (3,15,24)
                                 ↓           ↓
                            Inc 9          Inc 9
                            (19>M)✗      (24>M)✗
                            Exc 9          Exc 9
                            (3,10,15)    (3,15,15)
                            ↓              ↓
                         Inc 15        Inc 15
                         (25>M)✗      (30>M)✗
                         Exc 15        Exc 15
                         (10≠M)✗      (15≠M)✗

SOLUTION PATH 1: Include 2, Include 7, Include 8
Weight: 2+7+8 = 17 ✓
x = {1, 1, 1, 0, 0}

Continue exploring other branches...

SOLUTION PATH 2: Include 8, Include 9
Need to trace: Exclude 2, Exclude 7, Include 8, Include 9
Weight: 8+9 = 17 ✓
x = {0, 0, 1, 1, 0}

Two Solutions Found:

1
Solution 1: {2, 7, 8}
x = (1, 1, 1, 0, 0)
Sum = 2 + 7 + 8 = 17 ✓
2
Solution 2: {8, 9}
x = (0, 0, 1, 1, 0)
Sum = 8 + 9 = 17 ✓

Pruning Examples:

  • At (2,9,32): Include 8 gives sum=17=M → SOLUTION!
  • At (3,19,15): 19 > 17, prune this branch
  • At some nodes: If sum + remaining < M, prune (can't reach M)

3.2 Time Complexity

Aspect Complexity
Worst Case O(2ⁿ) - binary tree of height n
With pruning Much better in practice
Space O(n)

4. Graph Coloring Problem

m-Coloring Problem
Given an undirected graph G = (V, E) and m colors, determine if the graph can be colored with at most m colors such that no two adjacent vertices have the same color.

4.1 Algorithm

Pseudocode
function M_COLORING(k, color[], n, m, graph):
    // k = current vertex, m = number of colors available
    // color[i] = color assigned to vertex i

    if k > n:
        PRINT_SOLUTION(color)
        return true

    for c = 1 to m:
        if IS_SAFE(k, c, color, graph):
            color[k] = c
            if M_COLORING(k+1, color, n, m, graph):
                return true
            color[k] = 0  // Backtrack

    return false

function IS_SAFE(k, c, color[], graph):
    for each neighbor v of k:
        if color[v] == c:
            return false
    return true
📝 Example: 3-Coloring a Graph
Graph to Color with 3 Colors
Graph:              Solution (R=Red, G=Green, B=Blue):
    1---2               1(R)---2(G)
    |\ /|               |    \ / |
    | X |       →       |     X  |
    |/ \|               |   / \  |
    3---4               3(G)---4(B)

Assignment: color[] = {R, G, G, B}
- Vertex 1: Red (no conflicts)
- Vertex 2: Green (adjacent to 1-Red, OK)
- Vertex 3: Green (adjacent to 1-Red, 4-Blue, OK; NOT adjacent to 2)
- Vertex 4: Blue (adjacent to 2-Green, 3-Green, OK)

4.2 Chromatic Number

The chromatic number χ(G) is the minimum number of colors needed to color graph G. Finding the chromatic number is NP-Hard.

4.3 Time Complexity

O(mⁿ) in worst case - trying m colors for each of n vertices

5. Hamiltonian Cycles

Hamiltonian Cycle
A Hamiltonian cycle is a path in a graph that visits every vertex exactly once and returns to the starting vertex. A graph containing a Hamiltonian cycle is called a Hamiltonian graph.

5.1 Algorithm

Pseudocode
function HAMILTONIAN_CYCLE(k, path[], visited[], n, graph):
    if k == n:
        // Check if last vertex connects back to first
        if graph[path[k-1]][path[0]] == 1:
            PRINT_CYCLE(path)
            return true
        return false

    for v = 1 to n:
        if IS_SAFE(v, k, path, visited, graph):
            path[k] = v
            visited[v] = true

            if HAMILTONIAN_CYCLE(k+1, path, visited, n, graph):
                return true

            // Backtrack
            visited[v] = false

    return false

function IS_SAFE(v, k, path[], visited[], graph):
    // Check if v is adjacent to previous vertex
    if graph[path[k-1]][v] == 0:
        return false

    // Check if v is already visited
    if visited[v]:
        return false

    return true
📝 Example: Finding Hamiltonian Cycle
Graph with Hamiltonian Cycle
    Graph:                      Hamiltonian Cycle:
    1 --- 2                     1 → 2 → 3 → 5 → 4 → 1
    |   / |
    |  /  |                     Path visits each vertex once
    | /   |                     and returns to start
    4 --- 3 --- 5

    Adjacency Matrix:
        1  2  3  4  5
    1 [ 0  1  0  1  0 ]
    2 [ 1  0  1  1  0 ]
    3 [ 0  1  0  1  1 ]
    4 [ 1  1  1  0  0 ]
    5 [ 0  0  1  0  0 ]

5.2 Hamiltonian Path vs Cycle

  • Hamiltonian Path: Visits every vertex exactly once (no return)
  • Hamiltonian Cycle: Visits every vertex exactly once AND returns to start

5.3 Time Complexity

O(n!) - factorial time in worst case (NP-Complete problem)

6. Introduction to Flow Networks

Flow Network
A flow network G = (V, E) is a directed graph where each edge (u, v) has a non-negative capacity c(u, v). It has two special vertices: source (s) and sink (t). Flow must satisfy capacity constraints and flow conservation.

6.1 Key Concepts

1
Capacity Constraint
For all edges: 0 ≤ f(u,v) ≤ c(u,v)
Flow cannot exceed edge capacity.
2
Flow Conservation
For all vertices except s and t: Flow in = Flow out
Σf(u,v) = Σf(v,w) for all v ∈ V - {s, t}
3
Maximum Flow
The maximum amount of flow that can pass from source to sink.
Flow Network Example
              10
        A ───────► B
       ↗           │
      8            │ 5
     ↗             │
    S              ▼
     ↘            D ────► T
      10         ↗       ↗
       ↘        8      10
        ► C ──►
              15

Edge notation: capacity (e.g., S→A has capacity 8)
Flow value shown as flow/capacity (e.g., 5/10)

6.2 Residual Network

Residual Network
Given a flow network G and flow f, the residual network Gf consists of edges that can admit more flow:
• Forward edge: capacity = c(u,v) - f(u,v) (remaining capacity)
• Backward edge: capacity = f(u,v) (can reduce flow)

6.3 Augmenting Path

Augmenting Path
A path from source s to sink t in the residual network Gf. The bottleneck (minimum residual capacity on the path) determines how much flow can be added along this path.

7. Ford-Fulkerson Method

Ford-Fulkerson Method
The Ford-Fulkerson method finds maximum flow by repeatedly finding augmenting paths in the residual network and pushing flow along them until no more augmenting paths exist.

7.1 Algorithm

📋 Ford-Fulkerson Algorithm
  1. Initialize flow f(u,v) = 0 for all edges
  2. While there exists an augmenting path p from s to t in Gf:
    • Find bottleneck capacity b = min{cf(u,v) : (u,v) in p}
    • Augment flow along path p by b
    • Update residual network Gf
  3. Return total flow
Pseudocode
function FORD_FULKERSON(G, s, t):
    Initialize flow f to 0 for all edges
    max_flow = 0

    while EXISTS_PATH(s, t) in residual network Gf:
        // Find augmenting path using BFS/DFS
        path = FIND_AUGMENTING_PATH(Gf, s, t)

        // Find bottleneck (minimum residual capacity on path)
        bottleneck = MIN(cf(u,v) for all (u,v) in path)

        // Augment flow along path
        for each edge (u,v) in path:
            if (u,v) is forward edge:
                f(u,v) += bottleneck
            else:  // backward edge
                f(v,u) -= bottleneck

        max_flow += bottleneck

    return max_flow
Solved Example 10 Marks
Find maximum flow using Ford-Fulkerson method.
Solution
Initial Network
                 10
           A ────────► B
          ↗            │
        10             │ 10
       ↗               │
      S                ▼
       ↘              T
        10            ↗
         ↘          10
          ► C ────►
              10

Iteration 1:

  • Path: S → A → B → T
  • Bottleneck: min(10, 10, 10) = 10
  • Push flow: 10
  • Total flow: 10

Iteration 2:

  • Path: S → C → T
  • Bottleneck: min(10, 10) = 10
  • Push flow: 10
  • Total flow: 20

Iteration 3: No more augmenting paths (edges S→A and S→C are saturated)

Maximum Flow = 20

7.2 Edmonds-Karp Algorithm

Edmonds-Karp is Ford-Fulkerson using BFS to find augmenting paths. This guarantees finding the shortest augmenting path each time.

7.3 Time Complexity

Algorithm Time Complexity
Ford-Fulkerson (general) O(E × max_flow)
Edmonds-Karp (BFS) O(V × E²)

8. Applications of Flow Networks

8.1 Bipartite Matching

Find maximum matching between two sets (e.g., jobs to workers). Model as flow network: source connects to one set, sink to other, all capacities = 1.

8.2 Network Connectivity

Find minimum cut (minimum edges to remove to disconnect s from t). By Max-Flow Min-Cut theorem: max flow = min cut.

8.3 Real-World Applications

  • Network routing: Maximize data flow through networks
  • Transportation: Optimize shipping routes
  • Project scheduling: Resource allocation
  • Image segmentation: Computer vision applications
  • Sports scheduling: Determining playoff possibilities
💡
Max-Flow Min-Cut Theorem: The maximum flow from source to sink equals the minimum capacity of any cut that separates source from sink.

9. Practice Questions

Question 1 (100% Frequency) 10 Marks
Explain Backtracking with N-Queen problem. Show working for N=4.
View Answer

See Section 2 for complete solution with state-space tree and both solutions.

Solutions: {2,4,1,3} and {3,1,4,2}

Question 2 (67% Frequency) 10 Marks
Write sum of subset algorithm for n=5, W={2,7,8,9,15}, M=17
View Answer

See Section 3 for complete solution with state-space tree.

Solutions: {2,7,8} and {8,9}

Question 3 5 Marks
Explain how Graph Coloring problem can be solved with backtracking.
View Answer

See Section 4. Key points:

  • Try coloring vertices one by one
  • For each vertex, try all m colors
  • Check if adjacent vertices have different colors
  • Backtrack if no valid color found
Question 4 10 Marks
Explain Ford-Fulkerson algorithm for finding maximum flow.
View Answer

See Section 7 for algorithm and example.

Key steps:

  1. Initialize all flows to 0
  2. Find augmenting path (BFS/DFS)
  3. Find bottleneck on path
  4. Augment flow by bottleneck
  5. Repeat until no augmenting path

Key Points Summary

📌 Remember for Exam
  • Backtracking: Build solution incrementally, prune invalid branches
  • N-Queens: 4-Queens has exactly 2 solutions: {2,4,1,3} and {3,1,4,2}
  • N-Queens promising: Check column and diagonal conflicts
  • Sum of Subsets {2,7,8,9,15}, M=17: Solutions are {2,7,8} and {8,9}
  • Graph Coloring: Try colors 1 to m, check adjacent vertices
  • Hamiltonian: Visit all vertices exactly once, return to start
  • Flow Network: Capacity constraint + Flow conservation
  • Augmenting Path: Path from s to t in residual network
  • Ford-Fulkerson: Find augmenting paths, push bottleneck flow
  • Max-Flow = Min-Cut (theorem)