Chap10alg

download Chap10alg

of 10

Embed Size (px)

Transcript of Chap10alg

  • 1. CHAPTER 10 P and NP

2. Algorithm 10.2.2 Crossword Puzzle This algorithm solves a crossword puzzle, represented by a Boolean matrixD [ i , j ], 1 =i,j=n, and a finite set of wordsW * .is the alphabet, andD [ i , j ] is true if the square is blank and false if it is blocked. We construct the solution in a new matrixS [ 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 ) { fori= 1 ton forj= 1 ton if ( D [ i , j ]) S [ i , j ] =guess ( ) else S [ i , j ] = blocked for each wordwinS if ( w W ) return false return true } 3. Algorithm 10.2.15 Graphk -coloring This algorithm finds ak -coloring ofG= ( V , E ), if there is one, and stores it in the arrayc. The algorithm returns true if a coloring is found. Input Parameters:G= ( V , E ),k Output Parameters: None graph_coloring ( G , k ) { for eachvinV c [ v ] =guess ({1,2,..., k }) for eachvinV for eachwinN ( v ) if ( c [ w ] ==c [ v ]) return false return true } 4. Algorithm 10.2.19 Hamiltonian Cycle This algorithm finds a Hamiltonian cycle inG= ( 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 | fori= 1 ton visited [ i ] = false fori= 1 ton{ c [ i ] =guess ( V ) visited [ c [ i ]] = true } c [0] =c [ n ] // first node is the same as last // check that only edges ofGare used fori= 0 ton- 1 if (( c [ i ], c [ i+ 1]) E ) return false // check that all vertices have been visited fori= 1 ton if (!( visited [ i ])) return false return true } 6. Algorithm 10.2.22 TSP This algorithm finds a Hamiltonian cycle inG= ( V ,E ,weight ) of total weight at mostwif there is one and returns true in that case. Input Parameters:G= ( V ,E ,weight ),w Output Parameters: None tsp ( G , w ) { n= | V | fori= 1 ton c [ i ] =guess ( V ) c [0] =c [ n ] ... 7.

  • ...
  • // check that only edges of G are used,
  • // and compute the total weight of the tour
  • totalweight= 0
    • for i = 1 to n - 1
    • if ((c[i],c[i + 1]) E)
    • return false
    • else
    • totalweight = totalweight + weight((c[i],c[i + 1]))
    • // reject tours whose total weight is too large
  • if ( totalweight>w )
  • return false
  • // check that all vertices are visited
  • for i = 1 to n
  • visited[i] = false
  • for i = 1 to n
  • visited[c[i]] = true
  • for i = 1 to n
  • if (!(visited[i]))
  • return false
  • return true
  • }

8. Example 10.2.26 graph_coloring ( G , k ) { for eachvinV c [ v ] = 0 for eachvinV{ c [ v ] =guess ({1,2,..., k }) for eachwinN ( 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 forin the arrayxor false if there is no such assignment. It assumes that we have an algorithmAthat decides whether a formula is satisfiable or not. 10. Input Parameter: Output Parameter:x satisfiability_witness ( ,x ) { if (!(A( )) return false fori= 1 ton{ = [ x i -> true] if (A( )) { x [ i ] = true = } else { x[i] = false = [ x i -> false] } } return true }