Vertical and Horizontal retrieval (MRT) on Tapes
Last Updated :
08 May, 2023
Prerequisite: Optimal Storage on Tapes
You are given tapes[] of different sizes and records[] of different volumes. The task is to fill the volumes in tapes horizontally or vertically such that Retrieval Time is minimised.
Examples:
Input: tape[] = { 25, 80, 160}, records[] = { 15, 2, 8, 23, 45, 50, 60, 120 }
Output:
Horizontal Filling:
1st tape : [ 2 8 15 ] MRT : 12.3333
2nd tape : [ 23 45 ] MRT : 45.5
3rd tape : [ 50 60 ] MRT : 80
Vertical Filling:
1st tape : [ 2 23 ] MRT : 13.5
2nd tape : [ 8 45 ] MRT : 30.5
3rd tape : [ 15 50 60 ] MRT : 68.3333
For Minimum Retrieval Time we will insert volumes of records in increasing order of their size.
The formula for Minimum Retrieval Time is:
MRT = (1/N)*Σ(A(i)*(N-i))
where 0 ≤ i < N
There are two ways of adding volumes on tapes for reducing retrieval time:
- Horizontal Retrieval
- Vertical Retrieval
Horizontal Retrieval
We have,
tapes[] = { 25, 80, 160}
records[] = { 2, 8, 15, 23, 45, 50, 60, 120 } (in increasing order)
Horizontal Filling on tape:
- Inserting 2
1st tape : [ 2 ]
2nd tape : [ ]
3rd tape : [ ]
-
- Inserting 8
1st tape : [ 2 8 ]
2nd tape : [ ]
3rd tape : [ ]
- Inserting 15
1st tape : [ 2 8 15 ]
2nd tape : [ ]
3rd tape : [ ]
- Inserting 23
1st tape : [ 2 8 15 ]
2nd tape : [ 23 ]
3rd tape : [ ]
- Inserting 45
1st tape : [ 2 8 15 ]
2nd tape : [ 23 45 ]
3rd tape : [ ]
- Inserting 50
1st tape : [ 2 8 15 ]
2nd tape : [ 23 45 ]
3rd tape : [ 50 ]
- Inserting 60
1st tape : [ 2 8 15 ]
2nd tape : [ 23 45 ]
3rd tape : [ 50 60 ]
Below is the program for Horizontal Filling
C++
#include <bits/stdc++.h>
using namespace std;
void horizontalFill( int records[],
int tape[], int nt)
{
int sum = 0;
int Retrieval_Time = 0;
double Mrt;
int current = 0;
vector< int > v;
for ( int i = 0; i < nt; i++) {
v.clear();
Retrieval_Time = 0;
sum = 0;
cout << "tape " << i + 1 << " : [ " ;
sum += records[current];
while (sum <= tape[i]) {
cout << records[current] << " " ;
v.push_back(records[current]);
current++;
sum += records[current];
}
cout << "]" ;
for ( int i = 0; i < v.size(); i++) {
Retrieval_Time += v[i] * (v.size() - i);
}
Mrt = ( double )Retrieval_Time / v.size();
cout << "\tMRT : " << Mrt << endl;
}
}
int main()
{
int records[] = { 15, 2, 8, 23, 45, 50, 60, 120 };
int tape[] = { 25, 80, 160 };
int n = sizeof (records) / sizeof (records[0]);
int m = sizeof (tape) / sizeof (tape[0]);
sort(records, records + n);
horizontalFill(records, tape, m);
}
|
Java
import java.util.*;
class GFG
{
static void horizontalFill( int records[],
int tape[], int nt)
{
int sum = 0 ;
int Retrieval_Time = 0 ;
double Mrt;
int current = 0 ;
Vector<Integer> v = new Vector<Integer>();
for ( int i = 0 ; i < nt; i++)
{
v.clear();
Retrieval_Time = 0 ;
sum = 0 ;
System.out.print( "tape " + (i + 1 ) + " : [ " );
sum += records[current];
while (sum <= tape[i])
{
System.out.print(records[current] + " " );
v.add(records[current]);
current++;
sum += records[current];
}
System.out.print( "]" );
for ( int j = 0 ; j < v.size(); j++)
{
Retrieval_Time += v.get(j) * (v.size() - j);
}
Mrt = ( double )Retrieval_Time / v.size();
System.out.print( "\tMRT : " + Mrt + "\n" );
}
}
public static void main(String[] args)
{
int records[] = { 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 };
int tape[] = { 25 , 80 , 160 };
int n = records.length;
int m = tape.length;
Arrays.sort(records);
horizontalFill(records, tape, m);
}
}
|
Python3
def horizontalFill(records, tape, nt) :
sum = 0 ;
Retrieval_Time = 0 ;
current = 0 ;
v = [];
for i in range (nt) :
v.clear();
Retrieval_Time = 0 ;
sum = 0 ;
print ( "tape" , i + 1 , ": [ " ,end = "");
sum + = records[current];
while ( sum < = tape[i]) :
print (records[current],end = " " );
v.append(records[current]);
current + = 1 ;
sum + = records[current];
print ( "]" ,end = "");
for i in range ( len (v)) :
Retrieval_Time + = v[i] * ( len (v) - i);
Mrt = Retrieval_Time / len (v);
print ( "tMRT :" , Mrt);
if __name__ = = "__main__" :
records = [ 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 ];
tape = [ 25 , 80 , 160 ];
n = len (records);
m = len (tape);
records.sort();
horizontalFill(records, tape, m);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void horizontalFill( int []records,
int []tape, int nt)
{
int sum = 0;
int Retrieval_Time = 0;
double Mrt;
int current = 0;
List< int > v = new List< int >();
for ( int i = 0; i < nt; i++)
{
v.Clear();
Retrieval_Time = 0;
sum = 0;
Console.Write( "tape " + (i + 1) + " : [ " );
sum += records[current];
while (sum <= tape[i])
{
Console.Write(records[current] + " " );
v.Add(records[current]);
current++;
sum += records[current];
}
Console.Write( "]" );
for ( int j = 0; j < v.Count; j++)
{
Retrieval_Time += v[j] * (v.Count - j);
}
Mrt = ( double )Retrieval_Time / v.Count;
Console.Write( "\tMRT : " + Mrt + "\n" );
}
}
public static void Main(String[] args)
{
int []records = { 15, 2, 8, 23, 45, 50, 60, 120 };
int []tape = { 25, 80, 160 };
int n = records.Length;
int m = tape.Length;
Array.Sort(records);
horizontalFill(records, tape, m);
}
}
|
Javascript
<script>
function horizontalFill(records, tape, nt)
{
let sum = 0;
let Retrieval_Time = 0;
let Mrt;
let current = 0;
let v = [];
for (let i = 0; i < nt; i++)
{
v = [];
Retrieval_Time = 0;
sum = 0;
document.write( "tape " + (i + 1) + " : [ " );
sum += records[current];
while (sum <= tape[i])
{
document.write(records[current] + " " );
v.push(records[current]);
current++;
sum += records[current];
}
document.write( "]" );
for (let j = 0; j < v.length; j++)
{
Retrieval_Time += v[j] * (v.length - j);
}
Mrt = Retrieval_Time / v.length;
if (i == 0)
{
document.write( " MRT : " + Mrt.toFixed(4) + "</br>" );
}
else {
document.write( " MRT : " + Mrt + "</br>" );
}
}
}
let records = [ 15, 2, 8, 23, 45, 50, 60, 120 ];
let tape = [ 25, 80, 160 ];
let n = records.length;
let m = tape.length;
records.sort( function (a, b){ return a - b});
horizontalFill(records, tape, m);
</script>
|
Output:
tape 1 : [ 2 8 15 ] MRT : 12.3333
tape 2 : [ 23 45 ] MRT : 45.5
tape 3 : [ 50 60 ] MRT : 80
Time complexity: O(nt2), where n is the number of records and t is the number of tapes.
Space complexity: O(n),
Vertical Retrieval
Vertical Filling on tape:
- Inserting 2
1st tape : [ 2 ]
2nd tape : [ ]
3rd tape : [ ]
-
- Inserting 8
1st tape : [ 2 ]
2nd tape : [ 8 ]
3rd tape : [ ]
- Inserting 15
1st tape : [ 2 ]
2nd tape : [ 8 ]
3rd tape : [ 15 ]
- Inserting 23
1st tape : [ 2 23 ]
2nd tape : [ 8 ]
3rd tape : [ 15 ]
- Inserting 45
1st tape : [ 2 23 ]
2nd tape : [ 8 45 ]
3rd tape : [ 15 ]
- Inserting 50
1st tape : [ 2 23 ]
2nd tape : [ 8 45 ]
3rd tape : [ 15 50 ]
- Inserting 60
1st tape : [ 2 23 60 ]
2nd tape : [ 8 45 ]
3rd tape : [ 15 50 ]
Below is the program for Vertical Filling
C++
#include <bits/stdc++.h>
using namespace std;
void vertical_Fill( int records[], int tape[],
int m, int n)
{
int v[m][n] = { 0 };
int sum = 0;
int Retrieval_Time = 0;
double Mrt;
int z = 0, j = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
sum = 0;
for ( int k = 0; k < i; k++) {
sum += v[j][k];
}
if (sum + records[z] <= tape[j]) {
v[j][i] = records[z];
z++;
}
}
if (v[2][i] == 0) {
break ;
}
}
for ( int i = 0; i < m; i++) {
Retrieval_Time = 0;
cout << "tape " << i + 1 << " : [ " ;
for (j = 0; j < n; j++) {
if (v[i][j] != 0) {
cout << v[i][j] << " " ;
}
else {
break ;
}
}
cout << "]" ;
for ( int k = 0; v[i][k] != 0; k++) {
Retrieval_Time += v[i][k] * (j - k);
}
Mrt = ( double )Retrieval_Time / j;
cout << "\tMRT : " << Mrt << endl;
}
}
int main()
{
int records[] = { 15, 2, 8, 23, 45, 50, 60, 120 };
int tape[] = { 25, 80, 160 };
int n = sizeof (records) / sizeof (records[0]);
int m = sizeof (tape) / sizeof (tape[0]);
sort(records, records + n);
vertical_Fill(records, tape, m, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void vertical_Fill( int records[], int tape[],
int m, int n)
{
int [][]v = new int [m][n];
int sum = 0 ;
int Retrieval_Time = 0 ;
double Mrt;
int z = 0 , j = 0 ;
for ( int i = 0 ; i < n; i++)
{
for (j = 0 ; j < m; j++)
{
sum = 0 ;
for ( int k = 0 ; k < i; k++)
{
sum += v[j][k];
}
if (sum + records[z] <= tape[j])
{
v[j][i] = records[z];
z++;
}
}
if (v[ 2 ][i] == 0 )
{
break ;
}
}
for ( int i = 0 ; i < m; i++)
{
Retrieval_Time = 0 ;
System.out.print( "tape " + (i + 1 )+ " : [ " );
for (j = 0 ; j < n; j++)
{
if (v[i][j] != 0 )
{
System.out.print(v[i][j]+ " " );
}
else
{
break ;
}
}
System.out.print( "]" );
for ( int k = 0 ; v[i][k] != 0 ; k++)
{
Retrieval_Time += v[i][k] * (j - k);
}
Mrt = ( double )Retrieval_Time / j;
System.out.print( "\tMRT : " + Mrt + "\n" );
}
}
public static void main(String[] args)
{
int records[] = { 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 };
int tape[] = { 25 , 80 , 160 };
int n = records.length;
int m = tape.length;
Arrays.sort(records);
vertical_Fill(records, tape, m, n);
}
}
|
Python3
import numpy as np
def vertical_Fill(records, tape, m, n) :
v = np.zeros((m, n)) ;
sum = 0 ;
Retrieval_Time = 0 ;
Mrt = None ;
z = 0 ; j = 0 ;
for i in range (n) :
for j in range (m) :
sum = 0 ;
for k in range (i) :
sum + = v[j][k];
if ( sum + records[z] < = tape[j]) :
v[j][i] = records[z];
z + = 1 ;
if (v[ 2 ][i] = = 0 ) :
break ;
for i in range (m) :
Retrieval_Time = 0 ;
print ( "tape" , i + 1 , ": [" , end = "");
for j in range (n) :
if (v[i][j] ! = 0 ) :
print (v[i][j], end = " " );
else :
break ;
print ( "]" , end = "");
k = 0 ;
while v[i][k] ! = 0 :
Retrieval_Time + = v[i][k] * (j - k);
k + = 1 ;
Mrt = Retrieval_Time / j;
print ( "MRT :" , Mrt);
if __name__ = = "__main__" :
records = [ 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 ];
tape = [ 25 , 80 , 160 ];
n = len (records);
m = len (tape);
records.sort();
vertical_Fill(records, tape, m, n);
|
C#
using System;
class GFG
{
static void vertical_Fill( int []records, int []tape,
int m, int n)
{
int [,]v = new int [m, n];
int sum = 0;
int Retrieval_Time = 0;
double Mrt;
int z = 0, j = 0;
for ( int i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
sum = 0;
for ( int k = 0; k < i; k++)
{
sum += v[j, k];
}
if (sum + records[z] <= tape[j])
{
v[j, i] = records[z];
z++;
}
}
if (v[2, i] == 0)
{
break ;
}
}
for ( int i = 0; i < m; i++)
{
Retrieval_Time = 0;
Console.Write( "tape " + (i + 1) + " : [ " );
for (j = 0; j < n; j++)
{
if (v[i, j] != 0)
{
Console.Write(v[i, j]+ " " );
}
else
{
break ;
}
}
Console.Write( "]" );
for ( int k = 0; v[i, k] != 0; k++)
{
Retrieval_Time += v[i, k] * (j - k);
}
Mrt = ( double )Retrieval_Time / j;
Console.Write( "\tMRT : " + Mrt + "\n" );
}
}
public static void Main(String[] args)
{
int []records = { 15, 2, 8, 23, 45, 50, 60, 120 };
int []tape = { 25, 80, 160 };
int n = records.Length;
int m = tape.Length;
Array.Sort(records);
vertical_Fill(records, tape, m, n);
}
}
|
Javascript
<script>
function vertical_Fill(records, tape, m, n)
{
let v = new Array(m);
for (let i = 0; i < m; i++)
{
v[i] = new Array(n);
for (let j = 0; j < n; j++)
{
v[i][j] = 0;
}
}
let sum = 0;
let Retrieval_Time = 0;
let Mrt;
let z = 0, j = 0;
for (let i = 0; i < n; i++)
{
for (let j = 0; j < m; j++)
{
sum = 0;
for (let k = 0; k < i; k++)
{
sum += v[j][k];
}
if (sum + records[z] <= tape[j])
{
v[j][i] = records[z];
z++;
}
}
if (v[2][i] == 0)
{
break ;
}
}
for (let i = 0; i < m; i++)
{
Retrieval_Time = 0;
document.write( "tape " + (i + 1)+ " : [ " );
for (j = 0; j < n; j++)
{
if (v[i][j] != 0)
{
document.write(v[i][j]+ " " );
}
else
{
break ;
}
}
document.write( "]" );
for (let k = 0; v[i][k] != 0; k++)
{
Retrieval_Time += v[i][k] * (j - k);
}
Mrt = Retrieval_Time / j;
if (i != m - 1)
{
document.write( " MRT : " + Mrt + "</br>" );
}
else {
document.write( " MRT : " + Mrt.toFixed(4) + "</br>" );
}
}
}
let records = [ 15, 2, 8, 23, 45, 50, 60, 120 ];
let tape = [ 25, 80, 160 ];
let n = records.length;
let m = tape.length;
records.sort( function (a, b){ return a - b});
vertical_Fill(records, tape, m, n);
</script>
|
Output:
tape 1 : [ 2 23 ] MRT : 13.5
tape 2 : [ 8 45 ] MRT : 30.5
tape 3 : [ 15 50 60 ] MRT : 68.3333
Time complexity: O(n * m2), where n is the number of records and m is the number of tapes.
Space complexity: O(m * n)
Comparing both the results we can use a particular tape filling technique which provides the best results(minimum MRT).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...