1 of 35

Unit-IV

By

SARVANI ANANDARAO

2 of 35

Backtracking

3 of 35

Backtracking

  • It is used to solve problems in which a sequence of objects is chosen from a specified set so that the sequence satisfy some criterion.

  • When?? - When we have different choices, where sufficient information is not available on the best choice

  • How?? – A Backtracking is a systematic method of trying out various sequence of decisions until you find out that works

4 of 35

Start(live node)

dead end

dead end

dead node

dead node

success node

E-node(as it leads to success node

Live Node

5 of 35

Cont…

  • Live Node: Which can be further generate

  • Enode: The Child node that leads to successor(success node)

  • Deadnode: Which cannot be extended further

6 of 35

2 types of constraints are

  1. Implicit : Rules that satisfy, specify each element in a tuple should be related

  • Explicit: Rules that restrict, each element to be chosen from a given set

7 of 35

Applications

  1. N queen problem
  2. Sum of subsets
  3. Graph Colouring
  4. Hamiltonian cycle

8 of 35

N Queen Problems

  • Consider NxN cheese board. ‘N’ queens must be places such that ‘No’ two queens can attack. Should not be placed on same row,same column and same diagonal
  • N = 2

  • N = 3

Q1

Q2 cannot be placed

Q1

Q2

Q3 cannot be placed

9 of 35

  • N= 4

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

10 of 35

Solution Set

2

4

1

3

The above are Column numbers

11 of 35

  • State Space Tree

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

12 of 35

8 Queen problem in Backtracking

  •  

13 of 35

  •  

1

2

3

4

5

6

7

8

Q1

Q2

Q3

Q4

Q5

Q6

Q7

Q8

 

14 of 35

Sum of Subsets

  • Among set of ‘n’ integers, we select subset of integers whose sum is ‘m’
  • {1,2,3,4,5,6,7} ; m=10

  • Solution1 = {1,2,3,4} = 10
  • Solution2 = {4,6}=10
  • Solution3 = {3,7} = 10

15 of 35

  •  

Xk=1

Xk=0

 

16 of 35

  • Example: {7,11,13,24}, m=31

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

17 of 35

Cont….

  •  

18 of 35

Graph Colouring Problem

  • Let G=(V,E) be a graph. In graph colouring problem, we have to find out whether all the vertices of the given graph are coloured or not, with the constraint that no two adjacent vertices have the same colour

a

d

c

e

b

19 of 35

  • The Problem has two versions:
  • m – Colourability Decision Problem
  • m- Colourability Optimization Problem

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

20 of 35

Example

  • degree = 2,
  • degree+1 = 2+1 = 3
  • So we need minimum ‘3’

Colours to colour the adjacent graph

A

D

C

B

21 of 35

22 of 35

CONT…

  • State Space Tree : For a 4 node graph with 3 colours, there are 18 solutions[Number of leaf nodes]

  • (x1,x2,x3,x4) = (1/2/3,1/2/3,1/2/3,1/2/3)

  • Time Complexity = ⃝(n.mn)

23 of 35

Assignment

1

2

3

4

5

1

4

5

2

3

24 of 35

  • Cons 1: All the vertices must be visited
  • Cons2: Every verticex must be visited only once except the starting verticex

25 of 35

Hamiltonian Cycle Problem in Backtracking

  • Let G=(V,E) is a connected graph with ‘V’ vertices. A Hamiltonian cycle is a round trip path along ‘E’ edges of ‘G’ that visits each and every vertex exactly once except the starting and ending vertex and returns to the starting vertex

26 of 35

Example

  • 1-2-8-7-6-5-4-3-1[except starting vertex, we have visited all other vertices exactly once].
  • 4-3-1-2-8-7-6-5-4
  • 3-4-5-6-7-8-2-1-3

1

2

3

8

7

6

5

4

27 of 35

  • This is not Hamiltonian Cycle because it contains junction
  • 1-5-2-4-3-2-1 ------🡪 ‘2’ is visited two times

1

4

5

2

3

Junction –Orticulation Point

28 of 35

  • Not Hamiltonian cycle.
  • 1-2-4-3-6-5-3-1.-----’3’ is visited two times

1

4

2

3

6

5

Junction Point

29 of 35

  • Not Hamiltonian Cycle: 1-2-4-5-4-3-1
  • ‘4’ is visited two times. Here ‘5’ and ‘6’ are pendant vertex. If the graph consists of pendant vertex then it is not Hamiltonian cycle.

1

4

2

3

5

6

Pendant Vertex

Pendant Vertex

30 of 35

  • 1-2-3-7-6-4-1
  • But No visiting of 5 vertex
  • So this is not Hamiltonian Cycle. According to definition all the vertices must be visited.

1

6

4

7

2

3

5

31 of 35

  • A-B-F-E-D-C-A.--- In this path all the vertices are visited and no vertex is repeated. Finally reached the source vertex “A”
  • A-D-E-F-B-C-A--- In this path all the vertices are visited and no vertex is repeated. Finally reached the source vertex “A”

  • B-F-E-D-A-C-B--- In this path all the vertices are visited and no vertex is repeated. Finally reached the source vertex “A”

  • This is Hamiltonian cycle.

D

A

C

E

F

B

32 of 35

33 of 35

  • In the paths
  • [A-B-C-E-D]. F is not present , not visited. So not Hamiltonian cycle
  • [A-B-C-E-F]: D is not visited. So not Hamiltonian cycle
  • [A-B-C-D-E-F]: not a Hamiltonian cycle because after reaching F. There is no direct edge from F to A. To go to ‘A’ we need to again visit ‘B’ vertex. Then vertex ‘B’ will be visited two times.
  • [A-B-E-C-D]: ‘F’ not visited. This is not Hamiltonian cycle
  • [A-B-E-F]: ‘C’,’D’, is not visited. This is not Hamiltonian cycle
  • [A-B-F-E-C-D]: This is Hamiltonian cycle because from ‘D’ we can visit to A
  • [A-C-B-E-D]: ‘F’ is not there. This is not Hamiltonian cycle
  • [A-C-B-E-F] : D is not visited. This is not Hamiltonian cycle
  • [A-C-B-F-E-D]: This is Hamiltonian cycle.

34 of 35

Cont…

Hamiltonian cycle are:

  1. A-B-F-E-C-D-A
  2. A-B-F-E-D-C-A
  3. A-C-B-F-E-D-A
  4. A-C-D-E-F-B-A
  5. A-D-E-F-B-C-A

35 of 35

END OF UNIT-IV