1

I am writing a program that can calculate all possible places for people sitting at a random amount of tables (can go up to 1000+):

enter image description here

As you see on the image, the black dots represent people (but in the computer, there are different types of people.) There are two types of tables : blue and pink ones. The blue ones can contain 3 people and the pink 2 people.

To get all possible places for any person to sit I could use foreach loops (8of them) and then I can run some extra code...

But what happens if I add 200 tables? Then do I need to use 200 foreach loops? Is there any way that this can be coded faster and less-space-consuming-coded?

What I tried? =>

  switch(listoftables.Count)
  {
      case 1:foreach(Table table in listoftables){ //code to add people to this table}break;
      case 2: foreach(Table table1 in listoftables)
      {foreach(Table table1 in       listoftables){//code to add people to this table
   }}break;
   }

INPUT : array with editable Table class objects (its a class created by myself) PROCESS : the above List is edited and is added to another List object, where after the whole foreach process has ended, the OUTPUT will write all possible configurations (who are in the other List object) to the screen.

Example part of output :

  // List<Table> listofalltables was processed
  List<listofalltables> output 

=> contains as [0] in array : List first => contains as [0] in array : Table.attachedpeople (is list)

  • 2
    show us what you tried. – Björn Roberg Sep 04 '13 at 11:24
  • Do you have an array of the tables? – H H Sep 04 '13 at 11:24
  • yes, just a list with each different object containing one table, who in each case has a maximum of 2 persons sitting there –  Sep 04 '13 at 11:25
  • do you need a count of the places or actual positions? – Johan Sep 04 '13 at 11:26
  • We need some example code to see what you are talking about. It is difficult without the code. – Dialecticus Sep 04 '13 at 11:27
  • each table has individual properties, its just like a class, you can assign the places to the table object, which is stored in a list.. The actual condition can be derived from the index number of the table object in the list –  Sep 04 '13 at 11:28
  • 9
    The number of ways to arrange just 100 people is ~9.3e157. If you could list a billion of those ways per second, it would still take more than 2.9e132 billion years... Now, about those 1000+ tables... – Matthew Watson Sep 04 '13 at 11:29
  • @Matthew Watson : I know but : I use multithreading for such purpose that can allow me to shorten that calculation time (however there won't be so much tables, it was just to illustrate that coding too much foreach loops is impossible) –  Sep 04 '13 at 11:30
  • Well, I guess you can do a recursive method that take in parameter a list of tables. Once you have give someone a sit you call the same function again and stop when there is no more room... – Guigui Sep 04 '13 at 11:40
  • @Guigui I'm not very expierienced with that, could you just give a very small code example? –  Sep 04 '13 at 11:42
  • 2
    @user2698666 and 2.9e132 billion years on an 8-core is .... – H H Sep 04 '13 at 11:46
  • yes yes I know that ... it was just to illustrate that coding too much foreach loops is impossible –  Sep 04 '13 at 11:48
  • The code that you provided makes no sense to me. What is the input and the output of your program? Please provide an example. – Dialecticus Sep 04 '13 at 11:50
  • An output would help alot :) – flindeberg Sep 04 '13 at 11:57
  • Is it so hard to add exact code you're using? Just describing what you're using for input and output doesn't help much, especially if you're using custom classes. – Marko Gresak Sep 04 '13 at 12:07
  • Look at the answer that Guigui gave, that is a good answer but i'm still looking in to it... –  Sep 04 '13 at 12:09

3 Answers3

0

Try a recursive method. A small example :

public List<Table> assignToTable(List<Person> invited, List<Table> tables)
{
    if(!tables.HasRoom)
         return tables;
     else
     {
         assign(tables,invited) //code to add a person to a table
         assignToTable(invited, tables);
     }
}

If I were you I'll create a object taht represent you tables with a propertie to know if there is still some room avaiblable. This will assign to every people a table without any foreach. Then in you main you could have a method that will rearrange the tables in all the way possible : Table 1 Table 2 Table 3

Then

Table 1 Table 3 Table 2 ...

Table 3 Table 2 Table 1

and call the recursive method on those lists and you will have all the possibility where poeple can sit...

Guigui
  • 1,105
  • 1
  • 11
  • 21
  • beware, as stated in the comments, depending on you number of tables this could take a looooooooooong time and if you have too many tables you could end on a StackOVerFlow (I don't know what is the limit of the Stack in .net) – Guigui Sep 04 '13 at 11:59
  • I know, but I think that I would only need to get to an amount of 100 tables –  Sep 04 '13 at 12:02
  • It is like trying to find a 100chars password... with 100 possibilities for the first char... Honestly I wish you good luck, that does not seem possible to me... As @mattew Watson said, the list of possiblities are 100! (factorial 100)... 9.33e157 possibilities just for 100 tables :s – Guigui Sep 04 '13 at 12:12
0

Taken @Guigui's answer and changed it to how I interpret the question. This will try to seat everyone everywhere (except when there are more people than chairs, is that a case? I assumed more chairs than people) by recursion and loops, as you see the complexity will be of the form O(Math.Power(nrPeople, nrSeats)) which is a lot (if I'm not mistaken).

Person[] peopleInvited = ....; // Avoid copying this, we are not modifying it
public void AssignToTable(int invited, SeatingArrangements tables)
{
  if(!tables.HasRoom || invited == peopleInvited.Length)
    // Do what? Print the seating?
  else
  {
    var personToSeat = peopleInvited[invited];
    foreach (var possibleSeating in tables.GetEmptyChairs()) 
    {
      // Add one person to a table somewhere, but don't modify tables
      var newArrangments = Assign(possibleSeating, personToSeat, tables) 
      AssignToTable(invited + 1, newArrangements);
    }
  }
}
flindeberg
  • 4,887
  • 1
  • 24
  • 37
  • ok- thanks - i'll also look into this but right now I need to log off - I'll reply soon! –  Sep 04 '13 at 12:21
0

Well, it more a question about math than programming. What you are trying to do is creating permutations of people. Basically you have N tables and 2N+2 seats. You can assign a number to each seat. Then the result will be the set of K-permutations of 2N+2, where K is the number of people invited, and N is the number of tables.

You can do this using loops, but you can also do it recursively. There are algorithms ready to use out there. For example:

Algorithm to generate all possible permutations of a list?

Community
  • 1
  • 1
Arie
  • 5,251
  • 2
  • 33
  • 54
  • Actually, you cannot do it with only loops in a static language, you have to create the loops dynamically. Which normally is done via recursion. – flindeberg Sep 04 '13 at 12:33