backtracking & brute force optimization intro2cs – weeks 12-13 1

33
Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

Upload: allan-john-perkins

Post on 18-Jan-2018

216 views

Category:

Documents


0 download

DESCRIPTION

Sudoku 3

TRANSCRIPT

Page 1: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

1

Backtracking & Brute Force Optimization

Intro2CS – weeks 12-13

Page 2: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

2

Backtracking

• A general way (using recursion) to find a solution to a problem.

• Given a partial solution, we have several options in extending it.

• Try one option, and if that doesn’t work, backtrack (undo the change) and try another

• Often used for “constraint satisfaction problems”

Page 3: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

3

Sudoku

Page 4: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

4

Sudoku Solving

Page 5: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

5

Page 6: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

6

Check the column

Check the row

Check the quadrant

Page 7: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

7

Page 8: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

8

Page 9: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

9

Questions

• Can you estimate the number of boards we need to explore?

• Provide a bound?

• How many of these will yield legal Sudoku puzzles?

Page 10: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

10

Number of valid Sudoku boards

• There are around legal Sudoku boards. This was estimated via search.

• Notice that a board can be relabeled to obtain a different legal board.

• Some permutations on the columns / rows also yield new boards.

Page 11: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

11

The 8 Queen Problem

• Find a way to place all 8 queens on a chess board so that:

• No two queens are on the same row• No two queens are on the same column• No two queens are on the same diagonal

• Again, how can we estimate how many board configurations there are (incl. illegal ones)?

Page 12: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

12

Page 13: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

13

From Wikipedia…

(Fundamental solutions can be rotated / reflected to obtain other solutions)

Page 14: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

14

• We use an 8x8 Boolean list as a board.• A cell is True if a queen is placed there.

Page 15: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

15

Page 16: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

16

Page 17: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

17

Page 18: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

18

Optimization

• Given some possible (large) set: U• A value function

• The task: to find that has maximal value.

– (minimization problems are equivalent to maximizing the negation of the value)

Page 19: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

19

Solving this sort of basic task is very beneficial.

The simplest algorithm: • go over all • Evaluate .• Keep the maximal.

Unfortunately, most interesting problems take a long time to solve.

(U is really large in most interesting cases).

Page 20: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

20

Example

• The Knapsack problem

• A thief breaks into a house and wants to steal items.

• There are items.• Item has a size , and a value . • The thief wants the set of items with the

highest total value that can fit into the bag

Page 21: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

21

Knapsack

• To define as an optimization problem:

• What is the size of U?• How do we solve the problem?

Page 22: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

22

• Another variant: each item has a weight, a size, and a value. The thief is limited both in weight and size.

• In python…

Page 23: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

23

Page 24: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

24

Page 25: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

25

Page 26: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

26

Idea for the algorithm

• Use recursion (backtracking).

• Given a partially filled bag, and a set of items• Try with the first item (& make recursive call)

– If the first item doesn’t fit, skip it. • Try without the first item (& make recursive call)

Page 27: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

27

Page 28: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

28

Page 29: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

29

Back to N-Queens

We can phrase the N-Queen problem as an optimization problem:

• Find the board setting that has the fewest violated constraints.

• If we find the minimal number of violated constraints, and it happens to be 0, we have a legal solution.

Page 30: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

30

Are there alternatives to brute force search?

• Yes!

• We can try guessing: start with random assignments until we find a good solution.

• We can try to have a solution with conflicts and try to move only conflicted pieces.

• Unfortunately, they are not much more efficient.

Page 31: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

31

Page 32: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

32

Page 33: Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1

33