14 Backtracking

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

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

from that position.

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

• One of the most common


Q
example of Backtracking
Q
• Place N Queens in a chess

board, size N x N Q

• None of the Queens can Q


attack each other
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

• Uses a recursive procedure to implement


backtracking
• Recursive because the later version of the
problem are similar to the original
• Backtracking to try different alternatives at
all levels
N Queens Algorithm

• Uses a Q Array to specify position of the


Queen in a row, initialized to zero
• Index of the array is the row position, value
stored in the column position of Queen
• Example: Q[2] = 3, means in the second
row, the Queen in positioned at column 3
• Reduces internal storage of algorithm to an
N size array, instead of matrix (N x N) size
Algorithm
Input: Chess Board [ N × N]

Output: Q [1..N] for Queen position in each row, initialized to zeroes (o)

Procedure NQueens (k) // Place Queen at row k


Begin
If k = N + 1 // If all N rows have Queens,
PRINT Q [ 1 .. N] // print Q array as Output
Else
Repeat for j = 1 to N step 1 // Check each column
Set Legal := TRUE
Repeat for i = 1 to (k-1) step 1
If (Q[i] = j) OR (Q[i] = j+k–i) OR (Q[i] = j-k+i))
// If new position is in same column or diagonal of
// a queen in previous rows, position not acceptable
Set Legal : = FALSE
Break Loop
End If
End Repeat
If Legal = TRUE
Set Q [k] := j // Queen placed at col j for row k
NQueens (k + 1) // Call Nqueens for (k+1) row
End If
End Repeat
End If // If no cols are free, go back to row (k-1)
End
Execution – 4 Queens

Start with row 1


1 2 3 4
1 Q
Position the Queen in the first Column

2 All positions covered by the Queen are shown in Red

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

2 Q Last position was in column 3, new position at column

4
3

4
Execution – 4 Queens

Now again at Row 3


1 2 3 4
1 Q
Only position available is in column 2

2 Q Place Queen there

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)

Procedure Graph_Color (k) // Algorithm to color vertex no. k


Begin
If k = n + 1 // If all vertices have colors
assigned,
PRINT VC [1 .. n] // print VC array as Output
Else
Repeat while TRUE
Set VC [k] := (VC[k] + 1) mod (m+1) // get next available color
If VC [k] = 0
Return // No color available, go back
End If
Set Legal := TRUE
Repeat for i = 1 to n step 1
If (AdjMat [k, i] = 1 AND VC [k] = VC [i])
// If vertex i is adjacent to k and have the same color,
// this color is not acceptable
Set Legal := FALSE
Break Loop
End If
End Repeat
If Legal = TRUE
Graph_Color (k+1) // Assign color to next
vertex
End If
End Repeat
End If
End
Graph Colour Algorithm
• VC array stores colour number for a Vertex
• VC [2] = 1 indicates vertex number 2 is assigned
colour no 1
• VC array is initialized to 0 at start of procedure
• To get a new colour for vertex k, add 1 to VC[k] (the
existing colour number), apply Modulo to limit the
number to m (number of colours)
• Use AdjMat to check with colours assigned to
adjacent vertices
• If no colour available (VC[k] = 0), procedure
backtracks to previous vertex, assigns a new colour
& resumes
Analysis
• The total number of options available for
colouring n vertices with m colours is (m.nm)
• Thus, the Growth Factor for Graph Colour
Algorithm is O (mnm) in the worst case
• As in N-Queens, the algorithm does not need to
check all options completely, hence in practice
the number of cases evaluated are quite less than
the worst case
The End

You might also like