Designa and Analysis of Algorithms3
Designa and Analysis of Algorithms3
1. WHAT IS AN ALGORITHM:
Informal Definition:
An Algorithm is any well-defined computational procedure that takes some value
or set of values as Input and produces a set of values or some value as output. Thus
algorithm is a sequence of computational steps that transforms the i/p into the o/p.
Formal Definition:
An Algorithm is a finite set of instructions that, if followed, accomplishes a
particular task. In addition, all algorithms should satisfy the following criteria.
ALGORITHM SPECIFICATION:
1. Pseudo-code Method:
In this method, we should typically describe
algorithms as program, which resembles language like Pascal & algol.
Pseudo-Code Conventions:
3. An identifier begins with a letter. The data types of variables are not
explicitly declared.
Here link is a pointer to the record type node. Individual data items of a
record can be accessed with and period.
<Variable>:= <expression>;
<statement-n>
}
For Loop:
For variable: = value-1 to value-2 step step do
{
<statement-1>
.
.
.
<statement-n>
}
repeat-until:
repeat
<statement-1>
.
.
.
<statement-n>
until<condition>
Case statement:
Case
{
}
As an example, the following algorithm fields & returns the maximum of ‘n’
given numbers:
3. algorithm Max(A,n)
4. // A is an array of size n
5. {
6. Result := A[1];
7. for I:= 2 to n do
8. if A[I] > Result then
9. Result :=A[I];
10. return Result;
11.}
Selection Sort:
3. ( From those elements that are currently unsorted ,find the smallest &place
it next in the sorted list.)
Algorithm:
1. For i:= 1 to n do
2. {
3. Examine a[I] to a[n] and suppose the smallest element isat
a[j];
4. Interchange a[I] and a[j];5.
}
t := a[i];
a[i]:=a[j];
a[j]:=t;
Theorem:
Algorithm selection sort(a,n) correctly sorts a set of n>=1 elements .Theresult
remains is a a[1:n] such that a[1] <= a[2] ….<=a[n].
Selection Sort:
Selection Sort begins by finding the least element in the list. This element
is moved to the front. Then the least element among the remaining element is found out
and put into second position. This procedure is repeated till the entire list has been
studied.
Example:
LIST L = 3,5,4,1,2
1 is selected , 1,5,4,3,2
2 is selected, 1,2,4,3,5
3 is selected, 1,2,3,4,5
4 is selected, 1,2,3,4,5
Proof:
We first note that any I, say I=q, following the execution of lines 6 to 9,it is
the case that a[q] Þ a[r],q<r<=n.
Also observe that when ‘i’ becomes greater than q, a[1:q] is unchanged.
Hence, following the last execution of these lines (i.e.I=n).We have a[1]
<= a[2] <=……a[n].
We observe this point that the upper limit of the for loop in the line 4can
be changed to n-1 without damaging the correctness of the algorithm.
Algorithms:
Towers of Hanoi:
.
.
.
Algorithm:
1. Algorithm TowersofHanoi(n,x,y,z)
2. //Move the top ‘n’ disks from tower x to tower y.3. {
.
.
.
4.if(n>=1) then
5. {
6. TowersofHanoi(n-1,x,z,y);
7. Write(“move top disk from tower “ X ,”to top of
tower “ ,Y);
Towersofhanoi(n-1,z,y,x);
}
}
{ (a,b,c),(a,c,b),(b,a,c),(b,c,a),(c,a,b),(c,b,a)}
1. It is easy to see that given ‘n’ elements there are n! different
permutations.
8. A simple algorithm can be obtained by looking at the case of 4
statement(a,b,c,d)
9. The Answer can be constructed by writing
Algorithm:
Algorithm perm(a,k,n)
{
if(k=n) then write (a[1:n]); // output permutation else
//a[k:n] ahs more than one permutation
// Generate this recursively.for
I:=k to n do
{
t:=a[k];
a[k]:=a[I];
a[I]:=t;
perm(a,k+1,n);
//all permutation of a[k+1:n]
t:=a[k];
a[k]:=a[I];
a[I]:=t;
}
}
PERFORMANCE ANALYSIS:
Space Complexity:
The space complexity of an algorithm is the amount of money itneeds
to run to compilation.
Time Complexity:
The time complexity of an algorithm is the amount of computertime
it needs to run to compilation.
The Space needed by each of these algorithms is seen to be the sum of the
following component.
1.A fixed part that is independent of the characteristics (eg:number,size)of the inputs
and outputs.
The part typically includes the instruction space (ie. Space for the code), space
for simple variable and fixed-size component variables (also called aggregate) space
for constants, and so on.
A variable part that consists of the space needed by component variables
whose size is dependent on the particular problem instance being solved, the
space needed by referenced variables (to the extent that is depends oninstance
characteristics), and the recursion stack space.
Example 2:
Algorithm sum(a,n)
{
s=0.0;
for I=1 to n do
s= s+a[I];
return s;
}
1. The problem instances for this algorithm are characterized by n,the number of
elements to be summed. The space needed d by ‘n’ is one word, since it is of
type integer.
2. The space needed by ‘a’a is the space needed by variables of tyepe array of
floating point numbers.
3. This is atleast ‘n’ words, since ‘a’ must be large enough to hold the ‘n’
elements to be summed.
4. So,we obtain Ssum(n)>=(n+s)
[ n for a[],one each for n,I a& s]
The time T(p) taken by a program P is the sum of the compile time and the run
time(execution time)
The compile time does not depend on the instance characteristics. Also we may
assume that a compiled program will be run several times without recompilation
.This rum time is denoted by tp(instance characteristics).
The number of steps any problem statemn t is assigned depends on the kind of
statement.
We introduce a variable, count into the program statement to increment count with
initial value 0.Statement to increment count by the appropriate amount are introduced
into the program.
This is done so that each time a statement in the original program is executes
count is incremented by the step count of that statement.
Algorithm: Algorithm
sum(a,n)
{
s= 0.0;
count = count+1;
for I=1 to n do
{
count =count+1;
s=s+a[I];
count=count+1;
}
count=count+1;
count=count+1;
return s;
}
If the count is zero to start with, then it will be 2n+3 on termination.
Total 2n+3
Most of the time, average-case analysis are performed under the more orless
realistic assumption that all instances of any given size are equally likely.
For sorting problems, it is simple to assume also that all the elements to besorted
are distinct.
Suppose we have ‘n’ distinct elements to sort by insertion and all n!
permutation of these elements are equally likely.
To determine the time taken on a average by the algorithm ,we could add the
times required to sort each of the possible permutations ,and then divideby n! the
answer thus obtained.
An alternative approach, easier in this case is to analyze directly the time
required by the algorithm, reasoning probabilistically as we proceed.
For any I,2 I n, consider the sub array, T[1….i].
The partial rank of T[I] is defined as the position it would occupy if the subarray
were sorted.
For Example, the partial rank of T[4] in [3,6,2,5,1,7,4] in 3 because T[1….4]once
sorted is [2,3,5,6].
Clearly the partial rank of T[I] does not depend on the order of the elementin
Sub array T[1…I-1].
Analysis
Best case:
This analysis constrains on the input, other than size. Resulting in the fasters
possible run time
Worst case:
This analysis constrains on the input, other than size. Resulting in
the fasters possible run time
Average case:
This type of analysis results in average running time over every
type of input.
Complexity:
Complexity refers to the rate at which the storage time grows as a
function of the problem size
Asymptotic analysis:
Expressing the complexity in term of its relationship to know
function. This type analysis is called asymptotic analysis.
Prepared By Mr.P.Muthyalu
NARAYANA ENGINEERING COLLEGE::GUDUR
ASYMPTOTIC NOTATION:
Big ‘oh’: the function f(n)=O(g(n)) iff there exist positive constants c and no suchthat
f(n)≤c*g(n) for all n, n ≥ no.
Omega: the function f(n)=Ω(g(n)) iff there exist positive constants c and no suchthat
f(n) ≥ c*g(n) for all n, n ≥ no.
Theta: the function f(n)=ө(g(n)) iff there exist positive constants c1,c2 and no suchthat c1
g(n) ≤ f(n) ≤ c2 g(n) for all n, n ≥ no.
Recursion:
Recursion may have the following definitions:
-The nested repetition of identical algorithm is recursion.
-It is a technique of defining an object/process by itself.
-Recursion is a process by which a function calls itself repeatedly until some
specified condition has been satisfied.
Recursion can be used for repetitive computations in which each action is stated
in terms of previous result. There are two conditions that must be satisfied by any
recursive procedure.
1 .Each time a function calls itself it should get nearer to the solution.
2 .There must be a decision criterion for stopping the process.
In making the decision about whether to write an algorithm in recursive or non- recursive
form, it is always advisable to consider a tree structure for the problem. If the structure is
simple then use non- recursive form. If the tree appears quite bushy, with little
duplication of tasks, then recursion is suitable.
The recursion algorithm for finding the factorial of a number is given below,
Algorithm : factorial-recursion
Input : n, the number whose factorial is to be found.Output : f,
the factorial of n
Method : if(n=0)
f=1
else
f=factorial(n-1) * nif
end
algorithm ends.
Prepared By Mr.P.Muthyalu
NARAYANA ENGINEERING COLLEGE::GUDUR
2. If the termination criterion is reached perform final computation andgoto
step 3 otherwise perform final computations and goto step 1
3. Restore the most recently saved parameters, local variable and return
address and goto the latest return address.
3. A recursive procedure can be called from within or outside itself and to ensure its
proper functioning it has to save in some order the return addresses so that, a
return to the proper location will result when the return to a calling statement is
made.
4. The recursive programs needs considerably more storage and will take more
time.
Prepared By Mr.P.Muthyalu
NARAYANA ENGINEERING COLLEGE::GUDUR
obviously uses time that is O(n) where as recursive function has an exponentialtime
complexity.
It is always true that recursion can be replaced by iteration and stacks. It is alsotrue
that stack can be replaced by a recursive program with no stack.
0 if n=0
T(n) = 3T(n ÷ 2)+n otherwise
8. First, we tabulate the value of the recurrence on the first few powers of 2.
n 1 2 4 8 16 32
T(n) 1 5 19 65 211 665
Then,
T(A) = 3 * T(2) +4
= 3*(3*1+2)+4
= (32*1)+(3*2)+4
n T(n)
o 1
o 3*1+2
22 32*1+3*2+22
23 33*1+32*2+3*22 +23
24 34*1+33*2+32*22+3*23+24
25 35*1+34*2+33*22+32*23+3*24+25
9. The pattern is now obvious.
= ∑ 3k-i 2i
= 3k ∑ (2/3)i
Let Sn be the sum of the first n terms of the geometric series a, ar,
2
ar ….Then
Sn = a(1-rn)/(1-r), except in the special case when r = 1; when Sn = an.
3 k+1 – 2k+1 3
= 3k * ----------------- * ----
3 k+1 1
3 k+1 – 2k+1
k
=3 * -----------------
3k+1-1
= 3k+1 – 2k+1
EG:2
0 n=0
tn = 5 n=1
3tn-1 + 4tn-2, otherwise
tn = 3tn-1 – 4tn-2 = 0General function
Characteristics Polynomial, x2 – 3x – 4 = 0
(x – 4)(x + 1) = 0
Roots r1 = 4, r2 = -1
C2=-1 , C1=1
fn = 1. 4n + (-1) . (-1)n
fn = 4n + 1n
GENERAL METHOD:
These sub problems must be solved, and then a method must be found to
combine sub solutions into a solution of the whole.
If the sub problems are still relatively large, then the divide-and-conquer
strategy can possibly be reapplied.
Often the sub problems resulting from a divide-and-conquer design are ofthe
same type as the original problem.
Combine is a function that determines the solution to p using the solutionsto the
‘k’ sub problems.
Where T(n) is the time for D And C on any I/p of size ‘n’.
g(n) is the time of compute the
answer directly for small
I/ps.
f(n) is the time for dividing P & combining the solution tosub
problems.
Example:
11) Consider the case in which a=2 and b=2. Let T(1)=2 & f(n)=n. We
have,
T(n) = 2T(n/2)+n
= 2[2T(n/2/2)+n/2]+n
= [4T(n/4)+n]+n
= 4T(n/4)+2n
= 4[2T(n/4/2)+n/4]+2n
= 4[2T(n/8)+n/4]+2n
= 8T(n/8)+n+2n
= 8T(n/8)+3n
In general, we see that T(n)=2^iT(n/2^i )+in., for any log n >=I>=1. T(n)
= n. T(n/n) + n log n
= n. T(1) + n log n [since, log 1=0, 2^0=1] = 2n + n log n
BINARY SEARCH:
Algorithm, describes this binary search method, where Binsrch has 4I/ps a[], I , l &x.
It is initially invoked as Binsrch (a,1,n,x)
A non-recursive version of Binsrch is given below.This
Binsearch has 3 i/ps a,n, & x.
The while loop continues processing as long as there are more elements left tocheck.
At the conclusion of the procedure 0 is returned if x is not present, or ‘j’ is
returned, such that a[j]=x.
We observe that low & high are integer Variables such that each time through the loop
either x is found or low is increased by at least one or high is decreased at least one.
Proof:
We assume that all statements work as expected and that comparisons such as
x>a[mid] are appropriately carried out.
Otherwise we observe that each time thro’ the loop the possible elementsto be
checked of or equality with x and a[low], a[low+1],……..,a[mid],……a[high].
Let us consider another simple problem that can be solved by the divide-and-
conquer technique.
The problem is to find the maximum and minimum items in a set of ‘n’elements.
More importantly, when the elements in a[1:n] are polynomials, vectors, very
large numbers, or strings of character, the cost of an element comparison is much
higher than the cost of the other operations.
Hence, the time is determined mainly by the total cost of the elementcomparison.
Straight MaxMin requires 2(n-1) element comparison in the best, average & worst
cases.
The worst case occurs when the elements are in decreasing order.The
no. of elements comparison is 2(n-1)
On the average a[I] is > than max half the time, and so, the avg. no. of comparison is 3n/2-
1.
A divide- and conquer algorithm for this problem would proceed as follows: Let
If the list has more than 2 elements, P has to be divided into smaller instances. For
example , we might divide ‘P’ into the 2 instances, P1=([n/2],a[1], ......................... a[n/2]) &
P2= (n-[n/2],a[[n/2]+1], ....... ,a[n])
After having divided ‘P’ into 2 smaller sub problems, we can solve them by
recursively invoking the same divide-and-conquer algorithm.
When ‘n’ is a power of 2, n=2^k for some +ve integer ‘k’, then T(n)
= 2T(n/2) +2
= 2(2T(n/4)+2)+2
= 4T(n/4)+4+2
*
*
= 2^k-1T(2)+
= 2^k-1+2^k-2
= 2^k/2+2^k-2
= n/2+n-2
= (n+2n)/2)-2
T(N)=(3N/2)-2
*Note that (3n/3)-3 is the best-average, and worst-case no. of comparisons when‘n’ is
a power of 2.
MERGE SORT
1. Algorithm MergeSort(low,high)
2. //a[low:high] is a global array to be sorted
3. //Small(P) is true if there is only one element
4. //to sort. In this case the list is already sorted.5. {
6. if (low<high) then //if there are more than one element 7.
{
8. //Divide P into subproblems
9. //find where to split the set
10. mid = [(low+high)/2];
11. //solve the subproblems.
12. mergesort (low,mid);
13. mergesort(mid+1,high);
14. //combine the solutions .
15. merge(low,mid,high);
16. }
17. }
1. Algorithm merge(low,mid,high)
2. //a[low:high] is a global array containing
3. //two sorted subsets in a[low:mid]
4. //and in a[mid+1:high].The goal is to merge these 2 sets into
5. //a single set residing in a[low:high].b[] is an auxiliary global array. 6. {
7. h=low; I=low; j=mid+1;
8. while ((h<=mid) and (j<=high)) do9.
{
10. if (a[h]<=a[j]) then
11. {
12. b[I]=a[h];
13. h = h+1;
14. }
15. else
16. {
17. b[I]= a[j];
18. j=j+1;
19. }
20. I=I+1;
21. }
22. if (h>mid) then
23. for k=j to high do
24. {
25. b[I]=a[k];
26. I=I+1;
27. }
28. else
29. for k=h to mid do
30. {
31. b[I]=a[k];
32. I=I+1;
33. }
34. for k=low to high do a[k] = b[k];
35. }
Consider the array of 10 elements a[1:10] =(310, 285, 179, 652, 351, 423,
861, 254, 450, 520)
Algorithm Mergesort begins by splitting a[] into 2 sub arrays each of sizefive
(a[1:5] and a[6:10]).
The elements in a[1:5] are then split into 2 sub arrays of size 3 (a[1:3] ) and
2(a[4:5])
Then the items in a a[1:3] are split into sub arrays of size 2 a[1:2] &
one(a[3:3])
The 2 values in a[1:2} are split to find time into one-element sub arrays, andnow
the merging begins.
(310| 285| 179| 652, 351| 423, 861, 254, 450, 520)
Repeated recursive calls are invoked producing the following sub arrays.(179,
285, 310, 351, 652| 423| 861| 254| 450, 520)
Next a[9] &a[10] are merged, and then a[6:8] & a[9:10] (179,
285, 310, 351, 652| 254, 423, 450, 520, 861 )
At this point there are 2 sorted sub arrays & the final merge produces the
fully sorted result.
(179, 254, 285, 310, 351, 423, 450, 520, 652, 861)
T(n)=O(n log n)
QUICK SORT
In merge sort, the file a[1:n] was divided at its midpoint into sub arrays which
were independently sorted & later merged.
In Quick sort, the division into 2 sub arrays is made so that the sorted subarrays
do not need to be merged later.
This is accomplished by rearranging the elements in a[1:n] such that a[I]<=a[j] for
all I between 1 & n and all j between (m+1) & n for some m, 1<=m<=n.
It is assumed that a[p]>=a[m] and that a[m] is the partitioning element. If m=1 &
p-1=n, then a[n+1] must be defined and must be greater than or equal to allelements in
a[1:n]
The assumption that a[m] is the partition element is merely for convenience,
other choices for the partitioning element than the first item in theset are better in
practice.
1. Algorithm Partition(a,m,p)
2. //within a[m],a[m+1],…..,a[p-1] the elements
3. // are rearranged in such a manner that if
4. //initially t=a[m],then after completion
5. //a[q]=t for some q between m and
6. //p-1,a[k]<=t for m<=k<q, and
7. //a[k]>=t for q<k<p. q is returned
8. //Set a[p]=infinite.
1. Algorithm Interchange(a,I,j)
2. //Exchange a[I] with a[j]
3. {
4. p=a[I];
5. a[I]=a[j];
6. a[j]=p;
7. }
1. Algorithm Quicksort(p,q)
2. //Sort the elements a[p],….a[q] which resides
3. //is the global array a[1:n] into ascending
4. //order; a[n+1] is considered to be defined
5. // and must be >= all the elements in a[1:n]6.
{
7. if(p<q) then // If there are more than one element8.
{
9. // divide p into 2 subproblems
10. j=partition(a,p,q+1);
11. //’j’ is the position of the partitioning element.
12. //solve the subproblems.
13. quicksort(p,j-1);
14. quicksort(j+1,q);
15. //There is no need for combining solution.
16. }
17. }
#include <stdio.h>
#include <conio.h>
Partition(int m, int p)
{
int v,I,j;
v=a[m];
i=m;
j=p;
do
{
do
i=i+1;
while(a[i]<v);
if (i<j)
interchange(I,j);
} while (I<j);
a[m]=a[j]; a[j]=v;
return j;
}
Interchange(int I, int j)
Output:
Enter the no. of elements 5
Enter the array elements
3
8
1
5
2
The sorted elements are,1
2
3
5
8
1. Let A and B be the 2 n*n Matrix. The product matrix C=AB is calculated by
using the formula,
3. Divide and conquer method suggest another way to compute the product of
n*n matrix.
6. If n>2,Then the elements are partitioned into sub matrix n/2*n/2..since ‘n’ is a
power of 2 these product can be recursively computed using the same formula .This
Algorithm will continue applying itself to smaller sub matrix until ‘N” become suitable
small(n=2) so that the product is computed directly .
For EX:
2222 1 1 11
4*4= 2222 1111
2222 *11 11
2222 1 11 1
22 2 2 1 1 1 1 4 4 4 4
22 22 * 11 1 1 = 44 44
22 2 2 1 1 1 1 4 4 4 4
22 2 2 11 1 1 4 4 4 4
Example
4 4 4 4
*
4 4 4 4
32 32
since n/2n/2 &matrix can be can be added in Cn for some constant C, The overall
computing time T(n) of the resulting divide and conquer algorithm is given by the
sequence.
That is T(n)=O(n^3)
* Matrix multiplication are more expensive then the matrix addition O(n^3).We can
attempt to reformulate the equation for Cij so as to have fewer multiplication and
possibly more addition .
12. Stressen has discovered a way to compute the Cij of equation (2) usingonly
7 multiplication and 18 addition or subtraction.
13. Strassen’s formula are
P= (A11+A12)(B11+B22)
Q= (A12+A22)B11
R= A11(B12-B22)
S= A22(B21-B11)
T= (A11+A12)B22
U= (A21-A11)(B11+B12)
V= (A12-A22)(B21+B22)
C11=P+S-T+V
C!2=R+t
C21=Q+T
C22=P+R-Q+V
As the name suggest they are short sighted in their approach taking decision on the
basis of the information immediately at the hand without worrying about the effect these
decision may have in the future.
DEFINITION:
A problem with N inputs will have some constraints .any subsets that satisfy these
constraints are called a feasible solution.
A feasible solution that either maximize can minimize a given objectives function is
called an optimal solution.
5.For i=1 to n do
6.{
7.x=select(a);
8.if(feasible(solution,x))then
9.solution=union(solution,x);
10.}
11.return solution;
12.}
* The function select an input from a[] and removes it. The select input value isassigned
to X.
The function Greedy describes the essential way that a greedy algorithm will once
a particular problem is chosen ands the function subset, feasible & union are
properly implemented.
Example
cents)
Quarters(25 cents)
Dimes( 10 cents)
Nickel(5 Cents)
Pennies(1 cent)
Our aim is paying a given amount to a customer using the smallest possible
number of coins.
KNAPSACK PROBLEM
we are given n objects and knapsack or bag with capacity M object I has a weightWi
where I varies from 1 to N.
The problem is we have to fill the bag with the help of N objects and the resultingprofit
has to be maximum.
There are so many ways to solve this problem, which will give many feasible
solution for which we have to find the optimal solution.
But in this algorithm, it will generate only one solution which is going to be
feasible as well as optimal.
Select an object with highest p/w ratio and check whether its height is lesser than the
capacity of the bag.
If so place 1 unit of the first object and decrement .the capacity of the bag by the weight
of the object you have placed.
Repeat the above steps until the capacity of the bag becomes less than the weight of the
object you have selected .in this case place a fraction of the object and come out of the
loop.
ALGORITHM:
p[i]/w[i] >=p[i+1]/W[i+1]
8.U=n;
9.For I=1 to n do
10.{
13.x[i]=1.0;U=U-w[i]
14.}
15.if(i<=n)then x[i]=U/w[i];
16.}
Example:
Capacity=20
N=3 ,M=20
Wi=18,15,10
Pi/Wi=25/18=1.36,24/15=1.6,15/10=1.5
Pi = 24 15 25
Wi = 15 10 18
Xi = 1 5/10 0
PiXi=1*24+0.5*15 31.5
X1 X2 X3 WiXi PiXi
1 2/5 0 20 18.2
0 2/3 1 20 31
0 1 ½ 20 31.5
Of these feasible solution Solution 4 yield the Max profit .As we shall soon see this
solution is optimal for the given problem instance.
Points To remember:
To complete a job, one has to process the job or a action for one unit of time.Only
A feasible solution for this problem is a subset of j of jobs such that each job inthis
subject can be completed by this deadline.
Since one job can be processed in a single m/c. The other job has to be in its
waiting state until the job is completed and the machine becomes free.
So the waiting time and the processing time should be less than or equal to thedead
line of the job.
Algorithm JS(d,j,n)
d[0]= J[0]=0;
{ // consider jobs in non increasing order of P[I];find the position for I and checkfeasibility
insertion
r=k;
while((d[J[r]]>d[i] )and
if (d[J[r]]<d[I])and (d[I]>r))then
J[r+1]=i;
return k;
1. n=5 (P1,P2,…P5)=(20,15,10,5,1)
(d1,d2….d3)=(2,2,1,3,3)
(1) (1) 20
(2) (2) 15
(3) (3) 10
(4) (4) 5
(5) (5) 1
1,2) (2,1) 35
(1,3) (3,1) 30
(1,4) (1,4) 25
(1,5) (1,5) 21
(2,3) (3,2) 25
(2,4) (2,4) 20
(2,5) (2,5) 16
(1,2,3) (3,2,1) 45
(1,2,4) (1,2,4) 40
n=4 (P1,P2,…P4)=(100,10,15,27)
(d1,d2….d4)=(2,1,2,1)
(2,3) (9,3) 25
(2,4) (4,2) 37
(3,4) (4,3) 42
(3) (3) 15
(4) (4) 27
Let G(V,E) be an undirected connected graph with vertices ‘v’ and edge ‘E’. A
The problem is to generate a graph G’= (V,E) where ‘E’ is the subset of E,G’ is a
Minimum spanning tree.
Each and every edge will contain the given non-negative length .connect all thenodes
with edge present in set E’ and weight has to be minimum.
NOTE:
The subset tree (i.e) any connected graph with ‘N’ vertices must have at least N-1
edges and also it does not form a cycle.
Definition:
A spanning tree of a graph is an undirected tree consisting of only those edge that are
necessary to connect all the vertices in the original graph.
A Spanning tree has a property that for any pair of vertices there exist only one path
between them and the insertion of an edge to a spanning tree form a unique cycle.
The cost of a spanning tree is the sum of cost of the edges in that trees.There are
1. Kruskal’s Algorithm
2. Prom’s Algorithm.
To find the minimum cost spanning tree the edge are inserted to tree in increasingorder
of their cost
Algorithm:
Algorithm kruskal(E,cost,n,t)
//cost[u,v] cost of edge (u,v).t set of edge in minimum cost spanning tree
not empty)) do
j=find(n);
k=find(v);
i=i+1
t[i,1]=u;
t[i,2]=v;
union(j,k);
Analysis
14. The time complexity of minimum cost spanning tree algorithm in worstcase
is O(|E|log|E|),
Step 1. In the graph, the Edge(g, h) is shortest. Either vertex g or vertex h could be
representative. Lets choose vertex g arbitrarily.
Step 2. The edge (c, i) creates the second tree. Choose vertex c as representativefor
second tree.
Step 3. Edge (g, g) is the next shortest edge. Add this edge and choose vertex g as
representative.
Step 5. Add edge (c, f) and merge two trees. Vertex c is chosen as the
representative.
Step 6. Edge (g, i) is the next next cheapest, but if we add this edge a cycle wouldbe
created. Vertex c is the representative of both.
Step 10. Again, if we add edge (b, c), it would create a cycle. Add edge (d, e)
instead to complete the spanning tree. In this spanning tree all trees joined and
vertex c is a sole representative.
PRIM'S ALGORITHM
Start from an arbitrary vertex (root). At each stage, add a new branch (edge) to the tree
already constructed; the algorithm halts when all the vertices in the graph have been
reached.
Algorithm prims(e,cost,n,t)
{
Let (k,l) be an edge of minimum cost in E;
Mincost :=cost[k,l];
T[1,1]:=k; t[1,2]:=l;
For I:=1 to n do
Else near[i]:=k;
Near[k]:=near[l]:=0;
Cost[j,near[j]] is minimum;
T[i,1]:=j; t[i,2]:=near[j];
Mincost:=mincost+ Cost[j,near[j]];
Near[j]:=0;
For k:=0 to n do
Near[k]:=j;
Return mincost;
}
15. The prims algorithm will start with a tree that includes only a minimum cost edge
of G.
16. Then, edges are added to the tree one by one. the next edge (i,j) to be added in
such that I is a vertex included in the tree, j is a vertex not yet included, and
cost of (i,j), cost[i,j] is minimum among all the edges.
1: Step 2:
Step 3: Step 4:
The problems defined by these questions are special case of the path problem we study
in this section. The length of a path is now defined to be the sum of the weights of the
edges on that path. The starting vertex of the
Example:
Consider the digraph of fig 7- 1. Let the numbers on the edges be the costs of travelling
along that route. If a person is interested travel from v1 to v2, then he encounters many
paths. Some of them are
o v1 v2 = 50 units
o v1 v3 v4 v2 = 10+15+20=45 units
o v1 v5 v4 v2 = 45+30+20= 95 units
o v1 v3 v4 v5 v4 v2 = 10+15+35+30+20=110 units
The cheapest path among these is the path along v1 v3 v4 v2. The cost of the
path is 10+15+20 = 45 units. Even though there are three edges on this path, it is
cheaper than travelling along the path connecting v1 and v2 directly i.e., the path v1 v2
that costs 50 units. One can also notice that, it is not possible to travel to v6 from any
other node.
Step 1: find the adjacency matrix for the given graph. The adjacency matrix for fig
is given below
V1 - 50 10 Inf 45 Inf
Step 2: consider v1 to be the source and choose the minimum entry in the row v1.In the
above table the minimum in row v1 is 10.
Step 3: find out the column in which the minimum is present, for the above
example it is column v3. Hence, this is the node that has to be next visited.
V2 V4 V5 V6
V1 Vw 50 Inf 45 Inf
Minimum 50 25 45 inf
V2 V5 V6
V1 Vw 50 45 Inf
Minimum 45 45 inf
V5 V6
V1 Vw 45 Inf
V1 V3 V4 V2 45+10 45+inf
Vw
Minimum 45 Inf
V6
V1 Vw Inf
V1 V3 V4 V2 V5 45+inf
Vw
Minimum inf
Finally the cheapest path from v1 to all other vertices is given by V1 V3 V4 V2 V5.
1. The idea of dynamic programming is thus quit simple: avoid calculating the same
thing twice, usually by keeping a table of known result that fills up a sub
instances are solved.
5. We usually start with the smallest and hence the simplest sub-instances.
7. The essential difference between the greedy method and dynamic programming
is that the greedy method only one decision sequence is evergenerated.
2. We want to calculate the length of the shortest path between each pair of nodes.
4. The principle of optimality applies: if k is the node on the shortest path from i to j then
the part of the path from i to k and the part from k to j must also be optimal, that is
shorter.
7. We have to perform N iteration after iteration k.the matrix D will give you the
distance between nodes with only (1,2...,k)as intermediate nodes.
8. At the iteration k, we have to check for each pair of nodes (i,j) whether or notthere
exists a path from i to j passing through node k.
D0=L= 05
500 15 5
30 0 15
15 5 0
175 1112 - -
2 72 21--24
33 - 32 - -
4 4 1 41 – 43 -
vertex 1:
7 5 11 12 - -
7 12 2 21 212 - 24
3 - 32 - -
4 9 1 41 412 43 –
vertex 2:
7 12 2 21 212 - 24
10 3 5 321 32 - 324
4 9 1 11 41 412 43 4124
vertex 3:
7 5 7 11 12 - 124
7 12 2 21 212 - 24
10 3 5 321 32 - 324
4 4 1 6 41 432 43 4324
vertex 4:
7 5 8 7 11 12 1243 124
4 4 1 6 41 432 43 4324
1. At 0th iteration it nil give you the direct distances between any 2 nodes
D0=0 5
50015 5
30 015
15 5 0
0 5
50 0 15 5 p[3,2]= 1
1520 5 0
15
30
5 50 5 15
15
3. likewise we have to find the value for N iteration (ie) for N nodes.
D2= 50 0 15 5 P[1,4] = 2
30 35 0 15
15 20 5 0
0 5 20 10
D3= 45 0 15 5 P[2,1]=3
30 35 0 15
15 20 5 0
0 5 15 10
20 0 10 5 P[1,3]=4
D4= 30 35 0 15 P[2,3]=4
15 20 5 0
5. If you want the exact path then we have to refer the matrix p.The matrix willbe,
0042
P= 0100
0100
Finally we see the trips from 1 to 2, & from 2 to 4, are also direct. The
ALGORITHM :
array D[1..n,1..n]
D=L
For k = 1 to n do
For i = 1 to n do
For j = 1 to n do
D [ i , j ] = min (D[ i, j ], D[ i, k ] + D[ k, j ]
Return D
ANALYSIS:
MULTISTAGE GRAPH
A multistage graph G = (V,E) is a directed graph in which the vertices are
portioned into K > = 2 disjoint sets Vi, 1 <= i<= k.
In addition, if < u,v > is an edge in E, then u < = Vi and V Vi+1 for some i, 1<= i
< k.
If there will be only one vertex, then the sets Vi and Vk are such that [Vi]=[Vk]
= 1.
The cost of a path from source (s) to destination (t) is the sum of the costs ofthe
edger on the path.
The MULTISTAGE GRAPH problem is to find a minimum cost path from ‘s’ to‘t’.
Each set Vi defines a stage in the graph. Every path from ‘s’ to ‘t’ starts in
stage-1, goes to stage-2 then to stage-3, then to stage-4, and so on, and
terminates in stage-k.
Forward Method.
o Backward Method.
FORWARD METHOD
In this FORWARD approach, we will find out the cost of each and every node
starling from the ‘k’ th stage to the 1st stage.
We will find out the path (i.e.) minimum cost path from source to the
destination (ie) [ Stage-1 to Stage-k ].
PROCEDURE:
V1 V2 V3 V4 V5
4 6
2 2
5 4
9 1
7 3 2
7
t
11 5 5
11 6
1. Maintain a cost matrix cost (n) which stores the distance from any vertex to the
destination.
2. If a vertex is having more than one path, then we have to choose the minimum
distance path and the intermediate vertex, which gives the minimum distance
path, will be stored in the distance array ‘D’.
3. In this way we will find out the minimum cost path from each and every
vertex.
4. Finally cost(1) will give the shortest distance from source to destination.
5. For finding the path, start from vertex-1 then the distance array D(1) will give the
minimum cost neighbour vertex which in turn give the next nearest vertex and
proceed in this way till we reach the Destination.
6. For a ‘k’ stage graph, there will be ‘k’ vertex in the path.
7. In the above graph V1…V5 represent the stages. This 5 stage graph can be
solved by using forward approach as follows,
STEPS: - DESTINATION, D
l Vi + 1
(j,l) E
= min (5 + 2, 6 + 5)
= min (7,11)
=7
cost(8) =7 =>D(8)=10
(4+4,3+2)
= min(8,5)
=5
cost(7) = 5 =>D(7) = 10
= min(6+4 , 5 +2)
= min(10,7)
=7
cost(6) = 7 =>D(6) = 10
= min(11+5 , 8 +7)
= min(16,15)
= 15
cost(5) = 15 =>D(5) = 18
= min(11+7)
= 18
cost(4) = 18 =>D(4) = 8
= min(2+7 , 7 +5)
= min(9,12)
=9
= min(11,7,8)
=7
cost(2) = 7 =>D(2) = 7
= min(16,16,21,17)
= 16
cost(1) = 16 =>D(1) = 2
(i.e.)
D(1) = 2
D(2) = 7
D(7) =10
D (10) = 12
9 2 3 2
cost[n]=0.0;
//compute cost[j],
// let ‘r’ be the vertex such that <j,r> is an edge of ‘G’ &
// c[j,r]+cost[r] is minimum.
d[j] =r;
P[1]=1;
P[k]=n;
P[j]=d[p[j-1]];
ANALYSIS:
2.8.2.BACKWARD METHOD
if there one ‘K’ stages in a graph using back ward approach. we will find out
the cost of each & every vertex starting from 1st
stage to the kth stage.
We will find out the minimum cost path from destination to source (ie)[fromstage
k to stage 1]
Maintain a cost matrix to store the cost of every vertices and a distance
matrix to store the minimum distance vertex.
Find out the cost of each and every vertex starting from vertex 1 up to
vertex k.
To find out the path star from vertex ‘k’, then the distance array D (k) will give the
minimum cost neighbor vertex which in turn gives the next nearest neighbor vertex and
proceed till we reach the destination.
STEP:
=min(13,9)
cost(6) = 9 =>D(6)=3
=min(14,13,11)
cost(7) = 11 =>D(7)=2
=min(10,14,10)
cost(8) = 10 =>D(8)=2
=min(15,15)
cost(9) = 15 =>D(9)=6
Cost(10)=min(c(6,10)+cost(6),c(7,10)+cost(7)),c (8,10)+cost(8))
=min(14,14,15)
cost(10)= 14 =>D(10)=6
cost(11) = 16 =>D(11)=8
cost(12)=min(c(9,12)+cost(9),c(10,12)+cost(10),c(11,12)+cost(11))
=min(19,16,21)
cost(12) = 16 =>D(12)=10
PATH:
D(12) = 10
D(10) = 6
D(6) = 3
D(3) = 1
17 32 65 102 12
bcost[1]=0.0; for
j=2 to n do
//compute bcost[j],
// let ‘r’ be the vertex such that <r,j> is an edge of ‘G’ &
// bcost[r]+c[r,j] is minimum.
d[j] =r;
P[1]=1;
P[k]=n;
For j= k-1 to 2 do
P[j]=d[p[j+1]];
The cost of the tour is the sum of cost of the edges on the tour.
The tour is the shortest path that starts and ends at the same vertex (ie) 1.
APPLICATION :
Suppose we have to route a postal van to pick up mail from the mail boxes
located at ‘n’ different sites.
One vertex represent the post office from which the postal van starts and
return.
Edge <i,j> is assigned a cost equal to the distance from site ‘i’ to site ‘j’.
the route taken by the postal van is a tour and we are finding a tour ofminimum
length.
every tour consists of an edge <1,k> for some k V-{} and a path from vertexk to
vertex 1.
1. Find g(i, ) =ci1, 1<=i<n, hence we can use equation(2) to obtain g(i,s) for alls to
size 1.
2. That we have to start with s=1,(ie) there will be only one vertex in set ‘s’.
10
15
10
15
20 8 913
8 6
12
7
Cost matrix
0101520
5 0 910
6 13012
8890
starting position
g(i,s) =min{cij +g(j,s-{j})
STEP 1:
g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}
min{10+25,15+25,20+23}
min{35,35,43}
=35
STEP 2:
g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}
min{9+20,10+15}
min{29,25}
=25
g(3,{2,4}) =min{c32+g(2{4}),c34+g(4,{2})}
min{13+18,12+13}
min{31,25}
=25
g(4,{2,3}) = min{c42+g(2{3}),c43+g(3,{2})}
min{8+15,9+18}
min{23,27}
=23
STEP 3:
1. g(3,{4}) = min{c34 +g{4, }} 12+8
=20
=15
=18
=13
9+6=15
13+5=18
STEP 4:
g{4, } =c41 = 8
g{3, } =c31 = 6
g{2, } =c21 = 5
s = 0.
i =1 to n.
g(1, ) = c11 => 0
s =1
i =2 to 4
g(2,{3}) = c23 + g(3, )
= 9+6 =15
= 10+8 =18
= 13+5 =18
= 12+8 =20
= 8+5 =13
= 9+6 =15
s =2
i 1, 1 s and i s.
g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}
min{9+20,10+15}
min{29,25}
=25
g(3,{2,4}) =min{c32+g(2{4}),c34+g(4,{2})}
min{13+18,12+13}
min{31,25}
=25
min{8+15,9+18}
min{23,27}
=23
s =3
g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}
min{10+25,15+25,20+23}
min{35,35,43}
=35
optimal cost is 35
1. This problem is similar to ordinary knapsack problem but we may not takea
fraction of an object.
3. The problem is, we have to fill the bag with the help of ‘ N ‘ objects and the
resulting profit has to be maximum.
n
subject to Xi W i L M
i=l- n
7. In the table T[i,j] will be the maximum valve of the objects i varies from 1 to n and
j varies from O to M.
1. If i=l and j < w(i) then T(i,j) =o, (ic) o pre is filled in the table.
2. If i=l and j w (i) then T (i,j) = p(i), the cell is filled with the profit p[i], sinceonly
one object can be selected to the maximum.
4. If i>l and j w(i) then T (i,j) = {f(i) +T(i-l,j-w(i)),. since only ‘l’ unit can be selected
to the maximum. If is the current profit + profit of the previous object to fill the
remaining capacity of the bag.
Start with the last position of i and j, T[i,j], if T[i,j] = T[i-l,j] then no object of‘i’ is
required so move up to T[i-l,j].
Repeat the same process until we reach T[i,o], then there will be nothing tofill
the bag stop the process.
Consider a Example,
M=6,
N=3
W1=2,W2=3,W 3=4
P1=1,P2=2,P3=5
j 0 to 6
o<2 T1,o =0
i=l, j=2
2 o,= T1,2 = l.
i=l, j=3
3>2,= T1,3 = l.
i=l, j=4
4>2,= T1,4 = l.
i=l, j=5
5>2,= T1,5 = l.
i=l, j=6
6>2,= T1,6 = l.
i=2, j=1
T 2,1 =0
3.1.DEFINING GRAPH:
2 3
FIG: Graph G
{(1,2),(1,3),(1,4),(2,3),(2,4)}.
UNDIRECTED GRAPH:
DIRECTED GRAPH
COMPLETE GRAPH:
In Breadth first search we start at vertex v and mark it as having been reached. The
vertex v at this time is said to be unexplored. A vertex is said to have been explored by
an algorithm when the algorithm has visited all vertices adjacent from it. All unvisited
vertices adjacent from v are visited next. There are new unexplored vertices. Vertex v
has now been explored. The newly visited vertices have not been explored and are put
onto the end of the list of unexplored vertices. The first vertex on this list is the next to be
explored. Exploration continues until no unexplored vertex is left. The list of unexplored
vertices acts as a queue and can be represented using any of the standard queue
representations.
In Breadth First Search we start at a vertex ‘v’ and mark it as having been
reached (visited).
A vertex is said to have been explored by an algorithm when the algorithm has
visited all vertices adjust from it.
All unvisited vertices adjust from ‘v’ are visited next. These are new
unexplored vertices.
Vertex ‘v’ has now been explored. The newly visit vertices have not been
explored and are put on the end of a list of unexplored vertices.
The first vertex on this list in the next to be explored. Exploration continues until
no unexplored vertex is left.
ALGORITHM:
// initialized to zero.
repeat
(visited[w]=0) then
{Add w to q;
visited[w]=1
until (false)
algorithm BFT(G,n)
for i= 1 to n do
visited[i] =0;
for i =1 to n do
if (visited[i]=0)then BFS(i)
A depth first search of a graph differs from a breadth first search in that the exploration of
a vertex v is suspended as soon as a new vertex is reached. At this time the exploration
of the new vertex u begins. When this new vertex has been explored, the exploration of
u continues. The search terminates when all reached vertices have been fully explored.
This search process is best-described recursively.
Algorithm DFS(v)
visited[v]=1
If (visited[w]=0)then
DFS(w);
BACKTRACKING
BACKTRACKING
Many problems which deal with searching for a set of solutions or for aoptimal
solution satisfying some constraints can be solved using the backtracking
formulation.
The major advantage of this method is, once we know that a partial
vector (x1,…xi) will not lead to an optimal solution that (mi+1 ........................ mn)
possible test vectors may be ignored entirely.
Many problems solved using backtracking require that all the solutionssatisfy
a complex set of constraints.
i) Explicit constraints.
Explicit constraints:
Explicit constraints are rules that restrict each Xi to take values onlyfrom
a given set.
Xi =0 or 1 or Si={0,1}.
Li Xi Ui or Si= {a: Li a Ui}
All tupules that satisfy the explicit constraint define a possible solutionspace
for I.
Algorithm:
k=1;
While (k 0) do
write(X[1:k]);
All solutions are generated in X[1:n] and printed as soon as they are
determined.
1. Sum of subsets.
2. Graph coloring.
3. Hamiltonian cycle.
4. N-Queens problem.
SUM OF SUBSETS:
1) We are given ‘n’ positive numbers called weights and we have to find all
combinations of these numbers whose sum is M. this is called sum of subsets
problem.
3) If the state space tree of the solution, for a node at level I, the left child
corresponds to X(i)=1 and right to X(i)=0.
Example:
S, n, r
0,1,73
X(1)=1 x(1)=0
5,2,68 0,2,68
X(4)=0 x(4)=0
B
15,5,33 5,5,33 10,5,33
X(5)=1 x(5)=1
A 20,6,18
In the state space tree, edges from level ‘i’ nodes to ‘i+1’ nodes are labeledwith
the values of Xi, which is either 0 or 1.
The left sub tree of the root defines all subsets containing Wi.
The right subtree of the root defines all subsets, which does not includeWi.
Assign X(k)<- 1.
After generating the left sub tree we have to generate the right sub tree, forthis
we have to check S+W(k+1)<=M.B’coz W(k) is omitted and W(k+1) has to be
selected.
Repeat the process and find all the possible combinations of the subset.
Algorithm:
Algorithm sumofsubset(s,k,r)
X{k]=1;
X{k]=0;
If the Hamiltonian cycle begins at some vertex V1 belongs to G and thevertex are
visited in the order of V1,V2…….Vn+1,then the edges are in E,1<=I<=n and the
Vi are distinct except V1 and Vn+1 which are equal.
1 2 3 4
8 7 6 5
->1,3,4,5,6,7,8,2,1 and
->1,2,8,7,6,5,4,3,1.
Procedure:
Define a solution vector X(Xi ............. Xn) where Xi represents the I th visited
vertex of the proposed cycle.
The solution array initialized to all zeros except X(1)=1,b’coz the cycleshould start
at vertex ‘1’.
The vertex from 1 to n are included in the cycle one by one by checking 2
conditions,
2. The current vertex must be distinct and should not have been visited
earlier.
6. When these two conditions are satisfied the current vertex is included inthe
cycle, else the next vertex is tried.
7. When the nth vertex is visited we have to check, is there any path from nth vertex
to first 8vertex. if no path, the go back one step and after the previous visited node.
Loop
If k=n then
Print (x)
Else
Hamiltonian (k+1);
End if
Repeat
Repeat
} Until (false);
3.7.8-QUEENS PROBLEM:
This 8 queens problem is to place n-queens in an ‘N*N’ matrix in such a way that no two
queens attack each otherwise no two queens should be in the same row, column,
diagonal.
Solution:
The function, which is used to check these two conditions, is [I, X (j)], which
gives position of the I th queen, where I represents the row and X (j)represents
the column position.
Also, every element on the same diagonal that runs from lower right toupper left
has the same value.
Initialize x array to zero and start by placing the first queen in k=1 in thefirst row.
To find the column position start from value 1 to n, where ‘n’ is the no. Of
columns or no. Of queens.
If k=1 then x (k)=1.so (k,x(k)) will give the position of the k th queen. Herewe
have to check whether there is any queen in the same column or diagonal.
For this considers the previous position, which had already, been foundout.
Check whether
If any one of the conditions is true then return false indicating that k thqueen can’t
be placed in position X (k).
For not possible condition increment X (k) value by one and precede d untilthe
position is found.
If the position X (k) n and k=n then the solution is generated completely.
If k<n, then increment the ‘k’ value and find position of the next queen.
If the position X (k)>n then k th queen cannot be placed as the size of thematrix is
‘N*N’.
So decrement the ‘k’ value by one i.e. we have to back track and after the
position of the previous queen.
Algorithm:
//return true if a queen can be placed in k th row and I th column. otherwise itreturns //
Return true;
For I=1 to n do
X [k]=I;
Else nquenns(k+1,n) ;
Q Q
Q Q
Q Q
Q Q
Solutin-1 Solution 2
(2413) (3142)
GRAPH COLORING:
Let ‘G’ be a graph and ‘m’ be a given positive integer. If the nodes of ‘G’ can
be colored in such a way that no two adjacent nodes have the samecolor. Yet
only ‘M’ colors are used. So it’s called M-color ability decisionproblem.
The graph G can be colored using the smallest integer ‘m’. This integer is
referred to as chromatic number of the graph.
A graph is said to be planar iff it can be drawn on plane in such a way thatno
two edges cross each other.
Suppose we are given a map then, we have to convert it into planar. Consider
each and every region as a node. If two regions are adjacent thenthe
corresponding nodes are joined by an edge.
4 5
3 is adjacent to 1,2,4
2 3
4 is adjacent to 1,2,3,5
5 is adjacent to 2, 4
5 4
Steps to color the Graph:
1. First create the adjacency matrix graph(1:m,1:n) for a graph, if there is anedge
between i,j then C(i,j) = 1 otherwise C(i,j) =0.
2. The Colors will be represented by the integers 1,2,…..m and the solutionswill
be stored in the array X(1),X(2), ........................ ,X(n) ,X(index) is the color, index
is the node.
4. First one chromatic number is assigned ,after assigning a number for ‘k’ node,
we have to check whether the adjacent nodes has got the same values if so then
we have to assign the next value.
5. Repeat the procedure until all possible combinations of colors are found.
6. The function which is used to check the adjacent nodes and same color is,If((
2
1
4 3
N= 4
M= 3
Adjacency Matrix:
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
The state space tree will give all possible colors in that ,the numbers which are inside
the circles are nodes ,and the branch with a number is the colors of the nodes.
Algorithm:
Algorithm mColoring(k)
// the graph is represented by its Boolean adjacency matrix G[1:n,1:n] .All assignments
//of 1,2,……….,m to the vertices of the graph such that adjacent vertices are assigned
//distinct integers are printed. ’k’ is the index of the next vertex to color.
repeat
Write(x[1:n]);
Else mcoloring(k+1);
Algorithm Nextvalue(k)
// X[1],……X[k-1] have been assigned integer values in the range[1,m] such that
//adjacent values have distinct integers. A value for X[k] is determined in the
//range[0,m].X[k] is assigned the next highest numbers color while maintaining
//distinctness form the adjacent vertices of vertex K. If no such color exists, thenX[k] is
0.
repeat
For j=1 to n do
Then break;
We are given ‘n’ positive weights Wi and ’n’ positive profits Pi, and a positive
number ‘m’ that is the knapsack capacity, the is problem calls forchoosing a
subset of the weights such that,
WiXi PiXi
1in m and 1 in is Maximized.
The Solution space is the same as that for the sum of subset’s problem.
Bounding functions are needed to help kill some live nodes without expanding
them. A good bounding function for this problem is obtained byusing an upper
bound on the value of the best feasible solution obtainableby expanding the
given live node.
The profits and weights are assigned in descending order depend upon theratio.
Solution :
Then We are going to the next object, if the object weight is exceeded that
object does not fit. So unit of that object is ‘0’.(i.e.) K=0.
Then We are going to the bounding function ,this function determines anupper
bound on the best solution obtainable at level K+1.
Algorithm:
Algorithm Bknap(k,cp,cw)
// ‘m’ is the size of the knapsack; ‘n’ no.of weights & profits. W[]&P[] are the
//weights & weights. P[I]/W[I] P[I+1]/W[I+1].
Y[k] =1;
fp = cp + P[k];
fw = Cw+W[k];
y[k] = 0;
fp = cp;
fw = cw;
Algorithm Bound(cp,cw,k)
b=cp;
c=cw;
for I =- k+1 to n do
c= c+w[I];
return b;
Example:
M= 6 Wi = 2,3,4 42 2
Xi = 1 0 1
5+1
6.
Fp = (-1)
1 3&0+4 6
k = k+2
2 3 but 7>6 so
y(2) = 0
So bound(5,4,2,6)
B=5
C=4
I=3 to 3
C=6
6 6
So return 5+(1-(6-6))/(2*1)
K=4.
If 4> 3 then
Fp =6,fw=6,k=3 ,x(1) 1 0 1
Profit 6
Weight 6.
The design technique known as branch and bound is very similar to backtracking
(seen in unit 4) in that it searches a tree model of the solution space and is applicable to
a wide variety of discrete combinatorial problems.
Each node in the combinatorial tree generated in the last Unit defines a problem
state. All paths from the root to other nodes define the state space of the problem.
Solution states are those problem states 's' for which the path from the root to 's'
defines a tuple in the solution space. The leaf nodes in the combinatorial tree are the
solution states.
Answer states are those solution states 's' for which the path from the root to 's'
defines a tuple that is a member of the set of solutions (i.e.,it satisfies the implicit
constraints) of the problem.
The tree organization of the solution space is referred to as the state space tree.
A node which has been generated and all of whose children have not yet been
generated is called a live node.
The live node whose children are currently being generated is called the E- node
(node being expanded).
Bounding functions are used to kill live nodes without generating all their children.
The term branch-and-bound refers to all state space search methods in which all
children of the E-node are generated before any other live node can become the E-
node.
A D-search (depth search) state space search will be called LIFO (Last In First Out)
search, as the list of live nodes is a list-in-first-out list (or stack).
Bounding functions are used to help avoid the generation of sub trees that do not
contain an answer node.
The branch-and-bound algorithms search a tree model of the solution spaceto get
the solution. However, this type of algorithms is oriented more toward optimization. An
algorithm of this type specifies a real -valued cost function for each of the nodes that
appear in the search tree.
Usually, the goal here is to find a configuration for which the cost function is
minimized. The branch-and-bound algorithms are rarely simple. They tend to be quite
complicated in many cases.
These nodes represent a chessboard with queen1 in row 1and columns 1, 2, 3, and
4 respectively.
The only live nodes 2, 18, 34, and 50.If the nodes are generated in this order, then
the next E-node are node 2.
Now the E- node is node 34.Figure 8.1 shows the portion of the tree of Figure
that is generated by a FIFO branch -and-bound search. Nodes that are killed asa result
of the bounding functions are a "B" under them.
Numbers inside the nodes correspond to the numbers in Figure 7.2.Numbers outside
the nodes give the order in which the nodes are generated by FIFObranch-and-bound.
At the time the answer node, node 31, is reached, the only live nodes remaining are
nodes 38 and 54.
In both LIFO and FIFO branch-and-bound the selection rule for the next E-node is rather
rigid and in a sense blind. The selection rule for the next E-node does not give any
preference to a node that has a very good chance of getting the search to an answer
node quickly.
The search for an answer node can often be speeded by using an "intelligent"
ranking function (.) for live nodes. The next E-node is selected on the basis of this
ranking function.
If in the 4-queens example we use a ranking function that assigns node 30 a better
rank than all other live nodes, then node 30 will become E -node, following node 29.The
remaining live nodes will never become E-nodes as the expansion of node 30 results in
the generation of an answer node (node 31).
The ideal way to assign ranks would be on the basis of the additional computational
effort (or cost) needed to reach an answer node from the live node.
For any node x, this cost could be
(1) The number of nodes on the sub-tree x that need to be generated before any
answer node is generated or, more simply,
(2) The number of levels the nearest answer node (in the sub-tree x) isfrom
x
Using cost measure (2), the cost of the root of the tree of Figure 8.1 is 4 (node 31 is
four levels from node 1).The costs of nodes 18 and 34,29 and 35,and 30 and 38 are
respectively 3, 2, and 1.The costs of all remaining nodes on levels 2, 3, and 4 are
respectively greater than 3, 2, and 1.
Using these costs as a basis to select the next E-node, the E-nodes are nodes 1, 18,
29, and 30 (in that order).The only other nodes to get generated are nodes 2, 34, 50,
19, 24, 32, and 31.
Let (x) be an estimate of the additional effort needed to reach an answer node from
x. node x is assigned a rank using a function (.) such that (x) =f
(h(x)) + (x), where h(x) is the cost of reaching x from the root and f(.) is any non-decreasing
function.
A search strategy that uses a cost function (x) =f (h(x)) + (x), to select the next e-
node would always choose for its next e-node a live node with least (.).Hence, such a
strategy is called an LC-search (least cost search).
Cost function c (.) is defined as, if x is an answer node, then c(x) is the cost (level,
computational difficulty, etc.) of reaching x from the root of the state space tree. If x is
not an answer node, then c(x) =infinity, providing the sub-tree x contains no answer
node; otherwise c(x) is equals the cost of a minimum cost answer node in the sub-tree x.
It should be easy to see that (.) with f (h(x)) =h(x) is an approximation to c (.).
From now on (x) is referred to as the cost of x.
Bounding:
A branch -and-bound searches the state space tree using any search
mechanism in which all the children of the E-node are generated before another node
becomes the E-node.
We assume that each answer node x has a cost c(x) associated with it and that
a minimum-cost answer node is to be found. Three common search strategies are FIFO,
LIFO, and LC.
Clearly, so long as the initial value for upper is no less than the cost of a
minimum-cost answer node, the above rule to kill live nodes will not result in the killing of
a live node that can reach a minimum-cost answer node
.Each time a new answer is found ,the value of upper can be updated.
The objective is to select a subset j of the n jobs such that all jobs in j can be
completed by their deadlines. Hence, a penalty can be incurred only on those jobs not in
j. The subset j should be such that the penalty incurred is minimum among all possible
subsets j. such a j is optimal.
Figure 8.6 corresponds to the variable tuple size formulations while figure 8.7
corresponds to the fixed tuple size formulation. In both figures square nodes represent
infeasible subsets. In figure 8.6 all non-square nodes are answer nodes. Node 9
represents an optimal solution and is the only minimum-cost answer node
.For this node j= {2,3} and the penalty (cost) is
8. In figure 8.7 only non-square leaf nodes are answer nodes. Node 25 represents the
optimal solution and is also a minimum-cost answer node.
STEP: Find least cost in a row and negate it with rest of the
elements.
Use cost matrix- Row reduced one for all columns do STEP 4.
STEP 5: Preserve cost matrix C [which row reduced first and then
column reduced] for the i th time.
STEP 7: Calculate effective cost of the edges. (i, j)=least cost in the i th rowexcluding (i,
j) + least cost in the j th column excluding (i, j).
STEP 8: Compare all effective cost and pick up the largest l. If two or more havesame
cost then arbitrarily choose any one among them.
STEP 9: Delete (i, j) means delete ith row and jth column change (j, i) value to
infinity. (Used to avoid infinite loop formation) If (i,j) not present, leave it.
STEP 10: Repeat step 1 to step 9 until the resultant cost matrix having order of 2*2and
reduce it. (Both R.R and C.C)
Choose an edge [i, j] having value =0, at the first time for a preserved matrix andleave
that matrix.
C1 [ROW REDUCTION:
STEP 6:
(1,2) = 2+1 =3
(2,1) = 12+3 = 15
(3,5) = 2+0 =2
(4,5) = 3+0 =3
(5,3) = 0+12 = 12
(5,4) = 0+2 =2
STEP 8:
PHASE II:
STEP1: C2(R, R)
STEP 3: C2 (C, R)
2 3 4 5
C2 =
STEP 6:
(1,5) = 1+0 =1
(3,5) = 2+0 =2
(5,4) = 0+1 =1
2 3 4
PHASE III:
STEP 1: C3 (R, R)
115
STEP 3: C3 (C, R)
(1,4)=12+0=12
(3,4)=11+0=11
(5,2)=0+11=11
(5,3)=0+12=12
STEP 8: Here we are having two edges (1,4) and (5,3) with cost = 12. Hencearbitrarily
choose (1,4)
STEP 9: Delete (i,j) (1,4) and make change in it (4,1) = if exists. Now costmatrix is
2 3
C4 (RR)=
2 3
0
3
0 0
5
C4 (C, R) =
2 3
3 0
0 0
5
Therefore,C4 =
2 3
3 0
0 0
5
C4
C3 2 3 4
1
12 0
3
11 0
5
0 0
C2 =
1 13 1 0
3
13 2 0
4 43 18 0
5
0 0 0
C1 =
1 2 3 4 5
1 0 15 3 2
2
0 12 22 20
3
18 14 2 0
4 3 44 18 0
5
15 1 0 0
STEP 12:
2 3
0
3
0 0
5
Here (3,2) =0
Hence, T (3,2)
Use C3 =
2 3 4
1
12 0
3
11 0
5
0 0
Here (1,4) =0
Use C2=
13 1 0
1
13 2 0
3
43 18 0
4
5 0 0 0
(4,5) 0
Use C1 =
1 2 3 4 5
1 0 15 3 2
2
0 12 22 20
3
18 14 2 0
4 3 44 18 0
5
15 1 0 0
SOLUTION:
3—2—1—4—5
This result now, we have to return to the same city where we started (Here 3).
Final result:
3—2—1—4—5—3
Cost is 15+15+31+6+7=64
COMPARISON TREES
We now show that MergeSort is also optimal on average, since nlog n is also a
lower bound (again, up to a constant) for the average behavior of comparison-based
sorting. This latter result will be established using a comparison tree argument. Given
any comparison-based algorithm with input list
L[0:n-1] = {x1, x2, ..., xn}, (internal) nodes in the comparison tree T associated with the
algorithm correspond to comparisons performed by the algorithm between list elements.
For specificity, our convention will be that if the comparison is made
between xi and xj, and i < j, then we will label the corresponding node xi:xj. If xi < xj, then
a left child will be the node corresponding to the next comparison made next by the
algorithm, or this left child will be a leaf node if the algorithm
terminates. Similarly, if xi > xj (we can assume distinct list elements for the purpose of
establishing lower bounds), then the right child will be the nodecorresponding to the next
comparison made by the algorithm, or will be a leaf node if the algorithm terminates.
The following figure illustrates the comparison-tree associated with InsertionSort for a list
L[0:2] of 3 distinct elements x1, x2, x3.
Key Fact. The comparson tree associated with any comparison-based sorting algorithm
has n! leaf nodes.
The Key Fact follows from the fact that there are n! factorial permutations of n symbols,
and different permutations must end up at different leaf nodes of the comparison tree
when input to the algorithm. Since the comparison tree associated with a comparison-
based sorting algorithm is a binary tree, lower bounds for both worst-case and average
complexity can be obtained from lower bounds for the depth and leaf path length (= sum
of the lengths of all paths from the root to a leaf), respectively, of a binary tree having L
leaf nodes.
Depth(T)≥ ceil(log2L)
Proposition 1 is clearly true for complete binary trees (verify this!), so it is intuitively
evident that it holds for arbitrary binary trees since the complete binary tree has the
smallest depth for a given number L of leaf nodes. The formal proof of Proposition 1 can
be found on p. 120 in the text. It follows immediately from Proposition 1 that
W(n) ≥ ceil(log2L)
Proposition 2. Let T be any binary tree having L leaf nodes. Then the leaf path
length LPL of T satisfies:
We now illustrate our third technique for establishing lower bounds, namely adversary
arguments. This technique establishes lower bounds by creating an input instance,
based on the performance of the algorithm, which guarantees that the algorithm must do
a determined amount of work on this input in order to be correct for this input. This
amount of work then gives a lower bound for the worst- case complexity of the algorithm.
Another adversary-type technique is to construct an input to an algorithm which
contradicts the correctness of the algorithm if the algorithm performs less than some
given number of basic operations. We start with an example of this type of adversary
argument.
The usual linear scan for finding the maximum element in a list L[0:n-1] of size n turns
out to be optimal, since ANY comparison-based algorithm for solving this problem must
make n - 1 comparsions between list elements. This might seem obvious, since certainly
every element must participate in at least one comparison. However, only n/2
comparisons are required to ensure that each element is involved in a comparison. Just
pair the elements up into disjoint pairs a make a comparison to the two elements in each
pair. Of course, this doesn't yet determine the maximum, but it shows the need for
further justification that n - 1 comparisons will eventually be required. Again, throughout
we will assume distinct list elements.
Proposition 3 shows that the familiar linear scan algorithm for finding the maximum is an
(exactly) optimal algorithm. It is interesting that there is another algorithm also
performing n - 1 comparisons to find the maximum, but this time it is based on the
familiar single elimination tournament model so familiar from the sporting world. For
simplicity, we assume that n = 2k. Divide up the list into disjoint pairs, and determine the
n/2 pair-wise winners (1st round of the tournament). Then divide up the n/2 first-round
winners into pairs and determine
the n/4 second round winners. After precisely log2n rounds the winner (maximum) will be
determined. But how many comparisons (matches) were made? Easy, we get, for n = 2k:
2k-1 + 2k-2 + ... + 1 = 2k - 1 = n - 1.
The most naive method MaxMin1 for finding the maximum and minimum elements in a
list is to make two linear scans (or run winner and loser tournaments), resulting in 2n - 2
comparisons. However, one imagines that this can be improved, since information about
both elements involved in a comparison might be utilized. In fact, the following slightly
less naive algorithm certainly improves MaxMin1, at least on average.
function MaxMin2(L[0:n-1]) Input:
L[0:n-1] (a list of size n) Output:
the maximum value in L
Max = Min = L[0] for
i = 1 to n-1 do
if L[i] > Max then Max = L[i]
else
if L[i] < Min then Min = L[i]
endif
endif
enfor
end MaxMin1
A(n) = 2n - 2 - E[m].
Let A*(n) = E[m]. Note that π(n) is equally likely to be 1, 2, ..., n. Hence,
Now it is clear that E[m| π(n) = n] = A*(n-1) + 1, whereas E[m| π(n) = i ≠ n] = A*(n-1).
Hence, we have the following recurrence for A*(n):
= ~ ln n.
Thus, we see that A(n) ~ W(n) ~ 2n.
Basic concepts:
Tractability: Some problems are tractable: that is the problems are solvable in
reasonable amount of time called polynomial time. Some problems are intractable: that is
as problem grow large, we are unable to solve them in reasonable amount of time called
polynomial time.
CLASS P PROBLEMS:
Class P problems are the set of decision problems solvable by deterministic algorithms in
polynomial-time.
A deterministic algorithm is (essentially) one that always computes the correct answer
CLASS NP PROBLEMS:
A nondeterministic algorithm is one that can “guess” the right answer or solution Examples:
NP-Complete Problems:
A problem ‘x’ is a NP class problem and also NP-Complete if and only if every other problem in NPcan be
reducible (solvable) using non-deterministic algorithm in polynomial time.
NP-Hard Problems:
A problem ‘x’ is a NP class problem and also NP-Hard if and only if every other problem in NP canbe
reducible (solvable) using non-deterministic algorithm in exponential time.
The NP-Hard problems are decision problems and sometimes may be optimization problems.
Nondeterministic Algorithms:
• Let A be an algorithm to solve problem P. A is called deterministic if it has only one choice in each step
throughout its execution. Even if we run algorithm A again and again, there is no change in output.
• Deterministic algorithms are identified with uniquely defined results in terms of output for a certaininput.
Nondeterministic Algorithms:
• Let A be a nondeterministic algorithm for a problem P. We say that algorithm A accepts an instanceof P if
and only if, there exists a guess that leads to a yes answer.
• In non deterministic algorithms, there is no uniquely defined result in terms of output for a certaininput.
• Nondeterministic algorithms are allowed to contain operations whose outcomes are limited to agiven
set of instances of P, instead of being uniquely defined results.
• The Nondeterministic algorithm uses three basic procedures, whose time complexity is O(1).
in favor of the algorithm, from the closed interval [1,n] or from the set S.
• Non deterministic algorithm terminates unsuccessfully if and only if there is no set of choicesleading
to successful completion of algorithm
• Non deterministic algorithm terminates successfully if and only if there exists set of choices leading to
successful completion of algorithm
{ }
( A[j] == x )
Nondeterministic Sort Algorithm: The following algorithm sorts ‘n’ positive integers in non-decreasing order and
produces output in sorted order. The array B[] is an auxiliary array initialized to 0 and is used for convenience.
Algorithm nd_sort ( A, n ) }
j = choice ( 0, n - 1 ); if
( B[j] != 0 ) failure();B[j]
= A[i];
// Verify order
write ( B );
success();
Let x1, x2 . . . denote a set of Boolean variables and xi denote the complement of x¯ i.A
(x3 V x¯ 4) ^ (x1 V x¯ 2)
Conjunctive normal form (CNF): A Boolean formula is said to be in conjunctive normal form(CNF)
if it is the conjunction of formulas.
Example: (x1 x¯ 2) (x¯ 1 x5)
Disjunctive normal form (DNF) : A Boolean formula is said to be in disjunctive normal form(CNF) if it
is the disjunction of formulas.
Example: (x1 x¯ 2) (x1 x¯ 5)
Satisfiability problem is to determine whether a formula is true for some assignment of truth values
to the variables
Non deterministically choose one of the 2n possible assignments of truth values to (x1, .
. . , xn) and verify that E(x1, . . . , xn) is true for that assignment
Algorithm eval ( E, n )
else
failure();
Decision Problem and Algorithm : Any problem for which the answer is either zero or one is called a
decision problem. An algorithm for a decision problem is termed a decision algorithm.
Optimization Problem and Algorithm: Any problem that involves the identification of an optimal (either
minimum or maximum) value of a given cost function is known as an optimization problem. An
optimization algorithm is used to solve an optimization problem.
The problem is to find out the feasible solution with the best associated value
Optimization problems can be cast into decision problems by imposing a bound on output or solution.
Decision problem is assumed to be easier (or no harder) to solve compared to the optimization problem.
Decision problem can be solved in polynomial time if and only if the corresponding optimization problem
can be solved in polynomial time. If the decision
For example consider, shortest path problem. Optimization problem is to find a shortest path between
two vertices in an undirected weighted graph, so that shortest path consists least number of edges.
Whereas the decision problem is to determine that given an integer k, whether a path exists between
two specified nodes consisting of at most k edges.
Maximal Clique:
Clique is a maximal complete sub-graph of a graph G = (V,E), that is a subset of vertices in V all
connected to each other by edges in E (i.e., forming a complete graph).
Example:
The Size of a clique is the number of vertices in it. The Maximal clique problem is an optimization
problem that has to determine the size of a largest clique in G. A decision problem is to determine
whether G has a clique of size at least ‘k’.
Input for Maximal clique problem: Input can be provided as a sequence of edges. Each edge in E(G) is a
pair of vertices (i, j) .The size of input for each edge (i, j) in binary representation is
Let us denote the deterministic decision algorithm for the clique decision problem as dclique(G,k). If |V |
= n, then the size of a maximal clique can be found by
If time complexity of dclique is f(n), size of maximal clique can be found in time g(n) <=n.f(n).
Therefore, the decision problem can be solved in time g(n)
Note that Maximal clique problem can be solved in polynomial time if and only if the clique
decision problem can be solved in polynomial time.
Algorithm DCK(G, n, k)
for i=1 to k do
t = Choice(1,n);
if t ε S then Failure();
S= SU{ t };
for all pairs (i,j) such that i ε S, j ε S and i!=j doif (i,
Success();
The for loop selects or discards each of the n items It also re-computes the total weightand
profit corresponding to the selection
The if statement checks to see the feasibility of assignment and whether the profit is above
a lower bound r
The time complexity of the algorithm is O(n) . If the input length is q in binary, then O(q).
algorithm nd_knapsack ( p, w, n, m, r, x )
x[i] = choice ( 0, 1 );
W += x[i] * w[i];
if ( ( W > m ) || ( P < r ) )
failure();
else
success();
Each step moves m + 1 bits of data and would take O(m) time on a conventional computer
Assuming one unit of time for each basic operation for a fixed word size, the complexityof
deterministic algorithm is O(nm)
algorithm sum_of_subsets ( A, n, m )
i = 1 to n
in s is 1
else
COOK’S THEOREM:
We know that, Class P problems are the set of all decision problems solvable by deterministic
algorithms in polynomial time. Similarly Class NP problems are set of alldecision problems solvable
by nondeterministic algorithms in polynomial time.
Cook formulated the following question: Is there any single problem in NP such that if we
showed it to be in P, then that would imply that P = NP? This led to Cook’s theorem as :
Q - denotes a formulae
Now, the formula ‘Q’ is satisfiable if and only if the non-deterministic algorithm ‘A’ has a
successful termination with input ‘I’.
If the time complexity of ‘A’ is p(n) for some polynomial p(), then the time needed to
construct the formula ‘Q’ by algorithm ‘A’ is given by O(p3(n)log n).
Therefore complexity of non-deterministic algorithm ‘A’ is O(p3(n)log n).
(NP)
Similarly, the formula ‘Q’ is satisfiable if and only if the deterministic algorithm ‘Z’ has a
successful termination with input ‘I’.
If the time complexity of ‘Z’ is q(m) for some polynomial q(), then the time needed to construct
the formula ‘Q’ by algorithm ‘Z’ is given by O(p3(n)log n + q(p3(n)log n)).
If satisfiability is in P, then q(m) is a polynomial function and the complexity of ‘Z’ becomesO(r(n)) for
some polynomial r(n).
Hence, P is satisfiable, then for every non-deterministic algorithm ‘A’ in NP can obtain a
deterministic algorithm ‘Z’ in P.
So, the above construction shows that “if satisfiability is in P, then P=NP”