本文整理汇总了Python中sage.graphs.all.DiGraph.is_directed_acyclic方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.is_directed_acyclic方法的具体用法?Python DiGraph.is_directed_acyclic怎么用?Python DiGraph.is_directed_acyclic使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sage.graphs.all.DiGraph
的用法示例。
在下文中一共展示了DiGraph.is_directed_acyclic方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RandomPoset
# 需要导入模块: from sage.graphs.all import DiGraph [as 别名]
# 或者: from sage.graphs.all.DiGraph import is_directed_acyclic [as 别名]
def RandomPoset(n,p):
r"""
Generate a random poset on ``n`` vertices according to a
probability ``p``.
INPUT:
- ``n`` - number of vertices, a non-negative integer
- ``p`` - a probability, a real number between 0 and 1 (inclusive)
OUTPUT:
A poset on ``n`` vertices. The construction decides to make an
ordered pair of vertices comparable in the poset with probability
``p``, however a pair is not made comparable if it would violate
the defining properties of a poset, such as transitivity.
So in practice, once the probability exceeds a small number the
generated posets may be very similar to a chain. So to create
interesting examples, keep the probability small, perhaps on the
order of `1/n`.
EXAMPLES::
sage: Posets.RandomPoset(17,.15)
Finite poset containing 17 elements
TESTS::
sage: Posets.RandomPoset('junk', 0.5)
Traceback (most recent call last):
...
TypeError: number of elements must be an integer, not junk
sage: Posets.RandomPoset(-6, 0.5)
Traceback (most recent call last):
...
ValueError: number of elements must be non-negative, not -6
sage: Posets.RandomPoset(6, 'garbage')
Traceback (most recent call last):
...
TypeError: probability must be a real number, not garbage
sage: Posets.RandomPoset(6, -0.5)
Traceback (most recent call last):
...
ValueError: probability must be between 0 and 1, not -0.5
"""
try:
n = Integer(n)
except:
raise TypeError("number of elements must be an integer, not {0}".format(n))
if n < 0:
raise ValueError("number of elements must be non-negative, not {0}".format(n))
try:
p = float(p)
except:
raise TypeError("probability must be a real number, not {0}".format(p))
if p < 0 or p> 1:
raise ValueError("probability must be between 0 and 1, not {0}".format(p))
D = DiGraph(loops=False,multiedges=False)
D.add_vertices(range(n))
for i in range(n):
for j in range(n):
if random.random() < p:
D.add_edge(i,j)
if not D.is_directed_acyclic():
D.delete_edge(i,j)
return Poset(D,cover_relations=False)