# Reference for cospectral_graphs function?

What is the reference for the cospectral_graphs function in sage math? I don’t see it in the documentation.

edit retag close merge delete

Sort by » oldest newest most voted

The cospectral_graphs function was added in #9141. Credits:

• Jason Grout (2010-06-04): cospectral_graphs

The implementation is quite straightforward. First the optional matrix_function argument is read:

if matrix_function is None:


Then the optional graphs argument is read:

from sage.graphs.graph_generators import graphs as graph_gen
if graphs is None:
graph_list = graph_gen(vertices, property=lambda _: True)
elif callable(graphs):
graph_list = iter(g for g in graph_gen(vertices, property=lambda _: True) if graphs(g))
else:
graph_list = iter(graphs)


Finally the cospectral graphs are generated, by putting each graph in a bucket labeled by the graph's characteristic polynomial:

from collections import defaultdict
charpolys = defaultdict(list)
for g in graph_list:
cp = matrix_function(g).charpoly()
charpolys[cp].append(g)


... and then taking the contents of those buckets containing more than one graph:

cospectral_graphs = []
for cp, g_list in charpolys.items():
if len(g_list) > 1:
cospectral_graphs.append(g_list)

return cospectral_graphs

more

Thank you for explaining!

( 2024-04-02 01:54:44 +0200 )edit