Ask Your Question
0

Output of RootedTrees()

asked 2025-05-13 12:37:11 +0200

Hola gravatar image

updated 2025-05-13 22:56:47 +0200

Dear all,

I’m currently working on rooted trees and I wanted to ask if there is an alternative output for rooted trees by using HashTables or edge sets.

For example, if I run the following code:

n = 5
trees = RootedTrees(n)

for T in trees:
    print(T)

The output I get is:

[[[[[]]]]]
[[[[], []]]]
[[[], [[]]]]
[[[], [], []]]
[[], [[[]]]]
[[], [[], []]]
[[[]], [[]]]
[[], [], [[]]]
[[], [], [], []]

Is there a different way to represent or obtain this output using HashTables or edge sets? For instance, I guess [[], [], [], []] means the tree G having E(G)={{1,2},{1,3},{1,4},{1,5}}

edit retag flag offensive close merge delete

Comments

What is "output using HashTables"?

Max Alekseyev gravatar imageMax Alekseyev ( 2025-05-13 19:32:34 +0200 )edit

For instance, the HashTable of G with V(G)={1,2,3,4,5} and E(G)={{1,2},{1,3},{1,4},{1,5}} is { 1 => {2,3,4,5}, 2 => {}, 3 => {}, 4 => {}, 5 => {} }

Hola gravatar imageHola ( 2025-05-13 22:58:36 +0200 )edit

It's called adjacency list - eg., see https://en.wikipedia.org/wiki/Adjacen...

Max Alekseyev gravatar imageMax Alekseyev ( 2025-05-13 23:02:22 +0200 )edit

I knew its name was "HashTable" from Macaulay2 language. Thank you for your clarification

Hola gravatar imageHola ( 2025-05-21 11:55:37 +0200 )edit

2 Answers

Sort by » oldest newest most voted
3

answered 2025-05-14 07:44:05 +0200

FrédéricC gravatar image

updated 2025-05-14 07:44:43 +0200

You can change them to planar trees and then use existing methods:

sage: a = RootedTrees().an_element()
sage: b = RootedTree([a, a])
sage: c = OrderedTree(b)
sage: c.to_poset()
Finite poset containing 3 elements
edit flag offensive delete link more

Comments

1

Nice pathway. So, a digraph from ordered tree T this way can be obtained as OrderedTree(T).to_poset().hasse_diagram().reverse(). Here is a sample code at Sagecell.

Max Alekseyev gravatar imageMax Alekseyev ( 2025-05-14 17:49:16 +0200 )edit

Thank you! I really appreciated

Hola gravatar imageHola ( 2025-05-21 11:56:28 +0200 )edit
1

answered 2025-05-14 00:01:39 +0200

Max Alekseyev gravatar image

A possible solution:

def sortkey_to_digraph(sk,G,root):
    if not sk:
        return
    G.add_vertex(root)
    d = sk.pop(0)
    for _ in range(d):
        child = max(G.vertices()) + 1
        sortkey_to_digraph(sk, G, child)
        G.add_edge(root, child)

# conversion function from RootedTree to a DiGraph with a root at 0
def rt_to_digraph(T):
    D = DiGraph()
    sortkey_to_digraph(list(T.sort_key()),D,0)
    return D

for T in RootedTrees(5):
    D = rt_to_digraph(T)
    D.show(layout='tree')
    print( { v: D.neighbors_out(v) for v in D.vertices() } )

To see how it works, run it at Sagecell.

edit flag offensive delete link more

Comments

Thank you! I really appreciated

Hola gravatar imageHola ( 2025-05-21 11:56:11 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2025-05-13 01:16:32 +0200

Seen: 150 times

Last updated: May 14