本文整理汇总了Python中sage.graphs.digraph.DiGraph.out_degree方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.out_degree方法的具体用法?Python DiGraph.out_degree怎么用?Python DiGraph.out_degree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sage.graphs.digraph.DiGraph
的用法示例。
在下文中一共展示了DiGraph.out_degree方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: is_partial_cube
# 需要导入模块: from sage.graphs.digraph import DiGraph [as 别名]
# 或者: from sage.graphs.digraph.DiGraph import out_degree [as 别名]
#.........这里部分代码省略.........
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.
if not G.is_bipartite():
return fail
# Set up data structures for algorithm:
# - contracted: contracted graph at current stage of algorithm
# - unionfind: union find data structure representing known edge equivalences
# - available: limit on number of remaining available labels
from sage.graphs.digraph import DiGraph
from sage.graphs.graph import Graph
from sage.sets.disjoint_set import DisjointSet
contracted = DiGraph({v: {w: (v, w) for w in G[v]} for v in G})
unionfind = DisjointSet(contracted.edges(labels = False))
available = n-1
# Main contraction loop in place of the original algorithm's recursion
while contracted.order() > 1:
# Find max degree vertex in contracted, and update label limit
deg, root = max((contracted.out_degree(v), v) for v in contracted)
if deg > available:
return fail
available -= deg
# Set up bitvectors on vertices
bitvec = {v:0 for v in contracted}
neighbors = {}
for i, neighbor in enumerate(contracted[root]):
bitvec[neighbor] = 1 << i
neighbors[1 << i] = neighbor
# Breadth first search to propagate bitvectors to the rest of the graph
for level in breadth_first_level_search(contracted, root):
for v in level:
for w in level[v]:
bitvec[w] |= bitvec[v]
# Make graph of labeled edges and union them together
labeled = Graph([contracted.vertices(), []])
for v, w in contracted.edge_iterator(labels = False):
diff = bitvec[v]^bitvec[w]
if not diff or bitvec[w] &~ bitvec[v] == 0:
continue # zero edge or wrong direction
if diff not in neighbors:
return fail
neighbor = neighbors[diff]
unionfind.union(contracted.edge_label(v, w),
contracted.edge_label(root, neighbor))
unionfind.union(contracted.edge_label(w, v),
contracted.edge_label(neighbor, root))
labeled.add_edge(v, w)