Optimal Storage on Tapes is one of the applications in the Greedy Method, The objective of this algorithm is to find the Optimal retrieval time for accessing programs that are stored on tape.
Greedy Method:
This algorithm works in steps. In each step, it selects the best available options until all options are finished.
Example:
Let n=3, (l1, l2, l3) = (5, 10, 3), therefore we can keep the programs in 3! (6 ways) possible orders.
From the above graph, we can see the min(MRT) = 9.66. So, the best possible order of programs is (Program3, Program1, Program2)
By this, we can assume that storing programs in increasing order of their lengths can give the solution for Mean Retrieval Time.
# Optimal Storage on Tapes using Greedy Method
def optimalstorage(n,new_dict):
sorted_dict = {}
temp = []
totalsum = t_sum = 0
print("Sorted Lengths for the programs ....")
s_v = sorted(new_dict.values())
for i in s_v:
for k in new_dict.keys():
if new_dict[k] == i:
sorted_dict[k] = new_dict[k]
break
print(sorted_dict)
for i in sorted_dict:
#print(sorted_dict[i])
v = sorted_dict[i]
t_sum += v
temp.append(t_sum)
totalsum = sum(temp)
print("Minimum value of sum of lengths",totalsum)
print("Mean Retrieval time is",totalsum/n)
n = int(input("Enter Number of Programs :"))
new_dict = {}
prgm = list()
length = list()
for i in range(n):
p = int(input("Enter the Program number :"))
l = int(input("Enter the Program length :"))
prgm.append(p)
length.append(l)
new_dict = dict( zip(prgm,length))
print("Entered Lengths for the programs ....")
print(new_dict)
optimalstorage(n,new_dict)
Comments