Академический Документы
Профессиональный Документы
Культура Документы
Unit 1
Word algorithm comes from name of Persian author Abu Jufar mohammed ibm
musa al khowarizmi(825ad)
Definition : Algorithm
An algorithm is a finite set of instructions that, if followed, accomplishes a particular
task. In addition algorithm must satisfy the following criteria:
Input: Zero or more quantities are externally supplied
Output: At least one quantity is produced.
Definiteness: Each instruction is clear and unambiguous
Finiteness: If we trace out instruction of algorithm, then for all cases the
algorithm terminates after finite number of steps.
Effectiveness: Every instruction must be very basic so that it can carried out.(in
principle by a person using only pencil & paper)
Algorithm specification:
Pseudocode convention:
1.//comments
2.{} blocks
3.identifier begins with a letter.
4.data types: not explicitly declared.
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Recursive Algorithm
Performance Analysis
1) Space complexity
2) Time complexity
3) Asymptotic Notations
4) Performance measurement
1.Space complexity
Space complexity of an algorithm is the amount of memory it needs to run to completion
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
E.g
Algorithm abc(a,b,c)
{
return a+b+b*c+(a+b-c)/(a+b) +4.0;
}
The space needed by each of these algorithm is seen to be the sum of the following
components:
1.A fixed part that is independent of the characteristics (e.g ,number , size ) of the inputs
and outputs, space for fixed size component variable, constants and so on.
2. A variable part that consist of the space needed by component variables whose size is
dependent on the particular problem instance, recursion stack space etc.
2.Time complexity
Time complexity of an algorithm is the amount of computer time it needs to run to
completion.
Program step
It is loosely defined as a syntactically meaningful segment of a program that has an
execution time that is independent of the instance characteristics.
Example 1 When the statements to increment count are introduced into a Algorithm the
result is following Algorithm. The change in the value of count by the time this program
terminates is the number of steps executed by Algorithm
It is easy to see that in the for loop, the value of count will increase by a total of 2n. If
count is zero to start with, then it will be 2n + 3 on termination. So each invocation of
Sum Algorithm executes a total of 2n + 3 steps.
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Example 2
When the statements to increment count are introduced into
Recursive algorithm then count will be calculated in following way.. Let tRsum(n) be the
increase in the value of count when Algorithm terminates. We see that tRSum(O) = 2. When
n > 0, count increases by 2 plus whatever increase results from the invocation of RSum
from within the else clause. From the definition of tRSum, it follows that this additional
increase is tRsum(n-1). So, if the value of count is zero initially, its value at the time of
termination is 2+tRsum(n-1), n>Q .
tRSum(n) = { 2 if n=0
2 + tRSum(n-1) if n>0
tRSum(n) = 2 + tRSum(n-1)
= 2 + 2 + tRSum(n-2)
= 2(2) + tRSum(n-2)
.
.
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
.
=n(2) + tRSum(0)
=2n+2, n>=0
So the step count for RSum is 2n+2
StepTable Method
Example
1 Algorithm RSum(a,n)
2{
3 if (n <= 0) then return 0.0;
4 else return RSum(a, n - 1) + a[n];
5}
Algorithm: Recursive function Rsum
3. Asymptotical Notations
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
4.Performance Measurement
Performance measurement is concerned with obtaining the space and time requirements
of a particular algorithm .These quantities depends on the compiler and options used as
well as on the computer on which the algorithm is run
Asymptotic analysis tells the behavior only for sufficiently large values on n.
for smaller values for n , the run time may not follow the asymptotic curve.
Even in the region where the asymptotic behavior is exhibited, the times may
not lie exactly on the periodic curve
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
RANDOMIZED ALGORITHMS
Informal Description:
• Los Vegas
• Monte carlo
O/p might differ from run to run for same i/p . i.e algorithm might
Give incorrect answer.
Error probability(e)
Randomized algorithms runs quickly (respectively gives the correct answer) with
probability at least 1-e
Las Vegas has a resource (time, space, etc) bound of õ (g(n)) H there exist constant c
such that the amount of resource used by the alg ( on any i/p of size n) is no more than
C α g(n) with probability >= 1-1/nα.
Stmt:
Let an array a[] of n numbers that has n/2 distinct elements & n/2 copies of another
element the problem is to identify the repeated element.
Algorithm:-
Algorithm RepeatedElement(a,n)
//finds the repeated element from a[1:n]
{
While (true) do
{
I:=Random() mod n+1;
J:=Random() mod n+1;
If ((i≠j) and a[I]=a[j]) then return I;
}
}
Runtime Calculation
1)An iteration of while loop will be successful(in identifying rep-element) if ‘i’ is any
one of n/2 array indices corresponding to the repeated element and j is any one of the
same n/2 indices other than i.
2)The probability that the algorithm quits in any given iteration of while loop is
N/2(n/2-1)
P=
N2
3)This implies that the probability that the algorithm does not quit is < 4/5
5) If 100 iterations then Probability (for algorithm does not quit) is (4/5)100 < 2.04 * 10-10
This is almost certainly algorithm will quit in 100 iterations.
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
6) In general the algorithm the algorithm doesn’t quit in the first c α logn iteration is
<(4/5) c α logn which will be less than n-α if c>=1/log(5/4)
7) Then the algorithm terminates in 1/log(5/4) α logn with propability >=1- n-α
Theorem
Algorithm prime(n,a)
{
q:=n-1;
For i:=1 to αlog(n) do
{
m:=q;y:=1;
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
a:=random()mod q+1;
z:=a;
//compute an-1 mod n
while(m>0) do
{
while (m mod z=0) do
{
x:=z;z:=z2 mod n;
return false;
m:=mm/2 ;
m:=m-1;y:=(y*z)mod n;
Return true;
}
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
• This strategy suggest, splitting the input into 'k' distinct subsets, 1<k<=n yielding
k sub problems. This sub problems must be solved and then a method must be
found to combine sub solutions into a solution of a whole.
• We can write a control abstraction that mirrors the way divide & conquer
algorithm will work.
Control Abstraction
Algorithm DAndC(P)
{
if Small(P) then return S(P)
else
{
divide P into smaller instances P1,P2....Pk,k>=1;
Apply DAndC to each of these subproblems;
return Combine(DAndC(P1),DAndC(P2),...DAndC(PK));
}
}
* Where T(n) is the time for DandC on any input of size ‘n’
* g(n) time to compute the answer directly form small input.
* f(n) time for dividing ‘p’ and Combining the solutions to sub problems
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
The complexity of many divide and conquer algorithm is given by recurrences of the
form
T(n)= { T(1)
aT(n/b)+f(n)
n=1
n>1 Eq.2
One of the methods for solving any such recurrence relation is called Substitution
method.
Example:
T(n)=2T(n/2) + n
=2 [2T(n/4)+n/2] +n
=4 T(n/4) + 2n
=4 [2T(n/8)+n/4] + 2n
=8T(n/8)+3n ……
• Thus T(n)=nT(1)+nlog2n=nlog2n+2n
h(n) u(n)
BINARY SEARCH
Theorm:
Algorithm Binarysearch (a,n,x) works correctly.
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Proof:
* Initially low:= 1, high:= n, n>=0&a[1]<=a[2] <=a[3]….<a[n].
* If n=0, while loop is not entered. So, ‘0’ is returned.
* Otherwise possible elements to be checked with ‘x’ in the loop(each time is)
a[low],a[low+1],….a[mid]…a[high].
* If x=a[mid], then the alg. Terminates successfully.
* Otherwise the range is narrowed by increasing low to(mid+1) or decreasing high to
(mid-1). This narrowing of the range doesn’t effect the search.
* If ‘low’ becomes greater than ‘high’, then ‘x’ is not present. Hence, the loop is exited.
RUN-TIME CALCULATION:
THEOREM 3.2:
If ‘n’ is in the range[2k-1,2k], then Binary search makes at most ‘k’ element
comparisons for a successful search and either ‘k-1’ or ‘k’ comparisons for an
unsuccessful search(In other words the time for a successful search is O(log n)and for an
unsuccessful search is θ(log n)).
3 1
1 5 9 13
2 4 6 8 10 12 14
Next comparison
if x > a[7], x with a[11]
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Each page through the tree represent a sequence of comparison in the binary search
method.
• If x is present, alg will end at one of the circular nodes[ that lists the index into
the array where x was found]
• If x is not present, alg will terminate at one of the square nodes.
Theorem
If n is in the range, (2k-1, 2k) then binary search make k element comparisons for
a successful search and either k-1 or k comparisons for an successful search ( in
other words the time for a successful search is O(log n) and for unsuccessful
search is O(log n)
Proof :
Consider the binary decision tree describing the action of Binary search on n
elements.
All Structural searches end at circular node and unsuccessful searches end at
square node.
If 2k-1 <= n <= 2k, then all circular nodes are at level 1,2,….k [ Note that root is at
level 1]
All square nodes are at levels k and k+1
Number of element comparisons needed to terminate at a circular node on level I
is I (Whereas for square node at level I is only I-1).The theorem follows.
Distance of Node : From the root is one less than it’s level.
Internal path length I
I = Σ distance of al internal nodes.
External Path Length E
E = Σ Distance of all External Nodes.
For any Binary Tree with n internal nodes
E = I + 2n → eq1
Eg: -
N=7
I = 10
E = I + 2n
= 10 + 14
= 24 //
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
3. For the given array a[] the tree of recursive calls of Maxmin in
(9)
1 , 9 , 60 , -80
(8)
(5)
6 , 9, 60, 17
1 ,5, 22, -8
(1) (2)
1, 2 , 22 , 13 3 , 3 , -5 ,-5
Numbers in brackets
represents the order in
which max & min are
assigned values
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
2
1 3 , 3 , -5 ,-5
1 ,5 , max ,min
1, 3, max,
1 , 3 , max ,min
(1)
1, 2, 22, 13 3, 3, -5 , -5
1 , 2 , 22 , 13
(2)
1, 3, 22, -5
T(n) =
0 n=1
{ 1 n=2
T(n/2) + T (n/2) +2 n>2
T(n) = 2 T(n/2) + 2
=2 (2 T(n/4) + 2 ) + 2
=4 T(n/4) + 4 + 2
.
.
.
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Advantage
Compared to straight forward method this is saving of 25% in comparisions.
Disadvantage
In terms of storage minmax is worse then stright forward method
Observations: -
1. Divide & conquer strategy is seen to be only a guide to better alg design
which may not always succeed.
2. Use of asymptotic notation is not enough of a discriminator.
MERGE SORT
PROBLEM DESCRIPTION:
Given a sequence of n elements (also called keys) a[1]…a[n], the general idea is to
imagine them in split into two sets a[1],…a[n/2] and a[(n/2)+1],…a[n]. Each set is
individually sorted and resulting sorted sequences are merged to produce a single
sequence of n elements.
{
if (low<high) then
{
mid := [(low+high)/2;
MergeSort (low,mid);
MergeSort (mid+1,high);
Merge (low,mid,high); }
else
{
b[i]:= a[j];j:=j+1;
}
i:=i+1;
}
if(h>mid) then
for k:=j to high do
{
b[i]:= a[k]; i:=i+1;
}
else
for k = h to mid do
{b[i]=a[k];i = i+1 }
Example: -
A[1 : 10] = 310, 285, 179, 652, 351, 423, 861, 254, 450, 520
where vertical bars indicates the boundaries of sub arrays (height indicates the order)
285, 310 | 179| 652, 351 | 423, 861, 254, 450, 520
Next a[6:8] a [9:10] are merged (after making a[9 : 10] in to two lists and a merge)
179, 285, 310, 351, 652 | 254, 423, 450, 520, 861
Now there are only two sorted list – So final merge produces the result.
179, 254, 285, 310, 351, 423, 450, 520, 652, 861
Time complexity
If the time of merging operation is proportional to n, then the recurrence relation
describes the computing time for merge sort:
T (n) = {a n = 1,A a constant
{2T(n/2)+cn n > 1, A a constant
T (n) = 2(2T(n/4)+cn/2)+cn
= 4T(n/4)+2cn
= 4(2T(n/8)+cn/4)+2cn
. .
.
=2k T (1)+kcn
=an+cn log n
It is easy to show that if 2k<n< =2k+1,then T (n)<=T (2k+1)
Therefore T (n)=O (n log n)
QUICK SORT
In quick sort , the division in to two sub arrays is made so that the sorted sub array 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 and m and all j between m+1 and n for some m.(1 <=
m <= n)
Partitioning:
The rearrangement of the element is accomplished by picking some elements of a[] , say t
= a[s] and then recording the other elements so that all elements appearing before t in
a[1:n] are less than or equal to t and all elements appearing are greater than or equal to t.
This rearranging is referred to as partitioning.
Algorithm QuickSort(P,q) Algorithm Partition(a,m,p)
{if (p<q) then { v:=a[m]; i=m; j=p;
{ repeat
j = partition(a,P,q+1) {
QuickSort(P,j-1) repeat
QuickSort(P,j-1) i=i+1
} until ( [i] >= v);
} repeat
j= j-1;
until(a[j] <= v);
if (i<j) then interchange (a,i,j);
} until (i>=j);
a[m]=a[j]; a[j] =v; return j;
}
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Algorithm Interchange(a,i,j)
{ P=a[i];
a[i]=a[j];a[j]=p;
}
One of the i/p on which Quicksort worst-case occur is elements are already in sorted
order.
Solution to this is the use of randomizer . While sorting array a[p:q] instead of picking
a[m], pick a random element as the partition element.
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
SELECTION
In this problem, we are given n elements a [1:n] and are required to determine the kth
smallest element.
Algorithm Select1(a, n, k)
{
low :=1; up := n+1;
a[n+1] := ∞;
repeat
{
j := partition(a, low, up)
if(k = j) then return;
else if (k<j) then up := j;
else low := j+1;
}
until(false);
}
Hence at most n calls to partition can be made. Thus, the worst-case complexity
of Selection1 is Ο(n2).
Average time
• Let TAK(n) be the average time to find the kth smallest element in a[1:n]
Example: -
A [2 ,5, 6] [2, 5, 6]
Then all its permutations are [2, 6, 5]
3! = 6 [5, 2, 6] [6, 2, 5]
[5, 6, 2] [6, 5, 2]
and
Proof: -
On the first call to partition, the partitioning, element V is the ith – smallest element with
probability 1/n.
The time required by partition and it statement in select 1 is 0(n)
Hence there is a constant C, C > 0 such that
K–1 n
k n
n–1 n-1
We assume C is chosen such that R (1) ≤ C & show, by induction on n, that R (n) ≤ 4 cn.
Since R(n) is nondecreasing fn of n, it follows that this is maximized if k = m/2 when mix
even (and k = m+1 where m is odd)
2
m-1
Since TA(n) ≤ R(n), it follows that TA (n) ≤ 4cn Hence TA (n) is O(n)
A worst-case Optimal Algorithm for SELECTION (Using mm rule)
By choosing the partition element v more carefully can obtain a selection algorithm with
worst case complexity O(n)
To obtain such algorithm v must be chosen so that at least some fraction of the elements
is smaller than v & at least some fraction of elements is greater than v.
MM Rule
In this rule the n elements are divided in to n/r groups of r elements each.
The remaining n-r[n/r] elements are not used
The median mi of each of these [n/r] group is found
Then, the median mm of the mi‘s is found.
The median mm is used as the partitioning element.
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
. . . . .
. . . . .
. . . . . Non
decreasing
. . mm . .
order
. . . . .
. . . . .
Elements > =
mm . . . . .
Unit 2
V.STRASSENS’S MATRIX-MULTIPLICATION ALGORITHM
Where,
C11 = A11* B11 + A12 *B21
C12 = A11* B12 + A12 *B22
C21 = A21* B11 + A22* B21
C22 = A21* B12 + A22* B22
Where,
mT (n/2)__ cost of multiplying m pairs of (n/2) x (n/2) matrices.
a.n2 /4__ cost of doing ‘a’ additions
By applying substitution method we can obtain T(n) = O(n3)
But this is no improvement over the conventional method.
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
STRASSEN’s METHOD:
He was able to multiply two n x n matrices in time O(n log n ) which is of order ~ n
2.81
Method:
Step1:
Step 2:
c11=m1+m2-m4+m6 c21=m6+m7
c12=m4+m5 c22=m2-m3+m5-m7
Let n=2k
Let T(n) be the no. of arithmetic operations needed to multiply n x n matrix
By recurrence relation eq 3.0
T (n) = 7 t ( n / 2 ) + 18 (n / 2) 2 , n>2
Thus T(n) is O(7 log n) ie O(n log 7)
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
DEF:
Convex hull of a se s of points in the plane is defined to be the smallest convex polygon
containing all the points of s.
Example:
This is NOT a convex
hull since this is
concave polygon
This is NOT a
Convex hull.
Since it is not This is what
covering all a Convex
point of plane hull
Identifies two points p1,p2 of X with smallest & largest x-coordinate values
respectively.
P2 X2
Lower
hull
P1
Convex hulls of x1,x2 are computed using Divide and conquer algorithm called
HULL.
Algorithm HULL
P3 P2
P
P
P
P1
Divide X1 into two parts .One part contains all the points of x1 that are to the left
of <p1,p3> (including p1 & p2).Other part contains all the points of x1 that are to
the left of <p3,p2> (including p1 & p2)
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
All the other points are interior points & can be dropped from consideration.
The convex hull of each part is computed recursively and merged easily by
placing one next to the other.
Time Complexity
2)Graham’s scan:
Procedure:-
If S is a set of points in the plane, Graham’s scan algorithm identifies the point P
from S with the lowest y-coordinate value.
It then sorts the points of S according to the angle subtended by the points and P
with the positive x-axis
Scanning through the sorted list starts at P,
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
• If there are three successive points, say P1,P2,P3 that form a left
turn then P2 will be eliminated.
Algorithm:
Point=record
{
float x; float y;
Point *prev; Point *next;
}
Algorithm Scan(list)
{
*P:=list; *P1:=list;
repeat
{
P2:=(P1->next)
If ((P2->next)≠0 then P3:=(P2->next);
Else return;
Temp:=Area((P1->x),(P1->y),(P2->x),(p2->y),(P3->x),(P3->y));
If (temp≥0.0) then P1:=(P1->next);
Else
{
(P1->next):=P3; (P3->prev):=P1;
delete P2; P1:=(P1->prev);
}
} until (false);
Algorithm ConvexHull(ptslist)
{
sort(ptslist); Scan(ptslist); PrintList(ptslist);
}
4
5
Example:- 3
6
7
9 8 2 1
10 •
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Sorted points 1 2 3 4 5 6 7 8 9 10
Start->
P 1 2 left Move to Next Point
1 2 3 right Delete 2& take
Previous point
P 1 3 left MNP
1 3 4 left MNP
3 4 5 left MNP
4 5 6 right Delete 5.Take
previous point
3 4 6 left MNP
4 6 7 left MNP
6 7 8 left MNP
7 8 9 right Delete 8,Take
previous point
6 7 9 right Delete 7 &,Take
previous point
4 6 9 left MNP
6 9 10 left MNP
End 9 10 P left Process terminated
since P reached
Result:
After deletion of points 2,5,8,7 the remaining points 1,3,4,6,9,10.
Time Complexity
Function scan runs in O(n) time since for each triplet examined ,(either the scan
moves one node ahead or one point gets removed)
The test for direction (Whether a left or right turn) is done in O(1).
Function Area computes the signed area.
But Major work in this Algorithm is Sorting .Since it takes O(n log n) time the
total time of Graham’s scan algorithm is O ( n log n)
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Given a set X of n points, the problem is reduced to finding the upper hull & lower hull
separately &
then putting them together
H1 u tangent
H2
Time Complexity
Lemma: Let H1,H2 be two upper hulas wit at most m points each. If p is any point
of H1, its point q of tangency with H2 can be found in O( log m ) time.
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Proof:
If q` is any point in H2 then we can check the following things (in O(1) time)
Let x, y are left & right neighbors of q` in H2.
If p q`x is right turn & pq`y is left then q is to the right of q` in H2.
If pq`x is right turn & pq`y is left then q is t the right of q`.
y H2
H1
q`
p
H2
H1 q`
p y
x
H2
H1 x
q`
p
LEMAA 3.2
If h1 & h2 are tow upper hulls with at most m points each, their common tangent
can be computed in O(log 2 n) time.
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Proof:
y H2
H1
q`
p
Greedy Method
Greedy Method is most straight forwarded design technique applied to a wide variety of
problems with the following property.
These problems have n inputs and require obtaining a sub set that satisfies some
constrains. Any subset that satisfies these constrains is called a FEASIBLE
SOLUTION.
We need to find a feasible solution that either maximizes or minimizes a given objective
function. A feasible solution that does this is called an OPTIMAL SOLUTION.
Knapsack Problem
We are given n objects & a knapsack or bag, Object i has a weight wi and the knapsack
has a capacity m. If a fraction xi, 0 <=xi , of object is placed into the knapsack ,then a
profit of pixi is earned . The Objective is to obtain a filling of the knapsack that
maximizes the total profit earned.
Formal statement
X1 X2 X3 ∑ wixi ∑piwi
½ 1/3 ¼ 16.5 24.25 Optimal
1 2/15 0 20 28.5 solution
0 2/3 1 20 31
0 1 1/2 20 31.5
In the above table all rows gives feasible solutions. Since they satisfy the criteria that
total weight limit ( ∑ wixi <= m =20).Out of this four row gives optimal solution. Since
profit (∑piwi) is maximum among all rows i.e 31.5
Demonstration:
Theorem
If p1/w1 > = p2/w2 >= … > = pn/wn then Greedy knapsack generates an optimal
solution to the given instance of the knapsack problem
Proof:
For z we have
= Σ piyi
1<=i<=n
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
If Σ pizi > Σ piyi then y can not have been an optimal solution.
If these sums are equal , then either z = x & x is optimal or z != x
Thus j is not optimal. Hence x is optimal.
Informal Def:
Given a network & a loss tolerance level , the Tree vertex splitting problem
(TVSP) is to determine an optimal placement of boosters. It assumed that the
boosters can only be placed in the nodes of the tree.
Let T/X be forest that result when each vertex u in x is split into two nodes ui
and uo
1 1
2 After 2
3 Split --> 3i
3o
4 5
4 5
Example
1
Signal loss
4 2 from node 1
to 3
2
3
2 1 3
4 5 6
1 4 2 3
7 8 9 10
Let δ = 5
For each leaf nodes 7,8,5,9, and 10 d(u) =0
Let u be any node and c(u) is set of all its children then
D(u) = max { d(v) + w(u,v)}
v є c(u)
Using this formula and the steps given before we can get the forest as follows
2i
3
2o
5 6i
4i
4o
6o
7 8
9 10
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Algorithm TVS(T, δ)
{
if (T != 0 ) then
{
d[t] := 0;
for each child v of T do
{
TVS (v, δ)
d[t] := max { d[t] , d[v] + w(T,v) };
}
if ((T is not the root ) and (d[T] + w (Parent(T),T) > δ )) then
{ write(T); d[T]:=0; }
T, d[T] T, d[T]
T, d[T]
1, 0 1, 0
1, 0
2,0
2,0
Call 4
Call 4 return d[4] value Call 5 Call 5 return
changed
after the call
return from
T, d[T]
its child
T, d[T]
1, 0 T, d[T] T, d[T]
1, 0
1, 0 1, 0
2,0
2,0
2,0 2,0
4,0
4,1
4,1 4 ,4
7, 0
7, 0
7, 0 8 ,0 7, 0 8 ,0
T, d[T]
1, 0
After returning from
second child d[4] will
become zero because
2,0 of last if stmt in the
TVS algorithm. Now
Write statement
produces output ‘4 ‘.ie.
4 ,0 node four has to be
spitted
Continue the same
operation 8 ,0
7, 0
Prob stmt:
We are given with a set of n jobs. Associated with job i is an integer deadline
di >=0 and a profit Pi > 0 . For any job i the profit Pi is earned iff the job is
completed by its deadline. To complete a job one has to process the job on a
machine for one unit of time.
Feasible solution
A Feasible solution for this problem is a subset J of jobs s.t each solution J should
contains sequence of jobs that can be completed with in their deadlines.
Optimal Solution:
EXAMPLE
J:={1}
For i := 2 to n do
{ if (all jobs in J U {i} can be completed by their
deadlines) then
J:=J U {i}
}
}
HIGH LEVEL DESCRIPTION OF job sequencing algorithm
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
n j
MRT= 1/n Σ tj Where tj = Σ lik
j=1 k=1
tj
- Time needed to retrieve program ij
Provided page are stored in the order I = i, i2…….in.
Feasible Solution
Optimal solution:
A particular order of programs which gives minimum MRT.
Example: Let n=3 & prg lengths are (l1, l2, l3) = (5, 10, 3). Hence 3! = 6 possible
ordering & their respective d values are
Ordering I D(I) –sum of time needed to retrieve all prgs MRT = d(I)/3
1,2,3 (5) + (5+10) + (5+10+3) 38/3
1,3,2 (5) + (5+3) + (5+3+10) 31/3
2,1,3 (10) + (10+5) + (10+5+3) 43/3
2,3,1 (10) + (10+3) + (10+3+5) 41/3
3,1,2 (3) + (3+5) + (3+5+10) 29/3
2,3,1 (3) + (3+10) + (3+10+5) 34/3
Minimum
MRT time
(Optimal
Greedy solution. solution)
Sort the programs by their length (Shortest first) the store the programs in the sorted
order. The MRT time for this arrangement is minimum. Thus Optimal; solution.
Design And Analysis of Algorithm –study material – B.Sheik Md Abdullah –MIIT
Algorithm Store(n,m)
//n is number of programs & m the number of tapes
{
j:=0;
for i:= 1 to n do
{
Write(“append program “.i,”to permutation for tape “,j);
j:=(j+1) mod m;
}
}
Theorem :
If l1, <= l2, < = … ln, then the ordering ij = j, 1 <= j <= n, minimizes
n k
Σ Σ tj
k=1 j=1
Over all permutations of the ij
Proof:
n k n
d(I)= Σ Σ lij = Σ (n-k+1) lij
k=1 j=1 k=1
If there exist a and b such that a < b an lia >lib then interchanging ia and ib results in a
permutation I` with
hence ,no permutation that is not in non decreasing order of the li’s can have minimum
d.It is easy to see that all permutations in nondecreasing order of the li’s have the same d
value. Hence , the ordering defined by ij =j , 1 <= j <= n , minimizes the d value.