Algorithm Desingh Unit 5
Algorithm Desingh Unit 5
UNIT 5
Introduction to Backtracking – Data Structure and Algorithm Tutorials
Basic terminologies:
Solution vector:
The desired solution X to a problem instance P of input size n is as a vector of candidate
solutions that are selected from some finite set of possible solutions S.
Thus, a solution can be represented as an n-tuple (X1, X2, …, Xn) and its partial solution is
given as (X1, X2,…, Xi) where i<n.
E.g. for a 4-queens problem X = {2,4,1,3) is a solution vector.
Constraints:
Constraints are the rules to confine the solution vector (X1, X2…… Xa).
They determine the values of candidate solutions and their relationship with each other. There
are two types of constraints:
N-Queens Problem
It can be seen that for n =1, the problem has a trivial solution, and no solution exists for n =2
and n =3. So first we will consider the 4 queens problem and then generate it to n - queens
problem.
Given a 4 x 4 chessboard and number the rows and column of the chessboard 1 through 4.
Since, we have to place 4 queens such as q 1 q2 q3 and q4 on the chessboard, such that no two
queens attack each other. In such a conditional each queen must be placed on a different row,
i.e., we put queen "i" on row "i."
Now, we place queen q1 in the very first acceptable position (1, 1). Next, we put queen q 2 so
that both these queens do not attack each other. We find that if we place q 2 in column 1 and 2,
then the dead end is encountered. Thus the first acceptable position for q 2 in column 3, i.e. (2,
3) but then no position is left for placing queen 'q 3' safely. So we backtrack one step and place
the queen 'q2' in (2, 4), the next best possible solution. Then we obtain the position for placing
'q3' which is (3, 2). But later this position also leads to a dead end, and no place is found
where 'q4' can be placed safely. Then we have to backtrack till 'q 1' and place it to (1, 2) and
then all other queens are placed safely by moving q 2 to (2, 4), q3 to (3, 1) and q4 to (4, 3). That
is, we get the solution (2, 4, 1, 3). This is one possible solution for the 4-queens problem. For
another possible solution, the whole method is repeated for all partial solutions. The other
solutions for 4 - queens problems is (3, 1, 4, 2) i.e.
Job Assignment Problem using Branch And Bound
Let there be N workers and N jobs. Any worker can be assigned to perform any job, incurring
some cost that may vary depending on the work-job assignment. It is required to perform all
jobs by assigning exactly one worker to each job and exactly one job to each agent in such a
way that the total cost of the assignment is minimized.
umng01
Read
Discuss
Courses
Practice
In computer science, there exist some problems whose solutions are not yet found, the
problems are divided into classes known as Complexity Classes. In complexity theory, a
Complexity Class is a set of problems with related complexity. These classes help scientists
to group problems based on how much time and space they require to solve problems and
verify the solutions. It is the branch of the theory of computation that deals with the resources
required to solve a problem.
The common resources are time and space, meaning how much time the algorithm takes to
solve a problem and the corresponding memory usage.
The time complexity of an algorithm is used to describe the number of steps required to solve
a problem, but it can also be used to describe how long it takes to verify the answer.
The space complexity of an algorithm describes how much memory is required for the
algorithm to operate.
Complexity classes are useful in organizing similar types of problems.
Types of Complexity Classes
This article discusses the following complexity classes:
1. P Class
2. NP Class
3. CoNP Class
4. NP-hard
5. NP-complete
P Class
The P in the P class stands for Polynomial Time. It is the collection of decision
problems(problems with a “yes” or “no” answer) that can be solved by a deterministic
machine in polynomial time.
Features:
1. The solution to P problems is easy to find.
2. P is often a class of computational problems that are solvable and tractable.
Tractable means that the problems can be solved in theory as well as in practice.
But the problems that can be solved in theory but not in practice are known as
intractable.
This class contains many natural problems:
1. Calculating the greatest common divisor.
2. Finding a maximum matching.
3. Decision versions of linear programming.
NP Class
The NP in NP class stands for Non-deterministic Polynomial Time. It is the collection of
decision problems that can be solved by a non-deterministic machine in polynomial time.
Features:
1. The solutions of the NP class are hard to find since they are being solved by a
non-deterministic machine but the solutions are easy to verify.
2. Problems of NP can be verified by a Turing machine in polynomial time.
Example:
Let us consider an example to better understand the NP class. Suppose there is a company
having a total of 1000 employees having unique employee IDs. Assume that there are 200
rooms available for them. A selection of 200 employees must be paired together, but the CEO
of the company has the data of some employees who can’t work in the same room due to
personal reasons.
This is an example of an NP problem. Since it is easy to check if the given choice of 200
employees proposed by a coworker is satisfactory or not i.e. no pair taken from the coworker
list appears on the list given by the CEO. But generating such a list from scratch seems to be
so hard as to be completely impractical.
It indicates that if someone can provide us with the solution to the problem, we can find the
correct and incorrect pair in polynomial time. Thus for the NP class problem, the answer is
possible, which can be calculated in polynomial time.
This class contains many problems that one would like to be able to solve effectively:
1. Boolean Satisfiability Problem (SAT).
2. Hamiltonian Path Problem.
3. Graph coloring.
Introduction to NP-Completeness
4. We have been writing about efficient algorithms to solve complex problems,
like shortest path, Euler graph, minimum spanning tree, etc. Those were all success
stories of algorithm designers. In this post, failure stories of computer science are
discussed.
5. Can all computational problems be solved by a computer? There are
computational problems that can not be solved by algorithms even with unlimited
time. For example Turing Halting problem (Given a program and an input, whether
the program will eventually halt when run with that input, or will run forever). Alan
Turing proved that a general algorithm to solve the halting problem for all possible
program-input pairs cannot exist. A key part of the proof is, that the Turing machine
was used as a mathematical definition of a computer and program (Source Halting
Problem).
Status of NP-Complete problems is another failure story, NP-complete problems are
problems whose status is unknown. No polynomial-time algorithm has yet been
discovered for any NP-complete problem, nor has anybody yet been able to prove that
no polynomial-time algorithm exists for any of them. The interesting part is, that if
any one of the NP-complete problems can be solved in polynomial time, then all of
them can be solved.
6. NP-complete problems are the hardest problems in the NP set. A decision problem
L is NP-complete if:
1) L is in NP (Any given solution for NP-complete problems can be verified
quickly, but there is no efficient known solution).
2) Every problem in NP is reducible to L in polynomial time (Reduction is defined
below).
7. A problem is NP-Hard if it follows property 2 mentioned above, and doesn’t need to
follow property 1. Therefore, the NP-Complete set is also a subset of the NP-Hard
set.
approximation Algorithms for NP -Hard Problems