Docsity

Backtracking - Data Structures and Algorithm - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Backtracking - Data Structures and Algorithm - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Backtracking

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.

Sudoku

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.

The Idea of Backtracking

At each step in the backtracking algorithm, we start from a given partial solution, say, a = (a 1 , a 2 , ..., 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 6 = โˆ… do ak = an element in Sk Sk = Sk โˆ’ ak Backtrack(a, k) }

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.

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 2 n^ 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)โˆ— โ†’ (โˆ’, โˆ’, โˆ’)โˆ— โ†’ (โˆ’, โˆ’) โ†’ (โˆ’) โ†’ ()

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

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); }