Module 5 Notes
Module 5 Notes
1 4
2 3
2 18 34 50
2 4 1 4 1 4 3
3 3 2 1 2
3 4 5 19 20 21 35 36 37 51 52 53
3 4 2 4 2 3 3 4 1 4 1 3 2 4 1 4 1 2 2 3 1 3 1 2
6 7 8 9 10 11 22 23 24 25 26 27 38 39 40 41 42 43 54 55 56 57 58 59
4 3 4 2 3 2 4 3 4 1 3 1 4 2 4 1 2 1 3 2 3 1 2 1
12 13 14 15 16 17 28 29 30 31 32 33 44 45 46 47 48 49 60 61 62 63 64 65
Solution 1 Solution 2
In 4-queens problems total 65 nodes and 4! = 24 are leaf nodes.
Sum of Subset Problem
In this technique S is a set of positive integer numbers S1, S2, …… Sn upto n integer numbers.
There a non-empty subset such that the sum of the subset is given as d integer. Then we have
to find a subset whose sum is equal to given positive integer d.
State space tree:
A space state tree is a tree that represents all of the possible states of the problem, from the
foot as an initial state to the leaf as a dead node or solution such tree is called as state space
tree. State space tree can be constructed using the following algorithm:
1) First arrange the set of items in ascending order.
2) The root of the binary tree represent ‘0’.
3) If item is selected at first level then write x1=1 else x1=0.
4) If item is selected at second level then write x2=1 else x2=0.
5) The left child represent item is included and right child represent item is not included.
6) Leaf node of the tree represent dead end or solution.
1
Example 1: Draw the state space tree of the backtracking algorithm applied to the instance
S={11, 13, 24, 7} and d=31 of the subset sum problem.
Solution: Given set S={7, 11, 13, 24} and d=15
Sum of Subset S1={7, 11, 13} & S2={7, 24}
0
x1=1 x1=0
7 0
x2=1 x2=0 x2=1 x2=0
18 7 11 0
x3=1 x3=0 x3=1 x3=0 x3=1 x3=0 x3=1 x3=0
31 18 20 7 23 11 13 0
X X x4=1 X X X X
Solution
31
Solution
Discussion: in above example two solutions and X indicate that leaf node value is less then or
greater d.
Leaf node value 31 is the solution
Leaf node value 11: if x4=1 (11 + 24 = 35> d) else if x4=0 (11 + 0 = 11< d)
Leaf node value 18: if x4=1 (18 + 24 = 42 > d) else if x4=0 (18 + 0 = 18 < d)
Leaf node value 20: if x4=1 (20 + 24 = 44 > d) else if x4=0 (20 + 0 = 20 < d)
Leaf node value 23: if x4=1(23 + 24 = 47> d) else if x4=0 (23 + 0 = 23< d)
Leaf node value 11: if x4=1(11 + 24 = 35> d) else if x4=0 (11 + 0 = 11< d)
Leaf node value 13: if x4=1(13 + 24 = 37> d) else if x4=0 (13 + 0 = 13< d)
Leaf node value 0: if x4=1 (0 + 24 = 24< d) else if x4=0 (0 + 0 = 0< d)
2
Example 2: Draw the state space tree of the backtracking algorithm applied to the instance
S={3, 5, 6, 7} and d=15 of the subset sum problem.
Solution: Given set S={3, 5, 6, 7} and d=15
Sum of Subset S1={3, 5, 7} only one solution
0
x1=1 x1=0
3 0
x2=1 x2=0 x2=1 x2=0
8 3 5 0
x3=1 x3=0 x3=1 x3=0 x3=1 x3=0 x3=1 x3=0
14 8 9 3 11 5 6 0
X x4=1 X X X X X X
15
Solution
Example 3: Draw the state space tree of the backtracking algorithm applied to the instance
S={5, 10, 12, 13, 15, 18} and d=30 of the subset sum problem.
Solution: Given set S={5, 10, 12, 13, 15, 18} and d=30
Sum of Subset S1={5, 10, 15}, S2={5, 12, 13} & S2={12, 18}
0
x1=1 x1=0
5 0
x2=1 x2=0 x2=1 x2=0
15 5 10 0
x3=1 x3=0 x3=1 x3=0 x3=1 x3=0 x3=1 x3=0
27 15 17 5 22 10 12 0
X x4=1 0 x4=1 x4=1 x4=1 x4=0 x4=0 x4=1
28 15 30 18 35 10 12 13
30 33 25 12
X X x6=1
Solution 1
3 30 Solution 3
0/1 Knapsack problem using Branch and Bound
N-number of items and it’s weight (w1, w2, …… wn) and profit is (P1, P2,…..Pn).the knapsack
capacity is M. It is required to find a subset of items that fit into the knapsack. The main
objective is to place the object into the knapsack so that maximum profit is obtained and
weight of the selected object is not greater than M.
𝑛
Maximize Pi Xi
𝑖=1
𝑛
4
1st item is selected 1st item is not selected
𝑣 𝑣
v = 40, w = 4, 2 𝑤2 = 6 v = 0, w = 0, 2 𝑤2 = 6
v = 0 + 40 = 40 v=0+0=0
w=0+4=4 w=0+0=0
𝑣 𝑣
𝑢𝑝 = 𝑣 + (𝑀 − 𝑤) × 2 𝑤2 𝑢𝑝 = 𝑣 + (𝑀 − 𝑤) × 2 𝑤2
𝑢𝑝 = 40 + 10 − 4 × 6 𝑢𝑝 = 0 + (10 − 0) × 6
𝒖𝒑 = 𝟕𝟔 𝑢𝑝 = 60
5
State space tree
w=0, v=0
UP = 100
Selected Not selected
Example 2: With the help of state space tree, solve the following instance of the knapsack
problem by branch and bound algorithm. Knapsack capacity is M=16.
Item (i) Weight (Wi) Profit (Pi)
1 10 100
2 7 63
3 8 56
4 4 12
Solution :
Item (i) Weight (Wi) Profit (Pi) v/w
1 10 100 10
2 7 63 9
3 8 56 7
4 4 12 3
Step 1: i=0
if i=0 it means no item and no value then
𝑣
v = 0, w = 0, 1 𝑤1 = 10 & M = 16
𝑣
𝑢𝑝 = 𝑣 + (𝑀 − 𝑤) × 𝑖+1 𝑤𝑖+1
6
𝑣1
𝑢𝑝 = 𝑣 + (𝑀 − 𝑤) × 𝑤1
𝑢𝑝 = 0 + (16 − 0) × 10
𝒖𝒑 = 𝟏𝟔𝟎
Step 2: i=1 and previous v = 0, w = 0
1st item is selected 1st item is not selected
𝑣 𝑣
v = 100, w = 10, 2 𝑤2 = 9 v = 0, w = 0, 2 𝑤2 = 9
v = 0 + 100 = 100 v=0+0=0
w = 0 + 10 = 10 w=0+0=0
𝑣2 𝑣
𝑢𝑝 = 𝑣 + (𝑀 − 𝑤) × 𝑤2 𝑢𝑝 = 𝑣 + (𝑀 − 𝑤) × 2 𝑤2
𝑢𝑝 = 100 + 16 − 10 × 9 𝑢𝑝 = 0 + (16 − 0) × 9
𝒖𝒑 = 𝟏𝟓𝟒 𝑢𝑝 = 144
st
Step 3: i=2 and if 1 item is selected than v = 100, w = 10
If 2nd item is selected
v = 63, w = 7
v = 100 + 63 = 163
w = 10 + 7 = 17
w > M than this is not feasible solution.
7
w>M 𝑢𝑝 = 119 + (16 − 15) × 0
𝒖𝒑 = 𝟏𝟏𝟗
State space tree
w=0, v=0
UP = 160
Selected Not selected
Example 2: Solve the sum of subset problem using the branch and bound technique and draw
the state space tree for the S={3, 5, 6, 7} and d=15 of the subset sum problem.
0
x1=1 x1=0
3 0
x2=1 x2=0 x2=1 x2=0
8 3 5 0
x3=1 x3=0 x3=1 x3=0 x3=1 x3=0 x3=1 x3=0
14 8 9 3 11 5 6 0
B x4=1 B B B B B B
15
Solution
8
4-Queen’s State space tree using Branch and Bound technique as shown below
1
1 4
2 3
2 10 17 50
2 4 1 4 1 4 3
3 3 2 1 2
3 4 5 11 12 13 18 19 20 51 52 53
B 3 B B B B
2 4 2 1 3 2 4 2 3 1 3 B
6 7 8 9 14 15 21 22 54 55 56 57
B B B B B B B B B B
3 2
16 23
Solution 1 Solution 2
9
NP-Complete and NP-Hard problems:
NP-hard and NP-complete
NP-hard and NP-complete are classes of problems that measure the computational
complexity and difficulty of finding solutions. NP stands for nondeterministic polynomial,
which means that a solution can be verified in polynomial time (a function of the input size)
by a hypothetical machine that can make random choices. NP-hard problems are at least as
hard as any NP problem, meaning that there is no efficient algorithm that can solve them in
polynomial time. NP-complete problems are a subset of NP-hard problems that are also in
NP, meaning that they can be verified in polynomial time. NP-complete problems are the
hardest problems in NP, and they have the property that any NP problem can be reduced to
them in polynomial time.
Informatics problems that are NP-hard or NP-complete can be found in various domains and
applications, such as the Traveling Salesman Problem (TSP), the Knapsack Problem, the
Boolean Satisfiability Problem (SAT), and the Protein Folding Problem. TSP requires finding
the shortest route that visits each city exactly once and returns to the starting point; it is NP-
hard and used for logistics, scheduling, DNA sequencing, and network routing. The
Knapsack Problem involves selecting a subset of items with a weight and a value that
maximizes the total value while staying within a weight limit; it is NP-complete and used for
resource allocation, cryptography, and bioinformatics. SAT requires finding an assignment of
values to variables that makes a logical expression true; it is NP-complete and used for
artificial intelligence, circuit design, and software verification. Finally, the Protein Folding
Problem requires finding the three-dimensional structure that minimizes the energy of a
protein; it is NP-hard and used for molecular biology, drug design, and biophysics.
NP-hard or NP-complete informatics problems are intractable in general, so you can't expect
to find optimal or exact solutions in reasonable time. However, you can attempt approximate
or heuristic solutions that are suitable for your purposes. Approximation algorithms guarantee
to find a solution within a certain margin of the optimal solution, heuristic algorithms use
intuition and experience to find a solution that may not be optimal or guaranteed, and met a
heuristic algorithms combine or modify heuristic algorithms to explore a large solution space.
Additionally, machine learning algorithms use data and statistical models to learn patterns
that can help solve a problem. All of these approaches can be useful when you need a fast,
provable, flexible, or data-driven solution.
A decision problem is a problem whose output is a single boolean value: YES or NO. Let me
define three classes of decision problems:
10
P: is the set of decision problems that can be solved in polynomial time. Intuitively, P is the
set of problems that can be solved quickly.
NP: is the set of decision problems with the following property: If the answer is YES, then
there is a proof of this fact that can be checked in polynomial time. Intuitively, NP is the set
of decision problems where we can verify a YES answer quickly if we have the solution in
front of us.
co-NP: is the opposite of NP. If the answer to a problem in co-NP is NO, then there is a proof
of this fact that can be checked in polynomial time.
EasyP
Medium NP
Hard NP-Complete
Hardest NP-Hard
And we can visualize their relationship, too:
Using the diagram we assume that P and NP are not the same set or in other words. We
assume that P≠NP.
For example, the circuit satisfiability problem is in NP. If the answer is YES, then any set of
m input values that produces TRUE output is a proof of this fact; we can check the proof by
evaluating the circuit in polynomial time. It is widely believed that circuit satisfiability is not
in P or in co-NP, but nobody actually knows.
Every decision problem in P is also in NP. If a problem is in P, we can verify YES answers in
polynomial time recomputing the answer from scratch! Similarly, any problem in P is also in
co-NP.
One of the most important open questions in theoretical computer science is whether or not
P=NP. Nobody knows. Intuitively, it should be obvious that P≠ NP; the home works and
exams in this class and others have (I hope) convinced you that problems can be incredibly
11
hard to solve, even when the solutions are obvious in retrospect. But nobody knows how to
prove it.
A more subtle but still open question is whether NP and co-NP are different. Even if we can
verify every YES answer quickly, there’s no reason to think that we can also verify NO
answers quickly. For example, as far as we know, there is no short proof that a boolean
circuit is not satisfiable. It is generally believed that NP ≠ co-NP, but nobody knows how to
prove it.
Intuitively, if we could solve one particular NP-hard problem quickly, then we could quickly
solve any problem whose solution is easy to understand, using the solution to that one special
problem as a subroutine. NP-hard problems are at least as hard as any problem in NP.
Saying that a problem is NP-hard is like saying ‘If I own a dog, then it can speak fluent
English.’ You probably don’t know whether or not I own a dog, but you’re probably pretty
sure that I don’t own a talking dog. Nobody has a mathematical proof that dogs can’t speak
English—the fact that no one has ever heard a dog speak English is evidence, as are the
hundreds of examinations of dogs that lacked the proper mouth shape, but mere evidence is
not a mathematical proof. Nevertheless, no sane person would believe me if I said I owned a
dog that spoke fluent English. So the statement ‘If I own a dog, then it can speak fluent
English’ has a natural corollary: No one in their right mind should believe that I own a dog!
Likewise, if a problem is NP-hard, no one in their right mind should believe it can be solved
in polynomial time.
12
More of what we think the world looks like.
It is not immediately clear that any decision problems are NP-hard or NP-complete. NP-
hardness is already a lot to demand of a problem; insisting that the problem also have a
nondeterministic polynomial-time algorithm seems almost completely unreasonable. The
following remarkable theorem was first published by Steve Cook in 1971 and independently
by Leonid Levin in 1973.4 I won’t even sketch the proof, since I’ve been (deliberately) vague
about the definitions.
13