Daa Final
Daa Final
Daa Final
ALGORITHM ANALYSIS
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:
3. Pseudo-code Method:
In this method, we should typically describe algorithms as program,
which resembles language like Pascal & algol.
-1-
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.
<statement-n>
}
-2-
For Loop:
{
<statement-1>
.
.
.
<statement-n>
}
repeat-until:
repeat
<statement-1>
.
.
.
<statement-n>
until<condition>
Case statement:
Case
{
: <condition-1> : <statement-1>
.
.
.
: <condition-n> : <statement-n>
: else : <statement-n+1>
}
9. Input and output are done using the instructions read & write.
-3-
As an example, the following algorithm fields & returns the maximum of ‘n’ given
numbers:
1. algorithm Max(A,n)
2. // A is an array of size n
3. {
4. Result := A[1];
5. for I:= 2 to n do
6. if A[I] > Result then
7. Result :=A[I];
8. return Result;
9. }
In this algorithm (named Max), A & n are procedure parameters. Result & I are Local
variables.
Selection Sort:
( 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 is at a[j];
4. Interchange a[I] and a[j];
5. }
t := a[i];
a[i]:=a[j];
a[j]:=t;
-4-
The first subtask can be solved by assuming the minimum is a[ I ];checking a[I]
with a[I+1],a[I+2]…….,and whenever a smaller element is found, regarding it as
the new minimum. a[n] is compared with the current minimum.
Putting all these observations together, we get the algorithm Selection sort.
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 4 can be
changed to n-1 without damaging the correctness of the algorithm.
Algorithm:
-5-
Recursive Algorithms
1. Towers of Hanoi:
.
.
.
-6-
To get the largest disk to the bottom of tower B, we move the remaining ‘n-1’
disks to tower C and then move the largest to tower B.
Now we are left with the tasks of moving the disks from tower C to B.
To do this, we have tower A and B available.
The fact, that towers B has a disk on it can be ignored as the disks larger than the
disks being moved from tower C and so any disk scan be placed on top of it.
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);
8. Towersofhanoi(n-1,z,y,x);
9. }
10. }
2. Permutation Generator:
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
-7-
// 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:
1. Space Complexity:
The space complexity of an algorithm is the amount of money it needs to run
to compilation.
2. Time Complexity:
The time complexity of an algorithm is the amount of computer time it needs
to run to compilation.
Space Complexity:
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.
2. 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 on instance characteristics), and the
recursion stack space.
-8-
The space requirement s(p) of any algorithm p may therefore be written as,
S(P) = c+ Sp(Instance characteristics)
Where ‘c’ is a constant.
Time Complexity:
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).
Interactive statement such as for, while & repeat-until Control part of the statement.
1. 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. So each
invocation of sum execute a total of 2n+3 steps.
First determine the number of steps per execution (s/e) of the statement and the
total number of times (ie., frequency) each statement is executed.
-9-
By combining these two quantities, the total contribution of all statements, the
step count for the entire algorithm is obtained.
1. Algorithm Sum(a,n) 0 - 0
2.{ 0 - 0
3. S=0.0; 1 1 1
4. for I=1 to n do 1 n+1 n+1
5. s=s+a[I]; 1 n n
6. return s; 1 1 1
7. } 0 - 0
Total 2n+3
Most of the time, average-case analysis are performed under the more or less 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 be sorted 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 divide by 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 sub array
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 element in
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:
- 10 -
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.
Asymptotic notation:
Big ‘oh’: the function f(n)=O(g(n)) iff there exist positive constants c and no such that
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 such that 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 such that c1 g(n)
≤ f(n) ≤ c2 g(n) for all n, n ≥ no.
In this section we examine some of the mathematical properties of big oh. In particular, suppose
we know that
.
The theorem addresses the asymptotic behavior of the sum of two functions whose asymptotic
behaviors are known:
there exist two integers, and and two constants and such that for
and for .
- 11 -
Let and . Consider the sum for :
Hence proved.
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) * n
if end
algorithm ends.
- 12 -
3. Restore the most recently saved parameters, local variable and return address and goto
the latest return address.
- 13 -
It is always true that recursion can be replaced by iteration and stacks. It is also true that stack
can be replaced by a recursive program with no stack.
SOLVING RECURRENCES
The indispensable last step when analyzing an algorithm is often to solve a recurrence
equation.
With a little experience and intention, most recurrence can be solved by intelligent
guesswork.
However, there exists a powerful technique that can be used to solve certain classes of
recurrence almost automatically.
This is a main topic of this section the technique of the characteristic equation.
0 if n=0
T(n) = 3T(n ÷ 2)+n otherwise
- 14 -
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)
1 1
2 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
= ∑ 3k-i 2i
= 3k ∑ (2/3)i
Let Sn be the sum of the first n terms of the geometric series a, ar, ar2….Then
Sn = a(1-rn)/(1-r), except in the special case when r = 1; when Sn = an.
- 15 -
= 3k * [ (1 – (2/3) k+1) / (1 – (2/3))]
3 k+1 – 2k+1 3
k
= 3 * ----------------- * ----
3 k+1 1
3 – 2k+1
k+1
= 3k * -----------------
3k+1-1
= 3k+1 – 2k+1
2. Homogenous Recurrences :
* We begin our study of the technique of the characteristic equation with the resolution of
homogenous linear recurrences with constant co-efficient, i.e the recurrences of the form,
a0tn + a1tn-1 + ….. + aktn-k = 0
* Consider for instance our non familiar recurrence for the Fibonacci sequence,
fn = f n-1 + f n-2
* This recurrence easily fits the mould of equation after obvious rewriting.
fn – f n-1 – f n-2 = 0
- 16 -
k
So ∑ ai f n-i = 0 & similarly for gn & fn
i=0
n if n=0 or n=1
fn =
f n-1 + f n-2 otherwise
1 ±√ (1 + 4)
= ----------------
2
1 ± √5
= ----------
2
1+√5 1 - √5
r1 = --------- and r2 = ---------
2 2
- 17 -
fn = C1r1n + C2r2n
when n=0, f 0 = C1 + C 2 = 0
when n=1, f1 = C1r1 + C2r2 = 1
C1 + C 2 = 0 (1)
C1r1 + C2r2 = 1 (2)
C1 = -C2
Substitute C1 in equation(2)
-C2r1 + C2r2 = 1
C2[r2 – r1] = 1
1 - √5 1 - √5
C2 --------- - --------- = 1
2 2
1 – √5 – 1 – √5
C2 --------------------- =1
2
-C2 * 2√5
-------------- = 1
2
– √5C2 = 1
C1 = 1/√5 C2 = -1/√5
Thus,
n n
1 1 + √5 -1 1 - √5
fn = ---- --------- + ---- --------
√5 2 √5 2
n n
1 1 + √5 1 – √5
= ---- --------- - ---------
√5 2 2
3. Inhomogeneous recurrence :
* The solution of a linear recurrence with constant co-efficient becomes more difficult
when the recurrence is not homogeneous, that is when the linear combination is not
equal to zero.
- 18 -
* Consider the following recurrence
a0tn + a1 t n-1 + … + ak t n-k = bn p(n)
* The left hand side is the same as before,(homogeneous) but on the right-hand side
we have bnp(n), where,
b is a constant
p(n) is a polynomial in ‘n’ of degree ‘d’.
Example1 :
substitute t1 in eqn(3),
Therefore tn = (t0-3)2n + 3. 3n
= Max[O[(t0 – 3) 2n], O[3.3n]]
= Max[O(2n), O(3n)] constants
- 19 -
= O[3n]
4. Change of variables:
* It is sometimes possible to solve more complicated recurrences by making a
change of variable.
* In the following example, we write T(n) for the term of a general recurrences,
and ti for the term of a new recurrence obtained from the first by a change of variable.
Example:
Consider the recurrence,
1 , if n=1
T(n) =
3T(n/2) + n , if ‘n’ is a power of 2, n>1
1
T(n) =
3T(n/2) + n
In this case,
b = 2, p(n) = 1, degree = 0
- 20 -
T(n) = C1. 3 log2n + C2. 2log2n
T(n) = C1 . nlog23 + C2.n [i = logn]
When ‘n’ is a power of 2, which is sufficient to conclude that,
5. Range Transformation:
EXAMPLE:
Consider the following recurrence , which defines T(n). where ‘n’ is the power
Of 2
1/3 , if n=1
T(n) =
n T2(n/2) , otherwise
ui – 2u i-1 = i
(x – 2)(x – 1)2 = 0
- 21 -
ui = C12i – i – 2
T(n) = 2 2n / 4n 3n
1. Newton Raphson method: x2= x1-f(x1)/f1(x1)
SEARCHING
Let us assume that we have a sequential file and we wish to retrieve an element matching
with key ‘k’, then, we have to search the entire file from the beginning till the end to check
whether the element matching k is present in the file or not.
There are a number of complex searching algorithms to serve the purpose of searching. The
linear search and binary search methods are relatively straight forward methods of searching.
Sequential search:
In this method, we start to search from the beginning of the list and examine each
element till the end of the list. If the desired element is found we stop the search and return the
index of that element. If the item is not found and the list is exhausted the search returns a zero
value.
In the worst case the item is not found or the search item is the last (n th) element. For both
situations we must examine all n elements of the array so the order of magnitude or complexity
of the sequential search is n. i.e., O(n). The execution time for this algorithm is proportional to n
that is the algorithm executes in linear time.
- 22 -
{
if(A[i]=k)
{
write("search successful")
write(k is at location i)
exit();
}
else
i++ ;
}
end
end
write (search unsuccessful);
algorithm ends.
Binary search:
Binary search method is also relatively simple method. For this method it is necessary to
have the vector in an alphabetical or numerically increasing order. A search for a particular item
with X resembles the search for a word in the dictionary. The approximate mid entry is located
and its key value is examined. If the mid value is greater than X, then the list is chopped off at
the (mid-1)th location. Now the list gets reduced to half the original list. The middle entry of the
left-reduced list is examined in a similar manner. This procedure is repeated until the item is
found or the list has no more elements. On the other hand, if the mid value is lesser than X, then
the list is chopped off at (mid+1)th location. The middle entry of the right-reduced list is
examined and the procedure is continued until desired key is found or the search interval is
exhausted.
- 23 -
{
write("search successful")
write(k is at location low)
exit();
}
else
write (search unsuccessful);
if end;
algorithm ends.
UNIT - II
DIVIDE AND CONQUER, GREEDY METHOD
- 24 -
G E NE RAL ME T H O D :
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 of the same type
as the original problem.
D And C(Algorithm) is initially invoked as D and C(P), where ‘p’ is the problem to be
solved.
Small(P) is a Boolean-valued function that determines whether the i/p size is small
enough that the answer can be computed without splitting.
These sub problems P1, P2 …Pk are solved by recursive application of D And C.
Combine is a function that determines the solution to p using the solutions to the ‘k’ sub
problems.
If the size of ‘p’ is n and the sizes of the ‘k’ sub problems are n1, n2 ….nk, respectively,
then the computing time of D And C is described by the recurrence relation.
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 to
sub problems.
- 25 -
2. {
3. if small(P) then return S(P);
4. else
5. {
6. divide P into smaller instances
P1, P2… Pk, k>=1;
7. Apply D And C to each of these sub problems;
8. return combine (D And C(P1), D And C(P2),…….,D And C(Pk));
9. }
10. }
Example:
1) 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
*
*
*
= n. T(n/n) + n log n
- 26 -
= 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 to check.
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.
Thus we have 2 sequences of integers approaching each other and eventually low
becomes > than high & causes termination in a finite no. of steps if ‘x’ is not present.
Example:
1) Let us select the 14 entries.
-15,-6,0,7,9,23,54,82,101,112,125,131,142,151.
Place them in a[1:14], and simulate the steps Binsearch goes through as
it searches for different values of ‘x’.
Only the variables, low, high & mid need to be traced as we simulate the
algorithm.
We try the following values for x: 151, -14 and 9.
for 2 successful searches &
1 unsuccessful search.
- 27 -
X=151 low high mid
1 14 7
8 14 11
12 14 13
14 14 14
Found
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 elements to be checked of
or equality with x and a[low], a[low+1],……..,a[mid],……a[high].
If x=a[mid], then the algorithm terminates successfully.
Otherwise, the range is narrowed by either increasing low to (mid+1) or decreasing high
to (mid-1).
Clearly, this narrowing of the range does not affect the outcome of the search.
If low becomes > than high, then ‘x’ is not present & hence the loop is exited.
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.
- 28 -
In analyzing the time complexity of this algorithm, we once again concentrate on the no.
of element comparisons.
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 element comparison.
Straight MaxMin requires 2(n-1) element comparison in the best, average & worst cases.
Now the best case occurs when the elements are in increasing order.
The no. of element comparison is (n-1).
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:
- 29 -
Here ‘n’ is the no. of elements in the list (a[I],….,a[j]) and we are interested in finding the
maximum and minimum of the list.
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.
- 30 -
The procedure is initially invoked by the statement,
MaxMin(1,n,x,y)
Suppose we simulate MaxMin on the following 9 elements
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
- 31 -
We assume throughout that the elements are to be sorted in non-decreasing order.
Given a sequence of ‘n’ elements a[1],…,a[n] the general idea is to imagine then split into
2 sets a[1],…..,a[n/2] and a[[n/2]+1],….a[n].
Each set is individually sorted, and the resulting sorted sequences are merged to produce
a single sorted sequence of ‘n’ elements.
Thus, we have another ideal example of the divide-and-conquer strategy in which the
splitting is into 2 equal-sized sets & the combining operation is the merging of 2 sorted
sets into one.
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)) do
9. {
10. if (a[h]<=a[j]) then
11. {
12. b[I]=a[h];
13. h = h+1;
14. }
15. else
- 32 -
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 size five (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, and now the
merging begins.
(310| 285| 179| 652, 351| 423, 861, 254, 450, 520)
- 33 -
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)
if the time for the merging operations is proportional to ‘n’, then the computing time for
merge sort is described by the recurrence relation.
When ‘n’ is a power of 2, n= 2^k, we can solve this equation by successive substitution.
GREEDY METHOD
- 34 -
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.
* The function select an input from a[] and removes it. The select input value is assigned to X.
Feasible is a Boolean value function that determines whether X can be included into the
solution vector.
The function Union combines X with The solution and updates the objective function.
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
Suppose we have in a country the following coins are available :
Dollars(100 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.
For example if we must pay 276 cents possible solution then,
- 35 -
1 doll+7 q+ 1 pen9 coins
2 doll +3Q +1 pen6 coins
2 doll+7dim+1 nic +1 pen11 coins.
KNAPSACK PROBLEM
we are given n objects and knapsack or bag with capacity M object I has a weight Wi
where I varies from 1 to N.
The problem is we have to fill the bag with the help of N objects and the resulting profit
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.
First, we find the profit & weight rates of each and every object and sort it according to
the descending order of the ratios.
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:
- 36 -
7.for I=1 to n do a[I]=0.0;
8.U=n;
9.For I=1 to n do
10.{
11.if (w[i]>u)then break;
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=25,24,15
Pi/Wi=25/18=1.36,24/15=1.6,15/10=1.5
X1 X2 X3 WiXi PiXi
½ 1/3 ¼ 16.6 24.25
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.
UNIT-3
- 37 -
DYNAMIC PROGRAMMING
Dynamic programing:
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.
When a problem is solved by divide and conquer, we immediately attack the complete
instance, which we then divide into smaller and smaller sub-instances as the algorithm
progresses.
We usually start with the smallest and hence the simplest sub- instances.
The essential difference between the greedy method and dynamic programming is that the
greedy method only one decision sequence is ever generated.
Let G=<N,A> be a directed graph ’N’ is a set of nodes and ‘A’ is the set of edges.
We want to calculate the length of the shortest path between each pair of nodes.
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.
Copy the above matrix-to-matrix D, which will give the direct distance between nodes.
- 38 -
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.
At the iteration k, we have to check for each pair of nodes (i,j) whether or not there exists a
path from i to j passing through node k.
D0 =L= 05
50 0 15 5
30 0 15
15 5 0
1 75 11 12 - -
2 72 21 - - 24
3 3 - 32 - -
4 41 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 5 7 11 12 - 124
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
- 39 -
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
6 6 3 2 241 2432 243 24
9 3 6 5 3241 32 3243 324
4 4 1 6 41 432 43 4324
D0= 0 5
50 0 15 5
30 0 15
15 5 0
0 5
50 0 15 5 p[3,2]= 1
D1= 30 35 0 15 p[4,2]= 1
15 20 5 0
15
30
5
5 50 5 15
15
- 40 -
0 5 20 10 P[1,3] = 2
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
0042
3040 0 direct path
P= 0 1 0 0
0100
- 41 -
ALGORITHM :
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
1. 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.
2. In addition, if < u,v > is an edge in E, then u < = Vi and V Vi+1 for some i, 1<= i < k.
3. If there will be only one vertex, then the sets Vi and Vk are such that [Vi]=[Vk] = 1.
4. Let ‘s’ and ‘t’ be the source and destination respectively.
5. The cost of a path from source (s) to destination (t) is the sum of the costs of the edger on
the path.
6. The Multistage graph problem is to find a minimum cost path from ‘s’ to ‘t’.
7. 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.
8. This Mulistage graph problem can be solved in 2 ways.
a) Forward Method.
b) Backward Method.
FORWARD METHOD
PROCEDURE:
- 42 -
Maintain a cost matrix cost (n) which stores the distance from any vertex to the
destination.
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’.
V1 V2 V3 V4 V5
4 6
2 2
5 4
9 1
4
7 3 2
7 t
s
3
11 5 5
2
11 6
In this way we will find out the minimum cost path from each and every vertex.
Finally cost(1) will give the shortest distance from source to destination.
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.
For a ‘k’ stage graph, there will be ‘k’ vertex in the path.
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
- 43 -
Cost ( 9)=4 D ( 9)=12
- 44 -
=7
cost(2) = 7 =>D(2) = 7
The path through which you have to find the shortest distance.
(i.e.)
D ( 1) = 2
D ( 2) = 7
D ( 7) = 10
D (10) = 12
9 2 3 2
- 45 -
// c[j,r]+cost[r] is minimum.
P[1]=1;
P[k]=n;
For j=2 to k-1 do
P[j]=d[p[j-1]];
}
ANALYSIS:
The time complexity of this forward method is O( V + E )
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)[from stage k to
stage 1]
PROCEDURE:
STEP:
- 46 -
cost(6) = 9 =>D(6)=3
cost(7) = 11 =>D(7)=2
cost(8) = 10 =>D(8)=2
cost(9) = 15 =>D(9)=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:
17 3 2 6 5 10 2
12
- 47 -
// The I/p is a k-stage graph G=(V,E) with ‘n’ vertex.
// Indexed in order of stages E is a set of edges.
// and c[i,J] is the cost of<i,j>,p[1:k] is a minimum cost path.
{
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.
P[1]=1;
P[k]=n;
For j= k-1 to 2 do
P[j]=d[p[j+1]];
}
APPLICATION :
1. Suppose we have to route a postal van to pick up mail from the mail boxes located at ‘n’
different sites.
2. An n+1 vertex graph can be used to represent the situation.
3. One vertex represent the post office from which the postal van starts and return.
4. Edge <i,j> is assigned a cost equal to the distance from site ‘i’ to site ‘j’.
5. the route taken by the postal van is a tour and we are finding a tour of minimum length.
6. every tour consists of an edge <1,k> for some k V-{} and a path from vertex k to
vertex 1.
7. the path from vertex k to vertex 1 goes through each vertex in V-{1,k} exactly once.
8. the function which is used to find the path is
- 48 -
g(1,V-{1}) = min{ cij + g(j,s-{j})}
1. Find g(i,) =ci1, 1<=i<n, hence we can use equation(2) to obtain g(i,s) for all s to size 1.
2. That we have to start with s=1,(ie) there will be only one vertex in set ‘s’.
3. Then s=2, and we have to proceed until |s| <n-1.
4. for example consider the graph.
10
15
10
15
20 8 9 13
8 6
12
7
Cost matrix
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0
starting position
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}
- 49 -
=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:
STEP 4:
g{4,} =c41 = 8
- 50 -
g{3,} =c31 = 6
g{2,} =c21 = 5
s = 0.
i =1 to n.
s =1
i =2 to 4
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}
- 51 -
=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
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
This problem is similar to ordinary knapsack problem but we may not take a fraction of
an object.
We are given ‘ N ‘ object with weight Wi and profits Pi where I varies from l to N and
also a knapsack with capacity ‘ M ‘.
The problem is, we have to fill the bag with the help of ‘ N ‘ objects and the resulting
profit has to be maximum.
n
Formally, the problem can be started as, maximize Xi Pi
i=l
n
- 52 -
subject to Xi Wi L M
i=l
To solve the problem by dynamic programming we up a table T[1…N, 0…M] (ic) the
size is N. where ‘N’ is the no. of objects and column starts with ‘O’ to capacity (ic) ‘M’.
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.
If i=l and j < w(i) then T(i,j) =o, (ic) o pre is filled in the table.
If i=l and j w (i) then T (i,j) = p(i), the cell is filled with the profit p[i], since only one
object can be selected to the maximum.
If i>l and j < w(i) then T(i,l) = T (i-l,j) the cell is filled the profit of previous object since
it is not possible with the current object.
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].
After moved, we have to check if, T[i,j]=T[i-l,j-w(i)]+ p[I], if it is equal then one unit of
object ‘i’ is selected and move up to the position T[i-l,j-w(i)]
Repeat the same process until we reach T[i,o], then there will be nothing to fill the bag
stop the process.
Consider a Example,
M = 6,
N=3
- 53 -
W1 = 2, W2 = 3, W3 = 4
P1 = 1, P2 =2, P3 = 5
i 1 to N
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
l<3= T(2,1) = T(i-l)
T 2,1 =0
- 54 -
UNIT - IV
BACKTRACKING
BACKTRACKING
Many problems which deal with searching for a set of solutions or for a optimal 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 (m i+1………..mn) possible test vectors may be ignored
entirely.
Many problems solved using backtracking require that all the solutions satisfy a complex
set of constraints.
i) Explicit constraints.
ii) Implicit constraints.
1) Explicit constraints:
Explicit constraints are rules that restrict each Xi to take values only from a given
set.
Some examples are,
Xi 0 or Si = {all non-negative real nos.}
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 solution space for I.
- 55 -
2) Implicit constraints:
The implicit constraint determines which of the tuples in the solution space I can
actually satisfy the criterion functions.
Algorithm:
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
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.
If we consider backtracking procedure using fixed tuple strategy , the elements X(i) of
the solution vector is either 1 or 0 depending on if the weight W(i) is included or not.
- 56 -
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:
In state space tree of the solution the rectangular node lists the values of s, k, r, where s is
the sum of subsets,’k’ is the iteration and ‘r’ is the sum of elements after ‘k’ in the
original set.
S, n, r
0,1,73
X(1)=1 x(1)=0
5,2,68 0,2,68
X(4)=1 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 labeled with the
values of Xi, which is either 0 or 1.
- 57 -
The left sub tree of the root defines all subsets containing Wi.
The right subtree of the root defines all subsets, which does not include Wi.
Assign X(k)<- 1.
If S+X(k)=M then we print the subset b’coz the sum is the required output.
If the above condition is not satisfied then we have to check S+X(k)+W(k+1)<=M. If so,
we have to generate the left sub tree. It means W(t) can be included so the sum will be
incremented and we have to check for the next k.
After generating the left sub tree we have to generate the right sub tree, for this 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)
{
//generate the left child. note s+w(k)<=M since Bk-1 is true.
X{k]=1;
If (S+W[k]=m) then write(X[1:k]); // there is no recursive call here as W[j]>0,1<=j<=n.
Else if (S+W[k]+W[k+1]<=m) then sum of sub (S+W[k], k+1,r- W[k]);
//generate right child and evaluate Bk.
If ((S+ r- W[k]>=m)and(S+ W[k+1]<=m)) then
{
X{k]=0;
sum of sub (S, k+1, r- W[k]);
}
}
HAMILTONIAN CYCLES
- 58 -
Let G=(V,E) be a connected graph with ‘n’ vertices. A HAMILTONIAN CYCLE is a
round trip path along ‘n’ edges of G which every vertex once and returns to its starting
position.
If the Hamiltonian cycle begins at some vertex V1 belongs to G and the vertex 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.
The backtracking algorithm helps to find Hamiltonian cycle for any type of graph.
Procedure:
1. Define a solution vector X(Xi……..Xn) where Xi represents the I th visited vertex of the
proposed cycle.
3. The solution array initialized to all zeros except X(1)=1,b’coz the cycle should start at
vertex ‘1’.
6. When these two conditions are satisfied the current vertex is included in the cycle, else
the next vertex is tried.
- 59 -
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.
}
Repeat
}
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:
- 60 -
th
The solution vector X (X1…Xn) represents a solution in which Xi is the column of the
row where I th queen is placed.
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.
Consider two dimensional array A[1:n,1:n] in which we observe that every element on
the same diagonal that runs from upper left to lower right has the same value.
Also, every element on the same diagonal that runs from lower right to upper left has the
same value.
Suppose two queens are in same position (i,j) and (k,l) then two queens lie on the same
diagonal , if and only if |j-l|=|I-k|.
Initialize x array to zero and start by placing the first queen in k=1 in the first 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. Here we have to
check whether there is any queen in the same column or diagonal.
For this considers the previous position, which had already, been found out. Check
whether
X (I)=X(k) for column |X(i)-X(k)|=(I-k) for the same diagonal.
If any one of the conditions is true then return false indicating that k th queen can’t be
placed in position X (k).
For not possible condition increment X (k) value by one and precede d until the 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 the matrix 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:
Algorithm place (k,I)
//return true if a queen can be placed in k th row and I th column. otherwise it returns //
- 61 -
//false .X[] is a global array whose first k-1 values have been set. Abs® returns the //absolute
value of r.
{
For j=1 to k-1 do
If ((X [j]=I) //two in same column.
Or (abs (X [j]-I)=Abs (j-k)))
Then return false;
Return true;
}
Example: 4 queens.
Two possible solutions are
Q Q
Q Q
Q Q
Q Q
Solutin-1 Solution 2
(2 4 1 3) (3 1 4 2)
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 same color. Yet only ‘M’ colors are
used. So it’s called M-color ability decision problem.
- 62 -
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 that no 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 then the corresponding nodes are
joined by an edge.
4 5
2
1
1 is adjacent to 2, 3, 4.
2 is adjacent to 1, 3, 4, 5
3 is adjacent to 1, 2, 4
4 is adjacent to 1, 2, 3, 5
5 is adjacent to 2, 4
2 3
5 4
A
Steps to color the Graph:lg
o
First create the adjacency
r matrix graph(1:m,1:n) for a graph, if there is an edge between
i,j then C(i,j) = 1i otherwise C(i,j) =0.
t
The Colors will hbe represented by the integers 1,2,…..m and the solutions will be stored
m
in the array X(1),X(2),………..,X(n) ,X(index) is the color, index is the node.
m
He formula is used
C to set the color is,
o
l
o
r - 63 -
i
n
g
k
)
/
/
t
h
e
X(k) = (X(k)+1) % (m+1)
g
r number is assigned ,after assigning a number for ‘k’ node, we have to
First one chromatic
a
check whether the adjacent nodes has got the same values if so then we have to assign the
p
next value. h
m
a
t
r
i
x
G
[
1
:
n
,
- 64 -
1
:
n
.
A
l
l
a
s
s
i
g
n
m
e
n
t
s
/
/
o
f
1
,
2
,
…
…
…
.
Algorithm: ,
m
Algorithm mColoring(k)
t
// the graph is represented by its Boolean adjacency matrix G[1:n,1:n] .All assignments //of 1,2,
o
……….,m to the vertices of the graph such that adjacent vertices are assigned //distinct integers
are printed. ’k’ is the index
t of the next vertex to color.
h
{ e
repeat
v
{ e
// generate all legal assignment
r for X[k].
Nextvalue(k); // Assign t to X[k] a legal color.
i
If (X[k]=0) then return; // No new color possible.
If (k=n) then c // Almost ‘m’ colors have been used to color the ‘n’ vertices
e
Write(x[1:n]);
s
Else mcoloring(k+1); o
}until(false); f
} t
h
Algorithm Nextvalue(k)e
g
// X[1],……X[k-1] have r been assigned integer values in the range[1,m] such that //adjacent
values have distinct integers.
a A value for X[k] is determined in the //range[0,m].X[k] is assigned
the next highest numbersp color while maintaining //distinctness form the adjacent vertices of
h exists, then X[k] is 0.
vertex K. If no such color
{ s
u
repeat c
{ h
t
h
a - 65 -
t
a
a
c
e
n
c
t
v
X[k] = (X[k]+1)mod(m+1); // next highest color.
e
If(X[k]=0) then return; //All colors have been used.
r
For j=1 to n dot
{ i
// Check if this c color is distinct from adjacent color.
If((G[k,j] 0)and(X[k]
e = X[j]))
// If (k,j) is ans edge and if adjacent vertices have the same color.
a
Then break; r
} e
a
if(j=n+1) then return; //new color found.
s
} until(false); //otherwise try to find another color.
s
} i
g
The time spent by Nextvalue
n to determine the children is (mn)
r
nd
Total time is = (m n).
/
Knapsack
/ Problem using Backtracking
d
The problem is isimilar to the zero-one (0/1) knapsack optimization problem is dynamic
s
programming algorithm.
t
i
We are given ‘n’n positive weights Wi and ’n’ positive profits Pi, and a positive number
‘m’ that is the knapsack
c capacity, the is problem calls for choosing a subset of the weights
such that, t
i
n WiXi
tn
1 i
m and 1i nPiXi is Maximized.
e
g
Xi Constitute
e Zero-one valued Vector.
r
The Solution space s is the same as that for the sum of subset’s problem.
a
Bounding functions r are needed to help kill some live nodes without expanding them. A
e
good bounding function for this problem is obtained by using an upper bound on the
value of the bestpfeasible solution obtainable by expanding the given live node.
r
The profits and weights
i are assigned in descending order depend upon the ratio.
n
t
(i.e.) Pi/Wi P(I+1) / W(I+1)
e
d
.
’
k
’
i
Solution : s
t
h
e - 66 -
i
n
e
x
o
f
t
h
e
After assigning the profit and weights ,we have to take the first object weights and check
if the first weightn is less than or equal to the capacity, if so then we include that object
(i.e.) the unit is 1.(i.e.)
e K 1.
x
Then We are going t to the next object, if the object weight is exceeded that object does not
v
fit. So unit of that
e object is ‘0’.(i.e.) K=0.
Then We are going r to the bounding function ,this function determines an upper bound on
the best solutiontobtainable at level K+1.
e
Repeat the processx until we reach the optimal solution.
t
Algorithm: o
Algorithm Bknap(k,cp,cw)
c
o
// ‘m’ is the size of the lknapsack; ‘n’ no.of weights & profits. W[]&P[] are the //weights &
o
weights. P[I]/W[I] P[I+1]/W[I+1].
r
//fwFinal weights of knapsack.
.
//fp final max.profit.
//x[k] = 0 if W[k] is not {the knapsack,else X[k]=1.
r
{ e
// Generate left child.
p
If((W+W[k] m) ethen
{ a
Y[k] =1; t
{
If(k<n) then Bnap(k+1,cp+P[k],Cw +W[k])
If((Cp + p[w] > fp) and (k=n)) then
/
{ /
fp = cp +gP[k];
e
fw = Cw+W[k];
n
for j=1 toek do X[j] = Y[j];
} r
} a
t
if(Bound(cp,cw,k) fp)
e then
{ a
y[k] = 0; l
if(k<n) then Bnap (K+1,cp,cw);
l
if((cp>fp) and (k=n)) lthen
{ e
g
fp = cp;
a
fw = cw; l
for j=1 to k doaX[j] = Y[j];
s
s
i
g - 67 -
n
m
e
t
f
o
r
X
[
k
} ]
} .
}
N
e
Algorithm for Bounding function:
x
t
Algorithm Bound(cp,cw,k) v
// cp current profit total.
a
l
//cw current weight total.
//kthe index of the lastu removed item.
e
//mthe knapsack size.(
k
{ )
b=cp; ;
c=cw;
for I =- k+1 to n do
/
{ /
c= c+w[I]; A
if (c<m) then b=b+p[I];
s
s
else return b+ (1-(c-m)/W[I]) * P[I];
} i
g
return b; n
}
t
Example: o
M= 6 Wi X
= 2,3,4
[
4 2 2
k
N= 3 Pi ]= 1,2,5 Pi/Wi (i.e.) 5 2 1
a
Xi = 1 0 1
The maximum weight isl 6
e
g
The Maximum profit is a(1*5) + (0*2) + (1*1)
5+1l
6.c
o
l
Fp = (-1) o
1 3 & 0+4 r6
cw = 4,cp = 5,y(1). =1
k = k+2
2 3 but 7>6
so y(2) = 0
I
f
So bound(5,4,2,6)
(
X
[
k - 68 -
]
=
0
t
h
e
n
r
e
t
B=5 u
C=4 r
n
I=3 to 3
;
C=6
6 6
So return 5+(1-(6-6))/(2*1)
If 4> 3 then c
Fpo=6,fw=6,k=3 ,x(1) 1 0 1
Thel solution Xi 1 0 1
o
r
p 6
Profit
Weight
o 6.
s
s
i
b
l
e
.
I
f
(
k
=
n
)
t
h
e
n
/
- 69 -
/
A
l
o
s
t
‘
m
’
c
o
l
o UNIT – V
r
s
GRAPHhTRAVERSALS,BRANCH AND BOUND
a
DEFINING GRAPH: v
e
A graphs g consists of a set V of vertices (nodes) and a set E of edges (arcs). We write
b
G=(V,E). V is a finite and
e non-empty set of vertices. E is a set of pair of vertices; these pairs are
called as edges . Therefore,
e
V(G).read as V of G, is na set of vertices and E(G),read as E of G is a set of edges.
An edge e=(v, w) is a pair of vertices v and w, and to be incident with v and w.
u
s
A GR A PH C A N B E PI C TO R I A L LY RE PR E SE NT E D AS FOL L OW S ,
e
d
t 1
o
2 3
c
o
4
l
o
r
t
FIG:
h Graph G
e
We have numbered the graph as 1,2,3,4. Therefore, V(G)=(1,2,3,4) and
‘
E(G) = {(1,2),(1,3),(1,4),(2,3),(2,4)}.
n
’
BASIC TERMINOLGIES v OF GRAPH:
e
UNDIRECTED GRAPH: r
t
An undirected graph is that in which, the pair of vertices representing the edges is
unordered. i
c
e
DIRECTED GRAPH: s
An directed graph is that in which, each edge is an ordered pair of vertices, (i.e.)
each edge is represented by a directed pair. It is also referred to as digraph.
DIRECTED GRAPH
W
r
i
t
e
(
x - 70 -
[
1
]
)
;
COMPLETE GRAPH: E
An n vertex
l undirected graph with exactly n(n-1)/2 edges is said to be complete
graph. The graph G is said
s to be complete graph .
e
TECHNIQUES FOR GRAPHS:
m
c
The fundamentalo problem concerning graphs is the reach-ability problem.
In it simplest from l it requires us to determine whether there exist a path in the given
graph, G +(V,E) osuch that this path starts at vertex ‘v’ and ends at vertex ‘u’.
A more general rform is to determine for a given starting vertex v6 V all vertex ‘u’ such
i from if it u.
that there is a path
n
This problem can g
be solved by starting at vertex ‘v’ and systematically searching the
graph ‘G’ for vertex
( that can be reached from ‘v’.
We describe 2 search k methods for this.
+
i. Breadth first1 Search and Traversal.
ii. Depth first) Search and Traversal.
;
}
BREADTH u FIRST SEARCH AND TRAVERSAL
n
Breadth first search: t
i
l
In Breadth first search
(
we start at vertex v and mark it as having been reached. The vertex
v at this time is said tof be unexplored. A vertex is said to have been explored by an algorithm
when the algorithm has avisited all vertices adjacent from it. All unvisited vertices adjacent from v
are visited next. There arel new unexplored vertices. Vertex v has now been explored. The newly
visited vertices have notsbeen explored and are put onto the end of the list of unexplored vertices.
The first vertex on this elist 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).
The vertex ‘v’ isAat this time said to be unexplored.
A vertex is said lto have been explored by an algorithm when the algorithm has visited all
vertices adjust from
g it.
o
All unvisited vertices adjust from ‘v’ are visited next. These are new unexplored vertices.
Vertex ‘v’ has nowr been explored. The newly visit vertices have not been explored and
i
are put on the endt
of a list of unexplored vertices.
The first vertex hon this list in the next to be explored. Exploration continues until no
unexplored vertex m is left.
The list of unexplored vertices operates as a queue and can be represented using any of
N
the start queue representation.
e
x
ALGORITHM: t
v
a
l
u - 71 -
e
(
k
/
/
X
[
1
]
,
…
// A breadth first search …of ‘G’ is carried out.
// beginning at vertex-v;XFor any node i, visit.
// if ‘i’ has already been [visited. The graph ‘v’
// and array visited [] arek global; visited []
// initialized to zero. -
1
{ y=v; // q is a queue of ]unexplored 1visited (v)= 1
repeat h
{ for all vertices ‘w’ adjacent
a from u do
v
{ if (visited[w]=0) then
{Add w to q; e
visited[w]=1 b
} e
} e
if q is empty then return;//
n No delete u from q;
} until (false)
a
}
s
s
algrothim : breadth firstitraversal
algorithm BFT(G,n) g
n
{ e
d
for i= 1 to n do i
visited[i] =0; n
for i =1 to n do t
if (visited[i]=0)theneBFS(i)
g
} e
r
here the time and spacevrequired by BFT on an n-vertex e-edge graph one O(n+e) and O(n) resp
a adjancey matrix is used then the bounds are O(n2) and O(n) resp
if adjacency list is used.if
l
Depth First Search u
e
s
A depth first search
i of a graph differs from a breadth first search in that the exploration of
a vertex v is suspendednas 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.
t
The search terminates when all reached vertices have been fully explored. This search process is
h
best-described recursively.
e
Algorithm DFS(v)
{ r
visited[v]=1 a
n from v do
for each vertex w adjacent
g
{
e
If (visited[w]=0)then [
DFS(w); 1
,
m
]
s - 72 -
u
c
h
t
h
a
t
/
/
a
} d
} j
a MINIMUM SPANNING TREE
c
e
n
Let G(V,E) be ant undirected connected graph with vertices ‘v’ and edge ‘E’.
A sub-graph t=(V,E’)
v of the G is a Spanning tree of G iff ‘t’ is a tree.3
The problem is to generate a graph G’= (V,E) where ‘E’ is the subset of
a E,G’ is a
Minimum spanning l tree.
u
Each and every eedge will contain the given non-negative length .connect all the nodes
with edge presents in set E’ and weight has to be minimum.
h
NOTE: a
We have to visit veall the nodes.
The subset tree (i.e) any connected graph with ‘N’ vertices must have at least N-1 edges
and also it does not
d form a cycle.
i
Definition: s
t
A spanning treei of a graph is an undirected tree consisting of only those edge that are
necessary to connect
n all the vertices in the original graph.
A Spanning treec has a property that for any pair of vertices there exist only one path
between them andt the insertion of an edge to a spanning tree form a unique cycle.
i
n
Application of the spanning
t tree:
1. Analysis of electrical ecircuit.
2. Shortest route problems.
g
e
Minimum cost spanning r tree:
s
The cost of a spanning
. tree is the sum of cost of the edges in that trees.
There are 2 method A to determine a minimum cost spanning tree are
1. Kruskal’s Algorithm v
2. Prom’s Algorithm. a
l
u
KRUSKAL’S ALGORITHM:
e
f
In kruskal's algorithm
o
the selection function chooses edges in increasing order of length
without worrying too much
r about their connection to previously chosen edges, except that never
to form a cycle. The result
X is a forest of trees that grows until all the trees in a forest (all the
components) merge in a[ single tree.
k
]
In this algorithm, a minimum
i cost-spanning tree ‘T’ is built edge by edge.
Edge are consideredsfor inclusion in ‘T’ in increasing order of their cost.
d
e
An edge is includedt in ‘T’ if it doesn’t form a cycle with edge already in T.
e
r
m - 73 -
i
n
e
i
n
t
h
e
To find the minimum cost spanning tree the edge are inserted to tree in increasing order
of their cost /
/
r
Algorithm: a
n
Algorithm kruskal(E,cost,n,t)
g
//Eset of edges in G has e ‘n’ vertices.
//cost[u,v]cost of edge[ (u,v).tset of edge in minimum cost spanning tree
0
// the first cost is returned.
,
{ m
for i=1 to n do parent[I]=-1;
]
I=0;mincost=0.0; .
While((I<n-1)and (heapXnot empty)) do
{ [
k
j=find(n); ]
k=find(v); i
if(j not equal k) than s
{ a
i=i+1 s
s
t[i,1]=u; i
t[i,2]=v; g
mincost=mincost+cost[u,v]; n
union(j,k); e
} d
}
t
if(i notequal n-1) then write(“No
h spanning tree”)
else return minimum cost;e
}
Analysis n
e
The time complexity of minimum cost spanning tree algorithm in worst case is O(|E|log|
x
E|), t
where E is the edge set h of G.
i
Example: Step by Stepgoperation of Kruskal algorithm.
h
e
s
Step 1. In the graph,t the Edge(g, h) is shortest. Either vertex g or vertex h could be
n vertex g arbitrarily.
representative. Lets choose
u
m
b
e
r
e
s
c
o
l
o
r
w
- 74 -
h
i
l
m
a
i
n
t
a
Step 2. The edge (c, i) creates
i the second tree. Choose vertex c as representative for second tree.
n
i
n
g
/
/
d
i
s
Step 3. Edge (g, g) is thet next shortest edge. Add this edge and choose vertex g as representative.
i
n
c
t
n
e
s
s
f
o
r
Step 4. Edge (a, b) creates
m a third tree.
t
h
e
a
d
j
a
c
e merge two trees. Vertex c is chosen as the representative.
Step 5. Add edge (c, f) and
n
t
v
e
r
t
i
c
e
s
o
Step 6. Edge (g, i) is the
f
next next cheapest, but if we add this edge a cycle would be created.
Vertex c is the representative
v of both.
e
r
t
e
x
K
.
I
f
n - 75 -
o
c
h
c
o
l
o
r
e
x
i
s
t
s
,
t
Step 7. Instead, add edgeh (c, d).
e
n
X
[
k
]
i
s
0
.
Step 8. If we add edge (h, i), edge(h, i) would make a cycle.
{
r
e
p
e
a
t
X
[ edge (b, c), it would create a cycle. Add edge (d, e) instead to complete
Step 10. Again, if we add
k
the spanning tree. In this] spanning tree all trees joined and vertex c is a sole representative.
=
(
X
[
k
]
+
1
)
m
o - 76 -
d
(
m
1
)
;
/
/
n
Prim's Algorithm e
x
t
Start from an arbitrary vertex (root). At each stage, add a new branch (edge) to the tree
h
already constructed; thei algorithm halts when all the vertices in the graph have been reached.
g
h
e
s
t
c
o
l
o
r
.
Algorithm prims(e,cost,n,t)
{
Let (k,l) be an edge of minimum cost in E;
Mincost :=cost[k,l]; I
T[1,1]:=k; t[1,2]:=l; f
(
For I:=1 to n do X
[
If (cost[i,l]<cost[i,k])kthen near[i]:=l;
Else near[i]:=k; ]
Near[k]:=near[l]:=0; =
For i:=2 to n-1 do 0
{ )
Let j be an index sucht that near[j]≠0 and
h
Cost[j,near[j]] is minimum;
e
T[i,1]:=j; t[i,2]:=near[j];
n
Mincost:=mincost+ Cost[j,near[j]];
Near[j]:=0; r
For k:=0 to n do e
t
If near((near[k]≠0) uand (Cost[k,near[k]]>cost[k,j])) then
Near[k]:=j; r
} n
Return mincost; ;
The prims algorithm will start with a tree that includes only a minimum cost edge of G.
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.
A
l
l
c
o - 77 -
l
o
r
h
a
v
e
b
e
The working of prims
e will be explained by following diagram
n
u
Step 1: s Step 2:
e
d
.
F
o
Step 3: r Step 4:
j
=
1
t
o
d
o
Step 5: Step 6:
Depth first node generation with bounding function is called backtracking. State generation
methods in which the E-node remains the E-node until it is dead lead to branch-and-bound
method.
The term branch-and-bound refers to all state space search methods in which all children of
the E-node are generatedI before any other live node can become the E-node.
f
( terminology breadth first search(BFS)- like state space search will be
In branch-and-bound
(
called FIFO (First In First Output) search as the list of live nodes is a first -in-first -out list(or
G
queue). [
k
A D-search (depth search)
, state space search will be called LIFO (Last In First Out) search,
j
as the list of live nodes is a list-in-first-out list (or stack).
]
Bounding functions0are used to help avoid the generation of sub trees that do not contain an
answer node. )
a
The branch-and-boundn algorithms search a tree model of the solution space to get the
solution. However, this dtype of algorithms is oriented more toward optimization. An algorithm of
this type specifies a real(X-valued cost function for each of the nodes that appear in the search tree.
[
Usually, the goal here
k 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. =
X
[
j
- 79 -
]
)
)
Example 8.1[4-queens] / Let us see how a FIFO branch-and-bound algorithm would search the
/
state space tree (figure 7.2) for the 4-queens problem.
I
f
(
k
,
j
)
i
s
a
n
e
d
g
e
a
n
d
i
f
a
d
j
a one live node, node1. This represents the case in which no queen has
Initially, there is only
c
been placed on the chessboard.
e This node becomes the E-node.
n
It is expanded and itst children, nodes2, 18, 34 and 50 are generated.
v
e
These nodes represent a chessboard with queen1 in row 1and columns 1, 2, 3, and 4
r
respectively. t
i
The only live nodes c2, 18, 34, and 50.If the nodes are generated in this order, then the next E-
node are node 2. e
s
h
It is expanded and the nodes 3, 8, and 13 are generated. Node 3 is immediately killed using
a
the bounding function. Nodes
v 8 and 13 are added to the queue of live nodes.
e
Node 18 becomes the next E-node. Nodes 19, 24, and 29 are generated. Nodes 19 and 24 are
t
killed as a result of the bounding functions. Node 29 is added to the queue of live nodes.
h
e
Now the E-node is node 34.Figure 8.1 shows the portion of the tree of Figure 7.2 that is
generated by a FIFO branch-and-bound
s search. Nodes that are killed as a result of the bounding
functions are a "B" under
a them.
m
Numbers inside e
the nodes correspond to the numbers in Figure 7.2.Numbers outside the nodes
c
o
l - 80 -
o
r
give the order in which T
the nodes are generated by FIFO branch-and-bound.
h
e
At the time the answer
n node, node 31, is reached, the only live nodes remaining are nodes 38
and 54.
b
r
e
a
k
;
i
f
Least Cost (LC) Search: (
j
=
In both LIFO and n 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
1 chance of getting the search to an answer node quickly.
a node that has a very good
)
Thus, in Example th8.1, when node 30 is generated, it should have become obvious to the
search algorithm that thise node will lead to answer node in one move. However, the rigid FIFO
rule first requires the expansion
n of all live nodes generated before node 30 was expanded.
r
The search for an answer node can often be speeded by using an "intelligent" ranking function
(.) for live nodes. Theetnext E-node is selected on the basis of this ranking function.
u
If in the 4-queens example
r we use a ranking function that assigns node 30 a better rank than
all other live nodes, then
n 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 annanswer node from the live node. For any node x, this cost could be
e
(1) The number ofwnodes on the sub-tree x that need to be generated before any answer
node is generated or,
c
more simply,
o
l
o
r - 81 -
f
o
u
d
.
}
(2) The number of ulevels the nearest answer node (in the sub-tree x) is from x
n
Using cost measure (2),
t the cost of the root of the tree of Figure 8.1 is 4 (node 31 is four levels
from node 1).The costsi of nodes 18 and 34,29 and 35,and 30 and 38 are respectively 3, 2, and
l
1.The costs of all remaining nodes on levels 2, 3, and 4 are respectively greater than 3, 2, and 1.
(
f
Using these costs as aabasis to select the next E-node, the E-nodes are nodes 1, 18, 29, and 30
(in that order).The only lother nodes to get generated are nodes 2, 34, 50, 19, 24, 32, and 31.
s
e
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 cost function a (.) such that (x) <=c(x) is used to provide lower bounds on solutions
obtainable from any noden x. If upper is an upper bound on the cost of a minimum-cost solution,
o
then all live nodes x with
t (x)>upper may be killed as all answer nodes reachable from x have
cost c(x)>= (x)>upper.hThe starting value for upper can be set to infinity.
e
As an example r optimization problem, consider the problem of job scheduling with
deadlines. We generalizec this problem to allow jobs with different processing times. We are given
o
n jobs and one processor.l Each job i has associated with it a three tuple
( ).job i requires
o units of processing time .if its processing is not completed by the
deadline , and then a penalty is incurred.
r
.
}
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
T
h - 82 -
e
t
m
e
s
p
e
n
be such that the penalty tincurred is minimum among all possible subsets j. such a j is optimal.
b
y
Consider the following instances: n=4,( , , )=(5,1,1),( , , )=(10,3,2),( , ,
)=(6,2,1),and( , , N )=(3,1,1).The solution space for this instances consists of all possible
subsets of the job index eset{1,2,3,4}. The solution space can be organized into a tree by means of
x
either of the twot formulations used for the sum of subsets problem.
v
a
l
u
e
t
o
d
e
t
e
r
m
i
n
e
t
h
e
c
h
i
l
d
r
e
n
i
s
(
m
n
) Figure 8.7
T
o
t
a to the variable tuple size formulations while figure 8.7 corresponds to
Figure 8.6 corresponds
l
the fixed tuple size formulation. In both figures square nodes represent infeasible subsets. In
t
figure 8.6 all non-square
i
nodes are answer nodes. Node 9 represents an optimal solution and is
the only minimum-cost m answer node .For this node j= {2,3} and the penalty (cost) is 8. In figure
e
i - 83 -
s
=
m
n
n
)
.
8.7 only non-square leafKnodes are answer nodes. Node 25 represents the optimal solution and is
also a minimum-cost answer node. This node corresponds to j={2,3} and a penalty of 8. The
n
costs of the answer nodes of figure 8.7 are given below the nodes.
a
p
s
NP-Hard Problems: a
c
We say that a decision problem Pi is NP-hard if every problem in NP is polynomial time
reducible to Pi.
k
In symbols, Pi is NP-hard if, for every Pj 2 NP, Pj
P require Pi to be in NP.
Note that this doesn’t
r it means that Pi is ‘as hard as’ all the problems in NP.
Highly informally,
o If Pi can obe solved in polynomial-time, then so can all problems in NP.
o Equivalently,
b if any problem in NP is ever proved intractable, then Pi must also be
intractable.
l
e
m
NP-Complete Problems:
u
We say that a decision problem Pi is NP-complete if
it is NP-hard andsit is also in the class NP itself.
In symbols, Pi isiNP-complete if Pi is NP-hard and Pi 2 NP
Highly informally, it means that Pi is one of the hardest problems in NP.
n
How to show a problem Pi is NP-complete (Method 1, from the definition)
g that Pi is a decision problem.
o First, confirm
o Then show Pi is in NP.
o Then show B that Pi is NP-hard by showing that every problem Pj in NP is
polynomial- timeareducible to Pi.
c
k
t
r
a
c
k
i
n
g
:
T
h
e
p
r
o - 84 -
b
l
e