# Rooted Product Graphs on Sagemath

How does one implement rooted product graphs on Sagemath? For those who haven't heard about it before, there is a Wikipedia page for it!

Any help would be highly appreciated!

edit retag close merge delete

This does not seem to be implemented in Sage, but you can use the formulas from the wikipedia article and the add_edge() method to build it yourself.

( 2021-08-02 09:56:37 +0100 )edit
( 2023-06-24 15:39:29 +0100 )edit

Sort by » oldest newest most voted

I haven't checked it thoroughly yet, so please let me know if there are any issues.

def rooted_product_of_graphs(G, H):
E = []
h0 = next(iter(H.nodes()))
for (gi, gk) in G.edges():
E.append(((gi, h0), (gk, h0)))

for hj, hk in H.edges():
for gi in G.nodes():
E.append(((gi, hj), (gi, hk)))

return E
import networkx as nx
G = nx.Graph()

H = nx.Graph()

print(rooted_product_of_graphs(G, H))


output: [((1, 'a'), (2, 'a')), ((2, 'a'), (3, 'a')), ((2, 'a'), (5, 'a')), ((3, 'a'), (4, 'a')), ((4, 'a'), (5, 'a')), ((1, 'a'), (1, 'b')), ((2, 'a'), (2, 'b')), ((3, 'a'), (3, 'b')), ((4, 'a'), (4, 'b')), ((5, 'a'), (5, 'b')), ((1, 'b'), (1, 'c')), ((2, 'b'), (2, 'c')), ((3, 'b'), (3, 'c')), ((4, 'b'), (4, 'c')), ((5, 'b'), (5, 'c')), ((1, 'b'), (1, 'd')), ((2, 'b'), (2, 'd')), ((3, 'b'), (3, 'd')), ((4, 'b'), (4, 'd')), ((5, 'b'), (5, 'd'))]

more