SlideShare a Scribd company logo
1 of 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 }

More Related Content

What's hot

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
 

What's hot (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
 

Viewers also liked

Gagasan perda perlindungan anak kota bandung, juli 2011
Gagasan perda perlindungan anak kota bandung, juli 2011Gagasan perda perlindungan anak kota bandung, juli 2011
Gagasan perda perlindungan anak kota bandung, juli 2011
Zamzam Muzaki Sm
 
Berenschot persetatie Succesvol Aanbesteden 051010
Berenschot persetatie Succesvol Aanbesteden 051010Berenschot persetatie Succesvol Aanbesteden 051010
Berenschot persetatie Succesvol Aanbesteden 051010
Johan Stuiver
 
Pm104 2004 2005
Pm104 2004 2005Pm104 2004 2005
Pm104 2004 2005
Munhchimeg
 
Program Edukacyjny W Fabryce Emalii Oskara Schindlera
Program Edukacyjny W Fabryce Emalii Oskara SchindleraProgram Edukacyjny W Fabryce Emalii Oskara Schindlera
Program Edukacyjny W Fabryce Emalii Oskara Schindlera
guestbdec7c
 
(Application pdf object) 50 str
(Application pdf object) 50 str(Application pdf object) 50 str
(Application pdf object) 50 str
ABC
 

Viewers also liked (19)

Lecture8
Lecture8Lecture8
Lecture8
 
Gagasan perda perlindungan anak kota bandung, juli 2011
Gagasan perda perlindungan anak kota bandung, juli 2011Gagasan perda perlindungan anak kota bandung, juli 2011
Gagasan perda perlindungan anak kota bandung, juli 2011
 
Berenschot persetatie Succesvol Aanbesteden 051010
Berenschot persetatie Succesvol Aanbesteden 051010Berenschot persetatie Succesvol Aanbesteden 051010
Berenschot persetatie Succesvol Aanbesteden 051010
 
CUEB Presentation
CUEB PresentationCUEB Presentation
CUEB Presentation
 
Homelessness and Housing – Moving from Policy to Action - Frank Murtagh
Homelessness and Housing – Moving from Policy to Action - Frank MurtaghHomelessness and Housing – Moving from Policy to Action - Frank Murtagh
Homelessness and Housing – Moving from Policy to Action - Frank Murtagh
 
Lecture911
Lecture911Lecture911
Lecture911
 
Pirates v. Mercenaries: Purely Private Transnational Violence at the Margins ...
Pirates v. Mercenaries: Purely Private Transnational Violence at the Margins ...Pirates v. Mercenaries: Purely Private Transnational Violence at the Margins ...
Pirates v. Mercenaries: Purely Private Transnational Violence at the Margins ...
 
Pm104 2004 2005
Pm104 2004 2005Pm104 2004 2005
Pm104 2004 2005
 
Housing First: Ending Homelessness and Supporting Recovery - Dr Sam Tsemberis
Housing First: Ending Homelessness and Supporting Recovery - Dr Sam TsemberisHousing First: Ending Homelessness and Supporting Recovery - Dr Sam Tsemberis
Housing First: Ending Homelessness and Supporting Recovery - Dr Sam Tsemberis
 
Program Edukacyjny W Fabryce Emalii Oskara Schindlera
Program Edukacyjny W Fabryce Emalii Oskara SchindleraProgram Edukacyjny W Fabryce Emalii Oskara Schindlera
Program Edukacyjny W Fabryce Emalii Oskara Schindlera
 
Lecture913
Lecture913Lecture913
Lecture913
 
Lecture916
Lecture916Lecture916
Lecture916
 
Hybrid worlds fungi updated - crews
Hybrid worlds   fungi updated - crewsHybrid worlds   fungi updated - crews
Hybrid worlds fungi updated - crews
 
(Application pdf object) 50 str
(Application pdf object) 50 str(Application pdf object) 50 str
(Application pdf object) 50 str
 
Chap08alg
Chap08algChap08alg
Chap08alg
 
Lecture5
Lecture5Lecture5
Lecture5
 
データ集 トマ・ピケティ『21 世紀の資本』
データ集 トマ・ピケティ『21 世紀の資本』データ集 トマ・ピケティ『21 世紀の資本』
データ集 トマ・ピケティ『21 世紀の資本』
 
Lecture910
Lecture910Lecture910
Lecture910
 
Ded algorithm
Ded algorithmDed algorithm
Ded algorithm
 

Similar to 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
 

Similar to 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
 

More from Munhchimeg (20)

Ded algorithm1
Ded algorithm1Ded algorithm1
Ded algorithm1
 
Tobch lecture1
Tobch lecture1Tobch lecture1
Tobch lecture1
 
Tobch lecture
Tobch lectureTobch lecture
Tobch lecture
 
Recursive
RecursiveRecursive
Recursive
 
Protsesor
ProtsesorProtsesor
Protsesor
 
Lecture916
Lecture916Lecture916
Lecture916
 
Lecture915
Lecture915Lecture915
Lecture915
 
Lecture914
Lecture914Lecture914
Lecture914
 
Lecture913
Lecture913Lecture913
Lecture913
 
Lecture912
Lecture912Lecture912
Lecture912
 
Lecture911
Lecture911Lecture911
Lecture911
 
Lecture9
Lecture9Lecture9
Lecture9
 
Lecture8
Lecture8Lecture8
Lecture8
 
Lecture7
Lecture7Lecture7
Lecture7
 
Lecture6
Lecture6Lecture6
Lecture6
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture3
Lecture3Lecture3
Lecture3
 
Protsesor
ProtsesorProtsesor
Protsesor
 
Pm104 standard
Pm104 standardPm104 standard
Pm104 standard
 
Lecture915
Lecture915Lecture915
Lecture915
 

Recently uploaded

Call Girls Bellandur ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Call Girls Bellandur ☎ 7737669865☎ Book Your One night Stand (Bangalore)Call Girls Bellandur ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Call Girls Bellandur ☎ 7737669865☎ Book Your One night Stand (Bangalore)
amitlee9823
 
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
rajveermohali2022
 
Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448
ont65320
 

Recently uploaded (20)

(TOP CLASS) Call Girls In Nungambakkam Phone 7427069034 Call Girls Model With...
(TOP CLASS) Call Girls In Nungambakkam Phone 7427069034 Call Girls Model With...(TOP CLASS) Call Girls In Nungambakkam Phone 7427069034 Call Girls Model With...
(TOP CLASS) Call Girls In Nungambakkam Phone 7427069034 Call Girls Model With...
 
📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call Girls📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call Girls
 
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
 
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
 
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034 Independe...
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034  Independe...Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034  Independe...
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034 Independe...
 
Hotel And Home Service Available Kolkata Call Girls Dum Dum ✔ 6297143586 ✔Cal...
Hotel And Home Service Available Kolkata Call Girls Dum Dum ✔ 6297143586 ✔Cal...Hotel And Home Service Available Kolkata Call Girls Dum Dum ✔ 6297143586 ✔Cal...
Hotel And Home Service Available Kolkata Call Girls Dum Dum ✔ 6297143586 ✔Cal...
 
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034 Independent Chenna...
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034  Independent Chenna...Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034  Independent Chenna...
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034 Independent Chenna...
 
Call Girls Bhandara Just Call 8617697112 Top Class Call Girl Service Available
Call Girls Bhandara Just Call 8617697112 Top Class Call Girl Service AvailableCall Girls Bhandara Just Call 8617697112 Top Class Call Girl Service Available
Call Girls Bhandara Just Call 8617697112 Top Class Call Girl Service Available
 
Call Girls Bellandur ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Call Girls Bellandur ☎ 7737669865☎ Book Your One night Stand (Bangalore)Call Girls Bellandur ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Call Girls Bellandur ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
 
Hotel And Home Service Available Kolkata Call Girls Lake Town ✔ 6297143586 ✔C...
Hotel And Home Service Available Kolkata Call Girls Lake Town ✔ 6297143586 ✔C...Hotel And Home Service Available Kolkata Call Girls Lake Town ✔ 6297143586 ✔C...
Hotel And Home Service Available Kolkata Call Girls Lake Town ✔ 6297143586 ✔C...
 
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
 
𓀤Call On 6297143586 𓀤 Park Street Call Girls In All Kolkata 24/7 Provide Call...
𓀤Call On 6297143586 𓀤 Park Street Call Girls In All Kolkata 24/7 Provide Call...𓀤Call On 6297143586 𓀤 Park Street Call Girls In All Kolkata 24/7 Provide Call...
𓀤Call On 6297143586 𓀤 Park Street Call Girls In All Kolkata 24/7 Provide Call...
 
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
 
Bhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRL
Bhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRLBhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRL
Bhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRL
 
Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448
 
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls ServiceCollege Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
 
Book Sex Workers Available Kolkata Call Girls Service Airport Kolkata ✔ 62971...
Book Sex Workers Available Kolkata Call Girls Service Airport Kolkata ✔ 62971...Book Sex Workers Available Kolkata Call Girls Service Airport Kolkata ✔ 62971...
Book Sex Workers Available Kolkata Call Girls Service Airport Kolkata ✔ 62971...
 
Hotel And Home Service Available Kolkata Call Girls Sonagachi ✔ 6297143586 ✔C...
Hotel And Home Service Available Kolkata Call Girls Sonagachi ✔ 6297143586 ✔C...Hotel And Home Service Available Kolkata Call Girls Sonagachi ✔ 6297143586 ✔C...
Hotel And Home Service Available Kolkata Call Girls Sonagachi ✔ 6297143586 ✔C...
 
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
 

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 }