Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1."— Presentation transcript:

1 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” 2

3 Sudoku 3

4 Sudoku Solving 4

5 5

6 6 Check the column Check the row Check the quadrant

7 7

8 8

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? 9

10 Number of valid Sudoku boards 10

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)? 11

12 12

13 From Wikipedia… 13 (Fundamental solutions can be rotated / reflected to obtain other solutions)

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

15 15

16 16

17 17

18 Optimization 18

19 19

20 Example 20

21 Knapsack 21

22 Another variant: each item has a weight, a size, and a value. The thief is limited both in weight and size. In python… 22

23 23

24 24

25 25

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) 26

27 27

28 28

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. 29

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. 30

31 31

32 32

33 33


Download ppt "Backtracking & Brute Force Optimization Intro2CS – weeks 12-13 1."

Similar presentations


Ads by Google