Chap10alg

188 views
156 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
188
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Chap10alg

  1. 1. CHAPTER 10 P and NP
  2. 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. 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. 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. 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. 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. 7. <ul><li>... </li></ul><ul><li>// check that only edges of G are used, </li></ul><ul><li>// and compute the total weight of the tour </li></ul><ul><li>totalweight = 0 </li></ul><ul><ul><li>for i = 1 to n - 1 </li></ul></ul><ul><ul><li>if ((c[i],c[i + 1])  E) </li></ul></ul><ul><ul><li>return false </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>totalweight = totalweight + weight((c[i],c[i + 1])) </li></ul></ul><ul><ul><li>// reject tours whose total weight is too large </li></ul></ul><ul><li>if ( totalweight > w ) </li></ul><ul><li>return false </li></ul><ul><li>// check that all vertices are visited </li></ul><ul><li>for i = 1 to n </li></ul><ul><li>visited[i] = false </li></ul><ul><li>for i = 1 to n </li></ul><ul><li>visited[c[i]] = true </li></ul><ul><li>for i = 1 to n </li></ul><ul><li>if (!(visited[i])) </li></ul><ul><li>return false </li></ul><ul><li>return true </li></ul><ul><li>} </li></ul>
  8. 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. 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. 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 }

×