Download presentation

Presentation is loading. Please wait.

Published byRosa Ward Modified over 2 years ago

1
SIGCSE 20031 Nifty assignments: Brute force and backtracking Steve Weiss Department of Computer Science University of North Carolina at Chapel Hill weiss@cs.unc.edu

2
SIGCSE 20032 The puzzle

3
SIGCSE 20033

4
4 Brute force problem solving Generate candidates Filter Solutions Trash

5
SIGCSE 20035 Requirements Candidate set must be finite. Must be an “Oh yeah!” problem.

6
SIGCSE 20036 Example Combination lock 60*60*60 = 216,000 candidates

7
SIGCSE 20037 Example

8
SIGCSE 20038 Oh no!

9
SIGCSE 20039 Oh yeah!

10
SIGCSE 200310 Additional restrictions Solution is a sequence s 1, s 2,…,s n Solution length, n, is known (or at least bounded) in advance. Each s i is drawn from a finite pool T.

11
SIGCSE 200311 Sequence class extend(x)Add x to the right end of the sequence. retract()Remove the rightmost element. size()How long is the sequence? …

12
SIGCSE 200312 Generating the candidates Classic backtrack algorithm: At decision point, do something new (extend something that hasn’t been added to this sequence at this place before.) Fail: Backtrack: Undo most recent decision (retract). Fail: done

13
SIGCSE 200313 Recursive backtrack algorithm (pseudo Java) backtrack(Sequence s) { for each si in T { s.extend(si); if (s.size() == MAX) // Complete sequence display(s); else backtrack(s); s.retract(); } // End of for } // End of backtrack

14
SIGCSE 200314 Problem solver backtrack(Sequence s) { for each si in T { s.extend(si); if (s.size() == MAX) // Complete sequence if (s.solution()) display(s); else backtrack(s); s.retract(); } // End of for } // End of backtrack

15
SIGCSE 200315 Problems Too slow, even on very fast machines. Case study: 8-queens Example: 8-queens has more than 281,474,976,711,000 candidates.

16
SIGCSE 200316 8-queens How can you place 8 queens on a chessboard so that no queen threatens any of the others. Queens can move left, right, up, down, and along both diagonals.

17
SIGCSE 200317 Problems Too slow, even on very fast machines. Case study: 8-queens Example: 8-queens has more than 281,474,976,711,000 candidates.

18
SIGCSE 200318 Faster! Reduce size of candidate set. Example: 8-queens, one per row, has only 16,777,216 candidates.

19
SIGCSE 200319 Faster still! Prune: reject nonviable candidates early, not just when sequence is complete. Example: 8-queens with pruning looks at about 16,000 partial and complete candidates.

20
SIGCSE 200320 Backtrack with pruning backtrack(Sequence s) { for each si in T if (s.okToAdd(si)) // Pruning { s.extend(si); if (s.size() == MAX) // Complete solution display(s); else backtrack(s); s.retract(); } // End of if } // End of backtrack

21
SIGCSE 200321 Nifty assignments 1.Map coloring: Given a map with n regions, and a palate of c colors, how many ways can you color the map so that no regions that share a border are the same color?

22
SIGCSE 200322 Nifty assignments Solution is a sequence of known length (n) where each element is one of the colors. 1 4 3 2

23
SIGCSE 200323 Nifty assignments 2. Running a maze: How can you get from start to finish legally in a maze? 20 x 20 grid

24
SIGCSE 200324 Nifty assignments Solution is a sequence of unknown length, but bounded by 400, where each element is S, L, or R.

25
SIGCSE 200325 Nifty assignments 3. Making change. How many ways are there to make $1.00 with coins. Don’t forget Sacagawea.

26
SIGCSE 200326 Nifty assignments 3. Making change. Have the coin set be variable. Exclude the penny.

27
SIGCSE 200327 Nifty assignments 4. Unscrambling a word “ptos” == “stop”, “post”, “pots”, ”spot”

28
SIGCSE 200328 Nifty assignments 4. Unscrambling a word Requires a dictionary Data structures and efficient search Permutations

29
SIGCSE 200329 Nifty assignments 5. Solving the 9 square problem. Solution is sequence of length 9 where each element is a different puzzle piece and where the touching edges sum to zero.

30
SIGCSE 200330 The puzzle

31
SIGCSE 200331 Nifty assignments Challenges: Data structures to store the pieces and the 3 x 3 board. Canonical representation so that solutions don’t appear four times. Pruning nonviable sequences: puzzle piece used more than once. edge rule violation not canonical

32
SIGCSE 200332 Nifty assignments Challenges: Algorithm analysis: instrumenting the program to keep track of running time and number of calls to the filter and to the backtrack method.

33
SIGCSE 200333

Similar presentations

OK

CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter

CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on diversity in our society Ppt on search engine project Ppt on abstract class in java Ppt on atomic structure class 11 Ppt on cross site scripting xss Ppt on hybrid air conditioning Ppt on search and rescue Ppt on viruses and antivirus-shop Powerpoint ppt on nanotechnology Ppt on water activity of foods