Chap10alg

10
CHAPTER 10 P and NP

Transcript of Chap10alg

Page 1: Chap10alg

CHAPTER 10

P and NP

Page 2: Chap10alg

Algorithm 10.2.2 Crossword PuzzleThis 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, WOutput Parameters: Nonepuzzle(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}

Page 3: Chap10alg

Algorithm 10.2.15 Graph k-coloringThis 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), kOutput Parameters: Nonegraph_coloring(G,k) {

for each v in Vc[v] = guess({1,2,...,k})

for each v in Vfor each w in N(v)

if (c[w] == c[v])return false

return true}

Page 4: Chap10alg

Algorithm 10.2.19 Hamiltonian CycleThis algorithm finds a Hamiltonian cycle in G = (V,E) if there is one and returns true in that case.

Page 5: Chap10alg

Input Parameter: G = (V,E)Output Parameters: Nonehamiltonian_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}

Page 6: Chap10alg

Algorithm 10.2.22 TSPThis 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), wOutput Parameters: Nonetsp(G,w) {

n = |V| for i = 1 to n c[i] = guess(V) c[0] = c[n]

...

Page 7: Chap10alg

...// 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}

Page 8: Chap10alg

Example 10.2.26

graph_coloring(G,k) {for each v in V

c[v] = 0for 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

}

Page 9: Chap10alg

Algorithm 10.3.18 Satisfiability WitnessThis algorithm takes as input a CNF formula ϕ on variables x1,...,xn, 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.

Page 10: Chap10alg

Input Parameter: ϕ Output Parameter: xsatisfiability_witness(ϕ, x) {

if (!(A(ϕ))return false

for i = 1 to n {ψ = ϕ[xi → true]if (A(ϕ)) {

x[i] = true ϕ = ψ

}else {

x[i] = false ϕ = ϕ[xi → false]

}}return true

}