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 | pm5 | a5 |
6 | pm6 | a6 |
Sample Output: (Vary based on your input)
Mobiles should be produced in the order: 2, 5, 6, 1, 4, 3.
Total production time for all mobiles is: 28
Idle Time of Assembly unit: 2
Question:
Which algorithmnto use?
Whats time complexity?
How to calculate Total production time for all mobiles, Idle Time of Assembly unit?
Note: You can assume any value for pm5, a6 etc. Output value shown above is just example.
Well for Algorithms. All the cases exits i.e; best case, average
case and the worst case.
Algorithms mainly depends on the input. The best suitable technique
here which I think is Shortest Job First (SJF) depending upon their
Parts Manufacturing Time. It will minimize the production
cost.
It can be any case depending upon the input.
Greedy Algorithm
TIme Coplexity is O(n)2
Code :
from array import *
def Order():
mobiles = int(input("Enter the total number of Phones you want to
Produce: "))
pmi = array('i')
ai = array('i')
pmicopy = array('i')
# Taking Input
for x in range(mobiles):
x = int(input("Enter the Parts Manufacturing Time for the Mobile{}
".format(x+1)))
pmi.append(x)
pmicopy = pmi.__copy__()
for y in range(mobiles):
y = int(input("Enter the Assembly Time for the Mobile{}
".format(x+1)))
ai.append(y)
# Sorting
for i in range(len(pmi) - 1):
if pmi[i] > pmi[i + 1]:
pmi[i], pmi[i + 1] = pmi[i + 1], pmi[i]
for i in range(len(ai) - 1):
if ai[i] > ai[i + 1]:
ai[i], ai[i + 1] = ai[i + 1], ai[i]
# Calculating IDLE Time
aitime = 0
idletime = pmi[0]
fidle = pmi[0]
for j in range(len(pmi)):
k = pmi[i]
dec = aitime - k
if dec > 0:
fidle = idletime + dec
e = sum(ai)
print(e)
production = e + fidle # Production Time
print("Idle Time of Assembly Unit is: ", fidle)
print("Mobiles Phones should be produced in the order: ")
for l in range(len(pmi)):
ind = pmicopy.index(pmi[l])
print(ind+1)
print("Production Cost is: ", production)
Order()
COMMENT DOWN FOR ANY QUERIES AND,
LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.