design and analysis of algorithms back tracking algorithms n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Design and Analysis of Algorithms Back Tracking Algorithms PowerPoint Presentation
Download Presentation
Design and Analysis of Algorithms Back Tracking Algorithms

Loading in 2 Seconds...

play fullscreen
1 / 45

Design and Analysis of Algorithms Back Tracking Algorithms - PowerPoint PPT Presentation


  • 204 Views
  • Uploaded on

Design and Analysis of Algorithms Back Tracking Algorithms. Chuhua Xian Email: chhxian@scut.edu.cn School of Computer Science and Engineering. Course Information. Website http://222.201.132.37/algorithms/ Textbook Algorithms Design Techniques and Analysis.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Design and Analysis of Algorithms Back Tracking Algorithms' - mufutau-kramer


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
design and analysis of algorithms back tracking algorithms

Design and Analysis of AlgorithmsBack Tracking Algorithms

Chuhua Xian

Email: chhxian@scut.edu.cn

School of Computer Science and Engineering

course information
Course Information
  • Website
    • http://222.201.132.37/algorithms/
  • Textbook
    • Algorithms Design Techniques and Analysis.

(Saudi Arabia) M. H. Alsuwaiyel.

Publishing House of Electronic Industry.

two good references
Two good references
  • The following two books are very good references, containing many materials that we don’t have time to cover.

Introduction to Algorithms, 3rd ed, T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, MIT Press, 2009.

Algorithm Design, J. Kleinberg and E. Tardos, Addison Wesley, 2005.

slide4

Design and Analysis of Algorithms

Back-Tracking Algorithms

Topics:

  • General Method
  • N-Queen problem
back tracking paradigm
Back-Tracking Paradigm
  • A design technique, like divide-and-conquer.
  • Useful for optimization problems and finding feasible solutions.
    • Solution to a problem is defined as an n-tuple: (x1, x2, …, xn), where xi is taken from a finite set Si . Generally, we need to:
      • finding a vector which maximize (or minimize) a specific objective function P(x1, x2, …, xn).
      • finding a (or all) vector(s) that satisfy a specific criterion function P(x1, x2, …, xn).
back tracking general method
Back-Tracking——General Method
  • Constraints
    • Explicit Constraints
      • constrain values of each component xi;
      • All tuples that satisfy explicit constraints make up a possible solution space.
    • Implicit Constraints:
      • inter-components constraints;
      • Implicit constraints identify those satisfying the criterion function in the solution space.
8 queen problem

1 2 3 4 5 6 7 8

1

2

3

4

5

6

7

8

8-Queen problem
  • Place 8 queens on a 8×8 chessboard, yielding that any two of them do not conflict, i.e. any two of them do not reside in the same row, column or diagonal.
8 queen problem1

1 2 3 4 5 6 7 8

1

2

3

4

5

6

7

8

8-Queen problem
  • Label the rows and columns by 1 to 8, and the queens too.
  • Assume that queen i resides in row i.
  • Solutions can be defined as a 8-tuple (x1, x2, …, x8),where xiis the column number of queen i.
  • The solution above is (4,6,8,2,7,1,3,5).
8 queen problem2

1 2 3 4 5 6 7 8

1

2

3

4

5

6

7

8

8-Queen problem
  • Explicit Constraints: xi Si,Si={1,2,3,4,5,6,7,8}, 1≤ i≤ 8. The solution space consists of 88 8-tuples.
  • Implicit Constraints: any two of them do not reside in the same row, column( any two ofxi differ ) or diagonal.
8 queen problem9

1 2 3 4 5 6 7 8

1

2

3

4

5

6

7

8

8-Queen problem

Q

Q

Q

Back-Tracking Paradigm finds answers by systematicsearching the solution space of a given problem.

state space tree

x1 = 4

x1 = 1

x1 = 2

x1 = 3

x2 = 1

4

1

4

1

4

1

4

2

3

2

3

2

3

2

3

x3 = 1

4

1

4

x3 = 1

4

2

3

2

3

2

3

………………………

x4 = 1

x4 = 1

4

4

…………………………………………………………

2

2

3

3

State Space Tree

The solution space consists of all the paths from the root to each leaf, e.g.(4,2,3,1)。

state space tree1

x1 = 4

x1 = 1

x1 = 2

x1 = 3

x2 = 1

4

1

4

1

4

1

4

2

3

2

3

2

3

2

3

x3 = 1

4

1

4

x3 = 1

4

2

3

2

3

2

3

………………………

x4 = 1

x4 = 1

4

4

…………………………………………………………

2

2

3

3

State Space Tree
  • Problem State
    • Each node in the tree identifies a problem state when solving the problem.
state space tree2

x1 = 4

x1 = 1

x1 = 2

x1 = 3

x2 = 1

4

1

4

1

4

1

4

2

3

2

3

2

3

2

3

x3 = 1

4

1

4

x3 = 1

4

2

3

2

3

2

3

………………………

x4 = 1

x4 = 1

4

4

…………………………………………………………

2

2

3

3

State Space Tree
  • State Space
    • All the paths from the root to each node
state space tree3

x1 = 4

x1 = 1

x1 = 2

x1 = 3

x2 = 1

4

1

4

1

4

1

4

2

3

2

3

2

3

2

3

x3 = 1

4

1

4

x3 = 1

4

2

3

2

3

2

3

………………………

x4 = 1

x4 = 1

4

4

…………………………………………………………

2

2

3

3

State Space Tree
  • State Space Tree
    • The above tree of the solution space
state space tree4

x1 = 4

x1 = 1

x1 = 2

x1 = 3

x2 = 1

4

1

4

1

4

1

4

2

3

2

3

2

3

2

3

x3 = 1

4

1

4

x3 = 1

4

2

3

2

3

2

3

………………………

x4 = 1

x4 = 1

4

4

…………………………………………………………

2

2

3

3

State Space Tree
  • Solution States
    • Those problem states to which the path from the root identifies a vector in the solution space (Satisfying explicit constraints).

only leaves in this problem.

state space tree5

x1 = 4

x1 = 1

x1 = 2

x1 = 3

x2 = 1

4

1

4

1

4

1

4

2

3

2

3

2

3

2

3

x3 = 1

4

1

4

x3 = 1

4

2

3

2

3

2

3

………………………

x4 = 1

x4 = 1

4

4

…………………………………………………………

2

2

3

3

State Space Tree
  • Answer States
    • Those solution states to which the path from the root identifies an answer (Satisfying implicit constraints).
state space tree6

x1 = 4

x1 = 1

x1 = 2

x1 = 3

x2 = 1

4

1

4

1

4

1

4

2

3

2

3

2

3

2

3

x3 = 1

4

1

4

x3 = 1

4

2

3

2

3

2

3

………………………

x4 = 1

x4 = 1

4

4

…………………………………………………………

2

2

3

3

State Space Tree

state space tree

problem state

state space

solution states

answer states

state space tree7

x1 = 4

x1 = 1

x1 = 2

x1 = 3

x2 = 1

4

1

4

1

4

1

4

2

3

2

3

2

3

2

3

x3 = 1

4

1

4

x3 = 1

4

2

3

2

3

2

3

………………………

x4 = 1

x4 = 1

4

4

…………………………………………………………

2

2

3

3

State Space Tree

how did we solve the queen

problem just now ?

DFS, this is the way how Back

Tracking algorithms solve a problem

Begin

Identify state space tree

Yes

All states?

No

Generate problem state

Is solution state?

Is answer state?

End

searching in state space tree
Searching in State Space Tree
  • Basic Concepts
    • Alive node: a generated node whose sons have not all been generated.
    • E-node: the node whose son is generating currently.
    • Dead node: a node that all his sons have been generated or there is no need to generate his sons.
  • Generating problem states in DFS way
    • Once a son C of the E-node R is generated, the generated son C become the new E-node, node R will become E-node again after all the tree rooted by node C is checked.

E-node

Back Tracking Paradigm

E-node

searching in state space tree1
Searching in State Space Tree
  • Basic Concepts
    • Alive node: a generated node whose sons have not all been generated.
    • E-node: the node whose son is generating currently.
    • Dead node: a node that all his sons have been generated or there is no need to generate his sons.
  • Generating problem states in BFS way
    • A E-node remains E-node until it becomes a dead node.

E-node

E-node

Branch & Bound Paradigm

state space tree8

x1 = 4

x1 = 1

x1 = 2

x1 = 3

x2 = 1

4

1

4

1

4

1

4

2

3

2

3

2

3

2

3

x3 = 1

4

1

4

x3 = 1

4

2

3

2

3

2

3

………………………

x4 = 1

x4 = 1

4

4

…………………………………………………………

2

2

3

3

State Space Tree

Begin

Identify state space tree

DFS in back Tracking paradigm

Yes

All states?

No

Generate problem state

Trying to generate all problem states?

Is solution state?

No! using bound functions to kill alive nodes

Is answer state?

End

This is how Back-Tracking varies from Brute-Force

4 queen problem

:dead node

:alive node

: E-node

x1=1

2

x2=1

x2=2

x2=4

x2=3

3

4

5

10

x4=2

x3=2

x3=4

x4=4

x3=2

x3=4

x4=1

x3=1

x3=1

x3=3

x4=3

x3=3

6

13

7

14

8

15

16

9

11

12

17

18

4-Queen problem

1

1

1

2

2

2

2

2

3

4

5

5

10

10

10

10

6

7

8

9

11

12

12

17

18

13

14

15

16

4 queen problem1

:dead node

:alive node

: E-node

x1=2

x1=1

1

1

1

19

2

x2=1

x2=2

x2=1

x2=3

x2=4

x2=4

x2=3

x2=3

2

2

2

2

2

21

3

20

4

5

22

23

10

x3=3

3

4

x4=2

x3=2

x4=4

x3=4

5

5

x3=2

x3=1

10

10

10

10

x3=4

x3=4

x3=1

x4=1

x3=1

x3=2

x4=3

x3=3

x3=3

31

29

30

6

13

7

14

8

15

9

16

11

24

12

17

18

6

7

8

9

11

12

12

17

18

x3=1

x3=4

x4=3

x3=2

25

26

27

28

13

14

15

16

4-Queen problem
general method of back tracking
General Method of Back Tracking
  • Construct solution in X(1:n), an answer is output immediately after it is determined.
  • T(X(1),...,X(k-1)) : returns all possible X(k) ,given X(1),…,X(k-1).
  • B(X(1),…,X(k)): returns whether X(1),…,X(k) satisfies the implicit constraints.

BACKTRACKING(n)

k1;

whilek>0do

if there are unchecked X(k), X(k)∈T(X(1),…,X(k-1)) and B(X(1),…,X(k))=true

thenif (X(1),…,X(k) is an answer)

then print (X(1),…,X(k))

if (k < n)

kk+1

else

kk-1

general method of back tracking1
General Method of Back Tracking

Whether there is any son that has not been generated for the current node.

Bound function is used here.

  • Construct solution in X(1:n), an answer is output immediately after it is determined.
  • T(X(1),...,X(k-1)) : returns all possible X(k) ,given X(1),…,X(k-1).
  • B(X(1),…,X(k)): returns whether X(1),…,X(k) satisfies the implicit constraints.

BACKTRACKING(n)

k1;

whilek>0do

if there are unchecked X(k), X(k)∈T(X(1),…,X(k-1)) and B(X(1),…,X(k))=true

thenif (X(1),…,X(k) is an answer)

then print (X(1),…,X(k))

if (k < n)

kk+1

else

kk-1

general method of back tracking2
General Method of Back Tracking
  • Construct solution in X(1:n), an answer is output immediately after it is determined.
  • T(X(1),...,X(k-1)) : returns all possible X(k) ,given X(1),…,X(k-1).
  • B(X(1),…,X(k)): returns whether X(1),…,X(k) satisfies the implicit constraints.

BACKTRACKING(n)

k1;

whilek>0do

if there are unchecked X(k), X(k)∈T(X(1),…,X(k-1)) and B(X(1),…,X(k))=true

thenif (X(1),…,X(k) is an answer)

then print (X(1),…,X(k))

if (k < n)

kk+1

else

kk-1

If the new generated node is an answer state, print the answer.

general method of back tracking3
General Method of Back Tracking
  • Construct solution in X(1:n), an answer is output immediately after it is determined.
  • T(X(1),...,X(k-1)) : returns all possible X(k) ,given X(1),…,X(k-1).
  • B(X(1),…,X(k)): returns whether X(1),…,X(k) satisfies the implicit constraints.

BACKTRACKING(n)

k1;

whilek>0do

if there are unchecked X(k), X(k)∈T(X(1),…,X(k-1)) and B(X(1),…,X(k))=true

thenif (X(1),…,X(k) is an answer)

then print (X(1),…,X(k))

if (k < n)

kk+1

else

kk-1

DFS: the new generated node becomes the E-node.

general method of back tracking4
General Method of Back Tracking
  • Construct solution in X(1:n), an answer is output immediately after it is determined.
  • T(X(1),...,X(k-1)) : returns all possible X(k) ,given X(1),…,X(k-1).
  • B(X(1),…,X(k)): returns whether X(1),…,X(k) satisfies the implicit constraints.

BACKTRACKING(n)

k1;

whilek>0do

if there are unchecked X(k), X(k)∈T(X(1),…,X(k-1)) and B(X(1),…,X(k))=true

thenif (X(1),…,X(k) is an answer)

then print (X(1),…,X(k))

if (k < n)

kk+1

else

kk-1

All sons have been checked, Tracking back.

back tracking algorithm for n queen problem
Back-Tracking Algorithm for n-Queen problem

NQUEENS(n)

X(1)0;k1

whilek>0do

X(k)X(k)+1

whileX(k) ≤ n and not PLACE(k)do

X(k)X(k)+1

ifX(k) ≤ n

then ifk=n

then print (X)

elsekk+1;X(k)0

else kk-1;

k : current row;

X(k) : column NO of queen k

Finding next valid column NO using bound function PLACE.

Found or not?

Found! Is it an answer?

Print the answer, or shift to the next row and find a place for next queen.

No! Tracking back.

bound function place
Bound function -- PLACE

PLACE(k)

i1

whilei<kdo

ifX(i)=X(k) or |X(i)-X(k)| = |i-k|

then return false

ii+1

return true

4 queen problem2

1

1

1

1

1

1

1

1

1

1

1

1

2

2

2

2

2

2

2

2

2

3

3

3

3

4

k=2

k=1

k=2

k=3

k=2

k=3

k=1

k=3

k=2

k=4

k=3

k=4

4-Queen Problem

NQUEENS(n)

X(1)0;k1

whilek>0do

X(k)X(k)+1

whileX(k) ≤ n and not PLACE(k)do

X(k)X(k)+1

ifX(k) ≤ n

then ifk=n

then print (X);

elsekk+1;X(k)0

else kk-1;

further reading
Further Reading
  • Further reading:
    • O(1) time PLACE.
    • Generate answers for n-queen problem w/o searching.