Common digraphs#
All digraphs in Sage can be built through the digraphs
object. In order to
build a circuit on 15 elements, one can do:
sage: g = digraphs.Circuit(15)
>>> from sage.all import *
>>> g = digraphs.Circuit(Integer(15))
To get a circulant graph on 10 vertices in which a vertex \(i\) has \(i+2\) and \(i+3\) as outneighbors:
sage: p = digraphs.Circulant(10,[2,3])
>>> from sage.all import *
>>> p = digraphs.Circulant(Integer(10),[Integer(2),Integer(3)])
More interestingly, one can get the list of all digraphs that Sage knows how to
build by typing digraphs.
in Sage and then hitting Tab.
Return a \(n\)dimensional butterfly graph. 

Return the circuit on \(n\) vertices. 

Return a circulant digraph on \(n\) vertices from a set of integers. 

Return a complete digraph on \(n\) vertices. 

Return the De Bruijn digraph with parameters \(k,n\). 

Return the generalized de Bruijn digraph of order \(n\) and degree \(d\). 

Return the digraph of Imase and Itoh of order \(n\) and degree \(d\). 

Return the Kautz digraph of degree \(d\) and diameter \(D\). 

Return an iterator yielding digraphs using nauty’s 

Return a Paley digraph on \(q\) vertices. 

Return a directed path on \(n\) vertices. 

Return a random (weighted) directed acyclic graph of order \(n\). 

Return a random growing network with copying (GNC) digraph with \(n\) vertices. 

Return a random labelled digraph on \(n\) nodes and \(m\) arcs. 

Return a random digraph on \(n\) nodes. 

Return a random growing network (GN) digraph with \(n\) vertices. 

Return a random growing network with redirection (GNR) digraph. 

Return a random semicomplete digraph of order \(n\). 

Return a random tournament on \(n\) vertices. 

Return a transitive tournament on \(n\) vertices. 

Iterator over all tournaments on \(n\) vertices using Nauty. 
AUTHORS:
Robert L. Miller (2006)
Emily A. Kirkman (2006)
Michael C. Yurko (2009)
David Coudert (2012)
Functions and methods#
 class sage.graphs.digraph_generators.DiGraphGenerators[source]#
Bases:
object
A class consisting of constructors for several common digraphs, including orderly generation of isomorphism class representatives.
A list of all graphs and graph structures in this database is available via tab completion. Type
digraphs.
and then hit Tab to see which digraphs are available.The docstrings include educational information about each named digraph with the hopes that this class can be used as a reference.
The constructors currently in this class include:
Random Directed Graphs:  RandomDirectedAcyclicGraph  RandomDirectedGN  RandomDirectedGNC  RandomDirectedGNP  RandomDirectedGNM  RandomDirectedGNR  RandomTournament  RandomSemiComplete Families of Graphs:  Complete  DeBruijn  GeneralizedDeBruijn  Kautz  Path  ImaseItoh  RandomTournament  TransitiveTournament  tournaments_nauty
ORDERLY GENERATION: digraphs(vertices, property=lambda x: True, augment=’edges’, size=None)
Accesses the generator of isomorphism class representatives [McK1998]. Iterates over distinct, exhaustive representatives.
INPUT:
vertices
– natural number orNone
to infinitely generate bigger and bigger digraphs.property
– any property to be tested on digraphs before generationaugment
– choices:'vertices'
– augments by adding a vertex, and edges incident to that vertex. In this case, all digraphs on up to n=vertices are generated. If for any digraph G satisfying the property, every subgraph, obtained from G by deleting one vertex and only edges incident to that vertex, satisfies the property, then this will generate all digraphs with that property. If this does not hold, then all the digraphs generated will satisfy the property, but there will be some missing.'edges'
– augments a fixed number of vertices by adding one edge. In this case, all digraphs on exactly n=vertices are generated. If for any graph G satisfying the property, every subgraph, obtained from G by deleting one edge but not the vertices incident to that edge, satisfies the property, then this will generate all digraphs with that property. If this does not hold, then all the digraphs generated will satisfy the property, but there will be some missing.
implementation
– which underlying implementation to use (see DiGraph?)sparse
– boolean (default:True
); whether to use a sparse or dense data structure. See the documentation ofGraph
.
EXAMPLES:
Print digraphs on 2 or less vertices:
sage: for D in digraphs(2, augment='vertices'): ....: print(D) Digraph on 0 vertices Digraph on 1 vertex Digraph on 2 vertices Digraph on 2 vertices Digraph on 2 vertices
>>> from sage.all import * >>> for D in digraphs(Integer(2), augment='vertices'): ... print(D) Digraph on 0 vertices Digraph on 1 vertex Digraph on 2 vertices Digraph on 2 vertices Digraph on 2 vertices
Print digraphs on 3 vertices:
sage: for D in digraphs(3): ....: print(D) Digraph on 3 vertices Digraph on 3 vertices ... Digraph on 3 vertices Digraph on 3 vertices
>>> from sage.all import * >>> for D in digraphs(Integer(3)): ... print(D) Digraph on 3 vertices Digraph on 3 vertices ... Digraph on 3 vertices Digraph on 3 vertices
Generate all digraphs with 4 vertices and 3 edges:
sage: L = digraphs(4, size=3) sage: len(list(L)) 13
>>> from sage.all import * >>> L = digraphs(Integer(4), size=Integer(3)) >>> len(list(L)) 13
Generate all digraphs with 4 vertices and up to 3 edges:
sage: L = list(digraphs(4, lambda G: G.size() <= 3)) sage: len(L) 20 sage: graphs_list.show_graphs(L) # long time # needs sage.plot
>>> from sage.all import * >>> L = list(digraphs(Integer(4), lambda G: G.size() <= Integer(3))) >>> len(L) 20 >>> graphs_list.show_graphs(L) # long time # needs sage.plot
Generate all digraphs with degree at most 2, up to 5 vertices:
sage: property = lambda G: (max([G.degree(v) for v in G] + [0]) <= 2) sage: L = list(digraphs(5, property, augment='vertices')) sage: len(L) 75
>>> from sage.all import * >>> property = lambda G: (max([G.degree(v) for v in G] + [Integer(0)]) <= Integer(2)) >>> L = list(digraphs(Integer(5), property, augment='vertices')) >>> len(L) 75
Generate digraphs on the fly (see http://oeis.org/classic/A000273):
sage: for i in range(5): ....: print(len(list(digraphs(i)))) 1 1 3 16 218
>>> from sage.all import * >>> for i in range(Integer(5)): ... print(len(list(digraphs(i)))) 1 1 3 16 218
 ButterflyGraph(n, vertices='strings')[source]#
Return a \(n\)dimensional butterfly graph.
The vertices consist of pairs \((v, i)\), where \(v\) is an \(n\)dimensional tuple (vector) with binary entries (or a string representation of such) and \(i\) is an integer in \([0..n]\). A directed edge goes from \((v, i)\) to \((w, i + 1)\) if \(v\) and \(w\) are identical except for possibly when \(v[i] \neq w[i]\).
A butterfly graph has \((2^n)(n+1)\) vertices and \(n2^{n+1}\) edges.
INPUT:
n
– a non negative integer; the dimension of the butterfly graphvertices
– string (default:'strings'
); specifies whether the vertices are zeroone strings (default) or tuples over GF(2) (vertices='vectors'
)
EXAMPLES:
sage: digraphs.ButterflyGraph(2).edges(sort=True, labels=False) [(('00', 0), ('00', 1)), (('00', 0), ('10', 1)), (('00', 1), ('00', 2)), (('00', 1), ('01', 2)), (('01', 0), ('01', 1)), (('01', 0), ('11', 1)), (('01', 1), ('00', 2)), (('01', 1), ('01', 2)), (('10', 0), ('00', 1)), (('10', 0), ('10', 1)), (('10', 1), ('10', 2)), (('10', 1), ('11', 2)), (('11', 0), ('01', 1)), (('11', 0), ('11', 1)), (('11', 1), ('10', 2)), (('11', 1), ('11', 2))] sage: digraphs.ButterflyGraph(2, vertices='vectors').edges(sort=True, # needs sage.modules sage.rings.finite_rings ....: labels=False) [(((0, 0), 0), ((0, 0), 1)), (((0, 0), 0), ((1, 0), 1)), (((0, 0), 1), ((0, 0), 2)), (((0, 0), 1), ((0, 1), 2)), (((0, 1), 0), ((0, 1), 1)), (((0, 1), 0), ((1, 1), 1)), (((0, 1), 1), ((0, 0), 2)), (((0, 1), 1), ((0, 1), 2)), (((1, 0), 0), ((0, 0), 1)), (((1, 0), 0), ((1, 0), 1)), (((1, 0), 1), ((1, 0), 2)), (((1, 0), 1), ((1, 1), 2)), (((1, 1), 0), ((0, 1), 1)), (((1, 1), 0), ((1, 1), 1)), (((1, 1), 1), ((1, 0), 2)), (((1, 1), 1), ((1, 1), 2))] sage: pos = digraphs.ButterflyGraph(2).get_pos() sage: pos['11', 0] (0, 0)
>>> from sage.all import * >>> digraphs.ButterflyGraph(Integer(2)).edges(sort=True, labels=False) [(('00', 0), ('00', 1)), (('00', 0), ('10', 1)), (('00', 1), ('00', 2)), (('00', 1), ('01', 2)), (('01', 0), ('01', 1)), (('01', 0), ('11', 1)), (('01', 1), ('00', 2)), (('01', 1), ('01', 2)), (('10', 0), ('00', 1)), (('10', 0), ('10', 1)), (('10', 1), ('10', 2)), (('10', 1), ('11', 2)), (('11', 0), ('01', 1)), (('11', 0), ('11', 1)), (('11', 1), ('10', 2)), (('11', 1), ('11', 2))] >>> digraphs.ButterflyGraph(Integer(2), vertices='vectors').edges(sort=True, # needs sage.modules sage.rings.finite_rings ... labels=False) [(((0, 0), 0), ((0, 0), 1)), (((0, 0), 0), ((1, 0), 1)), (((0, 0), 1), ((0, 0), 2)), (((0, 0), 1), ((0, 1), 2)), (((0, 1), 0), ((0, 1), 1)), (((0, 1), 0), ((1, 1), 1)), (((0, 1), 1), ((0, 0), 2)), (((0, 1), 1), ((0, 1), 2)), (((1, 0), 0), ((0, 0), 1)), (((1, 0), 0), ((1, 0), 1)), (((1, 0), 1), ((1, 0), 2)), (((1, 0), 1), ((1, 1), 2)), (((1, 1), 0), ((0, 1), 1)), (((1, 1), 0), ((1, 1), 1)), (((1, 1), 1), ((1, 0), 2)), (((1, 1), 1), ((1, 1), 2))] >>> pos = digraphs.ButterflyGraph(Integer(2)).get_pos() >>> pos['11', Integer(0)] (0, 0)
 Circuit(n)[source]#
Return the circuit on \(n\) vertices.
The circuit is an oriented
CycleGraph()
.EXAMPLES:
A circuit is the smallest strongly connected digraph:
sage: circuit = digraphs.Circuit(15) sage: len(circuit.strongly_connected_components()) == 1 True
>>> from sage.all import * >>> circuit = digraphs.Circuit(Integer(15)) >>> len(circuit.strongly_connected_components()) == Integer(1) True
 Circulant(n, integers)[source]#
Return a circulant digraph on \(n\) vertices from a set of integers.
INPUT:
n
– integer; number of verticesintegers
– iterable container (list, set, etc.) of integers such that there is an edge from \(i\) to \(j\) if and only if \((ji) \pmod{n}\) is an integer
EXAMPLES:
sage: digraphs.Circulant(13,[3,5,7]) Circulant graph ([3, 5, 7]): Digraph on 13 vertices
>>> from sage.all import * >>> digraphs.Circulant(Integer(13),[Integer(3),Integer(5),Integer(7)]) Circulant graph ([3, 5, 7]): Digraph on 13 vertices
 Complete(n, loops=False)[source]#
Return the complete digraph on \(n\) vertices.
INPUT:
n
– integer; number of verticesloops
– boolean (default:False
); whether to add loops or not, i.e., edges from \(u\) to itself
See also
EXAMPLES:
sage: n = 10 sage: G = digraphs.Complete(n); G Complete digraph: Digraph on 10 vertices sage: G.size() == n*(n1) True sage: G = digraphs.Complete(n, loops=True); G Complete digraph with loops: Looped digraph on 10 vertices sage: G.size() == n*n True sage: digraphs.Complete(1) Traceback (most recent call last): ... ValueError: the number of vertices cannot be strictly negative
>>> from sage.all import * >>> n = Integer(10) >>> G = digraphs.Complete(n); G Complete digraph: Digraph on 10 vertices >>> G.size() == n*(nInteger(1)) True >>> G = digraphs.Complete(n, loops=True); G Complete digraph with loops: Looped digraph on 10 vertices >>> G.size() == n*n True >>> digraphs.Complete(Integer(1)) Traceback (most recent call last): ... ValueError: the number of vertices cannot be strictly negative
 DeBruijn(k, n, vertices='strings')[source]#
Return the De Bruijn digraph with parameters \(k,n\).
The De Bruijn digraph with parameters \(k,n\) is built upon a set of vertices equal to the set of words of length \(n\) from a dictionary of \(k\) letters.
In this digraph, there is an arc \(w_1w_2\) if \(w_2\) can be obtained from \(w_1\) by removing the leftmost letter and adding a new letter at its right end. For more information, see the Wikipedia article De_Bruijn_graph.
INPUT:
k
– two possibilities for this parameter :An integer equal to the cardinality of the alphabet to use, that is, the degree of the digraph to be produced.
An iterable object to be used as the set of letters. The degree of the resulting digraph is the cardinality of the set of letters.
n
– integer; length of words in the De Bruijn digraph whenvertices == 'strings'
, and also the diameter of the digraph.vertices
– string (default:'strings'
); whether the vertices are words over an alphabet (default) or integers (vertices='string'
)
EXAMPLES:
de Bruijn digraph of degree 2 and diameter 2:
sage: db = digraphs.DeBruijn(2, 2); db # needs sage.combinat De Bruijn digraph (k=2, n=2): Looped digraph on 4 vertices sage: db.order(), db.size() # needs sage.combinat (4, 8) sage: db.diameter() # needs sage.combinat 2
>>> from sage.all import * >>> db = digraphs.DeBruijn(Integer(2), Integer(2)); db # needs sage.combinat De Bruijn digraph (k=2, n=2): Looped digraph on 4 vertices >>> db.order(), db.size() # needs sage.combinat (4, 8) >>> db.diameter() # needs sage.combinat 2
Building a de Bruijn digraph on a different alphabet:
sage: # needs sage.combinat sage: g = digraphs.DeBruijn(['a', 'b'], 2) sage: g.vertices(sort=True) ['aa', 'ab', 'ba', 'bb'] sage: g.is_isomorphic(db) True sage: g = digraphs.DeBruijn(['AA', 'BB'], 2) sage: g.vertices(sort=True) ['AA,AA', 'AA,BB', 'BB,AA', 'BB,BB'] sage: g.is_isomorphic(db) True
>>> from sage.all import * >>> # needs sage.combinat >>> g = digraphs.DeBruijn(['a', 'b'], Integer(2)) >>> g.vertices(sort=True) ['aa', 'ab', 'ba', 'bb'] >>> g.is_isomorphic(db) True >>> g = digraphs.DeBruijn(['AA', 'BB'], Integer(2)) >>> g.vertices(sort=True) ['AA,AA', 'AA,BB', 'BB,AA', 'BB,BB'] >>> g.is_isomorphic(db) True
 GeneralizedDeBruijn(n, d)[source]#
Return the generalized de Bruijn digraph of order \(n\) and degree \(d\).
The generalized de Bruijn digraph was defined in [RPK1980] [RPK1983]. It has vertex set \(V=\{0, 1,..., n1\}\) and there is an arc from vertex \(u \in V\) to all vertices \(v \in V\) such that \(v \equiv (u*d + a) \mod{n}\) with \(0 \leq a < d\).
When \(n = d^{D}\), the generalized de Bruijn digraph is isomorphic to the de Bruijn digraph of degree \(d\) and diameter \(D\).
INPUT:
n
– integer; number of vertices of the digraph (must be at least one)d
– integer; degree of the digraph (must be at least one)
See also
sage.graphs.generic_graph.GenericGraph.is_circulant()
– checks whether a (di)graph is circulant, and/or returns all possible sets of parameters.
EXAMPLES:
sage: GB = digraphs.GeneralizedDeBruijn(8, 2) sage: GB.is_isomorphic(digraphs.DeBruijn(2, 3), certificate=True) # needs sage.combinat (True, {0: '000', 1: '001', 2: '010', 3: '011', 4: '100', 5: '101', 6: '110', 7: '111'})
>>> from sage.all import * >>> GB = digraphs.GeneralizedDeBruijn(Integer(8), Integer(2)) >>> GB.is_isomorphic(digraphs.DeBruijn(Integer(2), Integer(3)), certificate=True) # needs sage.combinat (True, {0: '000', 1: '001', 2: '010', 3: '011', 4: '100', 5: '101', 6: '110', 7: '111'})
 ImaseItoh(n, d)[source]#
Return the ImaseItoh digraph of order \(n\) and degree \(d\).
The ImaseItoh digraph was defined in [II1983]. It has vertex set \(V=\{0, 1,..., n1\}\) and there is an arc from vertex \(u \in V\) to all vertices \(v \in V\) such that \(v \equiv (u*da1) \mod{n}\) with \(0 \leq a < d\).
When \(n = d^{D}\), the ImaseItoh digraph is isomorphic to the de Bruijn digraph of degree \(d\) and diameter \(D\). When \(n = d^{D1}(d+1)\), the ImaseItoh digraph is isomorphic to the Kautz digraph [Kau1968] of degree \(d\) and diameter \(D\).
INPUT:
n
– integer; number of vertices of the digraph (must be greater than or equal to two)d
– integer; degree of the digraph (must be greater than or equal to one)
EXAMPLES:
sage: II = digraphs.ImaseItoh(8, 2) sage: II.is_isomorphic(digraphs.DeBruijn(2, 3), certificate=True) # needs sage.combinat (True, {0: '010', 1: '011', 2: '000', 3: '001', 4: '110', 5: '111', 6: '100', 7: '101'}) sage: II = digraphs.ImaseItoh(12, 2) sage: b,D = II.is_isomorphic(digraphs.Kautz(2, 3), certificate=True) # needs sage.combinat sage: b # needs sage.combinat True sage: D # random isomorphism # needs sage.combinat {0: '202', 1: '201', 2: '210', 3: '212', 4: '121', 5: '120', 6: '102', 7: '101', 8: '010', 9: '012', 10: '021', 11: '020'}
>>> from sage.all import * >>> II = digraphs.ImaseItoh(Integer(8), Integer(2)) >>> II.is_isomorphic(digraphs.DeBruijn(Integer(2), Integer(3)), certificate=True) # needs sage.combinat (True, {0: '010', 1: '011', 2: '000', 3: '001', 4: '110', 5: '111', 6: '100', 7: '101'}) >>> II = digraphs.ImaseItoh(Integer(12), Integer(2)) >>> b,D = II.is_isomorphic(digraphs.Kautz(Integer(2), Integer(3)), certificate=True) # needs sage.combinat >>> b # needs sage.combinat True >>> D # random isomorphism # needs sage.combinat {0: '202', 1: '201', 2: '210', 3: '212', 4: '121', 5: '120', 6: '102', 7: '101', 8: '010', 9: '012', 10: '021', 11: '020'}
 Kautz(k, D, vertices='strings')[source]#
Return the Kautz digraph of degree \(d\) and diameter \(D\).
The Kautz digraph has been defined in [Kau1968]. The Kautz digraph of degree \(d\) and diameter \(D\) has \(d^{D1}(d+1)\) vertices. This digraph is built from a set of vertices equal to the set of words of length \(D\) over an alphabet of \(d+1\) letters such that consecutive letters are different. There is an arc from vertex \(u\) to vertex \(v\) if \(v\) can be obtained from \(u\) by removing the leftmost letter and adding a new letter, distinct from the rightmost letter of \(u\), at the right end.
The Kautz digraph of degree \(d\) and diameter \(D\) is isomorphic to the ImaseItoh digraph [II1983] of degree \(d\) and order \(d^{D1}(d+1)\).
See the Wikipedia article Kautz_graph for more information.
INPUT:
k
– two possibilities for this parameter. In either case the degree must be at least one:An integer equal to the degree of the digraph to be produced, that is, the cardinality of the alphabet to be used minus one.
An iterable object to be used as the set of letters. The degree of the resulting digraph is the cardinality of the set of letters minus one.
D
– integer; diameter of the digraph, and length of a vertex label whenvertices == 'strings'
(must be at least one)vertices
– string (default:'strings'
); whether the vertices are words over an alphabet (default) or integers (vertices='strings'
)
EXAMPLES:
sage: # needs sage.combinat sage: K = digraphs.Kautz(2, 3) sage: b, D = K.is_isomorphic(digraphs.ImaseItoh(12, 2), certificate=True) sage: b True sage: D # random isomorphism {'010': 8, '012': 9, '020': 11, '021': 10, '101': 7, '102': 6, '120': 5, '121': 4, '201': 1, '202': 0, '210': 2, '212': 3} sage: K = digraphs.Kautz([1,'a','B'], 2) # needs sage.combinat sage: K.edges(sort=True) # needs sage.combinat [('1B', 'B1', '1'), ('1B', 'Ba', 'a'), ('1a', 'a1', '1'), ('1a', 'aB', 'B'), ('B1', '1B', 'B'), ('B1', '1a', 'a'), ('Ba', 'a1', '1'), ('Ba', 'aB', 'B'), ('a1', '1B', 'B'), ('a1', '1a', 'a'), ('aB', 'B1', '1'), ('aB', 'Ba', 'a')] sage: K = digraphs.Kautz([1,'aA','BB'], 2) # needs sage.combinat sage: K.edges(sort=True) # needs sage.combinat [('1,BB', 'BB,1', '1'), ('1,BB', 'BB,aA', 'aA'), ('1,aA', 'aA,1', '1'), ('1,aA', 'aA,BB', 'BB'), ('BB,1', '1,BB', 'BB'), ('BB,1', '1,aA', 'aA'), ('BB,aA', 'aA,1', '1'), ('BB,aA', 'aA,BB', 'BB'), ('aA,1', '1,BB', 'BB'), ('aA,1', '1,aA', 'aA'), ('aA,BB', 'BB,1', '1'), ('aA,BB', 'BB,aA', 'aA')]
>>> from sage.all import * >>> # needs sage.combinat >>> K = digraphs.Kautz(Integer(2), Integer(3)) >>> b, D = K.is_isomorphic(digraphs.ImaseItoh(Integer(12), Integer(2)), certificate=True) >>> b True >>> D # random isomorphism {'010': 8, '012': 9, '020': 11, '021': 10, '101': 7, '102': 6, '120': 5, '121': 4, '201': 1, '202': 0, '210': 2, '212': 3} >>> K = digraphs.Kautz([Integer(1),'a','B'], Integer(2)) # needs sage.combinat >>> K.edges(sort=True) # needs sage.combinat [('1B', 'B1', '1'), ('1B', 'Ba', 'a'), ('1a', 'a1', '1'), ('1a', 'aB', 'B'), ('B1', '1B', 'B'), ('B1', '1a', 'a'), ('Ba', 'a1', '1'), ('Ba', 'aB', 'B'), ('a1', '1B', 'B'), ('a1', '1a', 'a'), ('aB', 'B1', '1'), ('aB', 'Ba', 'a')] >>> K = digraphs.Kautz([Integer(1),'aA','BB'], Integer(2)) # needs sage.combinat >>> K.edges(sort=True) # needs sage.combinat [('1,BB', 'BB,1', '1'), ('1,BB', 'BB,aA', 'aA'), ('1,aA', 'aA,1', '1'), ('1,aA', 'aA,BB', 'BB'), ('BB,1', '1,BB', 'BB'), ('BB,1', '1,aA', 'aA'), ('BB,aA', 'aA,1', '1'), ('BB,aA', 'aA,BB', 'BB'), ('aA,1', '1,BB', 'BB'), ('aA,1', '1,aA', 'aA'), ('aA,BB', 'BB,1', '1'), ('aA,BB', 'BB,aA', 'aA')]
 Paley(q)[source]#
Return a Paley digraph on \(q\) vertices.
Parameter \(q\) must be the power of a prime number and congruent to 3 mod 4.
EXAMPLES:
A Paley digraph has \(n * (n1) / 2\) edges, its underlying graph is a clique, and so it is a tournament:
sage: g = digraphs.Paley(7); g # needs sage.rings.finite_rings Paley digraph with parameter 7: Digraph on 7 vertices sage: g.size() == g.order() * (g.order()  1) / 2 # needs sage.rings.finite_rings True sage: g.to_undirected().is_clique() # needs sage.rings.finite_rings True
>>> from sage.all import * >>> g = digraphs.Paley(Integer(7)); g # needs sage.rings.finite_rings Paley digraph with parameter 7: Digraph on 7 vertices >>> g.size() == g.order() * (g.order()  Integer(1)) / Integer(2) # needs sage.rings.finite_rings True >>> g.to_undirected().is_clique() # needs sage.rings.finite_rings True
A Paley digraph is always selfcomplementary:
sage: g.complement().is_isomorphic(g) # needs sage.rings.finite_rings True
>>> from sage.all import * >>> g.complement().is_isomorphic(g) # needs sage.rings.finite_rings True
 Path(n)[source]#
Return a directed path on \(n\) vertices.
INPUT:
n
– integer; number of vertices in the path
EXAMPLES:
sage: g = digraphs.Path(5) sage: g.vertices(sort=True) [0, 1, 2, 3, 4] sage: g.size() 4 sage: g.automorphism_group().cardinality() # needs sage.groups 1
>>> from sage.all import * >>> g = digraphs.Path(Integer(5)) >>> g.vertices(sort=True) [0, 1, 2, 3, 4] >>> g.size() 4 >>> g.automorphism_group().cardinality() # needs sage.groups 1
 RandomDirectedAcyclicGraph(n, p, weight_max=None)[source]#
Return a random (weighted) directed acyclic graph of order \(n\).
The method starts with the sink vertex and adds vertices one at a time. A vertex is connected only to previously defined vertices, and the probability of each possible connection is given by the probability \(p\). The weight of an edge is a random integer between
1
andweight_max
.INPUT:
n
– number of nodes of the graphp
– probability of an edgeweight_max
– (default:None
); by default, the returned DAG is unweighted. Whenweight_max
is set to a positive integer, edges are assigned a random integer weight between1
andweight_max
.
EXAMPLES:
sage: D = digraphs.RandomDirectedAcyclicGraph(5, .5); D RandomDAG(5, 0.500000000000000): Digraph on 5 vertices sage: D.is_directed_acyclic() True sage: D = digraphs.RandomDirectedAcyclicGraph(5, .5, weight_max=3); D RandomWeightedDAG(5, 0.500000000000000, 3): Digraph on 5 vertices sage: D.is_directed_acyclic() True
>>> from sage.all import * >>> D = digraphs.RandomDirectedAcyclicGraph(Integer(5), RealNumber('.5')); D RandomDAG(5, 0.500000000000000): Digraph on 5 vertices >>> D.is_directed_acyclic() True >>> D = digraphs.RandomDirectedAcyclicGraph(Integer(5), RealNumber('.5'), weight_max=Integer(3)); D RandomWeightedDAG(5, 0.500000000000000, 3): Digraph on 5 vertices >>> D.is_directed_acyclic() True
 RandomDirectedGN(n, kernel=None, seed=None)[source]#
Return a random growing network (GN) digraph with \(n\) vertices.
The digraph is constructed by adding vertices with a link to one previously added vertex. The vertex to link to is chosen with a preferential attachment model, i.e. probability is proportional to degree. The default attachment kernel is a linear function of degree. The digraph is always a tree, so in particular it is a directed acyclic graph. See [KR2001b] for more details.
INPUT:
n
– integer; number of verticeskernel
– the attachment kernel (default: identity function)seed
– arandom.Random
seed or a Pythonint
for the random number generator (default:None
)
EXAMPLES:
sage: # needs networkx sage: D = digraphs.RandomDirectedGN(25) sage: D.num_verts() 25 sage: D.num_edges() 24 sage: D.is_connected() True sage: D.parent() is DiGraph True sage: D.show() # long time
>>> from sage.all import * >>> # needs networkx >>> D = digraphs.RandomDirectedGN(Integer(25)) >>> D.num_verts() 25 >>> D.num_edges() 24 >>> D.is_connected() True >>> D.parent() is DiGraph True >>> D.show() # long time
 RandomDirectedGNC(n, seed=None)[source]#
Return a random growing network with copying (GNC) digraph with \(n\) vertices.
The digraph is constructed by adding vertices with a link to one previously added vertex. The vertex to link to is chosen with a preferential attachment model, i.e. probability is proportional to degree. The new vertex is also linked to all of the previously added vertex’s successors. See [KR2005] for more details.
INPUT:
n
– integer; number of verticesseed
– arandom.Random
seed or a Pythonint
for the random number generator (default:None
)
EXAMPLES:
sage: # needs networkx sage: D = digraphs.RandomDirectedGNC(25) sage: D.is_directed_acyclic() True sage: D.topological_sort() [24, 23, ..., 1, 0] sage: D.show() # long time
>>> from sage.all import * >>> # needs networkx >>> D = digraphs.RandomDirectedGNC(Integer(25)) >>> D.is_directed_acyclic() True >>> D.topological_sort() [24, 23, ..., 1, 0] >>> D.show() # long time
 RandomDirectedGNM(n, m, loops=False)[source]#
Return a random labelled digraph on \(n\) nodes and \(m\) arcs.
INPUT:
n
– integer; number of verticesm
– integer; number of edgesloops
– boolean (default:False
); whether to allow loops
PLOTTING: When plotting, this graph will use the default springlayout algorithm, unless a position dictionary is specified.
EXAMPLES:
sage: D = digraphs.RandomDirectedGNM(10, 5) sage: D.num_verts() 10 sage: D.num_edges() 5
>>> from sage.all import * >>> D = digraphs.RandomDirectedGNM(Integer(10), Integer(5)) >>> D.num_verts() 10 >>> D.num_edges() 5
With loops:
sage: D = digraphs.RandomDirectedGNM(10, 100, loops = True) sage: D.num_verts() 10 sage: D.loops() [(0, 0, None), (1, 1, None), (2, 2, None), (3, 3, None), (4, 4, None), (5, 5, None), (6, 6, None), (7, 7, None), (8, 8, None), (9, 9, None)]
>>> from sage.all import * >>> D = digraphs.RandomDirectedGNM(Integer(10), Integer(100), loops = True) >>> D.num_verts() 10 >>> D.loops() [(0, 0, None), (1, 1, None), (2, 2, None), (3, 3, None), (4, 4, None), (5, 5, None), (6, 6, None), (7, 7, None), (8, 8, None), (9, 9, None)]
 RandomDirectedGNP(n, p, loops=False, seed=None)[source]#
Return a random digraph on \(n\) nodes.
Each edge is inserted independently with probability \(p\). See [ER1959] and [Gil1959] for more details.
INPUT:
n
– integer; number of nodes of the digraphp
– float; probability of an edgeloops
– boolean (default:False
); whether the random digraph may have loopsseed
– integer (default:None
); seed for random number generator
PLOTTING: When plotting, this graph will use the default springlayout algorithm, unless a position dictionary is specified.
EXAMPLES:
sage: D = digraphs.RandomDirectedGNP(10, .2) sage: D.num_verts() 10 sage: D.parent() is DiGraph True
>>> from sage.all import * >>> D = digraphs.RandomDirectedGNP(Integer(10), RealNumber('.2')) >>> D.num_verts() 10 >>> D.parent() is DiGraph True
 RandomDirectedGNR(n, p, seed=None)[source]#
Return a random growing network with redirection (GNR) digraph with \(n\) vertices and redirection probability \(p\).
The digraph is constructed by adding vertices with a link to one previously added vertex. The vertex to link to is chosen uniformly. With probability p, the arc is instead redirected to the successor vertex. The digraph is always a tree. See [KR2001b] for more details.
INPUT:
n
– integer; number of verticesp
– redirection probabilityseed
– arandom.Random
seed or a Pythonint
for the random number generator (default:None
)
EXAMPLES:
sage: # needs networkx sage: D = digraphs.RandomDirectedGNR(25, .2) sage: D.is_directed_acyclic() True sage: D.to_undirected().is_tree() True sage: D.show() # long time # needs sage.plot
>>> from sage.all import * >>> # needs networkx >>> D = digraphs.RandomDirectedGNR(Integer(25), RealNumber('.2')) >>> D.is_directed_acyclic() True >>> D.to_undirected().is_tree() True >>> D.show() # long time # needs sage.plot
 RandomSemiComplete(n)[source]#
Return a random semicomplete digraph on \(n\) vertices.
A directed graph \(G=(V,E)\) is semicomplete if for any pair of vertices \(u\) and \(v\), there is at least one arc between them.
To generate randomly a semicomplete digraph, we have to ensure, for any pair of distinct vertices \(u\) and \(v\), that with probability \(1/3\) we have only arc \(uv\), with probability \(1/3\) we have only arc \(vu\), and with probability \(1/3\) we have both arc \(uv\) and arc \(vu\). We do so by selecting a random integer \(coin\) in \([1,3]\). When \(coin==1\) we select only arc \(uv\), when \(coin==3\) we select only arc \(vu\), and when \(coin==2\) we select both arcs. In other words, we select arc \(uv\) when \(coin\leq 2\) and arc \(vu\) when \(coin\geq 2\).
INPUT:
n
– integer; the number of nodes
See also
EXAMPLES:
sage: SC = digraphs.RandomSemiComplete(10); SC Random SemiComplete digraph: Digraph on 10 vertices sage: SC.size() >= binomial(10, 2) # needs sage.symbolic True sage: digraphs.RandomSemiComplete(1) Traceback (most recent call last): ... ValueError: the number of vertices cannot be strictly negative
>>> from sage.all import * >>> SC = digraphs.RandomSemiComplete(Integer(10)); SC Random SemiComplete digraph: Digraph on 10 vertices >>> SC.size() >= binomial(Integer(10), Integer(2)) # needs sage.symbolic True >>> digraphs.RandomSemiComplete(Integer(1)) Traceback (most recent call last): ... ValueError: the number of vertices cannot be strictly negative
 RandomTournament(n)[source]#
Return a random tournament on \(n\) vertices.
For every pair of vertices, the tournament has an edge from \(i\) to \(j\) with probability \(1/2\), otherwise it has an edge from \(j\) to \(i\).
INPUT:
n
– integer; number of vertices
EXAMPLES:
sage: T = digraphs.RandomTournament(10); T Random Tournament: Digraph on 10 vertices sage: T.size() == binomial(10, 2) # needs sage.symbolic True sage: T.is_tournament() True sage: digraphs.RandomTournament(1) Traceback (most recent call last): ... ValueError: the number of vertices cannot be strictly negative
>>> from sage.all import * >>> T = digraphs.RandomTournament(Integer(10)); T Random Tournament: Digraph on 10 vertices >>> T.size() == binomial(Integer(10), Integer(2)) # needs sage.symbolic True >>> T.is_tournament() True >>> digraphs.RandomTournament(Integer(1)) Traceback (most recent call last): ... ValueError: the number of vertices cannot be strictly negative
 StronglyRegular(n)[source]#
Return a Strongly Regular digraph with \(n\) vertices.
The adjacency matrix of the graph is constructed from a skew Hadamard matrix of order \(n+1\). These graphs were first constructed in [Duv1988].
INPUT:
n
– integer, the number of vertices of the digraph.
EXAMPLES:
A Strongly Regular digraph satisfies the condition \(AJ = JA = kJ\) where \(A\) is the adjacency matrix:
sage: # needs sage.combinat sage.modules sage: g = digraphs.StronglyRegular(7); g Strongly regular digraph: Digraph on 7 vertices sage: A = g.adjacency_matrix()*ones_matrix(7) sage: B = ones_matrix(7)*g.adjacency_matrix() sage: A == B == A[0, 0]*ones_matrix(7) True
>>> from sage.all import * >>> # needs sage.combinat sage.modules >>> g = digraphs.StronglyRegular(Integer(7)); g Strongly regular digraph: Digraph on 7 vertices >>> A = g.adjacency_matrix()*ones_matrix(Integer(7)) >>> B = ones_matrix(Integer(7))*g.adjacency_matrix() >>> A == B == A[Integer(0), Integer(0)]*ones_matrix(Integer(7)) True
 TransitiveTournament(n)[source]#
Return a transitive tournament on \(n\) vertices.
In this tournament there is an edge from \(i\) to \(j\) if \(i<j\).
See the Wikipedia article Tournament_(graph_theory) for more information.
INPUT:
n
– integer; number of vertices in the tournament
EXAMPLES:
sage: g = digraphs.TransitiveTournament(5) sage: g.vertices(sort=True) [0, 1, 2, 3, 4] sage: g.size() 10 sage: g.automorphism_group().cardinality() # needs sage.groups 1
>>> from sage.all import * >>> g = digraphs.TransitiveTournament(Integer(5)) >>> g.vertices(sort=True) [0, 1, 2, 3, 4] >>> g.size() 10 >>> g.automorphism_group().cardinality() # needs sage.groups 1
 nauty_directg(graphs, options='', debug=False)[source]#
Return an iterator yielding digraphs using nauty’s
directg
program.Description from
directg help
: Read undirected graphs and orient their edges in all possible ways. Edges can be oriented in either or both directions (3 possibilities). Isomorphic directed graphs derived from the same input are suppressed. If the input graphs are nonisomorphic then the output graphs are also.INPUT:
graphs
– aGraph
or an iterable containingGraph
. The graph6 string of these graphs is used as an input fordirectg
.options
– a string passed todirectg
as if it was run at a system command line. Available options fromdirectg help
:e<int>  e<int>:<int> specify a value or range of the total number of arcs o orient each edge in only one direction, never both a only make acyclic orientations (implies o) f<int> Use only the subgroup that fixes the first <int> vertices setwise V only output graphs with nontrivial groups (including exchange of isolated vertices). The f option is respected. s<int>/<int> Make only a fraction of the orientations: The first integer is the part number (first is 0) and the second is the number of parts. Splitting is done per input graph independently.
debug
– boolean (default:False
); ifTrue
directg
standard error and standard output are displayed.
EXAMPLES:
sage: gen = graphs.nauty_geng("c 3") sage: dgs = list(digraphs.nauty_directg(gen)) sage: len(dgs) 13 sage: dgs[0] Digraph on 3 vertices sage: dgs[0]._bit_vector() '001001000' sage: len(list(digraphs.nauty_directg(graphs.PetersenGraph(), options="o"))) 324
>>> from sage.all import * >>> gen = graphs.nauty_geng("c 3") >>> dgs = list(digraphs.nauty_directg(gen)) >>> len(dgs) 13 >>> dgs[Integer(0)] Digraph on 3 vertices >>> dgs[Integer(0)]._bit_vector() '001001000' >>> len(list(digraphs.nauty_directg(graphs.PetersenGraph(), options="o"))) 324
Generate nonisomorphic acyclic orientations:
sage: K = graphs.CompleteGraph(4) sage: all(d.is_directed_acyclic() for d in digraphs.nauty_directg(K, options='a')) True sage: sum(1 for _ in digraphs.nauty_directg(K, options='a')) 1 sage: S = graphs.StarGraph(4) sage: all(d.is_directed_acyclic() for d in digraphs.nauty_directg(S, options='a')) True sage: sum(1 for _ in digraphs.nauty_directg(S, options='a')) 5
>>> from sage.all import * >>> K = graphs.CompleteGraph(Integer(4)) >>> all(d.is_directed_acyclic() for d in digraphs.nauty_directg(K, options='a')) True >>> sum(Integer(1) for _ in digraphs.nauty_directg(K, options='a')) 1 >>> S = graphs.StarGraph(Integer(4)) >>> all(d.is_directed_acyclic() for d in digraphs.nauty_directg(S, options='a')) True >>> sum(Integer(1) for _ in digraphs.nauty_directg(S, options='a')) 5
 tournaments_nauty(n, min_out_degree=None, max_out_degree=None, strongly_connected=False, debug=False, options='')[source]#
Iterator over all tournaments on \(n\) vertices using Nauty.
INPUT:
n
– integer; number of verticesmin_out_degree
,max_out_degree
– integers; if set toNone
(default), then the min/max outdegree is not constraineddebug
– boolean (default:False
); ifTrue
the first line of genbg’s output to standard error is captured and the first call to the generator’snext()
function will return this line as a string. A line leading with “>A” indicates a successful initiation of the program with some information on the arguments, while a line beginning with “>E” indicates an error with the input.options
– string; anything else that should be forwarded as input to Nauty’s genbg. See its documentation for more information : https://pallini.di.uniroma1.it.
EXAMPLES:
sage: for g in digraphs.tournaments_nauty(4): ....: print(g.edges(sort=True, labels = False)) [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)] [(1, 0), (1, 3), (2, 0), (2, 1), (3, 0), (3, 2)] [(0, 2), (1, 0), (2, 1), (3, 0), (3, 1), (3, 2)] [(0, 2), (0, 3), (1, 0), (2, 1), (3, 1), (3, 2)] sage: tournaments = digraphs.tournaments_nauty sage: [len(list(tournaments(x))) for x in range(1,8)] [1, 1, 2, 4, 12, 56, 456] sage: [len(list(tournaments(x, strongly_connected = True))) for x in range(1,9)] [1, 0, 1, 1, 6, 35, 353, 6008]
>>> from sage.all import * >>> for g in digraphs.tournaments_nauty(Integer(4)): ... print(g.edges(sort=True, labels = False)) [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)] [(1, 0), (1, 3), (2, 0), (2, 1), (3, 0), (3, 2)] [(0, 2), (1, 0), (2, 1), (3, 0), (3, 1), (3, 2)] [(0, 2), (0, 3), (1, 0), (2, 1), (3, 1), (3, 2)] >>> tournaments = digraphs.tournaments_nauty >>> [len(list(tournaments(x))) for x in range(Integer(1),Integer(8))] [1, 1, 2, 4, 12, 56, 456] >>> [len(list(tournaments(x, strongly_connected = True))) for x in range(Integer(1),Integer(9))] [1, 0, 1, 1, 6, 35, 353, 6008]