chapter 5 - backtracking

Upload: himanshu-tiwari

Post on 06-Apr-2018

235 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Chapter 5 - Backtracking

    1/10

    Tehran Technical College of Shariaty

    Fall 2008

    Chapter 5

    BacktrackingEhsan Adeli ([email protected])

    Maze of hedges by Hampton Court Palace

    A sequence of objects is chosen from a specified set soa e sequence sa s es some cr er on

    Example: n-Queens problem

    Set: n2 possible positions

    Criterion:no two ueens can threaten each other

    Depth-first search of a tree (preorder tree traversal)

    Algorithms ([email protected])2

    Algorithms ([email protected])3

    Algorithms ([email protected])4

  • 8/3/2019 Chapter 5 - Backtracking

    2/10

    -

    a e space ree

    Algorithms ([email protected]) 5

    Algorithms ([email protected])6

    Algorithms ([email protected])7

    Nonpromising node

    Promising node

    Promisin function

    Pruning the state space tree

    Algorithms ([email protected])8

  • 8/3/2019 Chapter 5 - Backtracking

    3/10

    Algorithms ([email protected])9

    -

    Algorithms ([email protected])10

    -

    Algorithms ([email protected])11

    -

    Algorithms ([email protected])12

  • 8/3/2019 Chapter 5 - Backtracking

    4/10

    -

    Algorithms ([email protected])13

    -

    Algorithms ([email protected])14

    Pruned state s ace tree

    Algorithms ([email protected])15

    Algorithms ([email protected])16

  • 8/3/2019 Chapter 5 - Backtracking

    5/10

    -

    Check whether two queens threaten

    each other:co co =

    col(i) col(k) = k - i

    Algorithms ([email protected]) 17

    Efficienc

    Checking the entire state space tree (number of nodes

    checked)

    Taking the advantage that no two queens can be placed ine same row or n e same co umn

    1 + n + n(n-1) + n(n-1)(n-2) + + n! promising nodes

    Algorithms ([email protected]) 18

    Algorithms ([email protected])19

    - -

    Algorithms ([email protected])20

  • 8/3/2019 Chapter 5 - Backtracking

    6/10

    w1 = 2, w2 = 4, w3 = 5

    Algorithms ([email protected]) 21

    = = = = 1 , 2 , 3

    Algorithms ([email protected])22

    Sort the weights in nondecreasing order

    To check the node at level i

    weight+ wi+1 > W

    weight+ total< W

    Algorithms ([email protected])23

    en = an w1 = , w2 = , w3 = , w4 =

    Algorithms ([email protected])24

  • 8/3/2019 Chapter 5 - Backtracking

    7/10

    .

    _ _ , ,

    if(promising (i))

    if(weight == W)cou nc u e t roug nc u e ;

    else{

    include [i+ 1] = "yes";

    sum_of_subsets (i+ 1, weight + w[i+ 1], total - w[i+ 1]);

    include [i+ 1] = "no";

    sum_of_subsets (i+ 1, weight, total - w[i+ 1]);

    }

    }

    bool promising(index i);{

    return (weight + total >=W) &&

    (weight == W|| weight + w[i+ 1]

  • 8/3/2019 Chapter 5 - Backtracking

    8/10

    Planargraph

    It can be drawn in a plane in such a way that no two edges

    cross each other.

    To every map there corresponds a planar graph

    Algorithms ([email protected])29

    Map

    Algorithms ([email protected])30

    corresponded planar graph

    Algorithms ([email protected])31

    Algorithms ([email protected])32

  • 8/3/2019 Chapter 5 - Backtracking

    9/10

    .

    void m_coloring(index i) {

    int color;if(promising (i))

    if(i == n)

    cout

  • 8/3/2019 Chapter 5 - Backtracking

    10/10

    Hamiltonian Circuit

    [v1, v2, v8, v7, v6, v5, v4, v3, v2]

    Algorithms ([email protected])37

    No Hamiltonian Circuit!

    Algorithms ([email protected])38

    .

    vo am ton an n ex

    indexj;

    if romisin i

    if(i == n-1)cout