-3

There are n jobs to be scheduled on m machines, with each machine taking a different time t_i for a job. The machines are ordered, with priority given to 1st machine if free and so on.

I have to code in C++ an algorithm to efficiently calculate the machine on which the nth job will run.

So far, my pseudo-code looks like this :

initialise rem_time[m] to 0 // Remaining time for m machines
for each element(i) in job array
  machine(j)= find_min(rem_time[]) //Find the lowest rem_time among all machines
  append joblist[j] with element_i   
  rem_time[j] += t_j

I'm looking for other solutions I can use here to further optimise the solution as using find_min n times, and storing all the jobs scheduled on every machine seems like a waste.

Thanks!

  • For anyone downvoting, any criticism they might want to leave behind would be welcome so that I can improve and/or possibly remove the question on seeing the problem. Thanks! :) – creativesol Jun 11 '17 at 08:33
  • I suggest you show us your current solution in code. In addition, when asking for a better solution, it's beneficial to state better in what way. – o_weisman Jun 11 '17 at 08:42
  • Thank you. Have provided pseudo-code of my current solution and clarified what I am looking for. – creativesol Jun 11 '17 at 09:06
  • Take a look at any competent algorithms/data structure book, and you will get an answer. – Passer By Jun 11 '17 at 09:39
  • Maybe it's just me, but your pseudo-code looks like gibberish. I'd suggest that you try to execute it with a simple case on paper. – shinobi Jun 11 '17 at 12:09

1 Answers1

0

One method to improve the algorithm would be to use a binary heap: https://en.wikipedia.org/wiki/Binary_heap (I suggest you read the entry).
If n < m, your immediate answer is machine[n] obviously.
If n > m, you build a min-heap for the machines. Each node in the heap stores the amount of time its current assigned job takes as well as the index of the machine running it. The amount of time is the key for the heap.
It takes O(m) to create the heap. You initialize the heap with the first m jobs, so your nodes will have key = joblist[1] machine = 1 to key = joblist[m], machine = m.
Now you start your loop for each consequent job: You find the job in your heap with the minimum time and remove it from the heap (while storing the index of the machine) which takes O(log m), you then add a new node with its job time being the time for the next job and the machine index you just removed from the heap. This also capped by O(log m).
When you reach the job you want j, you just get the index of the machine you removed before you are about to insert it to the heap and that is your answer. It's easy to see that this process is capped by O(n log m) in case j = n. I'll leave the implementation to you, it shouldn't be too hard using the Wikipedia entry.
If you need more help, don't hesitate to ask.

o_weisman
  • 804
  • 7
  • 19