Presentation On: Presented To Dr. Vinay Pathak
Presentation On: Presented To Dr. Vinay Pathak
Presentation On: Presented To Dr. Vinay Pathak
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
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
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
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
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 .
T S
T’ S’
b x
a1 a2 a1 a2
THANK YOU !