Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Successfully reported this slideshow.

Like this presentation? Why not share!

- 2015 Portfolio Highlight - Media Re... by Dannielle Blument... 258 views
- Chap08alg by Munhchimeg 416 views
- Lecture5 by Munhchimeg 391 views
- Keeping America On The Cutting Edge... by Dannielle Blument... 218 views
- Dannielle Blumenthal's blog, full t... by Dannielle Blument... 128 views
- Cyber-Vandalism Readiness Toolkit -... by Dannielle Blument... 363 views

234 views

Published on

No Downloads

Total views

234

On SlideShare

0

From Embeds

0

Number of Embeds

2

Shares

0

Downloads

0

Comments

0

Likes

1

No embeds

No notes for slide

- 1. CHAPTER 10 P and NP
- 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. <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. 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 }

No public clipboards found for this slide

Be the first to comment