Docsity

Backtracking Algorithm: Finding Solutions to Complex Problems - Prof. eskululu, Assignments of Compiler Design

Partial preview of the text

Download Backtracking Algorithm: Finding Solutions to Complex Problems - Prof. eskululu and more Assignments Compiler Design in PDF only on Docsity!

Backtracking

Chapter 5

start? ? dead end dead end ? ? dead end dead end ? success! dead end

Outline

  • (^) The general Method of Backtracking
  • (^) 8 queens problem,
  • (^) graph coloring
  • (^) Hamiltonian cycles
  • (^) Travelling sales person problems

4

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

5

Solving a maze

  • (^) Given a maze, find a path from start to finish
  • (^) At each intersection, you have to decide

between three or fewer choices:

  • (^) Go straight
  • (^) Go left
  • (^) Go right
  • (^) You don’t have enough information to choose correctly
  • (^) Each choice leads to another set of choices
  • (^) One or more sequences of choices may (or may not) lead to a solution
  • (^) Many types of maze problem can be solved with backtracking

Backtracking፡ Solving a maze

Junction Portion B Portion A

One strategy would be

to try going through Portion A of the maze.  (^) If you get stuck before you find your way out, then you "backtrack" to the junction.  At this point in time you know that Portion A will NOT lead you out of the maze,  (^) so you then start searching in Portion B

Backtracking

  • (^) Clearly, at a single junction you could have even more than 2 choices.
  • (^) The backtracking strategy says to try each choice, one after the other, - (^) if you ever get stuck, "backtrack" to the junction and try the next choice.
  • (^) If you try all choices and never found a way out, then there is no solution to the maze. Junction B C A

Backtracking – Eight Queens Problem

  • (^) Find an arrangement of 8 queens on a single

chess board such that no two queens are

attacking one another.

  • (^) In chess, queens can move all the way down

any row, column or diagonal (so long as no

pieces are in the way).

  • (^) Due to the first two restrictions, it's clear that each row and column of the board will have exactly one queen.
  • (^) Anima

ted

Exam

ple:

  • (^) http://

www.

hbmey

er.de/b

acktra

ck/ach

tdame

n/eight

.htm#u

p

Graph Coloring

  • (^) Graph coloring is the problem of coloring each

vertex in a graph such that no two adjacent

vertices are the same color

  • (^) Some direct examples:
    • (^) Map coloring
    • (^) Register assignment

14

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

Graph Coloring

C A B F E D

  • (^) As an example:
    • (^) The vertices are enumerated in order A-F
    • (^) The colors are given in order: R, G, B

Graph Coloring

C A B F E D

Graph Coloring

C A B F E D

Graph Coloring

C A B F E D