python 工作排序(Job Sequencing Problem)

        给定一个作业数组,其中每个作业都有一个截止期限,如果作业在截止期限之前完成,则可获得相关利润。此外,每个作业都占用一个单位时间,因此任何作业的最小可能截止期限都是 1。如果一次只能安排一项作业,则最大化总利润。

例子: 
输入:四个工作,截止日期和利润如下
JobID 截止期限 利润
  一 4 20   
  二 1 10
  三 1 40  
  四 1 30

输出:以下是工作利润最大的序列:c、a   

输入:  五项工作,截止日期和利润如下

JobID 截止期限 利润

  a 2 100
  b 1 19
  c 2 27
  d 1 25
  e 3 15

输出:以下是工作利润最大的序列:c,a,e

朴素方法:要解决问题,请遵循以下想法:

生成给定作业集的所有子集,并检查各个子集是否可行。跟踪所有可行子集中的最大利润。

作业排序问题的贪婪方法:
贪婪地首先选择利润最高的工作,方法是按利润降序对工作进行排序。这将有助于最大化总利润,因为为每个时间段选择利润最高的工作最终将最大化总利润

按照给定的步骤解决问题:

按利润的降序对所有工作进行排序。 
按利润递减的顺序对工作进行迭代。对于每项工作,执行以下操作: 
找到一个时间段 i,使得时间段为空、i < 截止时间且 i 最大。将作业放入 
此时间段并将此时间段标记为已填充。 
如果不存在这样的 i,则忽略该工作。 
下面是上述方法的实现: 

# Python3 code for the above approach
 
# 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's Code
if __name__ == '__main__':
    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

输出
以下是工作的最大利润序列

c a e

计算机辅助设计
时间复杂度: O(N 2 )
辅助空间: O(N)

使用优先级队列(最大堆)的作业排序问题:
按截止日期的升序对作业进行排序,然后从末尾开始迭代,计算每两个连续截止日期之间的可用时隙。当空时隙可用且堆不为空时,将作业的利润包含在最大堆的根部,因为这有助于为每组可用时隙选择利润最大的作业。

下面是上述方法的实现:

# Python3 program for the above approach 
import heapq
 
 
def printJobScheduling(arr):
    n = len(arr)
 
    # arr[i][0] = job_id, arr[i][1] = deadline, arr[i][2] = profit
 
    # sorting the array on the
    # basis of their deadlines
    arr.sort(key=lambda x: x[1])
 
    # initialise the result array and maxHeap
    result = []
    maxHeap = []
 
    # starting the iteration from the end
    for i in range(n - 1, -1, -1):
 
        # calculate slots between two deadlines
        if i == 0:
            slots_available = arr[i][1]
        else:
            slots_available = arr[i][1] - arr[i - 1][1]
 
        # include the profit of job(as priority), deadline
        # and job_id in maxHeap
        # note we push negative value in maxHeap to convert
        # min heap to max heap in python
        heapq.heappush(maxHeap, (-arr[i][2], arr[i][1], arr[i][0]))
 
        while slots_available and maxHeap:
 
            # get the job with max_profit
            profit, deadline, job_id = heapq.heappop(maxHeap)
 
            # reduce the slots
            slots_available -= 1
 
            # include the job in the result array
            result.append([job_id, deadline])
 
    # jobs included might be shuffled
    # sort the result array by their deadlines
    result.sort(key=lambda x: x[1])
 
    for job in result:
        print(job[0], end=" ")
    print()
 
 
# Driver's Code
if __name__ == '__main__':
    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)
 
# This code is contributed
# by Shivam Bhagat

输出
以下是作业的最大利润序列

a c e 

时间复杂度: O(N log N)
辅助空间: O(N) 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hefeng_aspnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值