Functions for reading/building graphs/digraphs.

This module gathers functions needed to build a graph from any other data.

Note

This is an internal module of Sage. All features implemented here are made available to end-users through the constructors of Graph and DiGraph.

Note that because they are called by the constructors of Graph and DiGraph, most of these functions modify a graph inplace.

from_adjacency_matrix()

Fill G with the data of an adjacency matrix.

from_dict_of_dicts()

Fill G with the data of a dictionary of dictionaries.

from_dict_of_lists()

Fill G with the data of a dictionary of lists.

from_dig6()

Fill G with the data of a dig6 string.

from_graph6()

Fill G with the data of a graph6 string.

from_incidence_matrix()

Fill G with the data of an incidence matrix.

from_oriented_incidence_matrix()

Fill G with the data of an oriented incidence matrix.

from_seidel_adjacency_matrix()

Fill G with the data of a Seidel adjacency matrix.

from_sparse6()

Fill G with the data of a sparse6 string.

Functions

sage.graphs.graph_input.from_adjacency_matrix(G, M, loops=False, multiedges=False, weighted=False)

Fill G with the data of an adjacency matrix.

INPUT:

  • G – a Graph or DiGraph

  • M – an adjacency matrix

  • loops, multiedges, weighted – booleans (default: False); whether to consider the graph as having loops, multiple edges, or weights

EXAMPLES:

sage: from sage.graphs.graph_input import from_adjacency_matrix
sage: g = Graph()
sage: from_adjacency_matrix(g, graphs.PetersenGraph().adjacency_matrix())
sage: g.is_isomorphic(graphs.PetersenGraph())
True
sage.graphs.graph_input.from_dict_of_dicts(G, M, loops=False, multiedges=False, weighted=False, convert_empty_dict_labels_to_None=False)

Fill G with the data of a dictionary of dictionaries.

INPUT:

  • G – a graph

  • M – a dictionary of dictionaries

  • loops, multiedges, weighted – booleans (default: False); whether to consider the graph as having loops, multiple edges, or weights

  • convert_empty_dict_labels_to_None – booleans (default: False); whether to adjust for empty dicts instead of None in NetworkX default edge labels

EXAMPLES:

sage: from sage.graphs.graph_input import from_dict_of_dicts
sage: g = Graph()
sage: from_dict_of_dicts(g, graphs.PetersenGraph().to_dictionary(edge_labels=True))
sage: g.is_isomorphic(graphs.PetersenGraph())
True
sage.graphs.graph_input.from_dict_of_lists(G, D, loops=False, multiedges=False, weighted=False)

Fill G with the data of a dictionary of lists.

INPUT:

  • G – a Graph or DiGraph

  • D – a dictionary of lists

  • loops, multiedges, weighted – booleans (default: False); whether to consider the graph as having loops, multiple edges, or weights

EXAMPLES:

sage: from sage.graphs.graph_input import from_dict_of_lists
sage: g = Graph()
sage: from_dict_of_lists(g, graphs.PetersenGraph().to_dictionary())
sage: g.is_isomorphic(graphs.PetersenGraph())
True
sage.graphs.graph_input.from_dig6(G, dig6_string)

Fill G with the data of a dig6 string.

INPUT:

  • G – a graph

  • dig6_string – a dig6 string

EXAMPLES:

sage: from sage.graphs.graph_input import from_dig6
sage: g = DiGraph()
sage: from_dig6(g, digraphs.Circuit(10).dig6_string())
sage: g.is_isomorphic(digraphs.Circuit(10))
True
sage.graphs.graph_input.from_graph6(G, g6_string)

Fill G with the data of a graph6 string.

INPUT:

  • G – a graph

  • g6_string – a graph6 string

EXAMPLES:

sage: from sage.graphs.graph_input import from_graph6
sage: g = Graph()
sage: from_graph6(g, 'IheA@GUAo')
sage: g.is_isomorphic(graphs.PetersenGraph())
True
sage.graphs.graph_input.from_incidence_matrix(G, M, loops=False, multiedges=False, weighted=False)

Fill G with the data of an incidence matrix.

INPUT:

  • G – a graph

  • M – an incidence matrix

  • loops, multiedges, weighted – booleans (default: False); whether to consider the graph as having loops, multiple edges, or weights

EXAMPLES:

sage: from sage.graphs.graph_input import from_incidence_matrix
sage: g = Graph()
sage: from_incidence_matrix(g, graphs.PetersenGraph().incidence_matrix())
sage: g.is_isomorphic(graphs.PetersenGraph())
True
sage.graphs.graph_input.from_oriented_incidence_matrix(G, M, loops=False, multiedges=False, weighted=False)

Fill G with the data of an oriented incidence matrix.

An oriented incidence matrix is the incidence matrix of a directed graph, in which each non-loop edge corresponds to a \(+1\) and a \(-1\), indicating its source and destination.

INPUT:

  • G – a DiGraph

  • M – an incidence matrix

  • loops, multiedges, weighted – booleans (default: False); whether to consider the graph as having loops, multiple edges, or weights

EXAMPLES:

sage: from sage.graphs.graph_input import from_oriented_incidence_matrix
sage: g = DiGraph()
sage: from_oriented_incidence_matrix(g, digraphs.Circuit(10).incidence_matrix())
sage: g.is_isomorphic(digraphs.Circuit(10))
True
sage.graphs.graph_input.from_seidel_adjacency_matrix(G, M)

Fill G with the data of a Seidel adjacency matrix.

INPUT:

  • G – a graph

  • M – a Seidel adjacency matrix

EXAMPLES:

sage: from sage.graphs.graph_input import from_seidel_adjacency_matrix
sage: g = Graph()
sage: from_seidel_adjacency_matrix(g, graphs.PetersenGraph().seidel_adjacency_matrix())
sage: g.is_isomorphic(graphs.PetersenGraph())
True
sage.graphs.graph_input.from_sparse6(G, g6_string)

Fill G with the data of a sparse6 string.

INPUT:

  • G – a graph

  • g6_string – a sparse6 string

EXAMPLES:

sage: from sage.graphs.graph_input import from_sparse6
sage: g = Graph()
sage: from_sparse6(g, ':I`ES@obGkqegW~')
sage: g.is_isomorphic(graphs.PetersenGraph())
True