require 'set'connections = { 1 => 2, 2 => 3, 3 => 1, 4 => 1 }[1,2,3,4].to_set.divide { |x,y| connections[x] == y }# => #, #}>