本文整理汇总了Python中sage.graphs.digraph.DiGraph.vertices方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.vertices方法的具体用法?Python DiGraph.vertices怎么用?Python DiGraph.vertices使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sage.graphs.digraph.DiGraph
的用法示例。
在下文中一共展示了DiGraph.vertices方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ParentBigOh
# 需要导入模块: from sage.graphs.digraph import DiGraph [as 别名]
# 或者: from sage.graphs.digraph.DiGraph import vertices [as 别名]
#.........这里部分代码省略.........
if self._models.has_vertex(model):
self._default_model = model
else:
raise ValueError
def add_model(self,model):
from bigoh import BigOh
if not isinstance(model,list):
model = [model]
for m in model:
if not issubclass(m,BigOh):
raise TypeError("A precision model must derive from BigOh but '%s' is not"%m)
self._models.add_vertex(m)
def delete_model(self,model):
if isinstance(model,list):
model = [model]
for m in model:
if self._models.has_vertex(m):
self._models.delete_vertex(m)
def update_model(self,old,new):
from bigoh import BigOh
if self._models.has_vertex(old):
if not issubclass(new,BigOh):
raise TypeError("A precision model must derive from BigOh but '%s' is not"%new)
self._models.relabel({old:new})
else:
raise ValueError("Model '%m' does not exist"%old)
def add_modelconversion(self,domain,codomain,constructor=None,safemode=False):
if not self._models.has_vertex(domain):
if safemode: return
raise ValueError("Model '%s' does not exist"%domain)
if not self._models.has_vertex(codomain):
if safemode: return
raise ValueError("Model '%s' does not exist"%codomain)
path = self._models.shortest_path(codomain,domain)
if len(path) > 0:
raise ValueError("Adding this conversion creates a cycle")
self._models.add_edge(domain,codomain,constructor)
def delete_modelconversion(self,domain,codomain):
if not self._models.has_vertex(domain):
raise ValueError("Model '%s' does not exist"%domain)
if not self._models.has_vertex(codomain):
raise ValueError("Model '%s' does not exist"%codomain)
if not self._models_has_edge(domain,codomain):
raise ValueError("No conversion from %s to %s"%(domain,codomain))
self._modelfs.delete_edge(domain,codomain)
def uniformizer_name(self):
return self._uniformizer
def ambiant_space(self):
return self._ambiant_space
# ?!?
def __call__(self,*args,**kwargs):
return self._element_constructor_(*args,**kwargs)
def _element_constructor_(self, *args, **kwargs):
if kwargs.has_key('model'):
del kwargs['model']
return kwargs['model'](self, *args, **kwargs)
if len(args) > 0:
from precision.bigoh import BigOh, ExactBigOh
arg = args[0]
if isinstance(arg,BigOh) and arg.is_exact():
return ExactBigOh(self)
if self._models.has_vertex(arg.__class__) and arg.parent() is self:
return arg
if self._default_model is not None:
try:
return self._default_model(self,*args,**kwargs)
except (AttributeError,ValueError,TypeError):
pass
models = self._models.topological_sort()
models.reverse()
for m in models:
try:
return m(self,*args,**kwargs)
except (AttributeError,ValueError,TypeError,PrecisionError):
pass
raise PrecisionError("unable to create a BigOh object")
def __repr__(self):
return "Parent for BigOh in %s" % self._ambiant_space
def models(self,graph=False):
if graph:
return self._models
else:
return self._models.vertices()
def dimension(self):
return self._ambiant_space.dimension()
def indices_basis(self):
return self._ambiant_space.indices_basis()
示例2: is_partial_cube
# 需要导入模块: from sage.graphs.digraph import DiGraph [as 别名]
# 或者: from sage.graphs.digraph.DiGraph import vertices [as 别名]
def is_partial_cube(G, certificate=False):
r"""
Test whether the given graph is a partial cube.
A partial cube is a graph that can be isometrically embedded into a
hypercube, i.e., its vertices can be labelled with (0,1)-vectors of some
fixed length such that the distance between any two vertices in the graph
equals the Hamming distance of their labels.
Originally written by D. Eppstein for the PADS library
(http://www.ics.uci.edu/~eppstein/PADS/), see also
[Eppstein2008]_. The algorithm runs in `O(n^2)` time, where `n`
is the number of vertices. See the documentation of
:mod:`~sage.graphs.partial_cube` for an overview of the algorithm.
INPUT:
- ``certificate`` (boolean; ``False``) -- The function returns ``True``
or ``False`` according to the graph, when ``certificate = False``. When
``certificate = True`` and the graph is a partial cube, the function
returns ``(True, mapping)``, where ``mapping`` is an isometric mapping of
the vertices of the graph to the vertices of a hypercube ((0, 1)-strings
of a fixed length). When ``certificate = True`` and the graph is not a
partial cube, ``(False, None)`` is returned.
EXAMPLES:
The Petersen graph is not a partial cube::
sage: g = graphs.PetersenGraph()
sage: g.is_partial_cube()
False
All prisms are partial cubes::
sage: g = graphs.CycleGraph(10).cartesian_product(graphs.CompleteGraph(2))
sage: g.is_partial_cube()
True
TESTS:
The returned mapping is an isometric embedding into a hypercube::
sage: g = graphs.DesarguesGraph()
sage: _, m = g.is_partial_cube(certificate = True)
sage: m # random
{0: '00000',
1: '00001',
2: '00011',
3: '01011',
4: '11011',
5: '11111',
6: '11110',
7: '11100',
8: '10100',
9: '00100',
10: '01000',
11: '10001',
12: '00111',
13: '01010',
14: '11001',
15: '10111',
16: '01110',
17: '11000',
18: '10101',
19: '00110'}
sage: all(all(g.distance(u, v) == len([i for i in range(len(m[u])) if m[u][i] != m[v][i]]) for v in m) for u in m)
True
A graph without vertices is trivially a partial cube::
sage: Graph().is_partial_cube(certificate = True)
(True, {})
"""
G._scream_if_not_simple()
if G.order() == 0:
if certificate:
return (True, {})
else:
return True
if certificate:
fail = (False, None)
else:
fail = False
if not G.is_connected():
return fail
n = G.order()
# Initial sanity check: are there few enough edges?
# Needed so that we don't try to use union-find on a dense
# graph and incur superquadratic runtimes.
if 1 << (2*G.size()//n) > n:
return fail
# Check for bipartiteness.
# This ensures also that each contraction will be bipartite.
#.........这里部分代码省略.........