Professional Documents
Culture Documents
Course 6
Overview
Classes of Problems
P vs NP
Polynomial Reduction
NP-hard and NP-complete
Backtracking
n-Queens Problem
Graph Coloring Problem
Classes of Problems
Complexity of an algorithm
notation
Used to compare algorithms in order to determine which one
is better
Complexity of a problem?
We would like to know how difficult a problem is in order to
know what solution to look for
Used to compare problems in order to determine which one
is more difficult, regardless of the algorithm used to solve it!
In other words, by taking into consideration the best
algorithms we could devise to solve the problems
Classes of Problems (2)
We are able to define classes of problems, but not as many
than the classes of algorithms
It is unknown if P = NP, but most researchers believe that P and NP are not
the same class
1 million dollar prize for proving that P = NP or P != NP
http://en.wikipedia.org/wiki/P_versus_NP_problem
P vs NP (2)
Conclusion: the problems in NP \ P should be more
difficult than the problems in P
For these problems, we cannot find a solution in
polynomial time at this moment in time
Maybe we shall find in the future, especially if someone
manages to prove that P = NP
Therefore, at this moment in time we could separate
between problems in:
P – less difficult
NP \ P – more difficult
Polynomial Reduction
Given 2 decision problems, A1 and A2
Remember: A decision problem is a problem that has only two
possible outputs: yes/no
We say that problem A1 can be reduced in polynomial time to
problem A2 (A1 P A2) if:
There exists a polynomial time algorithm F that transforms any input
data for A1, x, into an input data for A2, F(x)
A1(x) == yes A2(F(x)) == yes
A1(x) == yes => A2(F(x)) == yes
A1(x) == no => A2(F(x)) == no
From the algorithm point of view, it seems that the algorithm for
solving A1 has a greater complexity than the one for solving A2
Complexity(SolveA1) = Complexity(F) + Complexity(SolveA2)
However, we are interested from the classes of problems point of
view:
We can use the best algorithm for solving A2 and then use F to solve A1
If A2 P => A1 P
If A2 NP => A1 P or NP (If A2 NP \ P => A1 P or NP\P)
Therefore, A1 is always easier or as at most as difficult as A2
Polynomial Reduction (4)
Another property of polynomial reduction:
If A1 NP => A2 NP (If A1 NP \ P => A2 NP\P)
Because A2 cannot be easier than A1
Quick Info:
A clique is a subset of vertices V’ V such that for any v1, v2 V’
there is an edge (v1, v2) E[G]
A vertex cover is a subset of vertices V’ V such that for any edge
(v1, v2) E[G], v1 V’ and/or v2 V’
At least an endpoint of any edge in the graph is covered in V’
NP-complete Problems (2)
Graph Coloring
N-Queens Problem
Hamiltonian Cycle
Travelling Salesman Problem
Minesweeper
Task scheduling
Etc.
We need to determine a value for each variable that is part of the domain
of that variable and this instantiation respects all the defined constraints
Constraints Satisfaction Problem (CSP)
Example – k-clique
We have k variables: V1, …, Vk – the vertices of the k-
clique
Each variable can take values from all the vertices of the
graph G(Vertices[n], Edges[m])
Dom(V1) = … = Dom(Vk) = {1, …, n}
Considering the vertices of the graph are labeled from 1..n
Constraints:
Vi != Vj for all 1 <= i < j <= k
(Vi,Vj) Edges for all 1 <= i < j <= k
Generate and Test – Revisited
For the new formulation of the problems
// when no next value exists, the index is reset to the first value
WHILE (ExistsNextValue(Vars[k], Domains[k]))
Vars[k] = NextValue(Vars[k] , Domains[k], Constraints)
IF (CheckConstraints(Vars, Constraints, k))
k++
BREAK
IF (!ExistsNextValue(Vars[k], Domains[k]))
k--
Example: n-Queens Problem
Given a table of chess size n x n, find a possible positioning of
n queens such that none of the queens attack themselves
Or find all possible positionings
n = 8 usual chess table
n = 1 => 1 solution
n = 2, 3 => 0 solutions
n = 4 => 2 solutions
…
n = 8 => 92 solutions
…
n = 25 => 2,207,893,435,808,352 solutions
n-Queens Problem
A possible solution for n = 8 (from Wikipedia)
n-Queens Problem
Three possible approaches
First approach
n2 variables – one for each position on the table
Each variable has the domain {0, 1} if a queen is placed on that particular
position
Complexity: O(2n*n)
Branching factor for each node: 2
Height of tree: n*n
Second approach
n variables – one for each queen
Each variable has the domain {1, …,n2} – the position on the table for
each queen
Complexity: O(n2*n)
Branching factor: n2
Height of tree: n
n-Queens Problem
CheckConsistency1(Vars[1..n*n], Domains, k)
FOR (i = 1..k-1)
rowi = (i – 1) / n
columni = (i - 1) % n
rowk = (k – 1) / n
columnk = (k – 1) % n
IF (rowi == rowk || columni == columnk || abs(rowi -rowk)
== abs(columni - columnk))
IF (Vars[i] == 1 AND Vars[k] == 1)
// we already have a queen on the same row
// or the same column or the same diagonal
RETURN false
RETURN true
n-Queens Problem
CheckConsistency2(Vars[1..n], Domains, k)
FOR (i = 1..k-1)
rowi = (Vars[i] – 1) / n
columni = (Vars[i] - 1) % n
rowk = (Vars[k] – 1) / n
columnk = (Vars[k] – 1) % n
IF (rowi == rowk || columni == columnk || abs(rowi -rowk)
== abs(columni - columnk))
// we already have a queen on the same row
// or the same column or the same diagonal
RETURN false
RETURN true
n-Queens Problem
Third approach
Idea: the queens cannot be placed on the same row!
n variables – one for the each position of queen i on row i
(i=1..n)
Each variable has the domain {1, …,n} – the column where the
queen is placed on each row
Complexity: O(nn)
Branching factor for each node: n
Height of tree: n
Least Constraining Value (LCV) – Choose the value that imposes the
least number of constraints on the remaining variables
http://ww3.algorithmdesign.net/sample/ch13-np.pdf
http://www.cs.cmu.edu/~avrim/451/lectures/lect1030.pdf
http://faculty.ksu.edu.sa/YAlohali/Documents/ch3-
Constraint%20Satisfaction%20Problems1.pptx