I have 20 pots that are used to create a schedule of 2 pot groupings. I need to find all possible schedules that can be created.
// create range of pots
IEnumerable<int> pots = Enumerable.Range(1, 20);
Get all the possible 2 pot groups:
public DataTable CreateCombinations(IEnumerable<int> pots)
{
DataTable testTable = new DataTable();
testTable.Columns.Add("Pot1");
testTable.Columns.Add("Pot2");
for (int i = 0; i < pots.Count(); i++)
{
for (int j = i + 1; j < pots.Count(); j++)
{
var row = testTable.NewRow();
row["Pot1"] = pots.ElementAt(i);
row["Pot2"] = pots.ElementAt(j);
testTable.Rows.Add(row);
}
}
return testTable;
}
List of all the possible schedules that can be created from the 1,n combinations:
public DataTable CreatePossibleSchedules(DataTable dt)
{
DataTable testTable = new DataTable();
testTable.Columns.Add("Pot1");
testTable.Columns.Add("Pot2");
testTable.Columns.Add("Schedule");
for (var i = 0; i < dt.Rows.Count; i++)
{
if(dt.Rows[i]["Pot1"].ToString() == "1")
{
for (var x = i; x < dt.Rows.Count; x++)
{
string pot1 = dt.Rows[x]["Pot1"].ToString();
string pot2 = dt.Rows[x]["Pot2"].ToString();
string schedule = (i + 1).ToString();
bool exists = testTable.AsEnumerable().Any(row =>
(pot1 == row.Field<string>("Pot1") || pot1 == row.Field<string>("Pot2") ||
pot2 == row.Field<string>("Pot1") || pot2 == row.Field<string>("Pot2")) &&
schedule == row.Field<string>("Schedule"));
if (!exists)
{
var row = testTable.NewRow();
row["Pot1"] = dt.Rows[x]["Pot1"];
row["Pot2"] = dt.Rows[x]["Pot2"];
row["Schedule"] = i + 1;
testTable.Rows.Add(row);
}
}
}
}
return testTable;
}
This gives me the possible schedules for 1,2 through 1,20:
Schedule 1:
1,2
3,4
...
19,20
Down to Schedule 20:
1, 20
2, 3
18, 19
What I don't know how to do is start over to start getting the rest of the possible schedules:
Schedule 21:
1, 2
3, 5
4, 6
Hope this makes sense, not sure if I've explained the problem very well.