Reputation: 5759
I am having trouble getting my head around how to approach this... I am not asking for an explicit answer, just a heads up about what approach to take. As I have run into problems with every one I have taken so far.
I have a set of nodes:
nodes = { ('A','B'),
('B','C'),
('C','D'),
('C','E'),
('B','E'),
('C','F')}
as a dictionary of sets:
nodedict = { 'A': {'B'},
'C': {'B', 'E', 'D', 'F'},
'B': {'A', 'C', 'E'},
'E': {'C', 'B'},
'D': {'C'},
'F': {'C'} }
what I want to do is to build a structure like this:
for 'A'
A
|
B
_________|_________
| |
C E
_____|_____ |
| | | C
D E F ____|____
| |
D F
so all possible routes from A can be found.
I have been using a list to represent the braches, and then trying to wrap a for loop in a while loop... appending each list with its children if the child is not in the list... but I keep coming unstuck. I sometimes get close, but this is when I am writing explicit for loops and know exactly what I am looking for.
is it best to try to get to one tip first, then backtrack and then to the next tip ...
for x in xs:
...
for a in x:
...
for b in a
but obviously I do not know how deep node 'n' goes... hmmm... any helpful suggestions much appreciated!
Upvotes: 1
Views: 71
Reputation: 36
I have no experience with python, however you have said you do not want an explicit answer.
You have a tree. This is one of the most well covered structures out there and there are many ways to represent it and traverse it.
At minimum you need to know the node at the root of the tree and be able to retrieve the children of any given node. From those two pieces of information it is possible to traverse the entire tree if you keep track of what you're doing.
Look into Tree traversal.
There is also a question on implementing a tree in python here.
Upvotes: 1