2. Algorithm 10.2.2 Crossword Puzzle This algorithm solves a crossword puzzle, represented by a Boolean matrix D [ i , j ], 1 = i,j = n , and a finite set of words W ⊆ Σ * . Σ is the alphabet, and D [ i , j ] is true if the square is blank and false if it is blocked. We construct the solution in a new matrix S [ i , j ], 1 = i,j = n . The algorithm returns true if the crossword can be solved. Input Parameters: D , W Output Parameters: None puzzle ( D , W ) { for i = 1 to n for j = 1 to n if ( D [ i , j ]) S [ i , j ] = guess ( Σ ) else S [ i , j ] = blocked for each word w in S if ( w W ) return false return true }
3. Algorithm 10.2.15 Graph k -coloring This algorithm finds a k -coloring of G = ( V , E ), if there is one, and stores it in the array c . The algorithm returns true if a coloring is found. Input Parameters: G = ( V , E ), k Output Parameters: None graph_coloring ( G , k ) { for each v in V c [ v ] = guess ({1,2,..., k }) for each v in V for each w in N ( v ) if ( c [ w ] == c [ v ]) return false return true }
4. Algorithm 10.2.19 Hamiltonian Cycle This algorithm finds a Hamiltonian cycle in G = ( V , E ) if there is one and returns true in that case.
5. Input Parameter: G = ( V , E ) Output Parameters: None hamiltonian_cycle ( G ) { n = | V | for i = 1 to n visited [ i ] = false for i = 1 to n { c [ i ] = guess ( V ) visited [ c [ i ]] = true } c [0] = c [ n ] // first node is the same as last // check that only edges of G are used for i = 0 to n - 1 if (( c [ i ], c [ i + 1]) E ) return false // check that all vertices have been visited for i = 1 to n if (!( visited [ i ])) return false return true }
6. Algorithm 10.2.22 TSP This algorithm finds a Hamiltonian cycle in G = ( V , E , weight ) of total weight at most w if there is one and returns true in that case. Input Parameters: G = ( V , E , weight ), w Output Parameters: None tsp ( G , w ) { n = | V | for i = 1 to n c [ i ] = guess ( V ) c [0] = c [ n ] ...
7.
8. Example 10.2.26 graph_coloring ( G , k ) { for each v in V c [ v ] = 0 for each v in V { c [ v ] = guess ({1,2,..., k }) for each w in N ( v ) if ( c [ w ] == c [ v ]) return false } return true }
9. Algorithm 10.3.18 Satisfiability Witness This algorithm takes as input a CNF formula ϕ on variables x 1 ,...,x n , and either returns a satisfying assignment for ϕ in the array x or false if there is no such assignment. It assumes that we have an algorithm A that decides whether a formula is satisfiable or not.
10. Input Parameter: ϕ Output Parameter: x satisfiability_witness ( ϕ , x ) { if (!(A( ϕ )) return false for i = 1 to n { ψ = ϕ [ x i -> true] if (A( ϕ )) { x [ i ] = true ϕ = ψ } else { x[i] = false ϕ = ϕ [ x i -> false] } } return true }