The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998

18.04.2013 Views

Constructing All Permutations Next: Constructing All Paths in Up: Backtracking Previous: Constructing All Subsets Constructing All Permutations To design a suitable state space for representing permutations, we start by counting them. There are n distinct choices for the value of the first element of a permutation of . Once we have fixed this value of , there are n-1 candidates remaining for the second position, since we can have any value except (repetitions are forbidden). Repeating this argument yields a total of distinct permutations. This counting argument suggests a suitable representation. To construct all n! permutations, set up an array/vector A of n cells. The set of candidates for the ith position will be the set of elements that have not appeared in the (i-1) elements of the partial solution, corresponding to the first i-1 elements of the permutation. To use the notation of the general backtrack algorithm, . The vector A contains a full solution whenever k = n+1. This representation generates the permutations of in the following order: The problem of generating permutations is more thoroughly discussed in Section . Algorithms Mon Jun 2 23:33:50 EDT 1997 file:///E|/BOOK/BOOK2/NODE86.HTM [19/1/2003 1:29:26]

Constructing All Paths in a Graph Next: Search Pruning Up: Backtracking Previous: Constructing All Permutations Constructing All Paths in a Graph Enumerating all the simple paths from to t through a given graph is a somewhat more complicated problem than listing permutations or subsets. Unlike the earlier problems, there is no explicit formula that counts the number of solutions as a function of the number of edges or vertices, because the number of paths depends upon the structure of the graph. Figure: The search tree enumerating all simple paths from vertex 1 in the graph Since the starting point of any path from to t is always , must be . The set of possible candidates for the second position are the vertices v such that (,v) is an edge of the graph, for the path wanders from vertex to vertex using edges to define the legal steps. In general, consists of the set of vertices adjacent to that have not been used in the partial solution A. We can report a successful path whenever . The solution vector A must have room for all n vertices, although most paths are likely to be shorter than this. Figure shows the search tree giving all paths from a particular vertex in an example graph. Algorithms Mon Jun 2 23:33:50 EDT 1997 file:///E|/BOOK/BOOK2/NODE87.HTM [19/1/2003 1:29:26]

Constructing All Paths in a Graph<br />

Next: Search Pruning Up: Backtracking Previous: Constructing All Permutations<br />

Constructing All Paths in a Graph<br />

Enumerating all the simple paths from to t through a given graph is a somewhat more complicated<br />

problem than listing permutations or subsets. Unlike the earlier problems, there is no explicit formula<br />

that counts the number of solutions as a function of the number of edges or vertices, because the number<br />

of paths depends upon the structure of the graph.<br />

Figure: <strong>The</strong> search tree enumerating all simple paths from vertex 1 in the graph<br />

Since the starting point of any path from to t is always , must be . <strong>The</strong> set of possible candidates for<br />

the second position are the vertices v such that (,v) is an edge of the graph, for the path wanders from<br />

vertex to vertex using edges to define the legal steps. In general, consists of the set of vertices<br />

adjacent to that have not been used in the partial solution A. We can report a successful path whenever<br />

. <strong>The</strong> solution vector A must have room for all n vertices, although most paths are likely to be<br />

shorter than this. Figure shows the search tree giving all paths from a particular vertex in an example<br />

graph.<br />

<strong>Algorithm</strong>s<br />

Mon Jun 2 23:33:50 EDT 1997<br />

file:///E|/BOOK/BOOK2/NODE87.HTM [19/1/2003 1:29:26]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!