Given a Map[String, Set[String]] what is an elegant and efficient way in Scala to determine the set of all pairs of distinct keys where the corresponding sets have a non-empty intersection?
For example fix the map as
val input = Map (
"a" -> Set("x", "z"),
"b" -> Set("f")
"c" -> Set("f", "z", "44")
"d" -> Set("99")
)
then the required output is
Set(
("a", "c"),
("b", "c")
)
Efficient in this context means better than O(n^2) where n is the sum of the number of elements in the family of sets given as input.