Recursive Backtracking - Fundamentals of Computer Science - Lecture Slides, Slides of Computer Fundamentals

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

2012/2013

Uploaded on 01/02/2013

umar
umar 🇮🇳

4.5

(4)

39 documents

1 / 40

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Recursive Backtracking
"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."
- David S. Touretzky, Common Lisp: A Gentle Introduction to
Symbolic Computation
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Recursive Backtracking - Fundamentals of Computer Science - Lecture Slides and more Slides Computer Fundamentals in PDF only on Docsity!

Recursive Backtracking

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

  • David S. Touretzky, Common Lisp: A Gentle Introduction to Symbolic Computation

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

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.

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

Solving Sudoku

Brute force Sudoku Soluton

  • if not open cells, solved
  • 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

1

Solving Sudoku – Later Steps

1 1 2 1 2 4

(^1 2 4 8 1 2 4 8 )

uh oh!

Sudoku – A Dead End

We have reached a dead end in our search

With the current set up none of the nine

digits work in the top right corner

1 2 4 8 9

Characteristics of Brute Force and Backtracking

Brute force algorithms are slow

The don't employ a lot of logic

  • For example we know a 6 can't go in the last 3 columns of the first row, but the brute force algorithm will plow ahead any way

But, brute force algorithms are fairly easy to

implement as a first pass solution

  • backtracking is a form of a brute force algorithm

Key Insights

After trying placing a digit in a cell we want to solve the new sudoku board

  • Isn't that a smaller (or simpler version) of the same problem we started with?!?!?!?

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

Recursive Backtracking

Pseudo code for recursive backtracking

algorithms

If at a solution, report success

for( every possible choice from current state /

node)

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.

Report failure

Goals of Backtracking

Possible goals

  • Find a path to success
  • Find all paths to success
  • Find the best path to success

Not all problems are exactly alike, and

finding one success node may not be the

end of the search

Start Success!

Success!

The 8 Queens Problem

A classic chess puzzle

  • Place 8 queen pieces on a chess board so that none of them can attack one another

The N Queens Problem

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

  • How many ways can you choose k things from a set of n items?
  • In this case there are 64 squares and we want to choose 8 of them to put queens on

Reducing the Search Space

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

A Solution to 8 Queens

 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