SlideShare ist ein Scribd-Unternehmen logo
1 von 10
CHAPTER 10 P and NP
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 }
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 }
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.
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 }
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 ] ...
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 }
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.
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 }

Weitere ähnliche Inhalte

Was ist angesagt?

Appendix b 2
Appendix b 2Appendix b 2
Appendix b 2
Loc Tran
 
PC Test 2 study guide 2011
PC Test 2 study guide 2011PC Test 2 study guide 2011
PC Test 2 study guide 2011
vhiggins1
 

Was ist angesagt? (16)

All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
 
Unit 03
Unit 03Unit 03
Unit 03
 
Unit 02
Unit 02Unit 02
Unit 02
 
Unit 04
Unit 04Unit 04
Unit 04
 
ikh323-05
ikh323-05ikh323-05
ikh323-05
 
Chap05alg
Chap05algChap05alg
Chap05alg
 
[Question Paper] Applied Mathematics – I (Revised Course) [April / 2014]
[Question Paper] Applied Mathematics – I (Revised Course) [April / 2014][Question Paper] Applied Mathematics – I (Revised Course) [April / 2014]
[Question Paper] Applied Mathematics – I (Revised Course) [April / 2014]
 
Appendix b 2
Appendix b 2Appendix b 2
Appendix b 2
 
Appendix b 2
Appendix b 2Appendix b 2
Appendix b 2
 
DSP System Assignment Help
DSP System Assignment HelpDSP System Assignment Help
DSP System Assignment Help
 
Arrays
ArraysArrays
Arrays
 
Tensor Train data format for uncertainty quantification
Tensor Train data format for uncertainty quantificationTensor Train data format for uncertainty quantification
Tensor Train data format for uncertainty quantification
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
Green Theorem
Green TheoremGreen Theorem
Green Theorem
 
PC Test 2 study guide 2011
PC Test 2 study guide 2011PC Test 2 study guide 2011
PC Test 2 study guide 2011
 
report
reportreport
report
 

Andere mochten auch (7)

10 pasos para develar el secreto
10 pasos para develar el secreto10 pasos para develar el secreto
10 pasos para develar el secreto
 
Product to a Power
Product to a PowerProduct to a Power
Product to a Power
 
Act comprensión 1
Act comprensión 1Act comprensión 1
Act comprensión 1
 
Proportions review
Proportions reviewProportions review
Proportions review
 
Power to a power
Power to a powerPower to a power
Power to a power
 
Act asimilación 4 te
Act asimilación 4 teAct asimilación 4 te
Act asimilación 4 te
 
Kindvriendelijke ontwerpen 1 - studiedag Speelruimte
Kindvriendelijke ontwerpen 1 - studiedag SpeelruimteKindvriendelijke ontwerpen 1 - studiedag Speelruimte
Kindvriendelijke ontwerpen 1 - studiedag Speelruimte
 

Ähnlich wie Chap10alg

Important Cuts and (p,q)-clustering
Important Cuts and (p,q)-clusteringImportant Cuts and (p,q)-clustering
Important Cuts and (p,q)-clustering
ASPAK2014
 
sublabel accurate convex relaxation of vectorial multilabel energies
sublabel accurate convex relaxation of vectorial multilabel energiessublabel accurate convex relaxation of vectorial multilabel energies
sublabel accurate convex relaxation of vectorial multilabel energies
Fujimoto Keisuke
 
Class1
 Class1 Class1
Class1
issbp
 

Ähnlich wie Chap10alg (20)

Chap11alg
Chap11algChap11alg
Chap11alg
 
CostFunctions.pdf
CostFunctions.pdfCostFunctions.pdf
CostFunctions.pdf
 
Complexity Classes and the Graph Isomorphism Problem
Complexity Classes and the Graph Isomorphism ProblemComplexity Classes and the Graph Isomorphism Problem
Complexity Classes and the Graph Isomorphism Problem
 
G03201034038
G03201034038G03201034038
G03201034038
 
Important Cuts and (p,q)-clustering
Important Cuts and (p,q)-clusteringImportant Cuts and (p,q)-clustering
Important Cuts and (p,q)-clustering
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdf
 
Hay hay
Hay hayHay hay
Hay hay
 
Unit 3-Greedy Method
Unit 3-Greedy MethodUnit 3-Greedy Method
Unit 3-Greedy Method
 
Bellman ford
Bellman fordBellman ford
Bellman ford
 
Chap06alg
Chap06algChap06alg
Chap06alg
 
Chap06alg
Chap06algChap06alg
Chap06alg
 
Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
sublabel accurate convex relaxation of vectorial multilabel energies
sublabel accurate convex relaxation of vectorial multilabel energiessublabel accurate convex relaxation of vectorial multilabel energies
sublabel accurate convex relaxation of vectorial multilabel energies
 
m.tech final
m.tech finalm.tech final
m.tech final
 
Tensor Completion for PDEs with uncertain coefficients and Bayesian Update te...
Tensor Completion for PDEs with uncertain coefficients and Bayesian Update te...Tensor Completion for PDEs with uncertain coefficients and Bayesian Update te...
Tensor Completion for PDEs with uncertain coefficients and Bayesian Update te...
 
Quantum Machine Learning and QEM for Gaussian mixture models (Alessandro Luongo)
Quantum Machine Learning and QEM for Gaussian mixture models (Alessandro Luongo)Quantum Machine Learning and QEM for Gaussian mixture models (Alessandro Luongo)
Quantum Machine Learning and QEM for Gaussian mixture models (Alessandro Luongo)
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Class1
 Class1 Class1
Class1
 

Mehr von Munkhchimeg (20)

Protsesor
ProtsesorProtsesor
Protsesor
 
Lecture916
Lecture916Lecture916
Lecture916
 
Lecture915
Lecture915Lecture915
Lecture915
 
Lecture914
Lecture914Lecture914
Lecture914
 
Lecture913
Lecture913Lecture913
Lecture913
 
Lecture911
Lecture911Lecture911
Lecture911
 
Lecture912
Lecture912Lecture912
Lecture912
 
Lecture910
Lecture910Lecture910
Lecture910
 
Lecture5
Lecture5Lecture5
Lecture5
 
Lecture9
Lecture9Lecture9
Lecture9
 
Lecture8
Lecture8Lecture8
Lecture8
 
Lecture7
Lecture7Lecture7
Lecture7
 
Lecture6
Lecture6Lecture6
Lecture6
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture3
Lecture3Lecture3
Lecture3
 
Ded Algorithm
Ded AlgorithmDed Algorithm
Ded Algorithm
 
Ded Algorithm1
Ded Algorithm1Ded Algorithm1
Ded Algorithm1
 
Tobch Lecture
Tobch LectureTobch Lecture
Tobch Lecture
 
Lecture914
Lecture914Lecture914
Lecture914
 
Tobch Lecture
Tobch LectureTobch Lecture
Tobch Lecture
 

Kürzlich hochgeladen

Ghansoli Escorts Services 09167354423 Ghansoli Call Girls,Call Girls In Ghan...
Ghansoli Escorts Services 09167354423  Ghansoli Call Girls,Call Girls In Ghan...Ghansoli Escorts Services 09167354423  Ghansoli Call Girls,Call Girls In Ghan...
Ghansoli Escorts Services 09167354423 Ghansoli Call Girls,Call Girls In Ghan...
Priya Reddy
 
Pakistani Call girls in Deira 0567006274 Deira Call girls
Pakistani Call girls in Deira 0567006274 Deira Call girlsPakistani Call girls in Deira 0567006274 Deira Call girls
Pakistani Call girls in Deira 0567006274 Deira Call girls
Monica Sydney
 
Deira Call girl agency 0567006274 Call girls in Deira
Deira Call girl agency 0567006274 Call girls in DeiraDeira Call girl agency 0567006274 Call girls in Deira
Deira Call girl agency 0567006274 Call girls in Deira
Monica Sydney
 
Dubai Call girls Service 0524076003 Call girls services in Dubai
Dubai Call girls Service 0524076003 Call girls services in DubaiDubai Call girls Service 0524076003 Call girls services in Dubai
Dubai Call girls Service 0524076003 Call girls services in Dubai
Monica Sydney
 
Dubai Call girls Service 0524076003 Call girls in Dubai
Dubai Call girls Service 0524076003 Call girls in DubaiDubai Call girls Service 0524076003 Call girls in Dubai
Dubai Call girls Service 0524076003 Call girls in Dubai
Monica Sydney
 
Haridwar Call Girls, 8699214473 Hot Girls Service Haridwar
Haridwar Call Girls, 8699214473 Hot Girls Service HaridwarHaridwar Call Girls, 8699214473 Hot Girls Service Haridwar
Haridwar Call Girls, 8699214473 Hot Girls Service Haridwar
ranekokila
 

Kürzlich hochgeladen (20)

Bhubaneswar🌹Patia ❤CALL GIRLS 9777949614 💟 CALL GIRLS IN bhubaneswar ESCORT S...
Bhubaneswar🌹Patia ❤CALL GIRLS 9777949614 💟 CALL GIRLS IN bhubaneswar ESCORT S...Bhubaneswar🌹Patia ❤CALL GIRLS 9777949614 💟 CALL GIRLS IN bhubaneswar ESCORT S...
Bhubaneswar🌹Patia ❤CALL GIRLS 9777949614 💟 CALL GIRLS IN bhubaneswar ESCORT S...
 
Dahod Call Girl 📞 8617370543 Low Price Genuine Service
Dahod Call Girl 📞 8617370543 Low Price Genuine ServiceDahod Call Girl 📞 8617370543 Low Price Genuine Service
Dahod Call Girl 📞 8617370543 Low Price Genuine Service
 
Ghansoli Escorts Services 09167354423 Ghansoli Call Girls,Call Girls In Ghan...
Ghansoli Escorts Services 09167354423  Ghansoli Call Girls,Call Girls In Ghan...Ghansoli Escorts Services 09167354423  Ghansoli Call Girls,Call Girls In Ghan...
Ghansoli Escorts Services 09167354423 Ghansoli Call Girls,Call Girls In Ghan...
 
Call Girls Bijnor Just Call 8617370543 Top Class Call Girl Service Available
Call Girls Bijnor  Just Call 8617370543 Top Class Call Girl Service AvailableCall Girls Bijnor  Just Call 8617370543 Top Class Call Girl Service Available
Call Girls Bijnor Just Call 8617370543 Top Class Call Girl Service Available
 
Pakistani Call girls in Deira 0567006274 Deira Call girls
Pakistani Call girls in Deira 0567006274 Deira Call girlsPakistani Call girls in Deira 0567006274 Deira Call girls
Pakistani Call girls in Deira 0567006274 Deira Call girls
 
Call girls Service Khammam - 9332606886 Rs 3000 Free Pickup & Drop Services 2...
Call girls Service Khammam - 9332606886 Rs 3000 Free Pickup & Drop Services 2...Call girls Service Khammam - 9332606886 Rs 3000 Free Pickup & Drop Services 2...
Call girls Service Khammam - 9332606886 Rs 3000 Free Pickup & Drop Services 2...
 
Call girls Service in Deira 0507330913 Deira Call girls
Call girls Service in Deira 0507330913 Deira Call girlsCall girls Service in Deira 0507330913 Deira Call girls
Call girls Service in Deira 0507330913 Deira Call girls
 
Genuine 8617370543 Hot and Beautiful 💕 Gomati Escorts call Girls
Genuine 8617370543 Hot and Beautiful 💕 Gomati Escorts call GirlsGenuine 8617370543 Hot and Beautiful 💕 Gomati Escorts call Girls
Genuine 8617370543 Hot and Beautiful 💕 Gomati Escorts call Girls
 
Hire 💕 8617370543 Dhalai Call Girls Service Call Girls Agency
Hire 💕 8617370543 Dhalai Call Girls Service Call Girls AgencyHire 💕 8617370543 Dhalai Call Girls Service Call Girls Agency
Hire 💕 8617370543 Dhalai Call Girls Service Call Girls Agency
 
Call Girls Bhubaneswar 9777949614 call me Independent Escort Service Bhubaneswar
Call Girls Bhubaneswar 9777949614 call me Independent Escort Service BhubaneswarCall Girls Bhubaneswar 9777949614 call me Independent Escort Service Bhubaneswar
Call Girls Bhubaneswar 9777949614 call me Independent Escort Service Bhubaneswar
 
Deira Call girl agency 0567006274 Call girls in Deira
Deira Call girl agency 0567006274 Call girls in DeiraDeira Call girl agency 0567006274 Call girls in Deira
Deira Call girl agency 0567006274 Call girls in Deira
 
Call Girls Rajnandgaon / 9332606886 Genuine Call girls with real Photos and N...
Call Girls Rajnandgaon / 9332606886 Genuine Call girls with real Photos and N...Call Girls Rajnandgaon / 9332606886 Genuine Call girls with real Photos and N...
Call Girls Rajnandgaon / 9332606886 Genuine Call girls with real Photos and N...
 
Call Girls In Gandhinagar 📞 8617370543 At Low Cost Cash Payment Booking
Call Girls In Gandhinagar 📞 8617370543  At Low Cost Cash Payment BookingCall Girls In Gandhinagar 📞 8617370543  At Low Cost Cash Payment Booking
Call Girls In Gandhinagar 📞 8617370543 At Low Cost Cash Payment Booking
 
Dubai Call girls Service 0524076003 Call girls services in Dubai
Dubai Call girls Service 0524076003 Call girls services in DubaiDubai Call girls Service 0524076003 Call girls services in Dubai
Dubai Call girls Service 0524076003 Call girls services in Dubai
 
Dubai Call girls Service 0524076003 Call girls in Dubai
Dubai Call girls Service 0524076003 Call girls in DubaiDubai Call girls Service 0524076003 Call girls in Dubai
Dubai Call girls Service 0524076003 Call girls in Dubai
 
Haridwar Call Girls, 8699214473 Hot Girls Service Haridwar
Haridwar Call Girls, 8699214473 Hot Girls Service HaridwarHaridwar Call Girls, 8699214473 Hot Girls Service Haridwar
Haridwar Call Girls, 8699214473 Hot Girls Service Haridwar
 
Hire 💕 8617370543 Auraiya Call Girls Service Call Girls Agency
Hire 💕 8617370543 Auraiya Call Girls Service Call Girls AgencyHire 💕 8617370543 Auraiya Call Girls Service Call Girls Agency
Hire 💕 8617370543 Auraiya Call Girls Service Call Girls Agency
 
Bhubaneswar🌹Call Girls Rasulgada ❤Komal 9777949614 💟 Full Trusted CALL GIRLS ...
Bhubaneswar🌹Call Girls Rasulgada ❤Komal 9777949614 💟 Full Trusted CALL GIRLS ...Bhubaneswar🌹Call Girls Rasulgada ❤Komal 9777949614 💟 Full Trusted CALL GIRLS ...
Bhubaneswar🌹Call Girls Rasulgada ❤Komal 9777949614 💟 Full Trusted CALL GIRLS ...
 
Deira call girls 0507330913 Call girls in Deira
Deira call girls 0507330913  Call girls in DeiraDeira call girls 0507330913  Call girls in Deira
Deira call girls 0507330913 Call girls in Deira
 
Hire 💕 8617370543 Khalilabad Call Girls Service Call Girls Agency
Hire 💕 8617370543 Khalilabad Call Girls Service Call Girls AgencyHire 💕 8617370543 Khalilabad Call Girls Service Call Girls Agency
Hire 💕 8617370543 Khalilabad Call Girls Service Call Girls Agency
 

Chap10alg

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