SlideShare a Scribd company logo
1 of 40
Download to read offline
Lecture 15:
         Backtracking
         Steven Skiena

Department of Computer Science
 State University of New York
 Stony Brook, NY 11794–4400

http://www.cs.sunysb.edu/∼skiena
Problem of the Day
The single-destination shortest path problem for a directed
graph is to find the shortest path from every vertex to a
specified vertex v. Give an efficient algorithm to solve the
single-destination shortest paths problem.
Problem of the Day
Let G be a weighted directed graph with n vertices and m
edges, where all edges have positive weight. A directed cycle
is a directed path that starts and ends at the same vertex and
contains at least one edge. Give an O(n3 ) algorithm to find
a directed cycle in G of minimum total weight. Partial credit
will be given for an O(n2 m) algorithm.
Sudoku
                               1 2   6   7   3   8   9   4   5   1   2
                     3 5             9   1   2   7   3   5   4   8   6
                 6             7     8   4   5   6   1   2   9   7   3
         7                 3         7   9   8   2   6   1   3   5   4
                 4         8         5   2   6   4   7   3   8   9   1
         1                           1   3   4   5   8   9   2   6   7
                 1 2                 4   6   9   1   2   8   7   3   5
             8                 4     2   8   7   3   5   6   1   4   9
             5             6         3   5   1   9   4   7   6   2   8
Solving Sudoku
Solving Sudoku puzzles involves a form of exhaustive search
of possible configurations.
However, exploiting constraints to rule out certain possibili-
ties for certain positions enables us to prune the search to the
point people can solve Sudoku by hand.
Backtracking is the key to implementing exhaustive search
programs correctly and efficiently.
Backtracking
Backtracking is a systematic method to iterate through all
the possible configurations of a search space. It is a general
algorithm/technique which must be customized for each
individual application.
In the general case, we will model our solution as a vector
a = (a1 , a2, ..., an), where each element ai is selected from a
finite ordered set Si.
Such a vector might represent an arrangement where ai
contains the ith element of the permutation. Or the vector
might represent a given subset S, where ai is true if and only
if the ith element of the universe is in S.
The Idea of Backtracking
At each step in the backtracking algorithm, we start from
a given partial solution, say, a = (a1 , a2 , ..., ak ), and try to
extend it by adding another element at the end.
After extending it, we must test whether what we have so far
is a solution.
If not, we must then check whether the partial solution is still
potentially extendible to some complete solution.
If so, recur and continue. If not, we delete the last element
from a and try another possibility for that position, if one
exists.
Recursive Backtracking

Backtrack(a, k)
if a is a solution, print(a)
else {
        k =k+1
        compute Sk
        while Sk = ∅ do
              ak = an element in Sk
              S k = S k − ak
              Backtrack(a, k)
}
Backtracking and DFS
Backtracking is really just depth-first search on an implicit
graph of configurations.
Backtracking can easily be used to iterate through all subsets
or permutations of a set.
Backtracking ensures correctness by enumerating all possi-
bilities.
For backtracking to be efficient, we must prune the search
space.
Implementation
bool finished = FALSE; (* found all solutions yet? *)

backtrack(int a[], int k, data input)
{
      int c[MAXCANDIDATES]; (* candidates for next position *)
      int ncandidates; (* next position candidate count *)
      int i; (* counter *)

     if (is a solution(a,k,input))
             process solution(a,k,input);
     else {
             k = k+1;
             construct candidates(a,k,input,c,&ncandidates);
             for (i=0; i<ncandidates; i++) {
                    a[k] = c[i];
                    backtrack(a,k,input);
                    if (finished) return; (* terminate early *)
             }
     }
}
is a solution(a,k,input)
This Boolean function tests whether the first k elements of
vector a are a complete solution for the given problem. The
last argument, input, allows us to pass general information
into the routine.
construct candidates(a,k,input,c,nca
This routine fills an array c with the complete set of possible
candidates for the kth position of a, given the contents of the
first k − 1 positions. The number of candidates returned in
this array is denoted by ncandidates.
process solution(a,k)
This routine prints, counts, or somehow processes a complete
solution once it is constructed.
Backtracking ensures correctness by enumerating all possi-
bilities. It ensures efficiency by never visiting a state more
than once.
Because a new candidates array c is allocated with each
recursive procedure call, the subsets of not-yet-considered
extension candidates at each position will not interfere with
each other.
Constructing all Subsets
How many subsets are there of an n-element set?
To construct all 2n subsets, set up an array/vector of n cells,
where the value of ai is either true or false, signifying whether
the ith item is or is not in the subset.
To use the notation of the general backtrack algorithm, Sk =
(true, f alse), and v is a solution whenever k ≥ n.
What order will this generate the subsets of {1, 2, 3}?
                         (1) → (1, 2) → (1, 2, 3)∗ →
                      (1, 2, −)∗ → (1, −) → (1, −, 3)∗ →
                        (1, −, −)∗ → (1, −) → (1) →
                        (−) → (−, 2) → (−, 2, 3)∗ →
                     (−, 2, −)∗ → (−, −) → (−, −, 3)∗ →
                      (−, −, −)∗ → (−, −) → (−) → ()
Constructing All Subsets
We can construct the 2n subsets of n items by iterating
through all possible 2n length-n vectors of true or false,
letting the ith element denote whether item i is or is not in
the subset.
Using the notation of the general backtrack algorithm, Sk =
(true, f alse), and a is a solution whenever k ≥ n.
is a solution(int a[], int k, int n)
{
      return (k == n); (* is k == n? *)
}

construct candidates(int a[], int k, int n, int c[], int *ncandidates)
{
      c[0] = TRUE;
      c[1] = FALSE;
      *ncandidates = 2;
}
process solution(int a[], int k)
{
      int i; (* counter *)

      printf(”(”);
      for (i=1; i<=k; i++)
      if (a[i] == TRUE) printf(”
      printf(” )”);
}
Main Routine: Subsets
Finally, we must instantiate the call to backtrack with the
right arguments.
generate subsets(int n)
{
      int a[NMAX]; (* solution vector *)

     backtrack(a,0,n);
}
Constructing all Permutations
How many permutations are there of an n-element set?
To construct all n! permutations, set up an array/vector of n
cells, where the value of ai is an integer from 1 to n which
has not appeared thus far in the vector, corresponding to the
ith element of the permutation.
To use the notation of the general backtrack algorithm, Sk =
(1, . . . , n) − v, and v is a solution whenever k ≥ n.
                 (1) → (1, 2) → (1, 2, 3)∗ → (1, 2) → (1) → (1, 3) →
            (1, 3, 2)∗ → (1, 3) → (1) → () → (2) → (2, 1) →
            (2, 1, 3)∗ → (2, 1) → (2) → (2, 3) → (2, 3, 1)∗ → (2, 3) → ()
                 (2) → () → (3) → (3, 1)(3, 1, 2)∗ → (3, 1) → (3) →
               (3, 2) → (3, 2, 1)∗ → (3, 2) → (3) → ()
Constructing All Permutations
To     avoid         repeating        permutation     elements,
Sk = {1, . . . , n} − a, and a is a solution whenever k = n:
construct candidates(int a[], int k, int n, int c[], int *ncandidates) {
      int i; (* counter *)
      bool in perm[NMAX]; (* who is in the permutation? *)

      for (i=1; i<NMAX; i++) in perm[i] = FALSE;
      for (i=0; i<k; i++) in perm[ a[i] ] = TRUE;

      *ncandidates = 0;
      for (i=1; i<=n; i++)
             if (in perm[i] == FALSE) {
             c[ *ncandidates] = i;
             *ncandidates = *ncandidates + 1;
      }
}
Auxilliary Routines
Completing the job of generating permutations requires
specifying process solution and is a solution, as
well as setting the appropriate arguments to backtrack.
All are essentially the same as for subsets:
process solution(int a[], int k)
{
      int i; (* counter *)

      for (i=1; i<=k; i++) printf(”
      printf(””);
}

is a solution(int a[], int k, int n)
{
      return (k == n);
}
Main Program: Permutations

generate permutations(int n)
{
      int a[NMAX]; (* solution vector *)

     backtrack(a,0,n);
}
Problem of the Day
A derangement is a permutation p of {1, . . . , n} such that no
item is in its proper position, i.e. pi = i for all 1 ≤ i ≤
n. Write an efficient backtracking program with pruning that
constructs all the derangements of n items.
The Eight-Queens Problem




The eight queens problem is a classical puzzle of positioning
eight queens on an 8 × 8 chessboard such that no two queens
threaten each other.
Eight Queens: Representation
What is concise, efficient representation for an n-queens
solution, and how big must it be?
Since no two queens can occupy the same column, we know
that the n columns of a complete solution must form a
permutation of n. By avoiding repetitive elements, we reduce
our search space to just 8! = 40,320 – clearly short work for
any reasonably fast machine.
The critical routine is the candidate constructor.         We
repeatedly check whether the kth square on the given row
is threatened by any previously positioned queen. If so, we
move on, but if not we include it as a possible candidate:
Candidate Constructor: Eight Queens

construct candidates(int a[], int k, int n, int c[], int *ncandidates)
{
      int i,j; (* counters *)
      bool legal move; (* might the move be legal? *)

      *ncandidates = 0;
      for (i=1; i<=n; i++) {
             legal move = TRUE;
             for (j=1; j<k; j++) {
                    if (abs((k)-j) == abs(i-a[j])) (* diagonal threat *)
                           legal move = FALSE;
                    if (i == a[j]) (* column threat *)
                           legal move = FALSE;
             }
             if (legal move == TRUE) {
                    c[*ncandidates] = i;
                    *ncandidates = *ncandidates + 1;
             }
      }
}
The remaining routines are simple, particularly since we are
only interested in counting the solutions, not displaying them:
process solution(int a[], int k)
{
      int i; (* counter *)

      solution count ++;
}

is a solution(int a[], int k, int n)
{
      return (k == n);
}
Finding the Queens: Main Program

nqueens(int n)
{
     int a[NMAX]; (* solution vector *)

     solution count = 0;
     backtrack(a,0,n);
     printf(”n=}



This program can find the 365,596 solutions for n = 14 in
minutes.
Can Eight Pieces Cover a Chess Board?
Consider the 8 main pieces in chess (king, queen, two rooks,
two bishops, two knights). Can they be positioned on a
chessboard so every square is threatened?

                              N   B       R
                              N


                  R
                      B
                          Q           K
Combinatorial Search
Only 63 square are threatened in this configuration. Since
1849, no one had been able to find an arrangement with
bishops on different colors to cover all squares.
We can resolve this question by searching through all possible
board configurations if we spend enough time.
We will use it as an example of how to attack a combinatorial
search problem.
With clever use of backtracking and pruning techniques,
surprisingly large problems can be solved by exhaustive
search.
How Many Chess Configurations Must be
Tested?
Picking a square for each piece gives us the bound:
       64!/(64 − 8)! = 178, 462, 987, 637, 760 ≈ 1015
Anything much larger than 108 is unreasonable to search on a
modest computer in a modest amount of time.
Exploiting Symmetry
However, we can exploit symmetry to save work. With
reflections along horizontal, vertical, and diagonal axis, the
queen can go in only 10 non-equivallent positions.
Even better, we can restrict the white bishop to 16 spots and
the queen to 16, while being certain that we get all distinct
configurations.
                                                            b
                                                        b   b
                                                  b
                                                b . . .      b
                                              b              b
                    Q Q Q                 b
                                              . . . .        b
                                      b
                     . . .                                  b
                     . . .        b       . . . .




16 × 16 × 32 × 64 × 2080 × 2080 = 2, 268, 279, 603, 200 ≈ 10 12
Covering the Chess Board
In covering the chess board, we prune whenever we find
there is a square which we cannot cover given the initial
configuration!
Specifically, each piece can threaten a certain maximum
number of squares (queen 27, king 8, rook 14, etc.) We prune
whenever the number of unthreated squares exceeds the sum
of the maximum remaining coverage.
As implemented by a graduate student project, this backtrack
search eliminates 95% of the search space, when the pieces
are ordered by decreasing mobility.
With precomputing the list of possible moves, this program
could search 1,000 positions per second.
End Game
But this is still too slow!
             1012 /103 = 109 seconds > 1000 days
Although we might further speed the program by an order of
magnitude, we need to prune more nodes!
By using a more clever algorithm, we eventually were able
to prove no solution existed, in less than one day’s worth of
computing.
You too can fight the combinatorial explosion!
The Backtracking Contest: Bandwidth
The bandwidth problem takes as input a graph G, with n
vertices and m edges (ie. pairs of vertices). The goal is to find
a permutation of the vertices on the line which minimizes the
maximum length of any edge.




            1   2   3   4   5   6   7   8   1   8   2   7   3   6   4   5



The bandwidth problem has a variety of applications, includ-
ing circuit layout, linear algebra, and optimizing memory
usage in hypertext documents.
The problem is NP-complete, meaning that it is exceedingly
unlikely that you will be able to find an algorithm with
polynomial worst-case running time. It remains NP-complete
even for restricted classes of trees.
Since the goal of the problem is to find a permutation,
a backtracking program which iterates through all the n!
possible permutations and computes the length of the longest
edge for each gives an easy O(n! · m) algorithm. But the goal
of this assignment is to find as practically good an algorithm
as possible.
The Backtracking Contest: Set Cover
The set cover problem takes as input a collection of subsets
S = {S1, . . . , Sm} of the universal set U = {1, . . . , n}. The
goal is to find the smallest subset of the subsets T such that
 |T |
∪i=1Ti = U .
Set cover arises when you try to efficiently acquire or
represent items that have been packaged in a fixed set of lots.
You want to obtain all the items, while buying as few lots as
possible. Finding a cover is easy, because you can always
buy one of each lot. However, by finding a small set cover
you can do the same job for less money.
Since the goal of the problem is to find a subset, a
backtracking program which iterates through all the 2m
possible subsets and tests whether it represents a cover gives
an easy O(2m ·nm) algorithm. But the goal of this assignment
is to find as practically good an algorithm as possible.
Rules of the Game

1. Everyone must do this assignment separately. Just this
   once, you are not allowed to work with your partner. The
   idea is to think about the problem from scratch.
2. If you do not completely understand what the problem
   is, you don’t have the slightest chance of producing
   a working program. Don’t be afraid to ask for a
   clarification or explanation!!!!!
3. There will be a variety of different data files of different
   sizes. Test on the smaller files first. Do not be afraid to
   create your own test files to help debug your program.
4. The data files are available via the course WWW page.
5. You will be graded on how fast and clever your program
   is, not on style. No credit will be given for incorrect
   programs.
6. The programs are to run on the whatever computer you
   have access to, although it must be vanilla enough that I
   can run the program on something I have access to.
7. You are to turn in a listing of your program, along with
   a brief description of your algorithm and any interesting
   optimizations, sample runs, and the time it takes on
   sample data files. Report the largest test file your program
   could handle in one minute or less of wall clock time.
8. The top five self-reported times / largest sizes will be
   collected and tested by me to determine the winner.
Producing Efficient Programs
1. Don’t optimize prematurely: Worrying about recursion
   vs. iteration is counter-productive until you have worked
   out the best way to prune the tree. That is where the
   money is.
2. Choose your data structures for a reason: What
   operations will you be doing? Is case of insertion/deletion
   more crucial than fast retrieval?
   When in doubt, keep it simple, stupid (KISS).
3. Let the profiler determine where to do final tuning:
   Your program is probably spending time where you don’t
   expect.

More Related Content

What's hot

LeetCode Solutions In Java .pdf
LeetCode Solutions In Java .pdfLeetCode Solutions In Java .pdf
LeetCode Solutions In Java .pdfzupsezekno
 
DS-MLR: Scaling Multinomial Logistic Regression via Hybrid Parallelism
DS-MLR: Scaling Multinomial Logistic Regression via Hybrid ParallelismDS-MLR: Scaling Multinomial Logistic Regression via Hybrid Parallelism
DS-MLR: Scaling Multinomial Logistic Regression via Hybrid ParallelismParameswaran Raman
 
Ee693 questionshomework
Ee693 questionshomeworkEe693 questionshomework
Ee693 questionshomeworkGopi Saiteja
 
Tensor Train decomposition in machine learning
Tensor Train decomposition in machine learningTensor Train decomposition in machine learning
Tensor Train decomposition in machine learningAlexander Novikov
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 
Connected roman domination in graphs
Connected roman domination in graphsConnected roman domination in graphs
Connected roman domination in graphseSAT Publishing House
 
A Fast Near Optimal Vertex Cover Algorithm (NOVCA)
A Fast Near Optimal Vertex Cover Algorithm (NOVCA)A Fast Near Optimal Vertex Cover Algorithm (NOVCA)
A Fast Near Optimal Vertex Cover Algorithm (NOVCA)Waqas Tariq
 
Quadratic programming (Tool of optimization)
Quadratic programming (Tool of optimization)Quadratic programming (Tool of optimization)
Quadratic programming (Tool of optimization)VARUN KUMAR
 
Spectral methods for solving differential equations
Spectral methods for solving differential equationsSpectral methods for solving differential equations
Spectral methods for solving differential equationsRajesh Aggarwal
 
2012 mdsp pr12 k means mixture of gaussian
2012 mdsp pr12 k means mixture of gaussian2012 mdsp pr12 k means mixture of gaussian
2012 mdsp pr12 k means mixture of gaussiannozomuhamada
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programmingKrish_ver2
 

What's hot (20)

Solution 3.
Solution 3.Solution 3.
Solution 3.
 
5.1 greedy 03
5.1 greedy 035.1 greedy 03
5.1 greedy 03
 
LeetCode Solutions In Java .pdf
LeetCode Solutions In Java .pdfLeetCode Solutions In Java .pdf
LeetCode Solutions In Java .pdf
 
DS-MLR: Scaling Multinomial Logistic Regression via Hybrid Parallelism
DS-MLR: Scaling Multinomial Logistic Regression via Hybrid ParallelismDS-MLR: Scaling Multinomial Logistic Regression via Hybrid Parallelism
DS-MLR: Scaling Multinomial Logistic Regression via Hybrid Parallelism
 
Assignment 2 daa
Assignment 2 daaAssignment 2 daa
Assignment 2 daa
 
Gate-Cs 1993
Gate-Cs 1993Gate-Cs 1993
Gate-Cs 1993
 
Ee693 questionshomework
Ee693 questionshomeworkEe693 questionshomework
Ee693 questionshomework
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Tensor Train decomposition in machine learning
Tensor Train decomposition in machine learningTensor Train decomposition in machine learning
Tensor Train decomposition in machine learning
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
Connected roman domination in graphs
Connected roman domination in graphsConnected roman domination in graphs
Connected roman domination in graphs
 
Daa unit 4
Daa unit 4Daa unit 4
Daa unit 4
 
A Fast Near Optimal Vertex Cover Algorithm (NOVCA)
A Fast Near Optimal Vertex Cover Algorithm (NOVCA)A Fast Near Optimal Vertex Cover Algorithm (NOVCA)
A Fast Near Optimal Vertex Cover Algorithm (NOVCA)
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
Quadratic programming (Tool of optimization)
Quadratic programming (Tool of optimization)Quadratic programming (Tool of optimization)
Quadratic programming (Tool of optimization)
 
Spectral methods for solving differential equations
Spectral methods for solving differential equationsSpectral methods for solving differential equations
Spectral methods for solving differential equations
 
2012 mdsp pr12 k means mixture of gaussian
2012 mdsp pr12 k means mixture of gaussian2012 mdsp pr12 k means mixture of gaussian
2012 mdsp pr12 k means mixture of gaussian
 
Lecture5
Lecture5Lecture5
Lecture5
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
 
redes neuronais
redes neuronaisredes neuronais
redes neuronais
 

Viewers also liked

Branch And Bound and Beam Search Feature Selection Algorithms
Branch And Bound and Beam Search Feature Selection AlgorithmsBranch And Bound and Beam Search Feature Selection Algorithms
Branch And Bound and Beam Search Feature Selection AlgorithmsChamin Nalinda Loku Gam Hewage
 
Randomized Algorithms in Linear Algebra & the Column Subset Selection Problem
Randomized Algorithms in Linear Algebra & the Column Subset Selection ProblemRandomized Algorithms in Linear Algebra & the Column Subset Selection Problem
Randomized Algorithms in Linear Algebra & the Column Subset Selection ProblemWei Xue
 
Solving The Shortest Path Tour Problem
Solving The Shortest Path Tour ProblemSolving The Shortest Path Tour Problem
Solving The Shortest Path Tour ProblemNozir Shokirov
 
5.5 back track
5.5 back track5.5 back track
5.5 back trackKrish_ver2
 
Subset sum problem Dynamic and Brute Force Approch
Subset sum problem Dynamic and Brute Force ApprochSubset sum problem Dynamic and Brute Force Approch
Subset sum problem Dynamic and Brute Force ApprochIjlal Ijlal
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisRajendran
 
Covering (Rules-based) Algorithm
Covering (Rules-based) AlgorithmCovering (Rules-based) Algorithm
Covering (Rules-based) AlgorithmZHAO Sam
 
Queue- 8 Queen
Queue- 8 QueenQueue- 8 Queen
Queue- 8 QueenHa Ninh
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtrackingmandlapure
 
Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Kumar
 
Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)
Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)
Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)Parinda Rajapaksha
 

Viewers also liked (20)

Branch And Bound and Beam Search Feature Selection Algorithms
Branch And Bound and Beam Search Feature Selection AlgorithmsBranch And Bound and Beam Search Feature Selection Algorithms
Branch And Bound and Beam Search Feature Selection Algorithms
 
Randomized Algorithms in Linear Algebra & the Column Subset Selection Problem
Randomized Algorithms in Linear Algebra & the Column Subset Selection ProblemRandomized Algorithms in Linear Algebra & the Column Subset Selection Problem
Randomized Algorithms in Linear Algebra & the Column Subset Selection Problem
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Chap08alg
Chap08algChap08alg
Chap08alg
 
Solving The Shortest Path Tour Problem
Solving The Shortest Path Tour ProblemSolving The Shortest Path Tour Problem
Solving The Shortest Path Tour Problem
 
21 backtracking
21 backtracking21 backtracking
21 backtracking
 
DP
DPDP
DP
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
Subset sum problem Dynamic and Brute Force Approch
Subset sum problem Dynamic and Brute Force ApprochSubset sum problem Dynamic and Brute Force Approch
Subset sum problem Dynamic and Brute Force Approch
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
 
Class warshal2
Class warshal2Class warshal2
Class warshal2
 
Covering (Rules-based) Algorithm
Covering (Rules-based) AlgorithmCovering (Rules-based) Algorithm
Covering (Rules-based) Algorithm
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Lect6 csp
Lect6 cspLect6 csp
Lect6 csp
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Queue- 8 Queen
Queue- 8 QueenQueue- 8 Queen
Queue- 8 Queen
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
 
Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20
 
Backtracking
Backtracking  Backtracking
Backtracking
 
Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)
Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)
Analysis of Feature Selection Algorithms (Branch & Bound and Beam search)
 

Similar to Skiena algorithm 2007 lecture15 backtracing

Natural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesNatural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesMark Brandao
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
Computation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine MatricesComputation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine MatricesLossian Barbosa Bacelar Miranda
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Pramit Kumar
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 
An Acceleration Scheme For Solving Convex Feasibility Problems Using Incomple...
An Acceleration Scheme For Solving Convex Feasibility Problems Using Incomple...An Acceleration Scheme For Solving Convex Feasibility Problems Using Incomple...
An Acceleration Scheme For Solving Convex Feasibility Problems Using Incomple...Ashley Smith
 
Skiena algorithm 2007 lecture17 edit distance
Skiena algorithm 2007 lecture17 edit distanceSkiena algorithm 2007 lecture17 edit distance
Skiena algorithm 2007 lecture17 edit distancezukun
 
Introduction to PyTorch
Introduction to PyTorchIntroduction to PyTorch
Introduction to PyTorchJun Young Park
 
Hierarchical matrix techniques for maximum likelihood covariance estimation
Hierarchical matrix techniques for maximum likelihood covariance estimationHierarchical matrix techniques for maximum likelihood covariance estimation
Hierarchical matrix techniques for maximum likelihood covariance estimationAlexander Litvinenko
 
Idea for ineractive programming language
Idea for ineractive programming languageIdea for ineractive programming language
Idea for ineractive programming languageLincoln Hannah
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfShiwani Gupta
 
EdSketch: Execution-Driven Sketching for Java
EdSketch: Execution-Driven Sketching for JavaEdSketch: Execution-Driven Sketching for Java
EdSketch: Execution-Driven Sketching for JavaLisa Hua
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyappasami
 
Design and Analysis of algorithms
Design and Analysis of algorithmsDesign and Analysis of algorithms
Design and Analysis of algorithmsDr. Rupa Ch
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESsuthi
 

Similar to Skiena algorithm 2007 lecture15 backtracing (20)

Natural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesNatural and Clamped Cubic Splines
Natural and Clamped Cubic Splines
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Unit 4 jwfiles
Unit 4 jwfilesUnit 4 jwfiles
Unit 4 jwfiles
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
Computation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine MatricesComputation of Semi-Magic Squares Generated by Serpentine Matrices
Computation of Semi-Magic Squares Generated by Serpentine Matrices
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
An Acceleration Scheme For Solving Convex Feasibility Problems Using Incomple...
An Acceleration Scheme For Solving Convex Feasibility Problems Using Incomple...An Acceleration Scheme For Solving Convex Feasibility Problems Using Incomple...
An Acceleration Scheme For Solving Convex Feasibility Problems Using Incomple...
 
Skiena algorithm 2007 lecture17 edit distance
Skiena algorithm 2007 lecture17 edit distanceSkiena algorithm 2007 lecture17 edit distance
Skiena algorithm 2007 lecture17 edit distance
 
Introduction to PyTorch
Introduction to PyTorchIntroduction to PyTorch
Introduction to PyTorch
 
Hierarchical matrix techniques for maximum likelihood covariance estimation
Hierarchical matrix techniques for maximum likelihood covariance estimationHierarchical matrix techniques for maximum likelihood covariance estimation
Hierarchical matrix techniques for maximum likelihood covariance estimation
 
M112rev
M112revM112rev
M112rev
 
Idea for ineractive programming language
Idea for ineractive programming languageIdea for ineractive programming language
Idea for ineractive programming language
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdf
 
EdSketch: Execution-Driven Sketching for Java
EdSketch: Execution-Driven Sketching for JavaEdSketch: Execution-Driven Sketching for Java
EdSketch: Execution-Driven Sketching for Java
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Design and Analysis of algorithms
Design and Analysis of algorithmsDesign and Analysis of algorithms
Design and Analysis of algorithms
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
 
Cis435 week02
Cis435 week02Cis435 week02
Cis435 week02
 

More from zukun

My lyn tutorial 2009
My lyn tutorial 2009My lyn tutorial 2009
My lyn tutorial 2009zukun
 
ETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCVETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCVzukun
 
ETHZ CV2012: Information
ETHZ CV2012: InformationETHZ CV2012: Information
ETHZ CV2012: Informationzukun
 
Siwei lyu: natural image statistics
Siwei lyu: natural image statisticsSiwei lyu: natural image statistics
Siwei lyu: natural image statisticszukun
 
Lecture9 camera calibration
Lecture9 camera calibrationLecture9 camera calibration
Lecture9 camera calibrationzukun
 
Brunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer visionBrunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer visionzukun
 
Modern features-part-4-evaluation
Modern features-part-4-evaluationModern features-part-4-evaluation
Modern features-part-4-evaluationzukun
 
Modern features-part-3-software
Modern features-part-3-softwareModern features-part-3-software
Modern features-part-3-softwarezukun
 
Modern features-part-2-descriptors
Modern features-part-2-descriptorsModern features-part-2-descriptors
Modern features-part-2-descriptorszukun
 
Modern features-part-1-detectors
Modern features-part-1-detectorsModern features-part-1-detectors
Modern features-part-1-detectorszukun
 
Modern features-part-0-intro
Modern features-part-0-introModern features-part-0-intro
Modern features-part-0-introzukun
 
Lecture 02 internet video search
Lecture 02 internet video searchLecture 02 internet video search
Lecture 02 internet video searchzukun
 
Lecture 01 internet video search
Lecture 01 internet video searchLecture 01 internet video search
Lecture 01 internet video searchzukun
 
Lecture 03 internet video search
Lecture 03 internet video searchLecture 03 internet video search
Lecture 03 internet video searchzukun
 
Icml2012 tutorial representation_learning
Icml2012 tutorial representation_learningIcml2012 tutorial representation_learning
Icml2012 tutorial representation_learningzukun
 
Advances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer visionAdvances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer visionzukun
 
Gephi tutorial: quick start
Gephi tutorial: quick startGephi tutorial: quick start
Gephi tutorial: quick startzukun
 
EM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysisEM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysiszukun
 
Object recognition with pictorial structures
Object recognition with pictorial structuresObject recognition with pictorial structures
Object recognition with pictorial structureszukun
 
Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities zukun
 

More from zukun (20)

My lyn tutorial 2009
My lyn tutorial 2009My lyn tutorial 2009
My lyn tutorial 2009
 
ETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCVETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCV
 
ETHZ CV2012: Information
ETHZ CV2012: InformationETHZ CV2012: Information
ETHZ CV2012: Information
 
Siwei lyu: natural image statistics
Siwei lyu: natural image statisticsSiwei lyu: natural image statistics
Siwei lyu: natural image statistics
 
Lecture9 camera calibration
Lecture9 camera calibrationLecture9 camera calibration
Lecture9 camera calibration
 
Brunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer visionBrunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer vision
 
Modern features-part-4-evaluation
Modern features-part-4-evaluationModern features-part-4-evaluation
Modern features-part-4-evaluation
 
Modern features-part-3-software
Modern features-part-3-softwareModern features-part-3-software
Modern features-part-3-software
 
Modern features-part-2-descriptors
Modern features-part-2-descriptorsModern features-part-2-descriptors
Modern features-part-2-descriptors
 
Modern features-part-1-detectors
Modern features-part-1-detectorsModern features-part-1-detectors
Modern features-part-1-detectors
 
Modern features-part-0-intro
Modern features-part-0-introModern features-part-0-intro
Modern features-part-0-intro
 
Lecture 02 internet video search
Lecture 02 internet video searchLecture 02 internet video search
Lecture 02 internet video search
 
Lecture 01 internet video search
Lecture 01 internet video searchLecture 01 internet video search
Lecture 01 internet video search
 
Lecture 03 internet video search
Lecture 03 internet video searchLecture 03 internet video search
Lecture 03 internet video search
 
Icml2012 tutorial representation_learning
Icml2012 tutorial representation_learningIcml2012 tutorial representation_learning
Icml2012 tutorial representation_learning
 
Advances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer visionAdvances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer vision
 
Gephi tutorial: quick start
Gephi tutorial: quick startGephi tutorial: quick start
Gephi tutorial: quick start
 
EM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysisEM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysis
 
Object recognition with pictorial structures
Object recognition with pictorial structuresObject recognition with pictorial structures
Object recognition with pictorial structures
 
Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities
 

Recently uploaded

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 

Recently uploaded (20)

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 

Skiena algorithm 2007 lecture15 backtracing

  • 1. Lecture 15: Backtracking Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794–4400 http://www.cs.sunysb.edu/∼skiena
  • 2. Problem of the Day The single-destination shortest path problem for a directed graph is to find the shortest path from every vertex to a specified vertex v. Give an efficient algorithm to solve the single-destination shortest paths problem.
  • 3. Problem of the Day Let G be a weighted directed graph with n vertices and m edges, where all edges have positive weight. A directed cycle is a directed path that starts and ends at the same vertex and contains at least one edge. Give an O(n3 ) algorithm to find a directed cycle in G of minimum total weight. Partial credit will be given for an O(n2 m) algorithm.
  • 4. Sudoku 1 2 6 7 3 8 9 4 5 1 2 3 5 9 1 2 7 3 5 4 8 6 6 7 8 4 5 6 1 2 9 7 3 7 3 7 9 8 2 6 1 3 5 4 4 8 5 2 6 4 7 3 8 9 1 1 1 3 4 5 8 9 2 6 7 1 2 4 6 9 1 2 8 7 3 5 8 4 2 8 7 3 5 6 1 4 9 5 6 3 5 1 9 4 7 6 2 8
  • 5. Solving Sudoku Solving Sudoku puzzles involves a form of exhaustive search of possible configurations. However, exploiting constraints to rule out certain possibili- ties for certain positions enables us to prune the search to the point people can solve Sudoku by hand. Backtracking is the key to implementing exhaustive search programs correctly and efficiently.
  • 6. Backtracking Backtracking is a systematic method to iterate through all the possible configurations of a search space. It is a general algorithm/technique which must be customized for each individual application. In the general case, we will model our solution as a vector a = (a1 , a2, ..., an), where each element ai is selected from a finite ordered set Si. Such a vector might represent an arrangement where ai contains the ith element of the permutation. Or the vector might represent a given subset S, where ai is true if and only if the ith element of the universe is in S.
  • 7. The Idea of Backtracking At each step in the backtracking algorithm, we start from a given partial solution, say, a = (a1 , a2 , ..., ak ), and try to extend it by adding another element at the end. After extending it, we must test whether what we have so far is a solution. If not, we must then check whether the partial solution is still potentially extendible to some complete solution. If so, recur and continue. If not, we delete the last element from a and try another possibility for that position, if one exists.
  • 8. Recursive Backtracking Backtrack(a, k) if a is a solution, print(a) else { k =k+1 compute Sk while Sk = ∅ do ak = an element in Sk S k = S k − ak Backtrack(a, k) }
  • 9. Backtracking and DFS Backtracking is really just depth-first search on an implicit graph of configurations. Backtracking can easily be used to iterate through all subsets or permutations of a set. Backtracking ensures correctness by enumerating all possi- bilities. For backtracking to be efficient, we must prune the search space.
  • 10. Implementation bool finished = FALSE; (* found all solutions yet? *) backtrack(int a[], int k, data input) { int c[MAXCANDIDATES]; (* candidates for next position *) int ncandidates; (* next position candidate count *) int i; (* counter *) if (is a solution(a,k,input)) process solution(a,k,input); else { k = k+1; construct candidates(a,k,input,c,&ncandidates); for (i=0; i<ncandidates; i++) { a[k] = c[i]; backtrack(a,k,input); if (finished) return; (* terminate early *) } } }
  • 11. is a solution(a,k,input) This Boolean function tests whether the first k elements of vector a are a complete solution for the given problem. The last argument, input, allows us to pass general information into the routine.
  • 12. construct candidates(a,k,input,c,nca This routine fills an array c with the complete set of possible candidates for the kth position of a, given the contents of the first k − 1 positions. The number of candidates returned in this array is denoted by ncandidates.
  • 13. process solution(a,k) This routine prints, counts, or somehow processes a complete solution once it is constructed. Backtracking ensures correctness by enumerating all possi- bilities. It ensures efficiency by never visiting a state more than once. Because a new candidates array c is allocated with each recursive procedure call, the subsets of not-yet-considered extension candidates at each position will not interfere with each other.
  • 14. Constructing all Subsets How many subsets are there of an n-element set? To construct all 2n subsets, set up an array/vector of n cells, where the value of ai is either true or false, signifying whether the ith item is or is not in the subset. To use the notation of the general backtrack algorithm, Sk = (true, f alse), and v is a solution whenever k ≥ n. What order will this generate the subsets of {1, 2, 3}? (1) → (1, 2) → (1, 2, 3)∗ → (1, 2, −)∗ → (1, −) → (1, −, 3)∗ → (1, −, −)∗ → (1, −) → (1) → (−) → (−, 2) → (−, 2, 3)∗ → (−, 2, −)∗ → (−, −) → (−, −, 3)∗ → (−, −, −)∗ → (−, −) → (−) → ()
  • 15. Constructing All Subsets We can construct the 2n subsets of n items by iterating through all possible 2n length-n vectors of true or false, letting the ith element denote whether item i is or is not in the subset. Using the notation of the general backtrack algorithm, Sk = (true, f alse), and a is a solution whenever k ≥ n. is a solution(int a[], int k, int n) { return (k == n); (* is k == n? *) } construct candidates(int a[], int k, int n, int c[], int *ncandidates) { c[0] = TRUE; c[1] = FALSE; *ncandidates = 2; }
  • 16. process solution(int a[], int k) { int i; (* counter *) printf(”(”); for (i=1; i<=k; i++) if (a[i] == TRUE) printf(” printf(” )”); }
  • 17. Main Routine: Subsets Finally, we must instantiate the call to backtrack with the right arguments. generate subsets(int n) { int a[NMAX]; (* solution vector *) backtrack(a,0,n); }
  • 18. Constructing all Permutations How many permutations are there of an n-element set? To construct all n! permutations, set up an array/vector of n cells, where the value of ai is an integer from 1 to n which has not appeared thus far in the vector, corresponding to the ith element of the permutation. To use the notation of the general backtrack algorithm, Sk = (1, . . . , n) − v, and v is a solution whenever k ≥ n. (1) → (1, 2) → (1, 2, 3)∗ → (1, 2) → (1) → (1, 3) → (1, 3, 2)∗ → (1, 3) → (1) → () → (2) → (2, 1) → (2, 1, 3)∗ → (2, 1) → (2) → (2, 3) → (2, 3, 1)∗ → (2, 3) → () (2) → () → (3) → (3, 1)(3, 1, 2)∗ → (3, 1) → (3) → (3, 2) → (3, 2, 1)∗ → (3, 2) → (3) → ()
  • 19. Constructing All Permutations To avoid repeating permutation elements, Sk = {1, . . . , n} − a, and a is a solution whenever k = n: construct candidates(int a[], int k, int n, int c[], int *ncandidates) { int i; (* counter *) bool in perm[NMAX]; (* who is in the permutation? *) for (i=1; i<NMAX; i++) in perm[i] = FALSE; for (i=0; i<k; i++) in perm[ a[i] ] = TRUE; *ncandidates = 0; for (i=1; i<=n; i++) if (in perm[i] == FALSE) { c[ *ncandidates] = i; *ncandidates = *ncandidates + 1; } }
  • 20. Auxilliary Routines Completing the job of generating permutations requires specifying process solution and is a solution, as well as setting the appropriate arguments to backtrack. All are essentially the same as for subsets: process solution(int a[], int k) { int i; (* counter *) for (i=1; i<=k; i++) printf(” printf(””); } is a solution(int a[], int k, int n) { return (k == n); }
  • 21. Main Program: Permutations generate permutations(int n) { int a[NMAX]; (* solution vector *) backtrack(a,0,n); }
  • 22. Problem of the Day A derangement is a permutation p of {1, . . . , n} such that no item is in its proper position, i.e. pi = i for all 1 ≤ i ≤ n. Write an efficient backtracking program with pruning that constructs all the derangements of n items.
  • 23. The Eight-Queens Problem The eight queens problem is a classical puzzle of positioning eight queens on an 8 × 8 chessboard such that no two queens threaten each other.
  • 24. Eight Queens: Representation What is concise, efficient representation for an n-queens solution, and how big must it be? Since no two queens can occupy the same column, we know that the n columns of a complete solution must form a permutation of n. By avoiding repetitive elements, we reduce our search space to just 8! = 40,320 – clearly short work for any reasonably fast machine. The critical routine is the candidate constructor. We repeatedly check whether the kth square on the given row is threatened by any previously positioned queen. If so, we move on, but if not we include it as a possible candidate:
  • 25. Candidate Constructor: Eight Queens construct candidates(int a[], int k, int n, int c[], int *ncandidates) { int i,j; (* counters *) bool legal move; (* might the move be legal? *) *ncandidates = 0; for (i=1; i<=n; i++) { legal move = TRUE; for (j=1; j<k; j++) { if (abs((k)-j) == abs(i-a[j])) (* diagonal threat *) legal move = FALSE; if (i == a[j]) (* column threat *) legal move = FALSE; } if (legal move == TRUE) { c[*ncandidates] = i; *ncandidates = *ncandidates + 1; } } }
  • 26. The remaining routines are simple, particularly since we are only interested in counting the solutions, not displaying them: process solution(int a[], int k) { int i; (* counter *) solution count ++; } is a solution(int a[], int k, int n) { return (k == n); }
  • 27. Finding the Queens: Main Program nqueens(int n) { int a[NMAX]; (* solution vector *) solution count = 0; backtrack(a,0,n); printf(”n=} This program can find the 365,596 solutions for n = 14 in minutes.
  • 28. Can Eight Pieces Cover a Chess Board? Consider the 8 main pieces in chess (king, queen, two rooks, two bishops, two knights). Can they be positioned on a chessboard so every square is threatened? N B R N R B Q K
  • 29. Combinatorial Search Only 63 square are threatened in this configuration. Since 1849, no one had been able to find an arrangement with bishops on different colors to cover all squares. We can resolve this question by searching through all possible board configurations if we spend enough time. We will use it as an example of how to attack a combinatorial search problem. With clever use of backtracking and pruning techniques, surprisingly large problems can be solved by exhaustive search.
  • 30. How Many Chess Configurations Must be Tested? Picking a square for each piece gives us the bound: 64!/(64 − 8)! = 178, 462, 987, 637, 760 ≈ 1015 Anything much larger than 108 is unreasonable to search on a modest computer in a modest amount of time.
  • 31. Exploiting Symmetry However, we can exploit symmetry to save work. With reflections along horizontal, vertical, and diagonal axis, the queen can go in only 10 non-equivallent positions. Even better, we can restrict the white bishop to 16 spots and the queen to 16, while being certain that we get all distinct configurations. b b b b b . . . b b b Q Q Q b . . . . b b . . . b . . . b . . . . 16 × 16 × 32 × 64 × 2080 × 2080 = 2, 268, 279, 603, 200 ≈ 10 12
  • 32. Covering the Chess Board In covering the chess board, we prune whenever we find there is a square which we cannot cover given the initial configuration! Specifically, each piece can threaten a certain maximum number of squares (queen 27, king 8, rook 14, etc.) We prune whenever the number of unthreated squares exceeds the sum of the maximum remaining coverage. As implemented by a graduate student project, this backtrack search eliminates 95% of the search space, when the pieces are ordered by decreasing mobility. With precomputing the list of possible moves, this program could search 1,000 positions per second.
  • 33. End Game But this is still too slow! 1012 /103 = 109 seconds > 1000 days Although we might further speed the program by an order of magnitude, we need to prune more nodes! By using a more clever algorithm, we eventually were able to prove no solution existed, in less than one day’s worth of computing. You too can fight the combinatorial explosion!
  • 34. The Backtracking Contest: Bandwidth The bandwidth problem takes as input a graph G, with n vertices and m edges (ie. pairs of vertices). The goal is to find a permutation of the vertices on the line which minimizes the maximum length of any edge. 1 2 3 4 5 6 7 8 1 8 2 7 3 6 4 5 The bandwidth problem has a variety of applications, includ- ing circuit layout, linear algebra, and optimizing memory usage in hypertext documents.
  • 35. The problem is NP-complete, meaning that it is exceedingly unlikely that you will be able to find an algorithm with polynomial worst-case running time. It remains NP-complete even for restricted classes of trees. Since the goal of the problem is to find a permutation, a backtracking program which iterates through all the n! possible permutations and computes the length of the longest edge for each gives an easy O(n! · m) algorithm. But the goal of this assignment is to find as practically good an algorithm as possible.
  • 36. The Backtracking Contest: Set Cover The set cover problem takes as input a collection of subsets S = {S1, . . . , Sm} of the universal set U = {1, . . . , n}. The goal is to find the smallest subset of the subsets T such that |T | ∪i=1Ti = U .
  • 37. Set cover arises when you try to efficiently acquire or represent items that have been packaged in a fixed set of lots. You want to obtain all the items, while buying as few lots as possible. Finding a cover is easy, because you can always buy one of each lot. However, by finding a small set cover you can do the same job for less money. Since the goal of the problem is to find a subset, a backtracking program which iterates through all the 2m possible subsets and tests whether it represents a cover gives an easy O(2m ·nm) algorithm. But the goal of this assignment is to find as practically good an algorithm as possible.
  • 38. Rules of the Game 1. Everyone must do this assignment separately. Just this once, you are not allowed to work with your partner. The idea is to think about the problem from scratch. 2. If you do not completely understand what the problem is, you don’t have the slightest chance of producing a working program. Don’t be afraid to ask for a clarification or explanation!!!!! 3. There will be a variety of different data files of different sizes. Test on the smaller files first. Do not be afraid to create your own test files to help debug your program. 4. The data files are available via the course WWW page.
  • 39. 5. You will be graded on how fast and clever your program is, not on style. No credit will be given for incorrect programs. 6. The programs are to run on the whatever computer you have access to, although it must be vanilla enough that I can run the program on something I have access to. 7. You are to turn in a listing of your program, along with a brief description of your algorithm and any interesting optimizations, sample runs, and the time it takes on sample data files. Report the largest test file your program could handle in one minute or less of wall clock time. 8. The top five self-reported times / largest sizes will be collected and tested by me to determine the winner.
  • 40. Producing Efficient Programs 1. Don’t optimize prematurely: Worrying about recursion vs. iteration is counter-productive until you have worked out the best way to prune the tree. That is where the money is. 2. Choose your data structures for a reason: What operations will you be doing? Is case of insertion/deletion more crucial than fast retrieval? When in doubt, keep it simple, stupid (KISS). 3. Let the profiler determine where to do final tuning: Your program is probably spending time where you don’t expect.