2

I'm creating an app that receives some map between finals and the respective periods during which they are held and a student's required finals. I was wondering if there was an algorithm to enumerate all complete schedulings of these finals. Concretely, I have a list of finals for a specific student and each $F_i$ has a list of periods $P_i$ that they are held. In my specific use case, there are $8$ periods.

$$ \begin{align} F_1 &\rightarrow \{P_1, P_2, P_8\} \\ F_2 &\rightarrow \{P_4, P_7\} \\ F_3 &\rightarrow \{P_4\} \end{align} $$

And I want to find all assignments of periods to finals that are non-overlapping and complete, i.e. the student can attend all finals. One such assignment could be:

$$ \begin{align} P_1 &\rightarrow F_1 \\ P_4 &\rightarrow F_3 \\ P_7 &\rightarrow F_2 \end{align} $$

This is complete because all finals are associated to a period and the periods are non-overlapping. Another assignment could be $P_4 \rightarrow F_3, P_7 \rightarrow F_2, P_8 \rightarrow F_1$. My question is then, how could I enumerate all these assignments? I was thinking of a greedy approach whereby I choose a period starting with the finals with the fewest amount of available periods, the choosing for the next-most restricted final. But that only gives me one particular working schedule, not all valid schedulings.

Andrew Li
  • 135
  • 4

1 Answers1

3

You're asking to enumerate all maximum matchings in a bipartite graph. Unfortunately that problem is #P-complete, so there is unlikely to be any efficient algorithm that works on arbitrary size graphs.

There might be algorithms that work well enough for your situation, given the small numbers you're dealing with. For instance, a simple approach is to use a backtracking search. First nondeterministically choose a period for final $F_1$ (out of all possibilities). Next choose a period for final $F_2$ (out of all possibilities, excluding the periods that have already been chosen), and so on. If at any point there are no legal choices available (e.g., you want to choose a period for $F_i$ but all options have already been filled with a final), terminate. Now replace the nondeterministic choices with branching over all possibilities, in a breadth-first or depth-first search, and that should provide a way to enumerate all valid schedules.

See also Counting and finding all perfect/maximum matchings in general graphs and https://en.wikipedia.org/wiki/Matching_%28graph_theory%29#Counting_problems for references to the literature on the general problem.

D.W.
  • 167,959
  • 22
  • 232
  • 500