Daa M-5
Daa M-5
Daa M-5
Outline
• Backtracking • 0/1 Knapsack problem
– General method – LC Branch and Bound
– N-Queens problem solution
– Sum of subsets problem – FIFO Branch and
– Graph coloring Bound solution
– Hamiltonian cycles • NP-Complete and NP-
• Branch and Bound Hard problems
– Basic concepts,
– Assignment Problem,
– non-deterministic
– Travelling Sales
algorithms,
Person problem
– P, NP, NP-Complete,
and NP-Hard classes
2
Outline
• Backtracking • 0/1 Knapsack problem
– General method – LC Branch and Bound
– N-Queens problem solution
– Sum of subsets problem – FIFO Branch and
– Graph coloring Bound solution
– Hamiltonian cycles • NP-Complete and NP-
• Branch and Bound Hard problems
– Basic concepts,
– Assignment Problem,
– non-deterministic
– Travelling Sales
algorithms,
Person problem
– P, NP, NP-Complete,
and NP-Hard classes
3
Backtracking
• Some problems can be solved, by exhaustive search.
• Backtracking is a more intelligent variation of
this approach.
• The principal idea is to construct solutions one
component at a time and evaluate such
partially constructed candidates as follows.
– If a partially constructed solution can be
developed, continue.
– If there is no legitimate option for the next
component, backtrack to replace the last
component of the partially constructed solution
with its next option.
4
Backtrackin
• State-space tree, g
represents the processing
• Its root represents an initial state
• The nodes of the first level in the tree represent
the choices made for the first component of a
solution
• The nodes of the second level represent the choices
for the second component, and so on.
• A node in a state-space tree is said to be promising
– if it corresponds to a partially constructed
solution that may still lead to a complete
solution;
– otherwise, it is called nonpromising.
• Leaves represent either nonpromising dead ends 5
Backtrackin
• In the majority ofgcases, a states-pace tree for a
backtracking algorithm is constructed in the
manner of depth-first search.
• If the algorithm reaches a complete solution to
the problem, it either stops (if just one solution is
required) or continues searching for other
possible solutions.
6
Outline
• Backtracking • 0/1 Knapsack problem
– General method – LC Branch and Bound
– N-Queens problem solution
– Sum of subsets problem – FIFO Branch and
– Graph coloring Bound solution
– Hamiltonian cycles • NP-Complete and NP-
• Branch and Bound Hard problems
– Basic concepts,
– Assignment Problem,
– non-deterministic
– Travelling Sales
algorithms,
Person problem
– P, NP, NP-Complete,
and NP-Hard classes
7
N-Queens Problem
Problem Definition
• The problem is to place n queens on an n × n
chessboard so that no two queens attack each
other by being in the same row or in the same
column or on the same diagonal.
8
4-Queens problem
• Since each of the four queens has to be placed in
its own row, all we need to do is to assign a
column for each queen on the board presented in
figure.
9
4-Queens problem
• The state-space tree
of this search is
shown in figure.
11
N-queens prblem
Algorithm to find all solutions of n-queens problem
12
N-queens prblem
13
Outline
• Backtracking • 0/1 Knapsack problem
– General method – LC Branch and Bound
– N-Queens problem solution
– Sum of subsets problem – FIFO Branch and
– Graph coloring Bound solution
– Hamiltonian cycles • NP-Complete and NP-
• Branch and Bound Hard problems
– Basic concepts,
– Assignment Problem,
– non-deterministic
– Travelling Sales
algorithms,
Person problem
– P, NP, NP-Complete,
and NP-Hard classes
14
Sum of Subsets Problem
Problem definition
• Find a subset of a given set A = {a1, . . . , an } of n
positive integers whose sum is equal to a given
positive integer d.
Example
• For A = {1, 2, 5, 6, 8} and d = 9, there are
two solutions: {1, 2, 6} and {1, 8}.
• Of course, some instances of this problem may
have no solutions.
• State space tree is shown in next slide
15
Sum of Subsets Problem
• It is convenient to sort the set’s elements in
increasing order.
• In the state space tree at a node, If sum is not equal to
d, we can terminate the node as non-promising if either
of the following two inequalities holds:
16
Sum of Subsets Problem
• State space tree
17
Outline
• Backtracking • 0/1 Knapsack problem
– General method – LC Branch and Bound
– N-Queens problem solution
– Sum of subsets problem – FIFO Branch and
– Graph coloring Bound solution
– Hamiltonian cycles • NP-Complete and NP-
• Branch and Bound Hard problems
– Basic concepts,
– Assignment Problem,
– non-deterministic
– Travelling Sales
algorithms,
Person problem
– P, NP, NP-Complete,
and NP-Hard classes
18
Graph Coloring
19
Graph Coloring
• Problem Statement
– Given an undirected graph and a number m,
– determine if the graph can be colored with at most
m colors
– such that no two adjacent vertices of the graph are
colored with same color.
• Problem is also called as m-colorability
decision problem
• Here coloring of a graph means assignment of
colors to all vertices.
20
Graph Coloring
Applications
To make exam schedule for a university
• This problem can be represented as a graph
where every vertex is a subject and an edge
between two vertices mean there is a common
student.
• So this is a graph coloring problem where
minimum number of time slots is equal to the
chromatic number of the graph.
21
Graph Coloring
Applications
Mobile Radio Frequency Assignment
• When frequencies are assigned to towers,
frequencies assigned to all towers at the
same location must be different.
• How to assign frequencies with this constraint?
What is the minimum number of frequencies
needed?
• This problem is also an instance of graph coloring
problem where every tower represents a vertex
and an edge between two towers represents that
they are in range of each other.
22
Graph Coloring
Applications
Map Coloring
• Geographical maps of countries or states where
no two adjacent cities cannot be assigned same
color.
• Four colors are sufficient to color any map ( really?
)
23
Graph Coloring
• If the degree of the graph is d, it can be colored
with d+1 colors
• m-colorability optimization problem finds the
smallest integer m, for which the graph G can
be colored.
• This integer is referred as the chromatic number
of the graph.
• Example: 3 colors sufficient for the graph
given below
24
Graph
• coloring
Represent the graph by adjacency matrix G[1:n,1:n]
• Colors represented by 1,2,3 … m
• Solutions are given by n-tuple (x1,x2,….. Xn), xi is the
color of node i
25
k - Index of the
vertex to color
Algorithm
X[1:m] is initialized to zeros
26
X[ ] is initialized to zeros
27
28
This state space tree shows all
the the successful coloring
(promising nodes leading to the
solution)
29
Analysis
30
Problem
• Apply graph coloring algorithm for the graph
given below. Assume m=3
31
Outline
• Backtracking • 0/1 Knapsack problem
– General method – LC Branch and Bound
– N-Queens problem solution
– Sum of subsets problem – FIFO Branch and
– Graph coloring Bound solution
– Hamiltonian cycles • NP-Complete and NP-
• Branch and Bound Hard problems
– Basic concepts,
– Assignment Problem,
– non-deterministic
– Travelling Sales
algorithms,
Person problem
– P, NP, NP-Complete,
and NP-Hard classes
32
Hamiltonian cycle
33
Hamiltonian Cycle
• TSP tour is a Hamiltonian cycle.
• Here we discuss, Backtracking algorithm that finds
all distinct cycles
• Solution will be in the form (x1, x2, . . . . . , xn)
34
Hamiltonian Cycle
35
36
Outline
• Backtracking • 0/1 Knapsack problem
– General method – LC Branch and Bound
– N-Queens problem solution
– Sum of subsets problem – FIFO Branch and
– Graph coloring Bound solution
– Hamiltonian cycles • NP-Complete and NP-
• Branch and Bound Hard problems
– Basic concepts,
– Assignment Problem
– non-deterministic
– Travelling Sales
algorithms
Person problem
– P, NP, NP-Complete
& NP-Hard classes
37
Branch and Bound
• Backtracking - cut off a branch of the problem’s
state-space tree as soon as we can deduce that
it cannot lead to a solution
• This idea can be strengthened further if we deal
with an optimization problem.
• An optimization problem seeks to minimize
or maximize some objective function like
– a tour length – in TSP,
– the value of items selected – in knapsack
subject to some constraints.
• An optimal solution is a feasible solution with the
best value of the objective function
38
Branch and Bound
Compared to backtracking, branch-and-bound
requires two additional items:
1. a way to provide, for every node of a
state-space tree, a bound on the best value of
the objective function on any solution,
that can be obtained by adding further
components to the partially constructed solution
represented by the node
2. the value of the best solution seen so far
39
Branch and Bound
In general, we terminate a search path for any one
of the following three reasons:
1. The value of the node’s bound is not better than
the value of the best solution seen so far.
2. The node represents no feasible solutions because
the constraints of the problem are already
violated.
3. The subset of feasible solutions represented by
the node consists of a single point (and hence no
further choices can be made)
40
Outline
• Backtracking • 0/1 Knapsack problem
– General method – LC Branch and Bound
– N-Queens problem solution
– Sum of subsets problem – FIFO Branch and
– Graph coloring Bound solution
– Hamiltonian cycles • NP-Complete and NP-
• Branch and Bound Hard problems
– Basic concepts,
– Assignment Problem
– non-deterministic
– Travelling Sales
algorithms
Person problem
– P, NP, NP-Complete
& NP-Hard classes
41
Assignment Problem
Problem Definition
• Assigning n people to n jobs so that the total cost
of the assignment is as small as possible.
42
Example
Note: Avoid the assigned jobs column for computation of lb, as it is not feasible
44
• Rather than generating a single child of the last
promising node as we did in backtracking, we will
generate all the children of the most promising
node among non-terminated leaves in the current
tree.
• How can we tell which of the nodes is most promising?
– We can do this by comparing the lower bounds
of the live nodes.
– It is sensible to consider a node with the best
bound as most promising (as of now)
• This variation of the strategy is called the
best-first branch-and-bound.
45
Note: After Computation of 9th Node, We have to explore nodes that have better lb than 13.
In this example we do not have such nodes. So we stop at 9.
46
Note: After Computation of 9th Node, We have to explore nodes that have better lb than 13.
In this example we do not have such nodes. So we stop at 9.
47
Outline
• Backtracking • 0/1 Knapsack problem
– General method – LC Branch and Bound
– N-Queens problem solution
– Sum of subsets problem – FIFO Branch and
– Graph coloring Bound solution
– Hamiltonian cycles • NP-Complete and NP-
• Branch and Bound Hard problems
– Basic concepts,
– Assignment Problem
– non-deterministic
– Travelling Sales
algorithms
Person problem
– P, NP, NP-Complete
& NP-Hard classes
48
Branch & Bound -
• We will beTSP
able to apply the branch-and-bound
technique to instances of the TSP if we come up with
a reasonable lower bound on tour lengths.
• One very simple lower bound can be obtained by
finding the smallest element in the intercity distance
matrix D and multiplying it by the number of cities n.
• But there is a less obvious and more informative
lower bound for instances with symmetric matrix D
• For each city i, 1≤ i ≤ n,
– Find si – sum of the distances from city i to the 2
nearest cities;
– compute s, the sum of si i=1..n, divide the result by 2
49
Branch & Bound – TSP -
Example
50
Branch & Bound -
To reduce TSP
the amount of potential work, we
take advantage of two observations.
1. First, without loss of generality, we can consider
only tours that start at a.
2. Second, because our graph is undirected, we
can generate only tours in which b is visited
before c. (Why?)
51
TSP
3. In addition, after visiting n−1= 4 cities, a tour has
no choice but to visit the remaining unvisited city
and return to the starting one
• The state-space tree tracing the
algorithm’s application is given in Figure
52
53
54
Outline
• Backtracking • 0/1 Knapsack problem
– General method – LC Branch and Bound
– N-Queens problem solution
– Sum of subsets problem – FIFO Branch and
– Graph coloring Bound solution
– Hamiltonian cycles • NP-Complete and NP-
• Branch and Bound Hard problems
– Assignment Problem – Basic concepts,
– Travelling Sales – non-deterministic
Person problem algorithms
– P, NP, NP-Complete
& NP-Hard classes
55
0/1 Knapsack problem
• Branch and Solution to 0/1 Knapsack problem
Problem Definition
• Given n items of known weights wi and values vi , i =
1, 2, . . . , n, and a knapsack of capacity W, find the
most valuable subset of the items that fit in the
knapsack.
56
0/1 Knapsack problem
• It is convenient to order the items of a given
instance in descending order by their
value-to-weight ratios.
57
How to compute the upper
• bound?
A simple way to compute the upper bound ub is to
– add to v, the total value of the items
already selected,
– the product of the remaining capacity of the
knapsack W − w and the best per unit payoff
among the remaining items, which is vi+1/wi+1
ub = v + (W − w)(vi+1/wi+1).
58
Example
• Consider the following problem.
• The items are already ordered in descending order
of their value-to-weight ratios.
59
60
61
Discussion
• Solving the knapsack problem by a
branch-and-bound algorithm has a rather unusual
characteristic.
• For the knapsack problem, every node of the tree
represents a subset, and also a feasible solution of the
items given.
• We can use this fact to update the information about the
best subset seen so far after generating each new node in
the tree.
• If we had done this for the instance investigated above,
we could have terminated nodes 2 and 6 before node 8
was generated because they both are inferior to the
subset of value 65 of node 5.
62
Outline
• Backtracking • 0/1 Knapsack problem
– General method – LC Branch and Bound
– N-Queens problem solution
– Sum of subsets problem – FIFO Branch and
– Graph coloring Bound solution
– Hamiltonian cycles • NP-Complete and NP-
• Branch and Bound Hard problems
– Assignment Problem – Basic concepts,
– Travelling Sales – non-deterministic
Person problem algorithms
– P, NP, NP-Complete
& NP-Hard classes
63
Some
• Live node Terminologies
- a node which has been generated and
all of whose children are not yet been generated.
• E-node - is a live node whose children are currently
being explored. In other words, an E-node is a
node currently being expanded.
• Dead node - a node that is either not to be
expanded further, or for which all of its children
have been generated
• Bounding Function - will be used to kill live
nodes without generating all their children.
64
Some
• Terminologies
Backtracking - is depth first node generation
with bounding functions.
• Branch-And-Bound is a method in which E-node
remains E-node until it is dead.
• Breadth-First-Search: Branch-and Bound with each
new node placed in a queue. The front of the
queen becomes the new E-node.
• Depth-Search (D-Search): New nodes are placed
in to a stack. The last node added is the first to be
explored.
65
Some Basic Concepts
66
0/1 Knapsack - B&B based solution
• The technique discussed here is applicable
for minimization problems
• So convert the knapsack problem (maximizing
the profit) into minimization problem by
negating the objective function
• Define cost
67
0/1 Knapsack - B&B based solution
Lower bound Upper bound
68
LCBB - Least Cost Branch &Bound solution
•
Example
Similar to the
Nodes with
computation
ෙ◌ා -22
of fractional least 𝒄
knapsack is
expanded
69
Computation method
Lower bound
Upper bound
72
Outline
• Backtracking • 0/1 Knapsack problem
– General method – LC Branch and Bound
– N-Queens problem solution
– Sum of subsets problem – FIFO Branch and
– Graph coloring Bound solution
– Hamiltonian cycles • NP-Complete and NP-
• Branch and Bound Hard problems
– Assignment Problem – Basic concepts,
– non-deterministic
– Travelling Sales Person lem algorithms
prob
– P, NP, NP-Complete
& NP-Hard classes
• ` 73
Basic Concepts
• For many of the problems we know and study, the
best algorithms for their solution have computing
times can be clustered into two groups;
– Solutions are bounded by the polynomial- Examples
include Binary search O(log n), Linear search O(n) or in
general O(nk) where k is a constant.
– Solutions are bounded by a non-polynomial -
Examples include travelling salesman problem
O(n22n) & knapsack problem O(2n/2).
As the time increases exponentially, even
moderate size problems cannot be solved.
74
Basic Concepts
• So far, no one has been able to device an
algorithm which is bounded by the polynomial for
the problems belonging to the non-polynomial.
75
Non deterministic
• algorithms
We also need the idea of two models of
computer (Turing machine): deterministic and
non- deterministic.
77
Decision vs Optimization algorithms
• An optimization problem tries to find an
optimal solution.
• A decision problem tries to answer a
yes/no question.
• Most of the problems can be specified in
decision and optimization versions.
• For example, TSP can be stated as two ways
– Optimization - find hamiltonian cycle of
minimum weight
– Decision - is there a hamiltonian cycle of weight
≤
k? 81
Decision vs Optimization algorithms
• For graph coloring problem,
– Optimization – find the minimum number of
colors needed to color the vertices of a graph so
that no two adjacent vertices are colored the
same color
– Decision - whether there exists such a coloring of
the graph’s vertices with no more than m colors?
Source:
Stackoverflow.com
85
NP - Example
Integer factorisation is in NP.
• This is the problem that given integers n and m, is
there an integer f with 1 < f < m, such that f divides n (f
is a small factor of n)?
• This is a decision problem because the answers are yes
or no.
• If someone hands us an instance of the problem (so they
hand us integers n and m) and an integer f with 1 < f <
m, and claim that f is a factor of n (the certificate), we
can check the answer in polynomial time by performing
the division n / f.
86
P,
• NP which are known to
But there are some problems
be in NP but don’t know if they’re in P.
• The example is the decision-problem version of
the Travelling Salesman Problem (decision-TSP).
• It’s not known whether decision-TSP is in P:
– there’s no known poly-time solution,
– but there’s no proof such a solution doesn’t exist.
87
NP-Complet
e
Reducible Problems
• Given decision problems P and Q, if an algorithm can
transform a solution for P into a solution for Q in
polynomial time, it’s said that Q is poly-time
reducible (or just reducible) to P.
Definition: NP-Complete
A decision problem D is said to be NP-complete if:
1. it belongs to class NP
2. every problem in NP is polynomially reducible to D
88
NP-Complet
• NP-Complete is ae
complexity class which represents the
set of all problems X in NP for which it is possible to
reduce any other NP problem Y to X in polynomial
time.
90
NP-Complet
• e
Example for NP-complete is
CNF-satisfiability problem.
• The CNF-satisfiability problem deals with
boolean expressions.
• This is given by Cook in 1971.
• The CNF-satisfiability problem asks whether or not
one can assign values true and false to variables of a
given boolean expression in its CNF form to make
the entire expression true.
91
Example for
• NP-Complete
Traveling salesman problem
• Hamiltonian cycle problem
• Clique problem
• Subset sum problem
• Boolean satisfiability problem
• Many thousands of other important
computational problems in computer science,
mathematics, economics, manufacturing,
communications, etc.
92
NP-Hard Problems
• These problems need not have any bound on
their running time.
• If any NP-Complete Problem is polynomial time
reducible to a problem X, that problem X
belongs to NP-Hard class.
• All NP-Complete problems are also NP-Hard.
• In other words if a NP-Hard problem is non-
deterministic polynomial time solvable, it is a
NP- Complete problem.
• Example of a NP problem that is not NPC is
Halting Problem.
93
NP-Hard Problems
94
NP-Hard Problems
• If a NP-Hard problem can be solved in
polynomial time then all NP-Complete can be
solved in polynomial time.
• “All NP-Complete problems are NP-Hard but not
all NP-Hard problems are not NP-Complete.”
• NP-Complete problems are subclass of NP-Hard
• The more conventional optimization version of TSP
for finding the shortest route is NP-hard, not
strictly NP-complete.
95
NP-Hard
• Intuitively, these are the problems that are at least as hard
as the NP-complete problems.
• Note that NP-hard problems do not have to be in NP, and
they
do not have to be decision problems.
• The precise definition here is that a problem X is NP-hard, if
there is an NP-complete problem Y, such that Y is reducible
to X in polynomial time.
Source:
Stackoverflow.com
97
98