
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































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.
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.
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.
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) }
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 *) } } }
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.
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.
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(โ )โ); }
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); }
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; } }
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); }