我可以使用哪种算法在n个部分中平均分配加权对象?

时间:2009-08-27 10:23:20

标签: algorithm

我想将x(i)个对象(x E {1...n})(每个对象的权重w(i))分发到n个部分。

应该以这样的方式进行分配,即对于所有部分,权重之和尽可能相等。

干杯! PRATIK

2 个答案:

答案 0 :(得分:10)

计算权重的总和,除以n,部分的数量,以获得所需的部分权重。然后使用bin packing algorithm尝试填充此最大尺寸的n个bin。

请注意,所有权重都必须小于部分权重才能正常工作。否则你将无法在任何地方放置重量大的物品。

答案 1 :(得分:2)

我认为你在描述Multiprocessor scheduling问题。

这是朱莉娅的实施:

"""
Solves the Multiprocessor Scheduling problem using the Longest Processing Time algorithm

    PROBLEM: (NP-hard)
        Given:
            - set of jobs, each with a length
            - a number of processors
        Find:
            - divide the jobs among the processors such that none overlap
              which minimizes the total processing time

    ALGORITHM:
        - sort the jobs by processing time
        - assign them to the machine with the earliest end time so far
        Achieves an upper bound of 4/3 - 1/(3m) of optimal

    RETURNS:
        assignments, ith index → machine for the ith job
"""
function multiprocessor_scheduling_longest_processing_time{R<:Real}(
    jobs::AbstractVector{R},
    m::Integer, # number of processors
    )

    durations = zeros(R, m)
    assignments = Array(Int, length(jobs))

    for i in sortperm(jobs, rev=true) # p[1] is the index of the longest job in `jobs`
        best_index = indmin(durations)
        durations[best_index] += jobs[i]
        assignments[i] = best_index
    end

    assignments
end

如果你使用优先级队列,你可能会做得更好。