Optimal Storage on Tapes

21 May, 2021
Given programs stored on a computer tape and length of each program is where , find the order in which the programs should be stored in the tape for which the Mean Retrieval Time (MRT given as ) is minimized.Example:  
Input : n = 3
        L[] = { 5, 3, 10 }
Output : Order should be { 3, 5, 10 } with MRT = 29/3
Prerequisites: Magnetic Tapes Data Storage  
Let us first break down the problem and understand what needs to be done.A magnetic tape provides only sequential access of data. In an audio tape/cassette, unlike a CD, a fifth song from the tape can’t be just directly played. The length of the first four songs must be traversed to play the fifth song. So in order to access certain data, head of the tape should be positioned accordingly.Now suppose there are 4 songs in a tape of audio lengths 5, 7, 3 and 2 mins respectively. In order to play the fourth song, we need to traverse an audio length of 5 + 7 + 3 = 15 mins and then position the tape head. Retrieval time of the data is the time taken to retrieve/access that data in its entirety. Hence retrieval time of the fourth song is 15 + 2 = 17 mins.Now, considering that all programs in a magnetic tape are retrieved equally often and the tape head points to the front of the tape every time, a new term can be defined called the Mean Retrieval Time (MRT).Let’s suppose that the retrieval time of program is . Therefore, MRT is the average of all such . Therefore , or The sequential access of data in a tape has some limitations. Order must be defined in which the data/programs in a tape are stored so that least MRT can be obtained. Hence the order of storing becomes very important to reduce the data retrieval/access time. Thus, the task gets reduced – to define the correct order and hence minimize the MRT, i.e. to minimize the term For e.g. Suppose there are 3 programs of lengths 2, 5 and 4 respectively. So there are total 3! = 6 possible orders of storage.
 
It’s clear that by following the second order in storing the programs, the mean retrieval time is least.In above example, the first program’s length is added ‘n’ times, the second ‘n-1’ times...and so on till the last program is added only once. So, careful analysis suggests that in order to minimize the MRT, programs having greater lengths should be put towards the end so that the summation is reduced. Or, the lengths of the programs should be sorted in increasing order. That’s the Greedy Algorithm in use – at each step we make the immediate choice of putting the program having the least time first, in order to build up the ultimate optimized solution to the problem piece by piece.Below is the implementation:  
C++
Java
C#
Javascript
// CPP Program to find the order// of programs for which MRT is// minimized#include <bits/stdc++.h> using namespace std; // This functions outputs the required// order and Minimum Retrieval Timevoid findOrderMRT(int L[], int n){    // Here length of i'th program is L[i]    sort(L, L + n);     // Lengths of programs sorted according to increasing    // lengths. This is the order in which the programs    // have to be stored on tape for minimum MRT    cout << "Optimal order in which programs are to be"            "stored is: ";    for (int i = 0; i < n; i++)        cout << L[i] << " ";    cout << endl;     // MRT - Minimum Retrieval Time    double MRT = 0;    for (int i = 0; i < n; i++) {        int sum = 0;        for (int j = 0; j <= i; j++)            sum += L[j];        MRT += sum;    }    MRT /= n;    cout << "Minimum Retrieval Time of this"           " order is " << MRT;} // Driver Code to test above functionint main(){    int L[] = { 2, 5, 4 };    int n = sizeof(L) / sizeof(L[0]);    findOrderMRT(L, n);    return 0;}
// Java Program to find the order// of programs for which MRT is// minimizedimport java.io.*;import java .util.*; class GFG{ // This functions outputs// the required order and// Minimum Retrieval Timestatic void findOrderMRT(int []L,                         int n){    // Here length of    // i'th program is L[i]    Arrays.sort(L);     // Lengths of programs sorted    // according to increasing lengths.    // This is the order in which    // the programs have to be stored    // on tape for minimum MRT    System.out.print("Optimal order in which " +              "programs are to be stored is: ");    for (int i = 0; i < n; i++)        System.out.print(L[i] + " ");        System.out.println();     // MRT - Minimum Retrieval Time    double MRT = 0;    for (int i = 0; i < n; i++)    {        int sum = 0;        for (int j = 0; j <= i; j++)            sum += L[j];        MRT += sum;    }    MRT /= n;    System.out.print( "Minimum Retrieval Time" +                    " of this order is " + MRT);} // Driver Codepublic static void main (String[] args){    int []L = { 2, 5, 4 };    int n = L.length;    findOrderMRT(L, n);}} // This code is contributed// by anuj_67.
// C# Program to find the// order of programs for// which MRT is minimizedusing System; class GFG{ // This functions outputs// the required order and// Minimum Retrieval Timestatic void findOrderMRT(int []L,                         int n){    // Here length of    // i'th program is L[i]    Array.Sort(L);     // Lengths of programs sorted    // according to increasing lengths.    // This is the order in which    // the programs have to be stored    // on tape for minimum MRT    Console.Write("Optimal order in " +                    "which programs are" +                  " to be stored is: ");    for (int i = 0; i < n; i++)        Console.Write(L[i] + " ");        Console.WriteLine();     // MRT - Minimum Retrieval Time    double MRT = 0;    for (int i = 0; i < n; i++)    {        int sum = 0;        for (int j = 0; j <= i; j++)            sum += L[j];        MRT += sum;    }    MRT /= n;    Console.WriteLine("Minimum Retrieval " +                  "Time of this order is " +                                       MRT);} // Driver Codepublic static void Main (){    int []L = { 2, 5, 4 };    int n = L.Length;    findOrderMRT(L, n);}} // This code is contributed// by anuj_67.
<script> // Javascript Program to find the order// of programs for which MRT is// minimized   // This functions outputs// the required order and// Minimum Retrieval Timefunction findOrderMRT(L, n){    // Here length of    // i'th program is L[i]    L.sort();       // Lengths of programs sorted    // according to increasing lengths.    // This is the order in which    // the programs have to be stored    // on tape for minimum MRT   document.write("Optimal order in which " +              "programs are to be stored is: ");    for (let i = 0; i < n; i++)        document.write(L[i] + " ");        document.write("<br/>");       // MRT - Minimum Retrieval Time    let MRT = 0;    for (let i = 0; i < n; i++)    {        let sum = 0;        for (let j = 0; j <= i; j++)            sum += L[j];        MRT += sum;    }    MRT /= n;   document.write( "Minimum Retrieval Time" +                    " of this order is " + MRT);} // driver code      let L = [ 2, 5, 4 ];    let n = L.length;    findOrderMRT(L, n);     </script>
Output:  
Optimal order in which programs are to be stored are: 2 4 5 
Minimum Retrieval Time of this order is 6.33333
Time complexity of the above program is the time complexity for sorting, that is (Since std::sort() operates in ) If you use bubble sort instead of std::sort(), it will take You may think that the time complexity for this particular above code should be due to both the loops in ‘mrt’ calculation, that is,, but do remember that intuitively, the for loops used can also be coded in this manner to avoid two loops :  
for (int i = 0; i < n; i++)
    MRT += (n - i) * L[i];
 
vt_m
Sagnik Chaudhuri
chinmoy1997pal
programming-puzzle
Arrays
Greedy
Arrays
Greedy
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Write a program to print all permutations of a given string
Huffman Coding | Greedy Algo-3
Coin Change | DP-7
Fractional Knapsack Problem
Policemen catch thieves