Shortest Remaining Time Scheduling - cpu

Can the results of a Shortest Remaining Time Schedule be the same preemptive and non preemptive? Like, can you still get the same times at the end of both or is this impossible?

It is not impossible.It depends on the arrival time and burst time of the processes.
For example:If we have processes with following arrival time and burst times.
P1 1,2
P2 2,4
P3 3,5
It gives the same result,be it preemptive or non preemptive.

Related

Max Tasks that can be completed in given time

I recently came across a this question in a forum:
You are given a straight line starting at 0 to 10^9. You start at zero and there are n tasks you can perform. i th task is located at point i in the line and requires 't' time to be performed. To perform the task you need to reach the point i and spend 't' time at that location.
example: (5,8) lies at 5 so travel distance is 5 and work effort is 8.
Total effort is calculated as travel distance + time required to complete the work.
It takes one sec to travel one unit of path.
Now we are given total T seconds and we need to complete as many tasks as possible and reach back to starting position
Find the max number of tasks that you can finish in time T.
example :
3 16 - 3 tasks and 16 units of total time
2 8 - task 1 at position 2 in line and takes 8 sec to complete
4 5 - task 2 at position 4 in line and takes 5 sec to complete
5 1 - task 3 at position 5 in line and takes 1 sec to complete
​​​​​​​
Output : 2
Explanation :
If we take task 1 at location 2 which requires 8 sec then getting to location 2 takes 2s and completing the task takes 8s leaving us with only 6s which is not enough for completing other task
On the other hand skipping the fist task leaves us enough time to complete the other two tasks.
Going to location and coming back costs 2x5 =10s and performing task at location 4 and 5 cost us 5+1 = 6s. Total time spent will be 10s+6s=16s.
I am new to graphs and DP so I was not sure which approach to use Hamiltonian cycle, Knapsack or Longest Path.
Can someone please help me with the most efficient approach to solve this.
Let's iterate from the first task to the last, according to distance. As we go, it's clear that after subtracting 2 * distance(i) + effort(i) for considering the current task as our last, the most tasks we can achieve can be found by greedily accumulating as many earlier tasks as possible into the remaining time, ordering them by increasing effort.
Therefore, an efficient solution could insert the seen element into a data-structure ordered by effort, dynamically updating the best solution so far. (I originally thought of using a treap and binary search but j_random_hacker suggested a much simpler way in the comments below this answer.)
Suggestion:
For each task n create a graph like this
Join up these graphs for all the tasks.
Run a travelling salesman algorithm to find the minimum time to do all the tasks ( = visit all the nodes in combined graph )
Remove tasks in an orderly sequence. This will give you a collection of results for different numbers of tasks performed. Choose the one that does the most number of tasks that still remains under the time limit.
Since you are maximizing the number of tasks performed, start by removing the longest tasks so that you will be left with lots of short tasks.

Greedy Algorithm: Assigning jobs to minimize cost

What is the best approach to take if I want to find the minimum total cost if I want to assign n jobs to a person in a sequence which have cost assigned to them? For eg. I have 2 jobs which have costs 4 and 5 respectively. Both jobs take 6 and 10 minutes respectively. So the finish time of the second job will be finish time of first job + time taken by this job. So the total cost will be finish time of each job multiplied by its cost.
If you have to assign n jobs to 1 person (or 1 machine) in scheduling literature terminology, you are looking to minimize weighted flow time. The problem is polynomially solvable.
The shortest weighted processing time sequence is optimal.
Sort and reindex jobs such that p_1/w_1 <= p_2/w_2 <= ... <= p_n/w_n,
where, p_i is the processing time of the ith job and w_i is its weight or cost.
Then, assign job 1 first, followed by 2 and so on until n.
If you look at what happens if you swap two adjacent values you will end up comparing terms like (A+c)m + (A+c+d)l and (A+d)l + (A+c+d)m, where A is the time consumed by earlier jobs, c and d are times, and l and m are costs. With some algebra and rearrangement you can see that the first version is smaller if c/m < d/l. So you could work out for each job the time taken by that job divided by its cost, and do first the jobs with smallest time per unit cost. - check: if you have a job that takes 10 years and has a cost of 1 cent, you want to do that last so that 10 year wait doesn't get multiplied by any other costs.

algorithm to find optimized scheduling

I have 7 tasks t1-t7, each task has an execution time associated (t1 takes 1 time unit,t2 takes 5 etc).
You don't have to pay for communication cost between t1 and t2 if you do them in the same processor. If you do t1 on p1 and t2 on p2, you need 5 time units to transfer data from p1 to p2. (5 is the weight of edge t1-t2)
As illustrated in the right image, the communication c(1,2) and c(3,4) can be done in the same time, they both end at the end of 5 time unit.
The makespan is the time taken to finish all 7 tasks. Given that there are as many processors as I want to, which algorithm can I use to find the scheduling result with minimum makespan (or at least near the minimum one)?
Please note that the results in the right image may not be optimized.

Algorithm to solve an assignment or matching with a constraint

Here is the problem. Suppose we have N workers and N jobs. We want to assign each job exactly one worker. For each worker i, he could do some jobs on some cost. Our goal is to minimize the total cost on the condition that any single cost should be less than some value.
For example, 10 workers and 10 jobs. Worker 1 can do job 1 with $0.8, job 2 with $2.3, job 3 with $15.8, jobs 4 to 8 with $100, job 9 with $3.2, job 10 with $15.3.
Worker 2 can do job 1 with $3.5, job 2 with $2.3, job 3 with $4.6, job 4 with $17, etc.
Our goal is to find a matching or we can call it an assignment such that the total cost is minimized but any single cost of the corresponding pair/matching between work i and job i is less than a value like $50.
I would very much like to solve it in MATLAB if possible.
This is a slight variation of the Assignment Problem. To handle your additional constraint that no single job cost should be more than some value, just change all entries in the matrix of costs that are greater than this threshold to a huge value (bigger than the sum of all other entries will suffice), and solve as usual, using e.g. the Hungarian Algorithm.

Priority based preemptive Shortest Job First. How to determine what process comes first

I have a question for Priority based preemptive Shortest Job First algorithm. If two processes have the same priority, who is the one to go first. The one that was put in first or the one with smaller burst time? Same goes with burst time if I have 2 processes with same burst time do I sort by priority? And what happens if 2 processes have same burst time and priority?
For example what would a Gantt chart based on this table look like?
Arrival Time Burst Time Priority
p0 0 8 2
p1 4 15 5
p2 7 9 3
p3 13 5 1
p4 9 13 4
p5 0 6 1
As the name implies, you first pick the set of highest priority jobs.
Then, from that set you select the shortest job. In this case I presume 'burst time' represents the expected execution time (or time to yield).
Therefore assuming that your lower priority numbers represent 'higher' priority jobs, p3 and p5 are the two highest priority jobs.
At that point, what matters is the expected job size (burst time) at which point you select the one with the shortest burst time. In this case it would be p3.

Resources