DAA Assignment (Module4)
DAA Assignment (Module4)
Module - 4
How to program:
1. Representation of objects
2. Procedure to fill the values of object in all possible ways.
3. Recurse (or Repeat)
4. Procedure to check solution at each leaf node.
5. Procedure to evaluate cost.
6. Procedure to update the previous value. (Backtrack)
75
Example1: N Queen Problem. Consider N = 4. That means 4 Queen problem.
1 2 3 4 5 6 7 8 9
c a c b c a c a b
S=3
1 2 3 4
b c a c
Here, location of pattern is found with shift s=3. i.e. After 3 shifting we find the location of pattern.
Prefix function
Prefix function is applied on Pattern.
For each character of pattern, we find a value prefix[ j ]
This value indicates how much of the last comparison can be reused.
This value decides the next value of shift during comparison.
Example: a b a Length of the Pattern = 3
Polynomial time verification: We will discuss some verification algorithms that verify in languages.
Example: Hamiltonian cycle
Let n = number of vertices in graph.
The solution-algorithm has to check n! combination of cycle.
So it can’t be solved in polynomial time.
But, a verification algorithm can verify a given solution as follows.
1. Verify each vertex of given solution belongs to graph. [ This will take O(n) time ]
2. Verify each edge of given solution belongs to graph. [ This will take O(n2) time ]
Total time of verification algorithm = O(n) + O(n2) = O(n2) ; it is a polynomial time.
Verification algorithm: We have to verify the solution of a problem such that A(x, y) = 1
where x = input , y = output
The language of a verification algorithm A is given below
L = { x {0 , 1}* : there exist y {0,1}* such that A(x, y) = 1 }
L1 L2
SAT
3CNF-SAT
CLIQUE SUBSET-SUM
VERTEX COVER
HAM-CYCLE
TSP
The above figure shows that HAM-CYCLE can be reduced to TSP, 3CNF-SAT can be reduced to SUBSET-
SUM etc.
SUBSET-SUM problem: Given a set S and a number t. We have to find possible number of subset of S.
Sum of subset-elements is equal to t.
Example: S = { 6, 3, 2, 7, 4, 5, 1 } t = 8 . The solutions are { 6, 2 } , { 3, 5 } , { 2, 5, 1 } , { 3, 4, 1 } etc.
83
Approximation algorithm:
Sometime a problem is NP complete but still we have to solve it anyhow. There are 3 techniques to solve
NPC problem.
1. If the input is very small, then algorithm with exponential time is satisfactory.
2. Solve those special cases of problem which take polynomial time.
3. Find a near optimal solution in polynomial time. These algorithms are called approximation algorithms.
Let n = input size, C = cost of solution given by approximation algorithm, C* = cost of optimal solution
C C *
Approximation Ratio max ,
C * C
NOTE:
Approximation ratio is inversely proportional to solution. Lesser is the approximation ratio, better is the
solution.
One of the approximation algorithms produces optimal solution.
Abstract Problem:
An abstract problem Q is a binary relation on I and S. where I = set of problem instances, S = set of problem
solutions
Example: Shortest path problem
Instance: <G, u, v> G = graph, u = source, v = destination
Solution: <u, v1, v2, . . . . , v>
Optimization problem:
An abstract problem is optimization problem in which some value is minimized or maximized.