Docsity

Backtracking - Programming Languages and Techniques II - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Backtracking - Programming Languages and Techniques II - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Backtracking

Backtracking

• Suppose you have to make a series of

decisions, among various choices, where

  • You don’t have enough information to know

what to choose

  • Each decision leads to a new set of choices
  • Some sequence of choices (possibly more than

one) may be a solution to your problem

• Backtracking is a methodical way of trying

out various sequences of decisions, until

you find one that “works”

Coloring a map

  • You wish to color a map with not more than four colors - red, yellow, green, blue
  • Adjacent countries must be in different colors
  • You don’t have enough information to choose colors
  • Each choice leads to another set of choices
  • One or more sequences of choices may (or may not) lead to a solution
  • Many coloring problems can be solved with backtracking

Solving a puzzle

  • In this puzzle, all holes but one are filled with white pegs
  • You can jump over one peg with another
  • Jumped pegs are removed
  • The object is to remove all but the last peg
  • You don’t have enough information to jump correctly
  • Each choice leads to another set of choices
  • One or more sequences of choices may (or may not) lead to a solution
  • Many kinds of puzzle can be solved with backtracking

Terminology I

7

There are three kinds of nodes:

A tree is composed of nodes

The (one) root node Internal nodes Leaf nodes

Backtracking can be thought of as searching a tree for a particular “goal” leaf node

Terminology II

• Each non-leaf node in a tree is a parent of one

or more other nodes (its children)

• Each node in the tree, other than the root, has

exactly one parent

8

parent

children

parent

children

Usually, however, we draw our trees downward, with the root at the top

The backtracking algorithm

• Backtracking is really quite simple--we

“explore” each node, as follows:

• To “explore” node N:

1. If N is a goal node, return “success”

2. If N is a leaf node, return “failure”

3. For each child C of N,

3.1. Explore C 3.1.1. If C was successful, return “success”

4. Return “failure”

Full example: Map coloring

• The Four Color Theorem states that any map

on a plane can be colored with no more than

four colors, so that no two countries with a

common border are the same color

• For most maps, finding a legal coloring is easy

• For some maps, it can be fairly difficult to find

a legal coloring

• We will develop a complete Java program to

solve this problem

Creating the map

13

0 1 2 4 3 5 6

int map[][];

void createMap() { map = new int[7][]; map[0] = new int[] { 1, 4, 2, 5 }; map[1] = new int[] { 0, 4, 6, 5 }; map[2] = new int[] { 0, 4, 3, 6, 5 }; map[3] = new int[] { 2, 4, 6 }; map[4] = new int[] { 0, 1, 6, 3, 2 }; map[5] = new int[] { 2, 6, 1, 0 }; map[6] = new int[] { 2, 3, 4, 1, 5 }; }

Setting the initial colors

14

static final int NONE = 0; static final int RED = 1; static final int YELLOW = 2; static final int GREEN = 3; static final int BLUE = 4;

int mapColors[] = { NONE, NONE, NONE, NONE, NONE, NONE, NONE };

The backtracking method

boolean explore(int country, int color) { if (country >= map.length) return true; if (okToColor(country, color)) { mapColors[country] = color; for (int i = RED; i <= BLUE; i++) { if (explore(country + 1, i)) return true; } } return false; }

Checking if a color can be used

boolean okToColor(int country, int color) { for (int i = 0; i < map[country].length; i++) { int ithAdjCountry = map[country][i]; if (mapColors[ithAdjCountry] == color) { return false; } } return true; }

Results

• true

map[0] is red

map[1] is yellow

map[2] is yellow

map[3] is red

map[4] is green

map[5] is green

map[6] is blue

Recap

  • We went through all the countries recursively, starting with country zero
  • At each country we had to decide a color
    • It had to be different from all adjacent countries
    • If we could not find a legal color, we reported

failure

  • If we could find a color, we used it and recurred

with the next country

  • If we ran out of countries (colored them all), we

reported success

  • When we returned from the topmost call, we were done