
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Recursive Backtracking, Backtracking, Concrete Example, Solving Sudoku, Brute Force, Attendance Question, Later Steps, Dead End, Backing Up, Brute Force Algorithms are the key important points of lecture slides of Fundamentals of Computer Science.
Typology: Slides
1 / 40
This page cannot be seen from the preview
Don't miss anything!

































"In ancient times, before computers were invented, alchemists studied the mystical properties of numbers. Lacking computers, they had to rely on dragons to do their work for them. The dragons were clever beasts, but also lazy and bad-tempered. The worst ones would sometimes burn their keeper to a crisp with a single fiery belch. But most dragons were merely uncooperative, as violence required too much energy. This is the story of how Martin, an alchemist’s apprentice, discovered recursion by outsmarting a lazy dragon."
Start Success!
Success!
Failure Problem space consists of states (nodes) and actions (paths that lead to new states). When in a node can can only see paths to connected nodes
If a node only leads to failure go back to its "parent" node. Try other alternatives. If these all lead to failure then more backtracking may be necessary.
1
1 1 2 1 2 4
(^1 2 4 8 1 2 4 8 )
uh oh!
1 2 4 8 9
Characteristics of Brute Force and Backtracking
After trying placing a digit in a cell we want to solve the new sudoku board
After placing a number in a cell the we need to remember the next number to try in case things don't work out.
We need to know if things worked out (found a solution) or they didn't, and if they didn't try the next number
If we try all numbers and none of them work in our cell we need to report back that things didn't work
Make that choice and take one step along path Use recursion to solve the problem for the new node / state If the recursive call succeeds, report the success to the next high level Back out of the current choice to restore the state at the beginning of the loop.
Start Success!
Success!
Place N Queens on an N by N chessboard so that none of them can attack each other
Number of possible placements?
In 8 x 8 64 * 63 * 62 * 61 * 60 * 59 * 58 * 57 = 178,462, 987, 637, 760 / 8! = 4,426,165,
n choose k
The previous calculation includes set ups like this one
Includes lots of set ups with multiple queens in the same column
How many queens can there be in one column?
Number of set ups 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 = 16,777,
We have reduced search space by two orders of magnitude by applying some logic
Q Q Q Q Q Q Q Q
If number of queens is fixed and I realize there can't be more than one queen per column I can iterate through the rows for each column for(int c0 = 0; c0 < 8; c0++){ board[c0][0] = 'q'; for(int c1 = 0; c1 < 8; c1++){ board[c1][1] = 'q'; for(int c2 = 0; c2 < 8; c2++){ board[c2][2] = 'q'; // a little later for(int c7 = 0; c7 < 8; c7++){ board[c7][7] = 'q'; if( queensAreSafe(board) ) printSolution(board); board[c7][7] = ' '; //pick up queen } board[c6][6] = ' '; // pick up queen