0
votes

I have a table "Node" with fields "id" and "name", also I have a table "Link" that links Node among themselves as many-to-many.

Node
id | name
1    node1
2    node2
3    node3
4    node4
5    node5
6    node4
7    node5

Link 
id | node_id | node2_id
1        1           2
2        2           3
3        3           5
4        2           4
5        3           6
6        3           7

         node4   node7
          |       |
node1-->node2-->node3-->node6
                  |
                 node5

How do I use python to generate this graph, list or dict with nested. I have a problem with the construction of the algorithm. I have function get_derrived with returned a list of derived elements. My code is:

    c.tree = {}
    def get_tree(node_id):
        for node in get_derrived(node_id):
            if not node in c.tree:
                c.tree[node] = {}
                get_tree(node.id)           
    get_tree(id)

In sum I have for node1:

c.tree = {node1: {}, node2: {}, node3:{}, node4: {}, node5: {}, node6: {}, node7: {}}

for node2:

c.tree = {node2: {}, node4: {}, node3: {}, node5: {}, node6: {}, node7: {}}

But I need dict {node2: {node4: {}, node3: {node7: {}, node5: {}, node6: {} }}}.

2

2 Answers

0
votes

Please have look at this example from the python-graph.

It shows how to programmatically create a grah and how to display it using graphviz (it should be already installed in your system if you're using any Linux distribution).

0
votes

Use dot and graphviz.

There are libraries to help generate the dot code, but you can easily write your own to dump out the text into a file.