Design and Analysis of Algorithm notes pdf3
Design and Analysis of Algorithm notes pdf3
in
SYLLABUS
UNIT – II: Disjoint set operations, union and find algorithms, AND/OR graphs, Connected
Components and Spanning trees, Bi-connected components Backtracking-General method,
applications The 8-queen problem, sum of subsets problem, graph coloring, Hamiltonian
cycles.
UNIT – III: Greedy method- General method, applications- Knapsack problem, Job
sequencing with deadlines, Minimum cost spanning trees, Single source shortest path
problem.
UNIT – I
INTRODUCTION TO ALGORITHMS
Analysis of Algorithm:
INTRODUCTION – ANALYZING CONTROL STRUCTURES- AVERAGE CASE ANALYSIS-
SOLVING RECURRENCES.
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:
2. Graphic representation called flowchart: This method will work well when the
algorithm is small& simple.
3. 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.
<statement-n>
}
For Loop:
For variable: = value-1 to value-2 step step do
{ <state
ment-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.
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;
• Putting all these observations together, we get the algorithm Selection sort.
Theorem:
Algorithm selection sort(a,n) correctly sorts a set of n>=1 elements .The result
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
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:
9. t:=a[I];
10. a[I]:=a[j];
11. a[j]:=t;
12. }
13. }
Recursive Algorithms:
1. Towers of Hanoi:
.
.
.
• According to legend, at the time the world was created, there was a diamond
tower (labeled A) with 64 golden disks.
• The disks were of decreasing size and were stacked on the tower in decreasing
order of size bottom to top.
• Besides these tower there were two other diamond towers(labeled B & C)
• Since the time of creation, Brehman priests have been attempting to move the
disks from tower A to tower B using tower C, for intermediate storage.
• As the disks are very heavy, they can be moved only one at a time.
• In addition, at no time can a disk be on top of a smaller disk.
• According to legend, the world will come to an end when the priest have
completed this task.
• A very elegant solution results from the use of recursion.
• Assume that the number of disks is ‘n’.
• 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:
{ (a,b,c),(a,c,b),(b,a,c),(b,c,a),(c,a,b),(c,b,a)}
• It is easy to see that given ‘n’ elements there are n! different permutations.
• A simple algorithm can be obtained by looking at the case of 4
statement(a,b,c,d)
• The Answer can be constructed by writing
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.
Example 2:
Algorithm sum(a,n)
{
s=0.0;
for I=1 to n do s=
s+a[I];
return s;
}
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).
The number of steps any problem statemn t is assigned depends on the kind
of statement.
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.
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 n1
6. return s; 1 1 0
7. } 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:
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.
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.
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.
0 if n=0
T(n) = 3T(n ÷ 2)+n otherwise
• It is tempting to restrict ‘n’ to being ever since in that case n÷2 = n/2, but
recursively dividing an even no. by 2, may produce an odd no. larger than 1.
• Therefore, it is a better idea to restrict ‘n’ to being an exact power of 2.
• 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,
n T(n)
T(A)
=3* 1 1
T(2)
2 3 * 1 +2
+4 2
2 32 * 1 + 3 * 2 + 22
23 33 * 1 + 32 * 2 + 3 * 22 + 23
=3* 4
(3 * 2 34 * 1 + 33 * 2 + 32 * 22 + 3 * 23 + 24
5 5 4 3 2 2 3 4 5
1
+2) +4
= (32 * 1) + (3 * 2) +4
2 3*1+3*2+3*2+3*2+3*2+2
= ∑ 3k-i 2i
= 3k ∑ (2/3)i
Let Sn be the sum of the first n terms of the geometric series a, ar,
ar ….Then Sn = a(1-rn)/(1-r), except in the special case when r = 1; when S n =
2
an.
3 k+1 – 2k+1 3
k
= 3 * -----------------
* ---- 3
k+1
1
3 k+1 – 2k+1
= 3k * ---------------
-- 3k+1-1
= 3k+1 – 2k+1
EG : 2
0
n=0 tn = 5
n=1 3t n-1 +
4tn-2, otherwise
Characteristics Polynomial, x2 – 3x – 4 = 0
(x – 4)(x + 1) = 0
Roots r1 = 4, r2 = -1
Eqn 1 C1 = -C2
r2 – r1
5
=
------
-1 + 4
= 5 / (-5) = -1
C2 = -1 , C1 = 1
fn = 1. 4n + (-
1) . (-1)n fn = 4n
+ 1n
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
* The recurrence is, linear because it does not contain terms of the form t n-i, t n-j, t2 n-i,
and soon homogeneous because the linear combination of the t n-i is equal to zero.
With constant co-efficient because the ai are constants
* 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
= C(a0 fn + a1 fn-1 + … +
ak fn-k)+ d(a0 gn + a1 gn-
1 + … + ak gn-k) =C*0
+d*0 = 0.
n if
n=0 or n=1
fn = f n-1 + f n-2
otherwise
We rewrite the
recurrence as,
fn – f n-1 – f n-2 = 0.
The characteristic
polynomial is,
x2 – x – 1 = 0.
1 ±√ (1 + 4)
= ----------
2
1 ± √5
= -------
2
1+√5 1 - √5
r1 = --------- and r2= ---
2 2
when n=0, f0 = C1 + C2 = 0
when n=1, f1 = C1r1 + C2r2 = 1
C1 + C2 = 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,
11 + √5 n -1 1
- √5 n
fn = ---- --------- +
---- -------- √5 2
√5 2
1 1 + √5 n 1 – √5 n
= ---- --------- - ---------
√5 2 2
3. Inhomogeneous recurrence :
Example(1) :
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
= O[3n]
Example :(2)
t1 – 2t0 = 18
t1 = 18 + 2t0
n=0, C1 + C2 = t0
(2) n=1, 2C1 + 3C2 + 3C3 = 18 +
2t0 (4) n=2, 4C1 + 9C2 + 18C3
= 99 + 4t0 (5)
Sub, C2 = 9 in eqn(2)
C1 + C2 = t0
C1 + 9 = t0
C1 = t0-9
= Max[O(2n), O(3n),
O(n3n)] tn = O[n3n]
Example: (3)
1
if n=0 tn =
4t n-1 – 2n otherwise
The general
solution ,
n n
tn = C1r1 + C2r2
tn = C14n + C22n (1)
-2C2 = 4t0 - 6
2C2 = 6 – 4t0
C2 = 3 – 2t0
3 – 2(1) = 1
C2 = 1
Example : (4)
0
if n=0 tn =
2t n-1 + n +2n otherwise
tn – 2t n-1 = n + 2n (A)
There are two polynomials.
sub. n=0 C1 + C3 = 0
(2) sub. n=1 2C1 + 2C2 + C3 + C4 = t1
(3)
– 2t0 = 3 t1
= 3 [since t0 = 0]
t2 – 2t1 =
2+4 t2
– 2t1 = 6
t2 = 6 + 2t1
t2 = 6 + 2.3
t2 = 6 + 6
t2 = 12
---------------------------------------
-4C1 – 16C2 – C4 = -23 (6)
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 t i for the term of a new recurrence obtained from the first by
a change of variable.
Example: (1)
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
2i
ti – 3t i-1 = 2i (A)
In this case,
b = 2, p(n) = 1, degree = 0
We use the fact that, T(2i) = ti & thus T(n) = tlogn when n= 2i to obtain,
T(n) = C1. 3 log2n + C2. 2log2 n
T(n) = C1 . nlog23 + C2.n [i = logn]
When ‘n’ is a power of 2, which is sufficient to conclude that,
Example: (2)
In this eqn,
b = 4, P(n) = 1, degree = 0
EXAMPLE : 3
In this case,
b = 2, P(n) = i, degree = 1
(x – 2)(x – 2)2 = 0
2i = nC1 + nC2 +
2
nC3(log n 2n)
tn = O(n.log22n)
Example: 4
T(n) = 2 ,n=1
2
5T(n/4) + Cn , n>1
ti = T(4i) = 5T(4i/4)
+ C(4i)2 =
i-1
5T 4 + C. 16i
= 5t i-1 + C.16i
ti – 5t i-1 = C. 16i
EXAMPLE: 5
2 ,n=1
T(n) =
T(n/2) + Cn , n > 1
T(n) = T(n/2) + Cn
=
i
T(2 /2) + C.
2i = T(2
i-1
) + C. 2i
ti = t i-1 + C.
2i ti – t i-1 =
C. 2i
= C1 . n log21 + C2.n
tn = O(n)
EXAMPLE: 6
1 , n =1
T(n) =
3T(n/2) + n; n is a power of 2
ti = T(2i) =
3T(2i/2) + 2i
= 3T(2 i-1) + 2i
ti = 3t i-1 + 2i
So, b = 2, P(n) =1, degree = 0
(x – 3)(x – 2) = 0
tn = O(n log23)
EXAMPLE: 7
ti = T(2i) =
2T(2i/2) + 2i . i
= 2T(2 i-1) +i. 2i
= 2t i-1 + i.2i ti –
2ti-1 = i. 2i
General solution,
tn = C1.r1i + C2.i.r2i + C3.
i2. r3i = C1.2i + C2. i.2i
2 i
+ C3. i .2
= C1. n+C2.n.log2n + C3.i2.n
= C1.n + C2.n.log2n + C3(2 log2n).n
tn = O(n. 2 log2n)
5. Range Transformation:
EXAMPLE: 1
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
= 2i t2 i-1
* This recurrence is not clear, furthermore the co-efficient 2i is not a constant.
* To transform the range, we create another recurrence by using ui to denote
lg ti ui = lgti = i + 2lg t i-1 = i + 2u i-1
ui – 2u i-1 = i
(x – 2)(x – 1)2 = 0
= 2C1n / 4n
T(n) = 2 2n / 4n 3n
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 (nth) 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.
.
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.
SORTING
One of the major applications in computer science is the sorting of information in
a table. Sorting algorithms arrange items in a set according to a predefined ordering
relation. The most common types of data are string information and numerical
information. The ordering relation for numeric data simply involves arranging items in
sequence from smallest to largest and from largest to smallest, which is called ascending
and descending order respectively.
The items in a set arranged in non-decreasing order are {7,11,13,16,16,19,23}. The items
in a set arranged in descending order is of the form {23,19,16,16,13,11,7}
Similarly for string information, {a, abacus, above, be, become, beyond}is in ascending
order and { beyond, become, be, above, abacus, a}is in descending order.
There are numerous methods available for sorting information. But, not even one of them
is best for all applications. Performance of the methods depends on parameters like, size
of the data set, degree of relative order already present in the data etc.
Selection sort :
The idea in selection sort is to find the smallest value and place it in an order, then
find the next smallest and place in the right order. This process is continued till the entire
table is sorted.
20 35 18 8 14 41 3 39
3 8 14 18 20 35 39 41
One way to sort the unsorted array would be to perform the following steps:
• Find the smallest element in the unsorted array
• Place the smallest element in position of a[1]
i.e., the smallest element in the unsorted array is 3 so exchange the values of a[1]
and a[7]. The array now becomes,
Now find the smallest from a[2] to a[8] , i.e., 8 so exchange the values of a[2] and
a[4] which results with the array shown below,
Repeat this process until the entire array is sorted. The changes undergone by the array is
shown in fig 2.2.The number of moves with this technique is always of the order O(n).
Bubble sort:
Bubble Sort is an elementary sorting algorithm. It works by repeatedly exchanging
adjacent elements, if necessary. When no exchanges are required, the file is sorted.
BUBBLESORT (A)
for i ← 1 to length [A] do
for j ← length [A] downto i+1 do
If A[A] < A[j-1] then
Exchange A[j] ↔ A[j-1]
Here the number of comparison made
2
1 + 2 + 3 + . . . + (n-1) = n(n-1)/2 = O(n )
Another well-known sorting method is bubble sort. It differs from the selection
sort in that instead of finding the smallest record and then performing an interchange two
records are interchanged immediately upon discovering that they are of out of order.
Dept. of CSE, SWEC, Hyderaba. Page 40
WWW.AKTutor.in
Design and Analysis of Algorithms
When this approach is used there are at most n-1 passes required. During the first pass
k1and k2 are compared, and if they are out of order, then records R1 AND R2 are
interchanged; this process is repeated for records R2 and R3, R3 and R4 and so on .this
method will cause with small keys to bubble up. After the first pass the record with the
largest key will be in the nth position. On each successive pass, the records with the next
largest key will be placed in the position n-1, n-2 …2 respectively, thereby resulting in a
sorted table.
After each pass through the table, a check can be made to determine whether any
interchanges were made during that pass. If no interchanges occurred then the table must
be sorted and no further passes are required.
Insertion sort :
Insertion sort is a straight forward method that is useful for small collection of
data. The idea here is to obtain the complete solution by inserting an element from the
unordered part into the partially ordered solution extending it by one element. Selecting
an element from the unordered list could be simple if the first element of that list is
selected.
Initially the whole array is unordered. So select the minimum and put it in place of
a[1] to act as sentinel. Now the array is of the form,
Now the element to be inserted is 8. 8 is less than 35 and 8 is also less than 18 so
move 35 and 18 one position right and place 8 at a[2]. This process is carried till the
sorted array is obtained.
Algorithm Insert(a,n)
{
// Insert a[n] into the heap which is stored in a[1:n-1]
The figure shows one example of how insert would insert a new value into an
existing heap of five elements. It is clear from the algorithm and the figure that the time
for insert can vary. In the best case the new elements are correctly positioned initially and
Dept. of CSE, SWEC, Hyderaba. Page 43
WWW.AKTutor.in
Design and Analysis of Algorithms
no new values need to be rearranged. In the worst case the number of executions of the
while loop is proportional to the number of levels in the heap. Thus if there are n
elements in the heap, inserting new elements takes O(log n) time in the worst case.
To delete the maximum key from the max heap, we use an algorithm called
Adjust. Adjust takes as input the array a[ ] and integer I and n. It regards a[1..n] as a
complete binary tree. If the subtrees rooted at 2I and 2I+1 are max heaps, then adjust will
rearrange elements of a[ ] such that the tree rooted at I is also a max heap. The maximum
elements from the max heap a[1..n] can be deleted by deleting the root of the
corresponding complete binary tree. The last element of the array, i.e. a[n], is copied to
the root, and finally we call Adjust(a,1,n-1).
Algorithm Adjust(a,I,n)
{
j=
2I
;
ite
m
=a
[I]
;
while (j<=n) do
{
if ((j<=n) and (a[j]< a[j+1]))
then j=j+1;
//compare left and right child and let j be the
right //child
if ( item >= a[I]) then
break; // a position
for item is found
a[i/2]=a[j]; j=2I; }
a[j/2]=item;
}
Algorithm Delmac(a,n,x)
// Delete the maximum from the heap a[1..n] and store it in x
{
if (n=0) then
{
write(‘heap is empty");
return (false);
} x=a[1]; a[1]=a[n];
Adjust(a,1,n-1);
Return(true);
}
Note that the worst case run time of adjust is also proportional to the height of the
tree. Therefore if there are n elements in the heap, deleting the maximum can be done in
O(log n) time.
To sort n elements, it suffices to make n insertions followed by n deletions from a
heap since insertion and deletion take O(log n) time each in the worst case this sorting
algorithm has a complexity of O(n log n).
Algorithm sort(a,n)
{
for i=1 to n do
Insert(a,i);
for i= n to 1 step –1 do
{
Delmax(a,i,x);
a[i]=x;
}
}
CHAPTER- 2
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 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.
• 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.
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
= n. T(1) + n log n [since, log 1=0, 2^0=1]
= 2n + n log n
BINARY SEARCH
15. }
• 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.
1 6 3
1 2 1
2 2 2
2 1 Not 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-
andconquer 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 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:
• If the list has more than 2 elements, P has to be divided into smaller instances.
• 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
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
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)
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.
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 sub arrays
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 all
elements 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 the set 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.
9. {
10. v=a[m];I=m;j=p;
11. repeat
12. {
13. repeat
14. I=I+1;
15. until(a[I]>=v);
16. repeat
17. j=j-1;
18. until(a[j]<=v);
19. if (I<j) then interchange(a,i.j);
20. }until(I>=j);
21. a[m]=a[j]; a[j]=v;
22. retun j;
23. }
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 element
8. {
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> int
a[20]; main() {
int n,I;
clrscr();
printf(“QUICK SORT”); printf(“\n
Enter the no. of elements “);
scanf(“%d”,&n); printf(“\nEnter the
array elements”); for(I=0;I<n;I++)
scanf(“%d”,&a[I]); quicksort(0,n-1);
printf(“\nThe array elements are”);
for(I=0;I<n;I++) printf(“\n%d”,a[I]);
getch(); }
quicksort(int p, int q)
{
int j;
if(p,q)
{ j=partition(p,q+
1); quicksort(p,j-
1);
quicksort(j+1,q);
}
}
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)
{
int p;
p= a[I];
a[I]=a[j];
a[j]=p;
}
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
• Let A and B be the 2 n*n Matrix. The product matrix C=AB is calculated by using
the formula,
• Divide and conquer method suggest another way to compute the product of n*n
matrix.
• We assume that N is a power of 2 .In the case N is not a power of 2 ,then enough
rows and columns of zero can be added to both A and B .SO that the resulting
dimension are the powers of two.
• 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 .
• The formula are
For EX:
2222 1 1 11
4*4= 2222 1 11 1
2222 * 1 11 1
2222 1 11 1
2 2 2 2 1 1 1 1 4 4 4 4
2 2 2 2 * 1 1 1 1 = 4 4 4 4
2 2 2 2 1 1 1 1 4 4 4 4
2 2 2 2 1 1 1 1 4 4 4 4
Example
4 4 4 4
*
4 4 4 4
P=(4*4)+(4+4)=64
Q=(4+4)4=32
R=4(4-4)=0
S=4(4-4)=0
T=(4+4)4=32
U=(4-4)(4+4)=0
V=(4-4)(4+4)=0
C11=(64+0-32+0)=32
C12=0+32=32
C21=32+0=32
C22=64+0-32+0=32
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 .
• Stressen has discovered a way to compute the Cij of equation (2) using only 7
multiplication and 18 addition or subtraction.
• 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
UNIT-III
GREEDY METHOD:
Greedy Method:
The greedy method is perhaps (maybe or possible) the most straight forward design
technique, used to determine a feasible solution that may or may not be optimal.
Feasible solution:- Most problems have n inputs and its solution contains a subset of
inputs that satisfies a given constraint(condition). Any subset that satisfies the constraint
is called feasible solution.
Optimal solution: To find a feasible solution that either maximizes or minimizes a given
objective function. A feasible solution that does this is called optimal solution.
The greedy method suggests that an algorithm works in stages, considering one input at a
time. At each stage, a decision is made regarding whether a particular input is in an
optimal solution.
Greedy algorithms neither postpone nor revise the decisions (ie., no back tracking).
Example: Kruskal’s minimal spanning tree. Select an edge from a sorted list, check,
decide, and never visit it again.
Application of Greedy Method:
Job sequencing with deadline
0/1 knapsack problem
Minimum cost spanning trees
Single source shortest path problem.
}
Selection Function, that selects an input from a[] and removes it. The selected input’s
value is assigned to x.
Feasible Boolean-valued function that determines whether x can be included into the
solution vector.
Union function that combines x with solution and updates the objective function.
Knapsack problem
Let there be items, to where has a value and weight . The maximum
weight that we can carry in the bag is W. It is common to assume that all values and
weights are nonnegative. To simplify the representation, we also assume that the items
are listed in increasing order of weight.
Maximize subject to
Maximize the sum of the values of the items in the knapsack so that the sum of the weights must
be less than the knapsack's capacity.
Greedy algorithm for knapsack
Algorithm GreedyKnapsack(m,n)
// p[i:n] and [1:n] contain the profits and weights respectively
// if the n-objects ordered such that p[i]/w[i]>=p[i+1]/w[i+1], m size of knapsack
and x[1:n] the solution vector
{
For i:=1 to n do x[i]:=0.0
U:=m;
For i:=1 to n do
{
if(w[i]>U) then break;
x[i]:=1.0;
U:=U-w[i];
}
If(i<=n) then x[i]:=U/w[i];
}
(1, 2/15, 0) 2 2
18x1+ x15 = 20 25x1+ x 24 = 28.2
15 15
(0, 2/3, 1) 2 2
x15+10x1= 20 x 24 +15x1 = 31
3 3
(0, 1, ½ ) 1 1
1x15+ x10 = 20 1x24+ x15 = 31.5
2 2
Analysis: - If we do not consider the time considered for sorting the inputs then
all of the three greedy strategies complexity will be O(n).
In the example solution ‘3’ is the optimal. In this solution only jobs 1&4 are
processed and the value is 127. These jobs must be processed in the order j4
followed by j1. the process of job 4 begins at time 0 and ends at time 1. And the
processing of job 1 begins at time 1 and ends at time2. Therefore both the jobs are
completed within their deadlines. The optimization measure for determining the
next job to be selected in to the solution is according to the profit. The next job to
include is that which increases ∑pi the most, subject to the constraint that the
resulting “j” is the feasible solution. Therefore the greedy strategy is to consider
the jobs in decreasing order of profits.
The greedy algorithm is used to obtain an optimal solution.
We must formulate an optimization measure to determine how the next job is
chosen.
algorithm js(d, j, n)
//d dead line, jsubset of jobs ,n total number of jobs
// d[i]≥1 1 ≤ i ≤ n are the dead lines,
// the jobs are ordered such that p[1]≥p[2]≥---≥p[n]
//j[i] is the ith job in the optimal solution 1 ≤ i ≤ k, k subset range
{
d[0]=j[0]=0;
j[1]=1;
k=1;
for i=2 to n do{
r=k;
while((d[j[r]]>d[i]) and [d[j[r]]≠r)) do
r=r-1;
if((d[j[r]]≤d[i]) and (d[i]> r)) then
{
for q:=k to (r+1) setp-1 do j[q+1]= j[q];
j[r+1]=i;
k=k+1;
}
}
return k;
}
Note: The size of sub set j must be less than equal to maximum deadline in given list.
Graphs can be used to represent the highway structure of a state or country with
vertices representing cities and edges representing sections of highway.
The edges have assigned weights which may be either the distance between the 2
cities connected by the edge or the average time to drive along that section of
highway.
For example if A motorist wishing to drive from city A to B then we must answer
the following questions
o Is there a path from A to B
o If there is more than one path from A to B which is the shortest path
The length of a path is defined to be the sum of the weights of the edges on that
path.
Given a directed graph G(V,E) with weight edge w(u,v). e have to find a shortest path
from source vertex S∈v to every other vertex v1∈ v-s.
To find SSSP for directed graphs G(V,E) there are two different algorithms.
Bellman-Ford Algorithm
Dijkstra’s algorithm
Bellman-Ford Algorithm:- allow –ve weight edges in input graph. This algorithm
either finds a shortest path form source vertex S∈V to other vertex v∈V or detect
a –ve weight cycles in G, hence no solution. If there is no negative weight cycles
are reachable form source vertex S∈V to every other vertex v∈V
Dijkstra’s algorithm:- allows only +ve weight edges in the input graph and finds a
shortest path from source vertex S∈V to every other vertex v∈V.
Consider the above directed graph, if node 1 is the source vertex, then shortest
path from 1 to 2 is 1,4,5,2. The length is 10+15+20=45.
As an optimization measure we can use the sum of the lengths of all paths so far
generated.
If we have already constructed ‘i’ shortest paths, then using this optimization
measure, the next path to be constructed should be the next shortest minimum
length path.
The greedy way to generate the shortest paths from Vo to the remaining vertices
is to generate these paths in non-decreasing order of path length.
For this 1st, a shortest path of the nearest vertex is generated. Then a shortest path
to the 2nd nearest vertex is generated and so on.
SPANNING TREE: - A Sub graph ‘n’ of o graph ‘G’ is called as a spanning tree if
(i) It includes all the vertices of ‘G’
(ii) It is a tree
Minimum cost spanning tree: For a given graph ‘G’ there can be more than one
spanning tree. If weights are assigned to the edges of ‘G’ then the spanning tree which
has the minimum cost of edges is called as minimal spanning tree.
The greedy method suggests that a minimum cost spanning tree can be obtained by
contacting the tree edge by edge. The next edge to be included in the tree is the edge that
results in a minimum increase in the some of the costs of the edges included so far.
There are two basic algorithms for finding minimum-cost spanning trees, and both are
greedy algorithms
Prim’s Algorithm
Kruskal’s Algorithm
Prim’s Algorithm: Start with any one node in the spanning tree, and repeatedly add the
cheapest edge, and the node it leads to, for which the node is not already in the spanning
tree.
PRIM’S ALGORITHM: -
i) Select an edge with minimum cost and include in to the spanning tree.
ii) Among all the edges which are adjacent with the selected edge, select the one
with minimum cost.
iii) Repeat step 2 until ‘n’ vertices and (n-1) edges are been included. And the sub
graph obtained does not contain any cycles.
Notes: - At every state a decision is made about an edge of minimum cost to be included
into the spanning tree. From the edges which are adjacent to the last edge included in the
spanning tree i.e. at every stage the sub-graph obtained is a tree.
let j be an index such that near (i) 0 & cost (j, near (i)) is minimum;
t (i,1): = j + (i,2): = near (j);
min cost: = Min cost + cost (j, near (j));
near (j): = 0;
for k:=1 to n do // update near ()
if ((near (k) 0) and (cost {k, near (k)) > cost (k,j)))
then near Z(k): = ji
}
return mincost;
}
The algorithm takes four arguments E: set of edges, cost is nxn adjacency matrix cost of
(i,j)= +ve integer, if an edge exists between i&j otherwise infinity. ‘n’ is no/: of vertices.
‘t’ is a (n-1):2matrix which consists of the edges of spanning tree.
E = { (1,2), (1,6), (2,3), (3,4), (4,5), (4,7), (5,6), (5,7), (2,7) }
n = {1,2,3,4,5,6,7)
i) The algorithm will start with a tree that includes only minimum cost edge
of G. Then edges are added to this tree one by one.
ii) The next edge (i,j) to be added is such that i is a vertex which is already
included in the treed and j is a vertex not yet included in the tree and cost
of i,j is minimum among all edges adjacent to ‘i’.
iii) With each vertex ‘j’ next yet included in the tree, we assign a value near
‘j’. The value near ‘j’ represents a vertex in the tree such that cost (j, near
(j)) is minimum among all choices for near (j)
iv) We define near (j):= 0 for all the vertices ‘j’ that are already in the tree.
v) The next edge to include is defined by the vertex ‘j’ such that (near (j)) 0
and cost of (j, near (j)) is minimum.
Analysis: -
The time required by the prince algorithm is directly proportional to the no/: of vertices.
If a graph ‘G’ has ‘n’ vertices then the time required by prim’s algorithm is 0(n2)
Kruskal’s Algorithm: Start with no nodes or edges in the spanning tree, and
repeatedly add the cheapest edge that does not create a cycle.
In Kruskals algorithm for determining the spanning tree we arrange the edges in the
increasing order of cost.
i) All the edges are considered one by one in that order and deleted from the
graph and are included in to the spanning tree.
ii) At every stage an edge is included; the sub-graph at a stage need not be a tree.
Infect it is a forest.
iii) At the end if we include ‘n’ vertices and n-1 edges without forming cycles
then we get a single connected component without any cycles i.e. a tree with
minimum cost.
At every stage, as we include an edge in to the spanning tree, we get disconnected trees
represented by various sets. While including an edge in to the spanning tree we need to
check it does not form cycle. Inclusion of an edge (i,j) will form a cycle if i,j both are in
same set. Otherwise the edge can be included into the spanning tree.
Kruskal minimum spanning tree algorithm
Algorithm Kruskal (E, cost, n,t)
//E is the set of edges in G. ‘G’ has ‘n’ vertices
//Cost {u,v} is the cost of edge (u,v) t is the set
//of edges in the minimum cost spanning tree
//The final cost is returned
{ construct a heap out of the edge costs using heapify;
for i:= 1 to n do parent (i):= -1 // place in different sets
//each vertex is in different set {1} {1} {3}
Consider the above graph of , Using Kruskal's method the edges of this graph are
considered for inclusion in the minimum cost spanning tree in the order (1, 2), (3, 6), (4,
6), (2, 6), (1, 4), (3, 5), (2, 5), (1, 5), (2, 3), and (5, 6). This corresponds to the cost
sequence 10, 15, 20, 25, 30, 35, 40, 45, 50, 55. The first four edges are included in T. The
next edge to be considered is (I, 4). This edge connects two vertices already connected in
T and so it is rejected. Next, the edge (3, 5) is selected and that completes the spanning
tree.
Analysis: - If the no/: of edges in the graph is given by /E/ then the time for Kruskals
algorithm is given by 0 (|E| log |E|).
UNIT-IV
DYNAMIC PROGRAMMING
Recalling Divide-and-Conquer
1. Partition the problem into particular subproblems.
2. Solve the subproblems.
3. Combine the solutions to solve the original one.
Let the vertex ‘s’ is the source, and ‘t’ the sink. Let c (i, j) be the cost of edge
<i, j>. The cost of a path from ‘s’ to ‘t’ is the sum of the costs of the edges
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. Because of the
constraints on E, 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 eventually terminates in stage
k.
ALGORITHM:
Algorithm Fgraph (G, k, n, p)
// The input is a k-stage graph G = (V, E) with
n vertices // indexed in order or 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.
{ co
s
t
[
n
]
:
=
0
.
0
;
[
j
,
r
]
c
o
s
t
[
r
]
;
[
j
]
:
=
r
:
}
p [1] := 1; p [k] := n; // Find a minimum cost path. for j := 2
to k - 1 do p [j] := d [p [j - 1]];}
The multistage graph problem can also be solved using the backward
approach. Let bp(i, j) be a minimum cost path from vertex s to j vertex in Vi.
Let Bcost(i, j) be the cost of bp(i,
j). From the backward approach we obtain:
Complexity Analysis:
The complexity analysis of the algorithm is fairly straightforward. Here, if G
has ~E~ edges, then the time for the first for loop is CJ ( V~ +~E ).
EXAMPLE 1:
Find the minimum cost path from s to t in the multistage graph of five stages
shown below. Do this first using forward approach and then using backward
approach.
FORWARD APPROACH:
We use the following equation to find the minimum cost path
from s to t: cost (i, j) = min {c (j, l) + cost (i + 1, l)}
l c Vi + 1
<j, l> c E
cost (1, 1) = min {c (1, 2) + cost (2, 2), c (1, 3) + cost (2, 3), c (1, 4) + cost
(2, 4), c (1, 5) + cost (2, 5)}
= min {9 + cost (2, 2), 7 + cost (2, 3), 3 + cost (2, 4), 2 + cost (2, 5)}
Now first starting with,
cost (2, 2) = min{c (2, 6) + cost (3, 6), c (2, 7) + cost (3, 7), c (2, 8) + cost (3,
8)} = min {4 + cost (3, 6), 2 + cost (3, 7), 1 + cost (3, 8)}
cost (4, 9) = min {c (9, 12) + cost (5, 12)} = min {4 + 0) = 4 cost (4,
2 4 6
2 6 __________ 9
9 1 2 5 4
4
7 3 7 2
7 ___________ 1 0 _____ 1 2
t
s 1 3 3
2 4 11
5
5
11 8 1 1
6
5 8
cost (3, 7) = min {c (7, 9) + cost (4, 9) , c (7, 10) + cost (4, 10)}
= min {4 + cost (4, 9), 3 + cost (4, 10)}
cost (4, 9) = min {c (9, 12) + cost (5, 12)} = min {4 + 0} = 4 Cost (4, 10)
The path is 1 2 7 10 12
or
1 3 6 10 12
cost (3, 8) = min {c (8, 10) + cost (4, 10), c (8, 11) +
cost (4, 11)} = min {5 + cost (4, 10), 6 + cost (4 +
11)}
Therefore, cost (2, 3) = min {c (3, 6) + cost (3, 6), c (3, 7) + cost (3, 7)}
= min {2 + cost (3, 6), 7 + cost (3, 7)}
= min {2 + 7, 7 + 5} = min {9, 12} = 9
l c vi – 1
<l, j> c E
Bcost (5, 12) = min {Bcost (4, 9) + c (9, 12), Bcost (4, 10) +
c (10, 12), Bcost (4, 11) + c (11, 12)}
= min {Bcost (4, 9) + 4, Bcost (4, 10) + 2, Bcost (4, 11) + 5}
Bcost (3, 7) = min {Bcost (2, 2) + c (2, 7), Bcost (2, 3) + c (3, 7),
Bcost (2, 5) + c (5, 7)}
Bcost (2, 5) = min {Bcost (1, 1) + c (1, 5)} = 2
Bcost (3, 7) = min {9 + 2, 7 + 7, 2 + 11} = min {11, 14, 13} = 11 Bcost (4,
9) = min {9
Bcost (4, 10) = min {Bcost (3, 6) + c (6, 10), Bcost (3, 7)
+ c (7, 10), Bcost (3, 8) + c (8,
10)}
Bcost (3, 8) = min {Bcost (2, 2) + c (2, 8), Bcost (2, 4) + c (4,
8),
Bcost (2, 5) + c (5, 8)}
Bcost (2, 4) = min {Bcost (1, 1) + c (1, 4)} = 3
Bcost (3, 8) = min {9 + 1, 3 + 11, 2 + 8} = min {10, 14, 10} = 10 Bcost (4,
10) = min {9
+ 5, 11 + 3, 10 + 5} = min {14, 14, 15) = 14
Bcost (4, 11) = min {Bcost (3, 8) + c (8, 11)} = min {Bcost (3, 8) + 6} = min
{10 + 6} =
16 Bcost (5, 12) = min {15 + 4, 14 + 2, 16 + 5} = min {19, 16,
Find the minimum cost path from s to t in the multistage graph of five
stages shown below. Do this first using forward approach and then using
backward approach.
3 4 1
2 4 7
7
5 6
3 6
s1 5 2 9 t
3
8
6
8 6 2
2 5
3
SOLUTION:
FORWARD APPROACH:
cost (i, J) = min {c (j, l) + cost (i + 1, l)}
l c Vi + 1
<J, l> EE
+ cost (3, 4), c (3, 5) + cost (3, 5), c (3, 6) + cost (3,6)} cost (3, 5)
= min {c (5, 7) + cost (4, 7), c (5, 8) + cost (4, 8)}= min {6 + 7, 2
+ 3} = 5
104
Therefore, cost (2, 3) = min {13, 10, 13} = 10
cost (1, 1) = min {5 + 8, 2 + 10} = min {13, 12} = 12
BACKWARD APPROACH:
Bcost (i, J) = min {Bcost (i
– 1, l) = c (l,
J)} l E vi – 1
<l ,j>E E
Bcost (5, 9) = min {Bcost (4, 7) + c (7, 9), Bcost (4, 8) + c (8, 9)}
= min {Bcost (4, 7) + 7, Bcost (4, 8) + 3}
Bcost (4, 7) = min {Bcost (3, 4) + c (4, 7), Bcost (3, 5) + c (5, 7),
Bcost (3, 6) + c (6, 7)}
= min {Bcost (3, 4) + 1, Bcost (3, 5) + 6, Bcost (3, 6) + 6}
Bcost (3, 4) = min {Bcost (2, 2) + c (2, 4), Bcost (2, 3) + c (3, 4)}
= min {Bcost (2, 2) + 3, Bcost (2, 3) + 6}
Bcost (2, 2) = min {Bcost (1, 1) + c (1, 2)} = min {0
min {0 + 2} = 2
Bcost (4, 8) = min {Bcost (3, 4) + c (4, 8), Bcost (3, 5) + c (5, 8), Bcost
(3, 6) + c (6, 8)}
= min {8 + 4, 7 + 2, 10 + 2} = 9
In the all pairs shortest path problem, we are to find a shortest path
between every pair of vertices in a directed graph G. That is, for every
pair of vertices (i, j), we are to find a shortest path from i to j as well as
one from j to i. These two paths are the same when G is undirected.
When no edge has a negative length, the all-pairs shortest path problem
may be solved by using Dijkstra’s greedy single source algorithm n times,
once with each of the n vertices as the source vertex.
The all pairs shortest path problem is to determine a matrix A such that A
(i, j) is the length of a shortest path from i to j. The matrix A can be
obtained by solving n single-source
105
problems using the algorithm shortest Paths. Since each application of
this procedure requires O (n2) time, the matrix A can be obtained in O (n3)
time.
Example 1:
6
1 2
4
0
~r 0 4 112 ~
3 1 1 2
~L3 ~
General formula: min {Ak-1 (i, k) + Ak-1 (k, j)}, c (i, j)}
1<k<n
Solve the problem for different values of k = 1,
A1 (1, 1) = min {(Ao (1, 1) + Ao (1, 1)), c (1, 1)} = min {0 + 0, 0} = 0 A1 (1,
2) = min {(Ao (1, 1) + Ao (1, 2)), c (1, 2)} = min {(0 + 4), 4} = 4
A1 (1, 3) = min {(Ao (1, 1) + Ao (1, 3)), c (1, 3)} = min {(0 + 11), 11} = 11 A1
(2, 1) = min {(Ao (2, 1) + Ao (1, 1)), c (2, 1)} = min {(6 + 0), 6} = 6
A1 (2, 2) = min {(Ao (2, 1) + Ao (1, 2)), c (2, 2)} = min {(6 + 4), 0)} = 0 A1
(2, 3) = min {(Ao (2, 1) + Ao (1, 3)), c (2, 3)} = min {(6 + 11), 2} = 2 A1 (3,
1) = min {(Ao (3, 1) + Ao (1, 1)), c (3, 1)} = min {(3 + 0), 3} = 3 A1 (3, 2) =
min
{(Ao (3, 1) + Ao (1, 2)), c (3, 2)} = min {(3 + 4), oc} = 7 A1 (3, 3) = min
{(Ao (3, 1) + Ao (1, 3)), c (3, 3)} = min {(3 + 11), 0} = 0
A(1) = ~0 4
~ 0 11
~6 ~
7
~L3 2~
0 ~U
A2 (1,
1) = min {(A1 (1, 2) + A1 (2, 1), c (1, 1)} = min {(4 + 6), 0} + A1 = 0
~6 0 ~
~L3 0 ~~
7
A3 (1, 1) = min {A2 (1, 3) + A2 (3, 1), c (1, 1)} = min {(6 + 3), 0} = 0
A3 (1, 2) = min {A2 (1, 3) + A2 (3, 2), c (1, 2)} = min {(6 + 7), 4} = 4
A3 (1, 3) = min {A2 (1, 3) + A2 (3, 3), c (1, A3 3)} = min {(6 + 0), 6} = 6
1)} = min {(2 + 3), 6} = 5
(2, 1) = min {A2 (2, 3) + A2 (3, 1), c (2,
2)} = min {(2 + 7), 0} = 0
A3 (2, 2) = min {A2 (2, 3) + A2 (3, 2), c (2,
3)} = min {(2 + 0), 2} = 2
A3 (2, 3) = min {A2 (2, 3) + A2 (3, 3), c (2, A3
1)} = min {(0 + 3), 3} = 3
(3, 1) = min {A2 (3, 3) + A2 (3, 1), c (3, 2)} = min {(0 + 7), 7} = 7
A3 (3, 2) = min {A2 (3, 3) + A2 (3, 2), c (3,
107
A3 (3, 3) = min {A2 (3, 3) + A2 (3, 3), c (3, 3)} = min {(0 + 0), 0} = 0
A(3) = ~ 0 4 6~
0 ~
~~ 7 2~
5~~ 3 0
~]
TRAVELLING SALESPERSON PROBLEM
Let G = (V, E) be a directed graph with edge costs Cij. The variable cij is defined such
that cij > 0 for all I and j and cij = a if < i, j> o E. Let |V| = n and assume n > 1. A tour
of G is a directed simple cycle that includes every vertex in V. The cost of a tour is the
sum of the cost of the edges on the tour. The traveling sales person problem is to find a
tour of minimum cost. The tour is to be a simple path that starts and ends at vertex 1.
Let g (i, S) be the length of shortest path starting at vertex i, going through all vertices
in S, and terminating at vertex 1. The function g (1, V – {1}) is the length of an optimal
salesperson tour. From the principal of optimality it follows that:
g(1, V - {1 }) = 2 ~ k ~ n ~c1k ~ g ~ k, V ~ ~ 1, k ~~ -- 1
~
min
-- 2
Generalizing equation 1, we obtain (for i o S)
g ( i, S ) = min{ci j j ES
The Equation can be solved for g (1, V – 1}) if we know g (k, V – {1, k})
for all choices of k.
Complexity Analysis:
For each value of |S| there + g (
i, S
- {
j } ) } are n – 1 choices for i. The number of distinct
sets S of
~ n- 2 ~
size k not including 1 and i is I k ~ ~ . ~
~ ~
Hence, the total number of g (i, S)’s to be computed before computing g (1, V – {1}) is:
~n-2~
~ ~n ~1~ ~
~k~
k~0 ~ ~
k~0 ~ ~
This is Φ (n 2n-2), so there are exponential number of calculate. Calculating one g (i, S)
require finding the minimum of at most n quantities. Therefore, the entire algorithm is
Φ (n2 2n-2). This is better than enumerating all n! different tours to find the best one. So,
we have traded on exponential growth for a much smaller exponential growth.
The most serious drawback of this dynamic programming solution is the space needed,
which is O (n 2n). This is too large even for modest values of n.
Example 1:
For the following graph find minimum cost tour for the traveling
salesperson problem:
1 2
r0 10 15 2
10
~ ~
3 4 The cost adjacency matrix = ~
0 9 12~
5 13 0 ~ ~6 8 9 0 1]
~
g (2, T) = C21
= 5 g (3, T) = C31 = 6
g (4, ~) = C41 = 8
Using equation – (2)
we obtain:
g (1, {2, 3, 4}) = min {c12 + g (2, {3,
4}, c13 + g (3, {2, 4}), c14 + g (4, {2, 3})}
g (2, {3, 4}) = min {c23 + g (3, {4}), c24 + g (4, {3})}
= min {9 + g (3, {4}), 10 + g (4, {3})} g (3,
{4}) = min {c34 + g (4, T)} = 12 + 8 = 20 g
(4, {3}) = min {c43 + g (3, ~)} = 9 + 6 = 15
Therefore, g (3, {2, 4}) = min {13 + 18, 12 + 13} = min {41,
25} = 25 g (4, {2, 3}) = min {c42 + g (2, {3}), c43 + g (3,
{2})}
g (2, {3}) = min {c23 + g (3, ~} = 9 + 6
= 15 g (3, {2}) = min {c32 + g (2, T}
= 13 + 5 = 18
Therefore, g (4, {2, 3}) = min {8 + 15, 9 + 18} = min {23, 27} = 23
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, 40, 43} = 35
Let P (i) be the probability with which we shall be searching for 'ai'. Let Q (i) be
the probability of an un-successful search. Every internal node represents a point
where a successful search may terminate. Every external node represents a point
where an unsuccessful search may terminate.
The expected cost contribution for the internal node for 'ai' is:
110
The expected cost of binary
search tree is: n n
~ P(i) * level (ai) + ~ Q (i) * level ((Ei ) - 1)
~ (nm
- m2 ) = O (n3
) 1 < m
Tree 1 <n
(a1, a2, a3) = (do, if, stop) are as follows. Given the equal
probabilities p (i) = Q (i) = 1/7 for all i, we have:
st o p
if
do
Tree 2
do
if
st o p
Tree 3
( 1 x 1 + 1 x 2 + 1 x 3~
Cost (tree # 1) = ~ +
~7 7 7 )
1 + 2 + 3 1 + 2 + 3 + 3 6 + 9 15
~
Cost (tree # 3) = ~ ~ 1 7 x 1 + 17 x 2 + 1 7x 3~ ~ +) (~~ 1 7 x 1 + 17 x 2 +
17 x 3 + 1 7x 3~ ~)
+
= 1 +7 2 + 3 + 1 + 2 +7 3 + 3 ~ 6 7( 19 x ~1 15 +7 1
7 x 2 + 17 x 3 + 1 7x 3~ ~)
= 1 + 7 2 + 3 -I- ~6 +7 9 ~15
= 7 + 7 ~ 7 ~7
( 1x1 +1 1 x 2~ ( 1 x 2 + 1 x 2 + 1 x 2 + 1 x 2~
Cost (tree # 2) = ~~ 7 7x2+7) + ~~ 7 7 7 7)
= 1 + 2 + 2 2 + 2 + 2 + 2 5 + 8 13
7 +7 ~ 7~7
Huffman coding tree solved by a greedy algorithm has a limitation of having the
data only at the leaves and it must not preserve the property that all nodes to the
left of the root have keys, which are less etc. Construction of an optimal binary
search tree is harder, because the data is not constrained to appear only at the
leaves, and also because the tree must satisfy the binary search tree property and
it must preserve the property that all nodes to the left of the root have keys, which
are less.
A dynamic programming solution to the problem of obtaining an optimal binary
search tree can be viewed by constructing a tree as a result of sequence of
decisions by holding the principle of optimality. A possible approach to this is to
make a decision as which of the ai's be arraigned to the root node at 'T'. If we
choose 'ak' then is clear that the internal nodes for a1, a2, . . . . . ak-1 as well as
the external nodes for the classes Eo, E1, . . . . . . . Ek-1 will lie in the left sub
tree, L, of the root. The remaining nodes will be in the right subtree, ft. The
structure of an optimal binary search tree is:
ak
L ft
K K
~
i ~ 1 i ~ 0
n n
~
i ~ K i ~ K
Equation (1) may be solved for C (0, n) by first computing all C (i, J) such that J - i =
1 Next, we can compute all C (i, J) such that J - i = 2, Then all C (i, J) with J - i = 3
and so on.
C (i, J) is the cost of the optimal binary search tree 'Tij' during computation we record
the root R (i, J) of each tree 'Tij'. Then an optimal binary search tree may be
constructed from these R (i, J). R (i, J) is the value of 'K' that minimizes equation (1).
We solve the problem by knowing W (i, i+1), C (i, i+1) and R (i, i+1), 0
≤ i < 4;
Knowing W (i, i+2), C (i, i+2) and R (i, i+2), 0 ≤ i < 3 and repeating until W (0, n), C
(0, n) and R (0, n) are obtained.
Example 1:
Let n = 4, and (a1, a2, a3, a4) = (do, if, need, while) Let P (1: 4) = (3, 3, 1, 1) and Q (0:
4) = (2, 3, 1, 1, 1)
Solution:
Table for recording W (i, j), C (i, j) and R (i, j):
Column 0 1 2 3 4
Row
0 2, 0, 0 3, 0, 0 1, 0, 0 1, 0, 0, 1, 0, 0
1 8, 8, 1 7, 7, 2 3, 3, 3 3, 3, 4
2 12, 19, 1 9, 12, 2 5, 8, 3
3 14, 25, 2 11, 19, 2
4 16, 32, 2
This computation is carried out row-wise from row 0 to row 4. Initially, W (i, i) = Q (i)
and C (i, i) = 0 and R (i, i) = 0, 0 < i < 4.
Solving for C (0, n):
First, computing all C (i, j) such that j - i = 1; j = i + 1 and as 0 < i < 4; i = 0, 1, 2 and
3; i < k ≤ J. Start with i = 0; so j = 1; as i < k ≤ j, so the possible value for k = 1
C (0, 2) = W (0, 2) + min {(C (0, 0) + C (1, 2)), (C (0, 1) + C (2, 2))} = 12
+ min {(0 + 7, 8 + 0)} = 19
ft (0, 2) = 1
Next, with i = 1; so j = 3; as i < k ≤ j, so the possible value for k = 2 and 3.
W (1, 3) = P (3) + Q (3) + W (1, 2) = 1 + 1+ 7 = 9
C (1, 3) = W (1, 3) + min {[C (1, 1) + C (2, 3)], [C (1, 3) 2) + C (3, 3)]}
= W (1, + min {(0 + 3), (7 + 0)} = 9 + 3 = 12
ft (1, 3) = 2
Fourth, Computing all C (i, j) such that j - i = 4; j = i + 4 and as 0 < i < 1; i = 0; i < k ≤
J.
Hence the left sub tree is 'T01' and right sub tree is T24. The root of 'T01' is 'a1' and the
root of 'T24' is a3.
The left and right sub trees for 'T01' are 'T00' and 'T11' respectively. The root of T01 is
'a1'
The left and right sub trees for T24 are T22 and T34 respectively.
a1 a3
T 01 T 24 do read
a4
T 00 T 11 T 22 T 34 while
Example 2:
Consider four elements a1, a2, a3 and a4 with Q0 = 1/8, Q1 = 3/16, Q2 = Q3 = Q4 =
1/16 and p1 = 1/4, p2 = 1/8, p3 = p4 =1/16. Construct an optimal binary search tree.
Solving for C (0, n):
First, computing all C (i, j) such that j - i = 1; j = i + 1 and as 0 < i < 4; i = 0, 1, 2 and
3; i
< k ≤ J. Start with i = 0; so j = 1; as i < k ≤ j, so the possible value for k = 1
+1=3
W (2, 3) = P (3) + Q (3) + W (2, 2) = 1 + 1 3)} = 3 + [(0 + 0)] = 3 C (2,
3) = W (2, 3) + min {C (2, 2) + C (3, ft (2, 3) = 3
Next with i = 3; so j = 4; as i < k ≤ j, so the possible value for k = 4
W (3, 4) = P (4) + Q (4) + W (3, 3) =1+1+1=3
C (3, 4) = W (3, 4) + min {[C (3, 3) + C (4, 4)]} = 3 + [(0 + 0)] = 3 ft
(3, 4) = 4
Third, Computing all C (i, j) such that J - i = 3; j = i + 3 and as 0 < i < 2; i = 0, 1; i < k
≤ J. Start with i = 0; so j = 3; as i < k ≤ j, so the possible values for k = 1, 2 and 3.
Fourth, Computing all C (i, j) such that J - i = 4; j = i + 4 and as 0 < i < 1; i = 0; i <
k ≤ J. Start with i = 0; so j = 4; as i < k ≤ j, so the possible values for k = 1, 2, 3 and
4.
W (0, 4) = P (4) + Q (4) + W (0, 3) = 1 + 1 + 14 = 16
C (0, 4) = W (0, 4) + min {[C (0, 0) + C (1, 4)], [C (0, 1) + C (2, 4)],
[C (0, 2) + C (3, 4)], [C (0, 3) + C (4, 4)]}
4 16, 33, 2
From the table we see that C (0, 4) = 33 is the minimum cost of a binary search tree for
(a1, a2, a3, a4)
The root of the tree 'T04' is 'a2'.
Hence the left sub tree is 'T01' and right sub tree is T24. The root of 'T01' is 'a1' and the
root of 'T24' is a3.
The left and right sub trees for 'T01' are 'T00' and 'T11' respectively. The root of T01 is
'a1'
The left and right sub trees for T24 are T22 and T34 respectively.
a1 a3
T 01 T 24 a1 a3
a4
T 00 T 11 T 22 T 34 a4
0/1 – KNAPSACK
We are given n objects and a knapsack. Each object i has a positive weight wi and a
positive value Vi. The knapsack can carry a weight not exceeding W. Fill the knapsack
so that the value of objects in the knapsack is optimized.
A solution to the knapsack problem can be obtained by making a sequence of decisions
on the variables x1, x2, . . . . , xn. A decision on variable xi involves determining which
of the values 0 or 1 is to be assigned to it. Let us assume that
decisions on the xi are made in the order xn, xn-1, . . . .x1. Following a decision on xn,
we may be in one of two possible states: the capacity remaining in m – wn and a profit of
pn has accrued. It is clear that the remaining decisions xn-1, . . . , x1 must be optimal
with respect to the problem state resulting from the decision on xn. Otherwise, xn,. . . . ,
x1 will not be optimal. Hence, the principal of optimality holds.
Fn (m) = max {fn-1 (m), fn-1 (m - wn) + pn} -- 1
Equation-2 can be solved for fn (m) by beginning with the knowledge fo (y) = 0 for all y
and fi (y) = - ~, y < 0. Then f1, f2, . . . fn can be successively computed using equation–2.
When the wi’s are integer, we need to compute fi (y) for integer y, 0 < y < m. Since fi
(y) = - ~ for y < 0, these function values need not be computed explicitly. Since each fi
can be computed from fi - 1 in Θ (m) time, it takes Θ (m n) time to compute fn. When
the wi’s are real numbers, fi (y) is needed for real numbers y such that 0 < y < m. So, fi
cannot be explicitly computed for all y in this range. Even when the wi’s are integer, the
explicit Θ (m n) computation of fn may not be the most efficient computation. So, we
explore an alternative method for both cases.
The fi (y) is an ascending step function; i.e., there are a finite number of y’s, 0 = y1 < y2
< . . . . < yk, such that fi (y1) < fi (y2) < . . . . . < fi (yk); fi (y) = - ~ , y < y1; fi (y) = f
(yk), y > yk; and fi (y) = fi (yj), yj < y < yj+1. So, we need to compute only fi (yj), 1 < j
< k. We use the ordered set Si = {(f (yj), yj) | 1 < j < k} to represent fi (y). Each number
of Si is a pair (P, W), where P = fi (yj) and W = yj. Notice that S0 = {(0, 0)}. We can
compute Si+1 from Si by first computing:
Now, Si+1 can be computed by merging the pairs in Si and Si 1 together. Note that if Si+1
contains two pairs (Pj, Wj) and (Pk, Wk) with the property that Pj < Pk and Wj > Wk,
then the pair (Pj, Wj) can be discarded because of equation-2. Discarding or purging rules
such as this one are also known as dominance rules. Dominated tuples get purged. In the
above, (Pk, Wk) dominates (Pj, Wj).
Reliability Design
The problem is to design a system that is composed of several devices connected in
series. Let ri be the reliability of device Di (that is ri is the probability that device i will
function properly) then the reliability of the entire system is fT ri. Even if the individual
devices are very reliable (the ri’s are very close to one), the reliability of the system may
not be very good. For example, if n = 10 and ri = 0.99, i < i < 10, then fT ri = .904.
Dept. of CSE, SWEC, Hyderabad Page 107
WWW.AKTutor.in
Design and Analysis of Algorithms
Hence, it is desirable to duplicate devices. Multiply copies of the same device type are
connected in parallel.
If stage i contains mi copies of device Di. Then the probability that all mi have a
malfunction is (1 - ri) mi. Hence the reliability of stage i becomes 1 – (1 - r )mi.
i
Assume each Ci > 0, each mi must be in the range 1 < mi < ui, where
+Ci
~~ n ~
~ ~
ui ~ ~ ~C C ~ Ci ~
IL k ~ ~J U
1 ~
The upper bound ui follows from the observation that mj > 1
An optimal solution m1, m2 . . . . . mn is the
result of a sequence of decisions, one
decision for each mi.
~ q$ mJ
( )
1<j< i
Let fi (x) represent the maximum value of
Subject to the constrains:
The last decision made requires one to choose mn from {1, 2, 3, . . . . . un}
Once a value of mn has been chosen, the remaining decisions must be such as to use the
remaining funds C – Cn mn in an optimal way.
The principle of optimality holds on
fn ~C ~ ~max { On (mn ) fn _ 1 (C - Cn
mn ) } 1 < mn < un
There is atmost one tuple for each different ‘x’, that result from a sequence of decisions
on m1, m2, . . . . mn. The dominance rule (f1, x1) dominate (f2, x2) if f1 ≥ f2 and x1 ≤
x2. Hence, dominated tuples can be discarded from Si.
Example 1:
Design a three stage system with device types D1, D2 and D3. The costs are $30, $15
and $20 respectively. The Cost of the system is to be no more than $105. The
reliability of each device is 0.9, 0.8 and 0.5 respectively.
Solution:
We assume that if if stage I has mi devices of type i in parallel, then 0 i (mi) =1 – (1- ri)mi
Since, we can assume each ci > 0, each mi must be in the range 1 ≤ mi ≤ ui. Where:
~~ n
~
~ ui = ~ IC + Ci -C
~J Ci ~
~
IL k ~ ~~
1 ~
Using the above equation compute u1, u2 and u3.
105+ 30- (30+15 + 20) 70
u1 = 30 = 30 = 2
u3 = (30+15 + 20) 60 = 3
105+ 20-
= 20 20
S1 = depends on u1 value, as u1 = 2, so
S1 = {S1, S1 }
1 2
S2 = depends on u2 value, as u2 = 3, so
S2 = {S2 , S2 , S2 }
1 2 3
S3 = depends on u3 value, as u3 = 3, so
S3 = {S3 , S3 , S3 }
1 2 3
Now find , 1 S (x), ~~x
Dept. of CSE, SWEC, Hyderabad Page 110
WWW.AKTutor.in
Design and Analysis of Algorithms
= 0.9
~~1~ ~ 1~ ~1 ~ r ~m 1 = 1 – (1 – 0.9)1
11 1 ~(2) = 1- (1- 0.9) 2 = 0.99
1 = 10.99 , 30 + 30 } = ( 0.99, 60
Therefore, S1 = {(0.9, 30), (0.99, 60)}
( )}
~2 ~ 2~ ~ 1 ~ ~1 ~ 0.8~ 2 = 0.96
02 ( 3) = 1 - (1 - 0.8) 3 = 0.992
1 2 3
By applying Dominance rule to S2:
If Si contains two pairs (f1, x1) and (f2, x2) with the property that f1 ≥ f2 and x1 ≤ x2,
then (f1, x1) dominates (f2, x2), hence by dominance rule (f2, x2) can be discarded.
Discarding or pruning rules such as the one above is known as dominance rule.
Dominating tuples will be present in Si and Dominated tuples has to be discarded from
Si.
0.875 (0.8928), 75 + 20 + 20 + 20
S3
3 = {(0.63, 105), (1.756, 120), (0.7812, 135)}
If cost exceeds 105, remove that tuples
The best design has a reliability of 0.648 and a cost of 100. Tracing back for the solution
through Si ‘s we can determine that m3 = 2, m2 = 2 and m1 = 1.
Other Solution:
Since, we can assume each ci > 0, each mi must be in the range 1 ≤ mi ≤ ui. Where:
~ ( n ~ ~
S2 ={(0.75 (0.72), 45 + 20 + 20), (0.75 (0.864), 60 + u
20 +20),
i
= ~ iC + Ci _ ~CJ r / Ci I
~~ i ~~ ~~
+ 20
=
30
105 15 30 + 55 ~3
u2 = 1515 20 ~
15
30
105 15 =3
u3 = 20 20 6020
(85)
f2 = max {2(1).f1(85 - 15), ~2(2).f1(85 - 15x2), ~2(3).f1(85 - 15x3)}
=
max {0.8 f1(70), 0.96 f1(55), 0.992 f1(40)}
= max {0.8 x 0.99, 0.96 x 0.9, 0.99 x 0.9} = 0.8928
(70)
f1 = max {~1(m1). f0(70 - 30m1)}
1 ! m1 ! u1
30m1)}
1 ! m1 ! u1
= max {~1(1) f0(40 - 30), t1(2) f0(40 - 30x2)}
= max {~1(1) x 1, t1(2) x -oo} = max{0.9, -oo} = 0.9
1 ! m1 ! u1
1 ! m1 ! u1
= max {~1(1).f0(35-30), ~1(2).f0(35-30x2)}
= max {~1(1) x 1, t1(2) x -oo} = max{0.9, -oo} = 0.9
f1 (20) = max {~1(m1). f0(20 - 30m1)}
1 ! m1 ! u1
= max {2(1) f1(45 - 15), ~2(2) f1(45 - 15x2), ~2(3) f1(45 - 15x3)}
= max {0.8 f1(30),
f1 (0) = -.
BACKTRACKING
(General method)
Many problems are difficult to solve algorithmically. Backtracking makes it possible to
solve at least some large instances of difficult combinatorial problems.
Suppose you have to make a series of decisions among various choices, where
You don’t have enough information to know what to choose
Each decision leads to a new set of choices.
Some sequence of choices ( more than one choices) may be a solution to your
problem.
Explicit constraints: Explicit constraints are rules that restrict each xi to take on values
only from a given set.
Example: xi ≥ 0 or si={all non negative real numbers}
Xi=0 or 1 or Si={0, 1}
li ≤ xi ≤ ui or si={a: li ≤ a ≤ ui }
The explicit constraint depends on the particular instance I of the problem being solved.
All tuples that satisfy the explicit constraints define a possible solution space for I.
Dept. of CSE, SWEC, Hyderabad Page 118
WWW.AKTutor.in
Design and Analysis of Algorithms
Implicit Constraints:
The implicit constraints are rules that determine which of the tuples in the solution space
of I satisfy the criterion function. Thus implicit constraints describe the way in which the
Xi must relate to each other.
Applications of Backtracking:
N Queens Problem
Sum of subsets problem
Graph coloring
Hamiltonian cycles.
N-Queens Problem:
It is a classic combinatorial problem. The eight queen’s puzzle is the problem of placing
eight queens puzzle is the problem of placing eight queens on an 8×8 chessboard so that
no two queens attack each other. That is so that no two of them are on the same row,
column, or diagonal.
The 8-queens puzzle is an example of the more general n-queens problem of placing n
queens on an n×n chessboard.
if(x[j]=i or Abs(x[j]-i)=Abs(j-k))) {
then return false X[k]:=I;
return true if(k==n) then write (x[1:n]);
} else NQueens(k+1, n);
}
}}
Given positive numbers wi 1 ≤ i ≤ n, & m, here sum of subsets problem is finding all
subsets of wi whose sums are m.
Definition: Given n distinct +ve numbers (usually called weights), desire (want) to find
all combinations of these numbers whose sums are m. this is called sum of subsets
problem.
To formulate this problem by using either fixed sized tuples or variable sized tuples.
Backtracking solution uses the fixed size tuple strategy.
For example:
If n=4 (w1, w2, w3, w4)=(11,13,24,7) and m=31.
Then desired subsets are (11, 13, 7) & (24, 7).
The two solutions are described by the vectors (1, 2, 4) and (3, 4).
In general all solution are k-tuples (x1, x2, x3---xk) 1 ≤ k ≤ n, different solutions may have
different sized tuples.
Explicit constraints requires xi ∈ {j / j is an integer 1 ≤ j ≤ n }
Implicit constraints requires:
No two be the same & that the sum of the corresponding w i’s be m
i.e., (1, 2, 4) & (1, 4, 2) represents the same. Another constraint is xi<xi+1 1 ≤ i ≤ k
The above equation specify that x1, x2, x3, --- xk cannot lead to an answer node if this
condition is not satisfied.
X[k]=1
If(S+w[k]=m) then write(x[1: ]); // subset found.
Else if (S+w[k] + w{k+1] ≤ M)
Then SumOfSub(S+w[k], k+1, r-w[k]);
if ((S+r - w{k] ≥ M) and (S+w[k+1] ≤M) ) then
{
X[k]=0;
SumOfSub(S, k+1, r-w[k]);
}
}
Graph Coloring:
Let G be a undirected graph and ‘m’ be a given +ve integer. The graph coloring problem
is assigning colors to the vertices of an undirected graph with the restriction that no two
adjacent vertices are assigned the same color yet only ‘m’ colors are used. The
optimization version calls for coloring a graph using the minimum number of coloring.
The decision version, known as K-coloring asks whether a graph is colourable using at
most k-colors. Note that, if ‘d’ is the degree of the given graph then it can be colored
with ‘d+1’ colors. The m- colorability optimization problem asks for the smallest integer
‘m’ for which the graph G can be colored. This integer is referred as “Chromatic
number” of the graph.
Example
A graph is said to be planar iff it can be drawn in a plane (flat) in such a way that
no two edges cross each other.
M-Colorability decision problem is the 4-color problem for planar graphs.
Given any map, can the regions be colored in such a way that no two adjacent
regions have the same color yet only 4-colors are needed?
To solve this problem, graphs are very useful, because a map can easily be
transformed into a graph.
Each region of the map becomes a node, and if two regions are adjacent, then the
corresponding nodes are joined by an edge.
o Example:
o
The above map requires 4 colors.
Many years, it was known that 5-colors were required to color this map.
After several hundred years, this problem was solved by a group of
mathematicians with the help of a computer. They show that 4-colors are
sufficient.
Suppose we represent a graph by its adjacency matrix G[1:n, 1:n]
Ex:
Adjacency matrix is
Hamiltonian Cycles:
Def: Let G=(V, E) be a connected graph with n vertices. A Hamiltonian cycle is a
round trip path along n-edges of G that visits every vertex once & returns to its
starting position.
It is also called the Hamiltonian circuit.
Hamiltonian circuit is a graph cycle (i.e., closed loop) through a graph that visits
each node exactly once.
A graph possessing a Hamiltonian cycle is said to be Hamiltonian graph.
Example:
g1
The above graph contains Hamiltonian cycle: 1,2,8,7,6,5,4,3,1
UNIT-V
BRANCH & BOUND
Branch & Bound (B & B) is general algorithm (or Systematic method) for finding optimal solution
of various optimization problems, especially in discrete and combinatorial optimization.
The B&B strategy is very similar to backtracking in that a state space tree is used to solve a
problem.
The differences are that the B&B method
Does not limit us to any particular way of traversing the tree.
It is used only for optimization problem
It is applicable to a wide variety of discrete combinatorial problem.
B&B is rather general optimization technique that applies where the greedy method & dynamic
programming fail.
It is much slower, indeed (truly), it often (rapidly) leads to exponential time complexities in the
worst case.
The term B&B 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”
Live node is a node that has been generated but whose children have not yet been generated.
E-nodeis a live node whose children are currently being explored.
Dead node is a generated node that is not to be expanded or explored any further. All
children of a dead node have already been expanded.
Two graph search strategies, BFS & D-search (DFS) in which the exploration of a new node
cannot begin until the node currently being explored is fully explored.
Both BFS & D-search (DFS) generalized to B&B strategies.
BFSlike state space search will be called FIFO (First In First Out) search as the list of live
nodes is “First-in-first-out” list (or queue).
D-search (DFS) Like state space search will be called LIFO (Last In First Out) search as the
list of live nodes is a “last-in-first-out” list (or stack).
In backtracking, bounding function are used to help avoid the generation of sub-trees that do
not contain an answer node.
We will use 3-types of search strategies in branch and bound
1) FIFO (First In First Out) search
2) LIFO (Last In First Out) search
3) LC (Least Count) search
FIFO B&B:
FIFO Branch & Bound is a BFS.
In this, children of E-Node (or Live nodes) are inserted in a queue.
Implementation of list of live nodes as a queue
Least() Removes the head of the Queue
Add() Adds the node to the end of the Queue
Assume that node ‘12’ is an answer node in FIFO search, 1 st we take E-node has ‘1’
LIFO B&B:
LIFO Brach & Bound is a D-search (or DFS).
In this children of E-node (live nodes) are inserted in a stack
Implementation of List of live nodes as a stack
Least() Removes the top of the stack
ADD()Adds the node to the top of the stack.
The search for an answer node can often be speeded by using an “intelligent” ranking function. It is
also called an approximate cost function “Ĉ”.
Expended node (E-node) is the live node with the best Ĉ value.
Branching: A set of solutions, which is represented by a node, can be partitioned into mutually
(jointly or commonly) exclusive (special) sets. Each subset in the partition is represented by a child
of the original node.
Lower bounding: An algorithm is available for calculating a lower bound on the cost of any solution
in a given subset.
Example:
8-puzzle
Def:- Find a tour of minimum cost starting from a node S going through other nodes only
once and returning to the starting point S.
B&B algorithms for this problem, the worest case complexity will not be any better than O(n22n)
but good bunding functions will enables these B&B algorithms to solve some problem instances in
much less time than required by the dynamic programming alogrithm.
State space tree for the travelling salesperson problem with n=4 and i0=i4=1
The above diagram shows tree organization of a complete graph with |V|=4.
Each leaf node ‘L’ is a solution node and represents the tour defined by the path from the root to L.
To use LCBB to search the travelling salesperson “State space tree” first define a cost function C(.)
and other 2 functions Ĉ(.) & u(.)
Such that Ĉ(r) ≤ C(r) ≤ u(r) for all nodes r.
1
Cost C(.) is the solution node with least C(.) corresponds to a shortest tour in G.
A row (column) is said to be reduced iff it contains at least one zero & remaining entries are
non negative.
A matrix is reduced iff every row & column is reduced.
𝑖𝑛𝑓 20 30 10 11
15 𝑖𝑛𝑓 16 4 2
3 5 𝑖𝑛𝑓 2 4
19 6 18 𝑖𝑛𝑓 3
[ 16 4 7 16 𝑖𝑛𝑓 ]
The TSP starts from node 1: Node 1
Reduced Matrix: To get the lower bound of the path starting at node 1
Row # 1: reduce by 10 Row #2: reduce 2 Row #3: reduce by 2
𝑖𝑛𝑓 10 20 0 𝑖𝑛𝑓
1 10 20 0 1𝑖𝑛𝑓 10 20 0 1
15 𝑖𝑛𝑓 16 4 132 𝑖𝑛𝑓 14 2 013 𝑖𝑛𝑓 14 2 0
3 5 𝑖𝑛𝑓 2 4 3 5 𝑖𝑛𝑓 2 4 1 3 𝑖𝑛𝑓 0 2
19 6 18 𝑖𝑛𝑓 193 6 18 𝑖𝑛𝑓 319 6 18 𝑖𝑛𝑓 3
[ 16 4 7 16 [𝑖𝑛𝑓
16 ] 4 7 16 𝑖𝑛𝑓
[ 16
] 4 7 16 𝑖𝑛𝑓 ]
Row # 4: Reduce by 3: Row # 5: Reduce by 4 Column 1: Reduce by 1
In summary:
So the live nodes we have so far are:
2: cost(2) = 35, path: 1->2
3: cost(3) = 53, path: 1->3
4: cost(4) = 25, path: 1->4
5: cost(5) = 31, path: 1->5
Explore the node with the lowest cost: Node 4 has a cost of 25
Vertices to be explored from node 4: 2, 3, and 5
Now we are starting from the cost matrix at node 4 is:
Xi=0 or 1 1 ≤ i ≤ n
o Every leaf node in state space tree represents an answer for which
is an answer node; other leaf nodes are infeasible
o For optimal solution, define
Define two functions ĉ(x) and u(x) such that for every node x,
ĉ(x) ≤ c(x) ≤ u(x)
Computing cˆ(·) and u(·)
UNIT- V
NP-HARD AND NP-COMPLETE PROBLEMS
Basic concepts:
NP Nondeterministic Polynomial time
The problems has best algorithms for their solutions have “Computing times”, that cluster into two
groups
Group 1 Group 2
Ex: Ex:
Ordered Search (O (log n)), Traveling Sales Person O(n2 2n)
Sorting O(n.log n)
No one has been able to develop a polynomial time algorithm for any problem in the 2nd group (i.e.,
group 2)
So, it is compulsory and finding algorithms whose computing times are greater than polynomial very
quickly because such vast amounts of time to execute that even moderate size problems cannot be
solved.
Theory of NP-Completeness:
Show that may of the problems with no polynomial time algorithms are computational time algorithms
are computationally related.
1. NP-Hard
2. NP-Complete
NP Complete Problem: A problem that is NP-Complete can solved in polynomial time if and only if
(iff) all other NP-Complete problems can also be solved in polynomial time.
NP-Hard: Problem can be solved in polynomial time then all NP-Complete problems can be solved in
polynomial time.
All NP-Complete problems are NP-Hard but some NP-Hard problems are not know to be
NPComplete.
Nondeterministic Algorithms:
Algorithms with the property that the result of every operation is uniquely defined are termed as
deterministic algorithms. Such algorithms agree with the way programs are executed on a computer.
Algorithms which contain operations whose outcomes are not uniquely defined but are limited to
specified set of possibilities. Such algorithms are called nondeterministic algorithms.
The machine executing such operations is allowed to choose any one of these outcomes subject to a
termination condition to be defined later.
P is the set of all decision problems solvable by deterministic algorithms in polynomial time.
NP is the set of all decision problems solvable by nondeterministic algorithms in polynomial time.
Since deterministic algorithms are just a special case of nondeterministic, by this we concluded that P
⊆ NP
If there any single problem in NP, such that if we showed it to be in ‘P’ then that would imply that
P=NP.
Notation of Reducibility
Let L1 and L2 be problems, Problem L1 reduces to L2 (written L1 α L2) iff there is a way to solve L1 by
a deterministic polynomial time algorithm using a deterministic algorithm that solves L 2 in polynomial
time
This implies that, if we have a polynomial time algorithm for L2, Then we can solve L1 in polynomial
time.
If the length of ‘I’ is ‘n’ and the time complexity of A is p(n) for some polynomial p()
then length of Q is O(p3(n) log n)=O(p4(n)) The time needed to construct Q is also O(p3(n)
log n).
A deterministic algorithm ‘Z’ to determine the outcome of ‘A’ on any input
‘I’
Algorithm Z computes ‘Q’ and then uses a deterministic algorithm for the satisfiability
problem to determine whether ‘Q’ is satisfiable.
If O(q(m)) is the time needed to determine whether a formula of length ‘m’ is
satisfiable then the complexity of ‘Z’ is O(p3(n) log n + q(p3(n)log n)).
If satisfiability is ‘p’, then ‘q(m)’ is a polynomial function of ‘m’ and the complexity of
‘Z’ becomes ‘O(r(n))’ for some polynomial ‘r()’.
Hence, if satisfiability is in p, then for every nondeterministic algorithm A in NP, we
can obtain a deterministic Z in p.
By this we shows that satisfiability is in p then P=NP