Professional Documents
Culture Documents
14 Backtracking
14 Backtracking
14 Backtracking
Algorithms
Backtracking
• An strategy that derives the solution of a
problem step-by-step, under a given constraint
• At every step, available choices are matched with
the constraint
• If valid choices are available, then the algorithm
proceeds to the next step till it reaches the final
solution
• If valid choices are not available, the algorithm
goes back (backtracks) to previous step and
makes new choices to try again
State Space Tree
Start
Success
Failure
State Space Tree consists of states (nodes) and choices (paths to new states). At a node, connected nodes show choices available
If no further nodes exist at any stage, system “backtracks” to the previous node. Other available choices are tried out. If no
further choices are available, backtrack to parent of “parent” node and resume the process.
N Queens Problem
board, size N x N Q
Q
N Queens Approach
• Process starts placing a Queen in the first row, at
the first available column
• Based on previous choice, a Queen is placed in
the next row at an appropriate column
• Proceeds to next set of rows and places a Queen
in each row as per columns available
• If a Queen cannot be placed in any row (no
columns are free), goes back to previous row,
changes position of Queen and proceeds again
N Queens Algorithm
Output: Q [1..N] for Queen position in each row, initialized to zeroes (o)
4
Execution – 4 Queens
Now at Row 2
1 2 3 4
1 Q
As columns 1 & 2 are not available, place the Queen in
the column 3
2 Q
Positions covered by both Queens are marked in Red
3
4
Execution – 4 Queens
Now at Row 3
1 2 3 4
1 Q
As all columns in row 3 are covered, Queen cannot be
placed
2 Q
Backtrack to Row 2
3
4
Execution – 4 Queens
Back in Row 2
1 2 3 4
1 Q
Position the Queen in the next available position
4
3
4
Execution – 4 Queens
3 Q
4
Execution – 4 Queens
At Row 4
1 2 3 4
1 Q
No position is free
2 Q
Backtrack to Row 3
3 Q
4
Execution – 4 Queens
Back at Row 3
1 2 3 4
1 Q
No more columns
2 Q
Backtrack to Row 2
4
Execution – 4 Queens
Back at Row 2
1 2 3 4
1 Q
No more columns
2 Backtrack to Row 1
4
Execution – 4 Queens
Back at Row 1
1 2 3 4
1 Q
New Position for Queen is in column 2
4
Execution – 4 Queens
Now at Row 2
1 2 3 4
1 Q
Only position available for Queen is in column 4
2 Q
4
Execution – 4 Queens
Now at Row 3
1 2 3 4
1 Q
Only position available for Queen is in column 1
2 Q
3 Q
4
Execution – 4 Queens
Now at Row 4
1 2 3 4
1 Q
Queen placed at column 3, only position available
2 Q
Success!!
3 Q All 4 Queens placed in 4 rows
4 Q
Execution – 4 Queens
1 2 3 4
1 Q
Another valid solution
2 Q
3 Q
4 Q
Execution – State Space
Q Start
Q Q
Q Q Q
Q
Q
Q
Q
Failure
Q Q
Q Q
Q Success Q
Q
Analysis
• To place N queens in a N x N board, Queen array
allows Queens positions in distinct columns only
• Total number of options for N-Queens is n!
• Growth Factor for N-Queens: O(n!) (Worst Case)
• For 4-Queens, total number of options are 24, but
actual number of valid outcomes are only 2
• As Algorithm does not check all options completely,
actual execution is more efficient that Worst Case
Backtracking Applications
• Sudoku
• Maze Problem
• Minesweeper
• Subset Sum
• Longest Increasing Subsequence
• Optimal Binary Search Trees
• Graph Colour
Graph Colour
• Objective: For a given graph and a set of colours,
assign colours to each vertex
• Constraint: Two adjacent vertices cannot have
the same colour
• Approach: Start assigning colours from first
vertex, go to the next adjacent vertex to assign a
different colour
• If not possible to assign a colour, backtrack to
previous vertex, change colour and start again
Algorithm
Input: Graph G, AdjMat [ n × n], number of colurs: m
Output: VC [1..n] for Colour assigned for each Vertex, VC[] initialized to zeroes (0)