title: Appointment scheduling algorithm (N people with N free-busy slots, constraint-satisfaction)
body: <p><strong>Problem statement</strong></p>
  
  <p>We have one employer that wants to interview N people, and therefore makes N interview slots. Every person has a free-busy schedule for those slots. Give an algorithm that schedules the N people into N slots if possible, and return a flag / error / etc if it is impossible. What is the fastest possible runtime complexity?</p>
  
  <p><strong>My approaches so far</strong></p>
  
  <p>Naive: there are N! ways to schedule N people. Go through all of them, for each permutation, check if it's feasible. O( n! )</p>
  
  <p>Backtracking: </p>
  
  <ol>
  <li>Look for any interview time slots that can only have 1 person. Schedule the person, remove them from the list of candidates and remove the slot. </li>
  <li>Look for any candidates that can only go into 1 slot. Schedule the person, remove them from the list of candidates and remove the slot. </li>
  <li>Repeat 1 &amp; 2 until there are no more combinations like that.</li>
  <li>Pick a person, schedule them randomly into one of their available slots. Remember this operation.</li>
  <li>Repeat 1, 2, 3 until we have a schedule or there is an unresolvable conflict. If we have a schedule, return it. If there's an unresolvable conflict, backtrack.</li>
  </ol>
  
  <p>This is O( n! ) worst case, I think - which isn't any better.</p>
  
  <p>There might be a D.P. solution as well - but I'm not seeing it yet.</p>
  
  <p><strong>Other thoughts</strong></p>
  
  <p>The problem can be represented in an NxN matrix, where the rows are "slots", columns are "people", and the values are "1" for free and "0" for busy. Then, we're looking for a row-column-swapped Identity Matrix within this matrix. Steps 1 &amp; 2 are looking for a row or a column with only one "1". (If the rank of the matrix is = N, I that means that there is a solution. But the opposite does not hold)
  Another way to look at it is to treat the matrix as an unweighed directed graph edge matrix. Then, the nodes each represent 1 candidate and 1 slot. We're then looking for a set of edges so that every node in the graph has one outgoing edge and one incoming edge. Or, with 2x nodes, it would be a bipartite graph.</p>
  
  <p>Example of a matrix:</p>
  
  <pre><code>1 1 1 1
  0 1 1 0
  1 0 0 1
  1 0 0 1
  </code></pre>