Skip to content

Instantly share code, notes, and snippets.

@mzdravkov
Created October 6, 2014 22:00
Show Gist options
  • Save mzdravkov/078242e9f510d468b08d to your computer and use it in GitHub Desktop.
Save mzdravkov/078242e9f510d468b08d to your computer and use it in GitHub Desktop.
nodes = %w[1 2 3 4 5 6 7 8]
constraints = {"1" => %w[3 6],
"2" => %w[1],
"4" => %w[3],
"5" => %w[3 4],
"6" => %w[1 3]}
edges = nodes.combination(2).to_a
nodes.map { |n| constraints[n].map { |c| edges.delete([n, c].sort) } if constraints[n] }
values = {"1" => 33,
"2" => 15,
"3" => 15,
"4" => 9,
"5" => 8,
"6" => 7,
"7" => 4,
"8" => 4}
all_coallitions = []
nodes.permutation.each do |p|
all_coallitions << p.inject([]) do |group, c|
unless group.map { |n| edges.include?([n,c].sort) }.include?(false)
group << c
else
group
end
end
end
uniq_coallitions = all_coallitions.map(&:sort).uniq
p uniq_coallitions
p uniq_coallitions.map { |c| c.inject(0) { |sum, n| sum + values[n] } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment