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.

Like this presentation? Why not share!

- ph-report.doc by butest 416 views
- DynamiX Edumanage Cloud Service for... by Gautam Patnaik 256 views
- Daily forex report 22 september 20... by Epic Research Pri... 116 views
- Why Lotus Notes/Domino? by Kamoshita Yoshihiro 1374 views
- Martin Laverty, Catholic Health Aus... by Informa Australia 1130 views
- The Utilization of DHHS Program Eva... by Washington Evalua... 201 views

188 views

156 views

156 views

Published on

No Downloads

Total views

188

On SlideShare

0

From Embeds

0

Number of Embeds

0

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

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment