📜  作业排序问题

📅  最后修改于: 2021-04-27 22:11:22             🧑  作者: Mango

给定一系列工作,其中每个工作都有一个截止日期,如果在截止日期之前完成工作,则每个工作都有相关的利润。还假定每个工作都占用单个时间单位,因此任何工作的最小可能截止期限为1。如果一次只能安排一个工作,那么如何使总利润最大化。

例子:

Input: Four Jobs with following 
deadlines and profits
JobID  Deadline  Profit
  a      4        20   
  b      1        10
  c      1        40  
  d      1        30
Output: Following is maximum 
profit sequence of jobs
        c, a   


Input:  Five Jobs with following
deadlines and profits
JobID   Deadline  Profit
  a       2        100
  b       1        19
  c       2        27
  d       1        25
  e       3        15
Output: Following is maximum 
profit sequence of jobs
        c, a, e

一个简单的解决方案是生成给定作业集的所有子集,并检查单个子集以确保该子集中作业的可行性。跟踪所有可行子集中的最大利润。该解决方案的时间复杂度是指数级的。
这是标准的贪婪算法问题。

以下是算法。

以下是上述算法的实现。

C++
// Program to find the maximum profit job sequence from a given array
// of jobs with deadlines and profits
#include
#include
using namespace std;
 
// A structure to represent a job
struct Job
{
   char id;     // Job Id
   int dead;    // Deadline of job
   int profit;  // Profit if job is over before or on deadline
};
 
// This function is used for sorting all jobs according to profit
bool comparison(Job a, Job b)
{
     return (a.profit > b.profit);
}
 
// Returns minimum number of platforms reqquired
void printJobScheduling(Job arr[], int n)
{
    // Sort all jobs according to decreasing order of prfit
    sort(arr, arr+n, comparison);
 
    int result[n]; // To store result (Sequence of jobs)
    bool slot[n];  // To keep track of free time slots
 
    // Initialize all slots to be free
    for (int i=0; i=0; j--)
       {
          // Free slot found
          if (slot[j]==false)
          {
             result[j] = i;  // Add this job to result
             slot[j] = true; // Make this slot occupied
             break;
          }
       }
    }
 
    // Print the result
    for (int i=0; i


Java
// Program to find the maximum profit
// job sequence from a given array
// of jobs with deadlines and profits
import java.util.*;
 
class Job
{
    // Each job has a unique-id,
    // profit and deadline
    char id;
    int deadline, profit;
 
    // Constructors
    public Job() {}
 
    public Job(char id, int deadline, int profit)
    {
        this.id = id;
        this.deadline = deadline;
        this.profit = profit;
    }
 
    // Function to schedule the jobs take 2
    // arguments arraylist and no of jobs to schedule
    void printJobScheduling(ArrayList arr, int t)
    {
        // Length of array
        int n = arr.size();
 
        // Sort all jobs according to
        // decreasing order of profit
        Collections.sort(arr,
                         (a, b) -> b.profit - a.profit);
 
        // To keep track of free time slots
        boolean result[] = new boolean[t];
 
        // To store result (Sequence of jobs)
        char job[] = new char[t];
 
        // Iterate through all given jobs
        for (int i = 0; i < n; i++)
        {
            // Find a free slot for this job
            // (Note that we start from the
            // last possible slot)
            for (int j
                 = Math.min(t - 1, arr.get(i).deadline - 1);
                 j >= 0; j--) {
 
                // Free slot found
                if (result[j] == false)
                {
                    result[j] = true;
                    job[j] = arr.get(i).id;
                    break;
                }
            }
        }
 
        // Print the sequence
        for (char jb : job)
        {
            System.out.print(jb + " ");
        }
        System.out.println();
    }
 
    // Driver code
    public static void main(String args[])
    {
        ArrayList arr = new ArrayList();
 
        arr.add(new Job('a', 2, 100));
        arr.add(new Job('b', 1, 19));
        arr.add(new Job('c', 2, 27));
        arr.add(new Job('d', 1, 25));
        arr.add(new Job('e', 3, 15));
       
        // Function call
        System.out.println("Following is maximum "
                           + "profit sequence of jobs");
 
        Job job = new Job();
 
        // Calling function
        job.printJobScheduling(arr, 3);
    }
}
 
// This code is contributed by Prateek Gupta


Python3
# Program to find the maximum profit
# job sequence from a given array
# of jobs with deadlines and profits
 
# function to schedule the jobs take 2
# arguments array and no of jobs to schedule
 
 
def printJobScheduling(arr, t):
 
    # length of array
    n = len(arr)
 
    # Sort all jobs according to
    # decreasing order of profit
    for i in range(n):
        for j in range(n - 1 - i):
            if arr[j][2] < arr[j + 1][2]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
 
    # To keep track of free time slots
    result = [False] * t
 
    # To store result (Sequence of jobs)
    job = ['-1'] * t
 
    # Iterate through all given jobs
    for i in range(len(arr)):
 
        # Find a free slot for this job
        # (Note that we start from the
        # last possible slot)
        for j in range(min(t - 1, arr[i][1] - 1), -1, -1):
 
            # Free slot found
            if result[j] is False:
                result[j] = True
                job[j] = arr[i][0]
                break
 
    # print the sequence
    print(job)
 
 
# Driver COde
arr = [['a', 2, 100],  # Job Array
       ['b', 1, 19],
       ['c', 2, 27],
       ['d', 1, 25],
       ['e', 3, 15]]
 
 
print("Following is maximum profit sequence of jobs")
 
# Function Call
printJobScheduling(arr, 3)
 
# This code is contributed
# by Anubhav Raj Singh


Javascript


输出
Following is maximum profit sequence of jobs 
c a e 

上述解决方案的时间复杂度为O(n 2 )。可以使用不交集数据结构对其进行优化。请参阅以下帖子以获取详细信息。

作业排序问题集合2(使用不交集)