Proving the greedy solution to the weighted task scheduling problem - algorithm

I am attempting to prove the following algorithm is fully correct (partial correctness + termination), but I can only seem to prove for arbitrary example inputs (not general ones).
Here is my pseudo-code:
IN :Listofjobs J, maxindex n
1:S ← an array indexed 0 to n, with null at each index
2:Sort J in non-increasing order of profits
3:for i from 0 to n
4:Find the largest t such that S[t] = null and t ≤ J[i].deadline (if one exists) if an index t was found
5: S[t] ← J[i]
OUT: S maximizes the profit of scheduled jobs that can be done in n 1 unit of time blocks
So for example, I created a table with jobs and their associated attributes (deadlines and profit):
jOB J1 J2 J3 J4 J5
Deadline 2 1 3 2 1
profit 62 100 20 40 20
From this example input, we'd be able to do J2,J1,J3 for a total profit of 182.
Can someone help me form a more generic way of showing my pseudo-code algorithm is fully correct?

Adding constraints helps you here.
In step 2 order J first by non-increasing profits, and then by non-increasing deadline.
Of all optimal solutions, at least one must be lexicographically last by the ordering of profits of jobs over time. (If multiple jobs have the same profit and deadlines, there may be multiple optimal solutions that are lexicographically last.)
Prove that any solution that does not have a job of the same profit at the same time as the first job that you place is either not optimal, or is not lexicographically last among optimal solutions.
Prove by induction that the solution that you found is identical in placement of sizes of jobs as any solution that is optimal and lexicographically last among optimal solutions.
Please note that this is an outline only. There are some subtle tricks needed at each step of this proof that I'm deliberately leaving as an exercise.

Related

Assignment problem with multiple persons needed on each job

First, sorry if my English isn't so good, I'm not a native English speaker.
I'm facing an assignment problem.
I have a list of jobs, with a certain number of persons needed for each job. Each person will let me know on how many jobs they want to be and their preferences.
I tried to do that with the Hungarian Algorithm but it seems I'm unable to get it done. With a large number of jobs and spots, some persons got multiple time the same job, which isn't ok.
I think it's all due to the fact I considered each spot as an individual job and I listed each person as many times as they need to be placed.
Do you know a better algorithm or a way to do it?
(It's not a coding problem, I'm doing it in Octave/Matlab for now, but I think I'll switch to Python.)
Thanks for your help.
In addition to Henrik's suggestion to use Linear Programming, your specific problem can also be solved using Minimum cost maximum flow.
You make a bipartite graph between people and jobs (like in the Hungarian algorithm), where the cost on the middle edges are the preference scores, and the capacity is 1.
The capacity of the edges from the jobs to the sink is the number of people you need for that job.
Assignment problems can be solved with linear programming:
Let xij = 1 if person i is assigned to job j and 0 otherwise. Let aij be the rank for person i of job j : aij = 1 for the job he wants most, aij = 2 for the next and so on. If he only wants k jobs you put aij to a very high number for all jobs beyond those k.
If you need at least bj workers on job j you have the constraint
x1j + ... + xmj >= bj (j = 1,...,n)
You also have the constraints xij >= 0 and xij <= 1 .
The linear function to minimize is
sum( aij xij ) over all i,j

Profit dependent on the previous job time - Job Scheduling problem

There are n jobs that need to be processed on a single machine. Job j requires tj time units to execute and has a profit value of pj. All the jobs are to schedule in time W = summation of tjtime units.
Scheduling job j to start at time sj earns a profit (W - sj)*pj.
I have already tried a greedy approach for pj and sj individually as well pj*tj but have been able to come up with a counterexample. I think it can be solved by a greedy algorithm using pj/tj in decreasing order but not able to prove it. I am just looking for some hints on how to prove it formally.
An approach I have seen before is to consider swapping two adjacent jobs in a proposed schedule. Suppose we have 1,2, where other stuff will take time K and then we hit a deadline. This is better left unswapped if
p1(K + t2) + p2K > p2(K + t1) + p1K
which simplifies to
p1t2 > p2t1
which simplifies to
p1 / t1 > p2 / t2
So if we sort in the way you guessed no swap of adjacent jobs will increase profits, but if there a schedule which does not follow this rule you can improve it by swapping adjacent jobs. So I think your guess is correct.

Follow up: Find the optimal sequence of stops where the number of stops are fixed

I'm trying to code the following problem:
There are n hotels given at a0, a1, ..., an such that 0 < a0 < a1 < ... < an. The only places you are allowed to stop are at these hotels, but you can choose which of the hotels you stop at. You must stop at the final hotel (at distance an), which is your destination. Moreover, you are required to complete your journey in exactly d days (i.e have to make d-1 stops in between). If you travel x miles during a day, the cost for that day is x2. You want to plan your trip so as to minimize the total cost - that is, the sum, over all travel days, of the daily cost. Find the optimal sequence of hotels at which to stop.
I came up with this dp solution:
Let dp(i) be the minimum cost such that the last stop is hotel i.
Base case: dp(0)=0.
To compute dp(i), I consider all possible places 0<=k<i, that we might have stopped before. Thus, the recurrence relation becomes:
for i=1;i<=n;i++
dp(i)=inf
prev(i)=undefined
for k=0;k<i;k++
if (dp(i)>dp(k)+(ai-ak)^2)
dp(i) = dp(k)+(ai-ak)^2)
prev(i) = k
How do we make sure that this algorithm takes exactly d stops?
I wrote this answer on your previous question too but I think you overlooked it. Anyways, let me know if this approach is useful.
Your algorithm would give you the position of stops (int[] k) which would minimize your costs without any consideration of d. I take from your question that you want to convert these k stops to d stops.
There are 3 cases:
1)
k.length == d;
Problem solved
2)
k.length < d;
while(k.length!=d) Find the minimal distance(min) between a(n-1) and a(n) for all n. Start traversal from first hotel. Find the first occurrence where a(n) - a(n-1) is min. Now if these two hotels are in your k, find the next occurrence, otherwise break your stop to include this as a separate stop in k and repeat.
If you reach end of hotels, you can do this with the second minimal distance and so on until convergence.
3)
k.length > d;
while(k.length!=d) This time you need to find minimal distances between two neighbors in k. Merge the two neighbors as one stop, until convergence.
I am not sure if this approach is the optimal one or the right one, but these are my two cents.

Formulating a knapsack style solution for spoj "BEHAPPY"?

The problem is described at problem ,I have been trying to reach an iterative dp solution to the above problem.
What i can guess from my experience in coding is ,it should have three dimensions, each state uniquely identified by :-
M = Gifts not distributed yet.
N = first N girlfriends available (Similar to 0-1 Knapsack)
C = Maximum gifts allowable for current girlfriend.
Now initializing for M=0,N,C (i.e. When 0 gifts remain to be distributed)
1 2 3 4 (girlfriends)
0
1
2
3
(Capacity)
I seem to have a problem , with initialization at k=0 as there is a low and high gift limit both for girlfriends ,hence deviating from standard knapsack having only maximum limit(not considering that knapsack finds optimum solution,and here i consider all possible solution)
Of course i maybe completely on the wrong path here ,if you feel is the case, what is the recurrence and initialization for this 3 state variable dp?
Thanks in advance
First remove all the minimum requirements. M -= sum(A[i]) and B[i] -= A[i]. These are the minimum so there's nothing for us to move around, just assign them and take them out of the computation.
Now your solution matrix sol[g, m] is the number of ways you can solve the first g girlfriends while having m gifts left. sol[g,m] = sum(sol[g-1, m-j], j= [0..B[g-1]]. You initialize sol[0, M] with 1 and the rest is 0.
Your solution will be sol[N+1, 0].
You only need the last line if you do it iteratively.

Resource allocation- Matching

I've got a problem which I'm not sure can be solved by Linear Programming. Essentially there are 2 groups of people who are list their preference for one another and will be subsequently matchd. I'm writing an algorithm for this. Group A has upto 4 choices from Group B and vice versa.
In formulating a solution, I am currently assigning a cost to each combination of pairs. For example if Person 1 from Group A ranks Person 3 from Group B as his/her number 1 choice and vice versa, then the cost is minimal (Pair 1-3 cost: 0.01). Similarly, I would allot a cost to other pairs, devising an objective function which seeks to have pairings which minimize overall cost.
However, I do not see this being feasible because I don't know how to define my constraints and overall objective function. Reading online and from textbooks, I find resource allocation problems to be different from what I am trying to do.
Can I seek your advise on how to proceed?
Your problem can be formulated as an "Assignment Problem." As a canonical case, assignment problems are for assigning "jobs" to "machines." They can just as easily be used for Matching two sets.
Here's the formulation:
Two sets of people A and B
Decision Variable Xij
Let Xij be 1 if person i (ith person in set A) is matched with jth person in set B; 0 otherwise
Parameters:
Let Cij be the cost of pairing person i with person j
Objective Function: Minimize (Sum over i) (sum over j) Cij * Xij
Constraints:
Every Person i gets paired exactly once
Sum over j Xij = 1 (for each i)
Every Person j gets paired exactly once
Sum over i Xij = 1 (for each j)
Xij are Binary variables
Xij = (0,1)
The neat thing about Assignment problems is that the optimal pairings can be found using the fairly easy to understand 'Hungarian Method.' You can also use an LP/IP solver you have at your disposal.
Hope that helps.

Resources