Secret santa algorithm

前端 未结 9 1426
温柔的废话
温柔的废话 2020-12-04 22:26

Every Christmas we draw names for gift exchanges in my family. This usually involves mulitple redraws until no one has pulled their spouse. So this year I coded up my own

9条回答
  •  没有蜡笔的小新
    2020-12-04 22:43

    Hmmm. I took a course in graph theory, but simpler is to just randomly permute your list, pair each consecutive group, then swap any element that is disallowed with another. Since there's no disallowed person in any given pair, the swap will always succeed if you don't allow swaps with the group selected. Your algorithm is too complex.

提交回复
热议问题