Professional Documents
Culture Documents
Lecture 17
Lecture 17
2
3
4
5
6
What Is A Brute Force Algorithm?
Brute force is a straightforward approach to solving a
problem, usually directly based on the problem’s
statement and definitions of the concepts involved.
The “force” implied by the strategy’s definitions is that of
a computer and not that of one’s intellect. “Just do it!”
would be another way to describe the brute-force
approach.
As an example, consider computing an for a given
number a and a nonnegative integer n.
– By the definition of exponentiation, an = a a a
n times
Backtracking
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
CS 307 Fundamentals of 9
Computer Science Recursive Backtracking
Solving Sudoku – Brute Force
A brute force algorithm is a
simple but general
approach
Try all combinations until
you find one that works
This approach isn’t clever,
but computers are fast
Then try and improve on the
brute force resuts
CS 307 Fundamentals of 10
Computer Science Recursive Backtracking
Solving Sudoku
Brute force Sudoku Soluton
– if not open cells, solved 1
– scan cells from left to right,
top to bottom for first open
cell
– When an open cell is found
start cycling through digits 1
to 9.
– When a digit is placed check
that the set up is legal
– now solve the board
CS 307 Fundamentals of 11
Computer Science Recursive Backtracking
Solving Sudoku – Later Steps
1 1 2 1 2 4
1 2 4 8 1 2 4 8 9
uh oh!
CS 307 Fundamentals of 12
Computer Science Recursive Backtracking
Sudoku – A Dead End
We have reached a dead end in our search
1 2 4 8 9