我只是保留一个(user_id, preferred_id, dispreferred_id)
与每个选择相对应的三元组数据库。
编辑:有一点时间玩这个。对于数百万个评分,以下内容会很慢,并且也会占用内存,但可能会给您一些想法。如果你这样做,你可能应该从 crontab 异步运行,而不是按需运行。
require 'set'
choices = [
[1, 4],
[1, 5],
[2, 3],
[2, 4],
[3, 1],
[4, 2],
[4, 3],
[5, 1],
[6, 7],
[8, 4],
]
dominates = Hash.new { |hash, key| hash[key] = Set.new }
choices.each do |p, d|
dominates[p].add(d)
end
prev_dominates = nil
while dominates != prev_dominates
prev_dominates = Hash.new
dominates.each { |big, smalls| prev_dominates[big] = smalls.clone }
prev_dominates.each do |big, smalls|
smalls.each do |small|
if prev_dominates.include?(small)
prev_dominates[small].each do |smaller|
if smaller != big and !prev_dominates[smaller].include?(big)
dominates[big] << smaller
end
end
end
end
end
end
top = dominates.max_by { |big, smalls| smalls.size }[0]
puts dominates.inspect
puts "BEST: #{top}"
顶部节点是最终支配大多数其他节点的节点。然而,鉴于图可以是循环的,如果另一个节点会更快地完成循环,我们将切断循环。