Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Does Sage have a built-in function to output an adjacency list?

We know that SageMath supports adjacency list input, but I haven't seen an output function.

Graph({0: [1, 5, 7, 8, 11],
 1: [0, 2, 5, 6, 8],
 2: [1, 3, 6, 8, 9],
 3: [2, 4, 6, 9, 10],
 4: [3, 5, 6, 10, 11],
 5: [0, 1, 4, 6, 11],
 6: [1, 2, 3, 4, 5],
 7: [0, 8, 9, 10, 11],
 8: [0, 1, 2, 7, 9],
 9: [2, 3, 7, 8, 10],
 10: [3, 4, 7, 9, 11],
 11: [0, 4, 5, 7, 10]})

So I wrote a simple script, but I'm not sure if there's a built-in function that accomplishes the same thing.

def get_adjacency_dict(graph):
    return {vertex: list(graph.neighbors(vertex)) for vertex in graph.vertices()}