Ask Your Question

Revision history [back]

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:
        matrix_function = lambda g: g.adjacency_matrix()

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:

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

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

    return cospectral_graphs

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:
     matrix_function = lambda g: g.adjacency_matrix()

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:

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

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

 return cospectral_graphs

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:
    matrix_function = lambda g: g.adjacency_matrix()

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:generated, by putting each graph in a bucket labeled by its 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 only 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

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:
    matrix_function = lambda g: g.adjacency_matrix()

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 its 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 only 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