Edited for a larger tree, for more examples.
I have a tree structure that I need to generate all of the possible permutations of, with some restrictions. Given a tree like this:
A1----(B1, B2)
\
\___(C1)
\__(E1, E2)
/
- A2----(B3, B4)
\ \ \
\ \__(D1)
\
\_(F1, F2)
|
(D4)
A3----(B5, B6)
\__(D2, D3)
Or if this is a little vague here's the same structure done with a Perl notation:
my $nodes = [
{
name => 'A1',
children => [
[ { name => 'B1', children => [] },
{ name => 'B2', children => [] }
],
[
{ name => 'C1',
children => [
[
{ name => 'E1', children => [] },
{ name => 'E2', children => [] }
]
]
}
]
]
},
{
name => 'A2',
children => [
[ { name => 'B3',
children => [
[
{ name => 'D1', children => [] }
]
]
},
{ name => 'B4', children => [] }
],
[
{ name => 'F1', children => [] },
{ name => 'F2', children => [
[ { name => 'D4', children => [] } ]
]
}
]
]
},
{
name => 'A3',
children => [
[ { name => 'B5', children => [] },
{ name => 'B6', children => [
[ { name => 'D2', children => [] },
{ name => 'D3', children => [] }
]
]
}
]
]
}
];
(Frankly, if you can figure this out in readable Perl I'll take that too.)
I'm looking to traverse the tree and retrieve all of the possible "paths" from the top level downward. All of the descendant groups of a node have to be represented by exactly 1 member in the "path". For example, in A1 one of (B1, B2) and one of (C1) needs to be represented. So each path descending from A1 will begin with either:
A1 B1 C1
or
A1 B2 C1
If B1, B2, or C1 have children, those will need to be represented as well.
Working this out by hand for the above tree I get these possibilities:
A1 B1 C1 E1
A1 B1 C1 E2
A1 B2 C1 E1
A1 B2 C1 E2
A2 B3 D1 F1
A2 B3 D1 F2 D4
A2 B4 F1
A2 B4 F2 D4
A3 B5
A3 B6 D2
A3 B6 D3
Each node here is a DataRow object:
internal class DataRow
{
internal string tag = "";
internal int id = 0;
internal Dictionary<string, List<DataRow>> children = null;
internal DataRow(int id, string tagname)
{
this.tag = tagname;
this.id = id;
} internal void AddChildren(string type, List<DataRow> drList)
{
if (children == null)
children = new Dictionary<string, List<DataRow>>();
if (!children.ContainsKey(type))
children[type] = new List<DataRow>();
children[type].AddRange(drList);
}
internal void AddChild(string type, DataRow dr)
{
List<DataRow> drList = new List<DataRow>();
drList.Add(dr);
AddChildren(type, drList);
}
public override string ToString()
{
return this.tag + " " + this.id;
}
}
To build the sample tree above (except for the E and F levels, added later):
DataRow fullList = new DataRow(null, "");
DataRow dr, dr2, dr3;
// First node above
dr = new DataRow(1, "A");
List<DataRow> drList = new List<DataRow>();
drList.Add(new DataRow(1, "B"));
drList.Add(new DataRow(2, "B"));
dr.AddChildren("B", drList);
drList.Clear();
dr2 = new DataRow(1, "C");
dr2.AddChild("C", new DataRow(1, "E"));
dr2.AddChild("C", new DataRow(2, "E"));
drList.Add(dr2);
dr.AddChildren("C", drList);
fullList.AddChild("A", dr);
// Second Node above (without the "F" stuff)
drList.Clear();
dr = new DataRow(3, "B");
dr.AddChild("D", new DataRow(1, "D"));
drList.Add(dr);
drList.Add(new DataRow(4, "B"));
dr = new DataRow(2, "A");
dr.AddChildren("B", drList);
fullList.AddChild("A", dr);
// Third node above
drList.Clear();
dr3 = new DataRow(6, "B");
dr3.AddChild("B", new DataRow(2, "D"));
dr3.AddChild("B", new DataRow(3, "D"));
dr2 = new DataRow(3, "A");
dr2.AddChild("B", new DataRow(5, "B"));
dr2.AddChild("B", dr3);
fullList.AddChild("A", dr2);
Walking the entire tree is trivial:
internal void PermutedList()
{
if ( children == null ) return;
foreach (string kidType in children.Keys)
{
foreach (DataRow dr in children[kidType])
{
dr.PermutedList();
}
}
}
But that's not what I need. This problem is a full tree walk, but in a specific order. What am I not getting? What kind of walk is this?
I've got a messy & slow implementation of this I wrote in Perl 10 years ago, but I can't decipher my own code anymore (shame on me!).
Edit: The graph and the lists below have been expanded, the code has not.
If I could describe the graph, I could program it. If I knew what it was called, I could look it up. But I can't. So let me explain a little further.
The bucket names aren't significant!
Each node has "buckets of children". A1 has two buckets one containing "B" and another containing "C". If that's all it had (and C didn't have buckets under it) I would have "A1 B1 C1" and "A1 B2 C1" -- at least one representative from all of the child buckets present.
So I think each bucket needs the cross-product of its children (all the way down).