P and NP - PowerPoint PPT Presentation

About This Presentation
Title:

P and NP

Description:

CHAPTER 10 P and NP Algorithm 10.2.2 Crossword Puzzle Algorithm 10.2.15 Graph k-coloring Algorithm 10.2.19 Hamiltonian Cycle Algorithm 10.2.22 TSP Example 10.2.26 ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 11
Provided by: MSc142
Category:

less

Transcript and Presenter's Notes

Title: P and NP


1
CHAPTER 10
  • P and NP

2
Algorithm 10.2.2 Crossword Puzzle
This algorithm solves a crossword puzzle,
represented by a Boolean matrix Di,j, 1 i,j
n , and a finite set of words W ? S. S is the
alphabet, and Di,j is true if the square is
blank and false if it is blocked. We construct
the solution in a new matrix Si,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 (Di,j) Si,j guess(S)
else Si,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 cv guess(1,2,...,k) for each v in
V for each w in N(v) if (cw
cv) 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 visitedi false for i 1 to n
ci guess(V) visitedci true
c0 cn // first node is the same as
last // check that only edges of G are used
for i 0 to n - 1 if ((ci,ci 1) ?
E) return false // check that all
vertices have been visited for i 1 to n
if (!(visitedi)) 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 ci guess(V) c0 cn ...
7
... // 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
((ci,ci 1) ? E) return false
else totalweight totalweight
weight((ci,ci 1)) // reject tours whose
total weight is too large if (totalweight gt
w) return false // check that all
vertices are visited for i 1 to n
visitedi false for i 1 to n
visitedci true for i 1 to n if
(!(visitedi)) return false return
true
8
Example 10.2.26
graph_coloring(G,k) for each v in V cv
0 for each v in V cv guess(1,2,...,k)
for each w in N(v) if (cw cv) return
false return true
9
Algorithm 10.3.18 Satisfiability Witness
This 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.
10
Input Parameter ? Output Parameter
x satisfiability_witness(?, x) if
(!(A(?)) return false for i 1 to n ?
?xi ? true if (A(?)) xi true
? ? else xi false ?
?xi ? false return true
Write a Comment
User Comments (0)
About PowerShow.com