PRESENTATION ON
●OPTIMAL STORAGE ON TAPES
●OPTIMAL MERGE PATTERNS
PRESENTED TO ■
DR. VINAY PATHAK
RESPENTED BY-
SUNIL KUMAR
III rd C.S.E.
S.R.No. – 108/07
Optimal Storage on Tapes
PROBLEM
INPUT
●
We are given n programs that are to be stored on a computer Tape of length l and associated with each program length l i
●
such that 1 ≤ i≤ n and
● ∑1 ≤ i≤ n li ≤ l
OUTPUT
●
A permutation from all n! for the n programs so that when they are stored on
tape in the order the MRT is minimized.
ALL SIX PERMUTATIONS FOR n=3
1 2 3
1 3 2
2 3 1
2 1 3
3 1 2
3 2 1
MRT(Mean Retrival Time)
Let there are n programs namely i1,i2,i3,i4.......................in on tape
then time tj required to retrive program ij
tj ∝ ∑ lik
1≤ k ≤j
If all program are retrived equally and every time head point to the front , then expected
MRT is given by-
MRT =(1/n)∑1≤j≤n tj
Minimizing the MRT is equivalent to minimizing
D( I ) li
1 j n 1 k j
k
Example
Let n = 3 and (l1,l2,l3) = (5,10,3)
Ordering I D(I)
1,2,3 5 + 5 + 10 + 5 + 10 + 3 = 38
1,3,2 5 + 5 + 3 + 5 + 3 + 10 = 31
2,1,3 10 + 10 + 5 + 10 + 5 + 3 = 43
2,3,1 10 + 10 + 3 + 10 + 3 + 5 = 41
3,1,2 3 + 3 + 5 + 3 + 5 + 10 = 29
3,2,1 3 + 3 + 10 + 3 + 10 + 5 = 34
The Greedy Solution
Make tape empty
for i := 1 to n do
grab the next shortest file
put it next on tape
The algorithm takes the best short term choice
without checking to see weather it is the best long
term decision.
Example
Let n=5 ,and l1=5, l2=7, l3=10, l4=20, l5=30
TAPE-
SORTED ORDER IS
5,7,10,20,30
Insert 5
5
Insert 7
5 7
Insert 10
5 7 10
Insert 20
5 7 10 20
Insert 30
5 7 10 20 30
ALGO FOR MORE THAN ONE TAPE
1. Algorithm Store(n,m)
2. //n is the no of programs and m the no of tapes
3. {
4. j:=1;//Next tape to store on
5. for i:=1 to n do
6. {
7. write(“append program”,i,”to permutation for
tape”,j);
8. j:=(j+1)mod m;
9. }
10. }
Example
We want to store files of lengths (in MB) {12 34 56 73 24 11
34 56 78 91 34 45} on three tapes.
How should we store
them on the three tapes so that the mean retrieval
time is minimized?
SOLUTION: STORE FILES BY NONDECREASING
LENGTH
First sort the files in increasing order of length. For
this we can use heapsort, meregesort or quicksort
algorithms.
Sorted Ele4ments are
11 12 24 34 34 34 45 56 56 73 78 91
Now distribute the files:
First element 11
Inserting 11
Tape 1 11
Tape 2
Tape 3
Inserting 12
Tape 1 11
Tape 2 12
Tape 3
Inserting 24
Tape 1 11
Tape 2 12
Tape 3 24
Inserting 34
Tape 1 11 34
Tape 2 12
Tape 3 24
Inserting 34
Tape 1 11 34
Tape 2 12 34
Tape 3 24
Inserting 45
Tape 1 11 34 45
Tape 2 12 34
Tape 3 24 34
Inserting 56
Tape 1 11 34 45
Tape 2 12 34 56
Tape 3 24 34
Inserting 56
Tape 1 11 34 45
Tape 2 12 34 56
Tape 3 24 34 56
Inserting 73
Tape 1 11 34 45 73
Tape 2 12 34 56
Tape 3 24 34 56
Inserting 78
Tape 1 11 34 45 73
Tape 2 12 34 56 78
Tape 3 24 34 56
Inserting 91
Tape 1 11 34 45 73
Tape 2 12 34 56 78
Tape 3 24 34 56 91
TIME COMPLEXITY
The time is consumed only in shorting becoz in writting
and finding the tape on which we have to write the
time consumed is constant.So time consumed is
equal to time taken by any sorting algo
T(n)=O(n ln n)+θ(1)
=O(n ln n)
Optimal Merge Patterns
PROBLEM
INPUT
●
We are given n sorted files containing records and we have to merge them in to a single file merging only 2 at a time
OUTPUT
●
We have to merge them in minimum time
As we know time consumed in merging is directly proportional to the no of
records moved from one file to another file
So we will focus on reducing the movement of records required for merging or
no of comparisions
Example.
Suppose there are 3 sorted lists L1, L2, and L3, of sizes 30, 20, and
10, respectively, which need to be merged into a combined
sorted list, but we can merge only two at a time. We intend
to find an optimal merge pattern which minimizes the total
number of comparisons. For example, we can merge L1 and L2,
which uses 30 + 20 = 50 comparisons resulting in a list of size
50. We can then merge this list with list L3, using another 50 +
10 = 60 comparisons, so the total number of comparisons is 50
+ 60 = 110. Alternatively, we can merge lists L2 and L3, using 20
+ 10 = 30 comparisons, the resulting list (size 30) can then be
merged with list L1, for another 30 + 30 = 60 comparisons. So
the total number of comparisons is 30 + 60 = 90. It doesn’t take
long to see that this latter merge pattern is the optimal one.
Binary Merge Trees: We can depict the merge patterns using a binary tree,
built from the leaf nodes (the initial lists) towards the root in which each
merge of two nodes creates a parent node whose size is the sum of the sizes
of the two children. For example, the two previous merge patterns are
depicted in the following two figures:
Cost = 30*2 +
Cost = 30*1 +
60 20*2 + 10*1 =
60 20*2 + 10*2 =
110
90
50 10 30 30
30 20 20 10
Merge L1 and L2, then with Merge L2 and L3, then with L1
L3
merge cost = sum of all weighted external path lengths
ALGORITHM
treenode = record{
treenode* lchild;
treenode*rchild;
integer weight;};
1. Algorithm Tree(n)
2. // list is a global list of n single node
3. //binary trees as described above
4. {
5. for i:= 1 to n-1 do
6. {
7. pt:= new treenode;//Get a new treevnode
8. (pt->lchild):=Least(list);
9. (pt->rchild):=Least(list);
10. (pt->wieght):= ((pt->lchild)->wieght)+((pt->rchild)-> wieght);
11. Insert (list,pt);//Merge 2 trees with smallest length
12. }
13. return Least(list) //tree left in list is merged tree
14. }
Example of the optimal merge tree algorithm:
2 3 5 7 9 Initially, 5 leaf nodes with
sizes
5
2 3 5 7 9
Iteration 1: merge 2 and 3 into 5
10
Iteration 2:
5 5 16 Iteration 3: merge 7 and
merge 5 and
9 (chosen among 7, 9,
5 into 10
2 3 7 9 and 10) into 16
26
Iteration 4: merge
16
10 10 and 16 into 26
5 5 7 9
Cost = 2*3 + 3*3 + 5*2 + 7*2 +
2 3 9*2 = 57.
TIME COMPLEXITY
The main for loop executed n-1 times
If list is kept in nondecreasing order of the weights of
roots then Least (list) take only O(1)time &
Insert(list,pt) will take O(n) time .
Hence the total time taken will be O(n2)
Greedy Solution
The solution is greedy becoz at each step we are merging
smallest files without caring of final result
The cost is directly proportional tothe sum of productof
depth of external leaves to their weights
∑1≤i≤n diqi called as weighted external path length
Proof of optimality of the tree algorithm
We use induction on n 1 to show that the tree algo is optimal in
that it gives the minimum total weighted external path lengths
(among all possible ways to merge the given leaf nodes into a
binary tree).
(Basis) When n = 1. There is no internal nodes so tree is optimal.
(Induction Hypothesis) Suppose the merge tree is optimal when
there are k leaf nodes, for some k 1
(Induction) Consider (k + 1) leaf nodes. Call them a1, a2, …, and
ak+1. We may assume nodes a1, a2 are of the smallest values, which
are merged in the first step of the merge algorithm into node b.
We call the merge tree T, the part excluding a1, a2 T’ (see figure).
Suppose an optimal binary merge tree is S. We make two
observations.
. (1) If node x of S is a deepest internal node, we may swap its
two children with nodes a1, a2 in S without increasing the total
weighted external path lengths. Thus, we may assume tree S
has a subtree S’ with leaf nodes x, a3, …, and ak+1. (2) The
tree S’ must be an optimal tree for k nodes x, a3, …, and ak+1
By induction hypothesis, tree S’ has a total weighted external
path lengths equal to that of tree T’. Therefore, the total
weighted external path lengths of T equals to that of tree S,
proving the optimality of T.
T S
T’ S’
b x
a1 a2 a1 a2
THANK YOU !