Unit-IV
By
SARVANI ANANDARAO
Backtracking
Backtracking
Start(live node)
dead end
dead end
dead node
dead node
success node
E-node(as it leads to success node
Live Node
Cont…
2 types of constraints are
Applications
N Queen Problems
Q1 | |
| |
Q2 cannot be placed
Q1 | | |
| | Q2 |
| | |
Q3 cannot be placed
Q1 | | | |
| | Q2 | |
| | | |
| | | |
As q3 cannot be placed in 3rd row. Backtrack to q2 and move the position of q2
Q1 | | | |
| | | Q2 |
| Q3 | | |
| | | |
As q4 cannot be placed in 4th row. Backtrack to q3 and move the position of q3
Q3 cannot be placed in another position, so backtrack to ‘q2’. Even ‘q2’ cannot be placed in another position so, backtrack to ‘q1’. After moving the position Q1, correspondingly Q2,Q3 must be moved.
| Q1 | | |
| | | Q2 |
Q3 | | | |
| | Q4 | |
Solution Set
2 | 4 | 1 | 3 |
The above are Column numbers
3
2
4
1
3
2
4
1
4
3
1
2
4
3
2
1
4
3
2
4
2
3
1
3
2
4
1
4
2
1
2
3
1
3
1
2
3
4
1
4
4
3
3
1
4
2
4
1
1
2
3
2
3
1
2
1
4
2
3
2
4
3
4
1
q1
q2
q3
q4
8 Queen problem in Backtracking
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| | | Q1 | | | | |
| | | | | Q2 | | |
| | | | | | | Q3 |
| Q4 | | | | | | |
| | | | | | Q5 | |
Q6 | | | | | | | |
| | Q7 | | | | | |
| | | | Q8 | | | |
Sum of Subsets
Xk=1
Xk=0
0,1,55
7,2,48(55-7)
18(7+11),3,37
31,4,24
18,4,24
7,3,37
20,4,24
7,4,24
31,5,0
0,2,48
11,3,37
24,4,24
11,4,24
0,3,37
13,4,24
0,4,24
24,5,0
X1=1
X1=0
X2=1
X2=0
X2=1
X2=0
X3=1
X3=0
X3=1
X3=0
X3=1
X3=0
X3=1
X3=0
X4=1
Solution 1
Solution 2
Cont….
Graph Colouring Problem
a
d
c
e
b
Chromatic Number: The Minimum number of colours required to colour all the vertices of the given graph is called chromatic number.
If the degree of the given graph is ‘d’ then it can be coloured with ‘d+1’ colours.
d=2,d+1 = 3 colours
Example
Colours to colour the adjacent graph
A
D
C
B
CONT…
Assignment
1
2
3
4
5
1
4
5
2
3
Hamiltonian Cycle Problem in Backtracking
Example
1
2
3
8
7
6
5
4
1
4
5
2
3
Junction –Orticulation Point
1
4
2
3
6
5
Junction Point
1
4
2
3
5
6
Pendant Vertex
Pendant Vertex
1
6
4
7
2
3
5
D
A
C
E
F
B
Cont…
Hamiltonian cycle are:
END OF UNIT-IV