title: Seat guests based on prioritized parameters
body: <p>The following data model represents tables with seats and guests, in an application that lets a user create tables and seats, visually using HTML5.</p>
  
  <pre><code>// The data model
  var data = {
      guests: [], // id, name, tags
      tables: [], // id, seats
      seats: [], // id, guest
      tags: [] // id, name
  };
  </code></pre>
  
  <p>The guests have tags (kind of categories) attached to them. These tags can be prioritized and set to work as "grouping" or "ungrouping" parameters. The user then clicks "Seat", and the guests are seated (seemingly randomly), while the prioritized parameters are respected.</p>
  
  <p>Full-blown example: <a href="http://jsfiddle.net/kBp49/2/" rel="nofollow">http://jsfiddle.net/kBp49/2/</a> (look for "SOLUTION GOES HERE" in JS panel)</p>
  
  <p><strong>The question is:</strong> How do I implement a function that seats the guests to the tables, while taking into consideration that some guests should be seated next to each other and others should be seated apart from each other in order to create one of the best seaating configurations? The number of guests can exceed 1000 but not 2000.</p>
  
  <h2>Real life example, to clarify the problem</h2>
  
  <p>Let's say we have 3 tables. They have 4 seats each. Let's also say that we have 9 guests to fill these tables with. They are as follows:</p>
  
  <ol>
  <li>Guest 1, Jewish, from US</li>
  <li>Guest 2, Jewish, from UK</li>
  <li>Guest 3, Christian, from US</li>
  <li>Guest 4, Christian, from UK</li>
  <li>Guest 5, Christian, from Sweden</li>
  <li>Guest 6, Atheist, from UK</li>
  <li>Guest 7, Atheist, from Sweden</li>
  <li>Guest 8, Muslim, from Saudi Arabia</li>
  <li>Guest 9, Muslim, from UK</li>
  </ol>
  
  <p>Now, the user prioritizes the parameters like this. First is most important.</p>
  
  <ol>
  <li>I want those with the same religion to sit apart from each other</li>
  <li>I want those with the same geographical location to sit next to each other</li>
  </ol>
  
  <p>This is OK:</p>
  
  <ol>
  <li>Table 1: Guests: 1, 3, 7, 8</li>
  <li>Table 2: Guests: 2, 4, 6, 9</li>
  <li>Table 3: Guests: 5</li>
  </ol>
  
  <p><strong>Update</strong>
  One solution could be the Minimax algorithm. It would calculate a score for each possibility and present the best found solution (found after, say 10 seconds of calculation). The algorithm is what I need help with, the implementation itself will of course require decisions that only I can make.</p>