In: Computer Science
There are N different models of mobiles manufactured at a mobile
manufacturing unit. Each mobile must go through 2 major phases:
‘parts manufacturing’ and ‘assembling’. Obviously, ‘parts
manufacturing’ must happen before “assembling’. The time for ‘parts
manufacturing’ and ‘assembling’ (pmi and ai for ith mobile) for
every mobile may be different. If we have only 1 unit for ‘parts
manufacturing’ and 1 unit for ‘assembling’, how should we produce n
mobiles in a suitable order such that the total production time is
minimized?
Requirements:
1. Write a Greedy Algorithm to select the mobile ‘parts
manufacturing’ and ‘assembling’ in such a way that total production
time is minimized.
2. Analyse the time complexity of your algorithm.
3. Implement the above problem statement using Python.
Input:
For example, now there are 6 different Mobiles in total. Time for
each mobile ‘parts manufacturing’ and ‘assembling’ are given as
shown:
Mobile i | pmi (minutes) | ai (minutes) |
1 | 5 | 7 |
2 | 1 | 2 |
3 | 8 | 2 |
4 | 5 | 4 |
5 | 5 | 5 |
6 | 6 | 6 |
i have used a this pattern [mobile with least production time,(descending order of remaining production time)] then calculating total time by looking assembly time and waiting time.
for eg:
mobile m1 m2 m3
prdtn 20 40 60
asmbl 10 15 20
then production unit work in this order [m1,m3,m2]
and assembly unit also work in this order [m1,m3,m2]
total time =(wt=20)+(ass=10)+(wt=50)+(ass=20)+(wt=40)+(ass=15)
print("enter the no. of mobiles")
n=int(input())
pmi={}
a=[]
for i in range(0, n):
print("enter the production time of %dth mobile"%(i+1))
pmi[i]=int(input())
print("enter the assembly time time of %dth mobile"%(i+1))
a.append(int(input()))
print(a)
#storing the least production time
l=min(pmi.values())
#new dictionary which store the mobile with least time as first
element
pmi1={list(pmi.keys())[list(pmi.values()).index(l)]:l}
#list of production time
val=list(pmi.values())
#sorting the list in descending order
val.sort(reverse=True)
#inserting into the new dictionary in descending order after the
first element
for i in val:
if(i!=l):
pmi1[list(pmi.keys())[list(pmi.values()).index(i)]]=i
#new list of production time after arranging acc to algorithm
val1=list(pmi1.values())
a1=[]
#changing the list assembly with respect mobile produced
for i in list(pmi1.keys()):
a1.append(a[i])
k=k+1
#time after each production are stored in a list
timl=[]
timl.append(val1[0])
for i in range(1,n):
timl.append(timl[i-1]+val1[i])
time=0
#calculating time which is production time + assembly time +
waiting time
for i in range(0,n):
while(True):
if(time==timl[i]):
time=time+a1[i]
break
else:
time=time+(timl[i]-time)
print("time required for total production",time)