本文整理汇总了Python中networkx.is_tree函数的典型用法代码示例。如果您正苦于以下问题:Python is_tree函数的具体用法?Python is_tree怎么用?Python is_tree使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_tree函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_disconnected_graph
def test_disconnected_graph():
# https://github.com/networkx/networkx/issues/1144
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 0), (3, 4)])
assert_false(nx.is_tree(G))
G = nx.DiGraph()
G.add_edges_from([(0, 1), (1, 2), (2, 0), (3, 4)])
assert_false(nx.is_tree(G))
示例2: euler_tour
def euler_tour(G, node=None, seen=None, visited=None):
"""
definition from
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.192.8615&rep=rep1&type=pdf
Example:
>>> # DISABLE_DOCTEST
>>> from utool.experimental.euler_tour_tree_avl import * # NOQA
>>> edges = [
>>> ('R', 'A'), ('R', 'B'),
>>> ('B', 'C'), ('C', 'D'), ('C', 'E'),
>>> ('B', 'F'), ('B', 'G'),
>>> ]
>>> G = nx.Graph(edges)
>>> node = list(G.nodes())[0]
>>> et1 = euler_tour(G, node)
>>> et2 = euler_tour_dfs(G, node)
"""
if node is None:
node = next(G.nodes())
if visited is None:
assert nx.is_tree(G)
visited = []
if seen is None:
seen = set([])
visited.append(node)
for c in G.neighbors(node):
if c in seen:
continue
seen.add(c)
euler_tour(G, c, seen, visited)
visited.append(node)
return visited
示例3: add_edge
def add_edge(self, u, v):
print('[euler_tour_forest] add_edge(%r, %r)' % (u, v))
if self.has_edge(u, v):
return
ru = self.find_root(u)
rv = self.find_root(v)
if ru is None:
self.add_node(u)
ru = u
if rv is None:
self.add_node(v)
rv = v
assert ru is not rv, (
'u=%r, v=%r not disjoint, can only join disjoint edges' % (u, v))
assert ru in self.trees, 'ru must be a root node'
assert rv in self.trees, 'rv must be a root node'
subtree1 = self.trees[ru]
subtree2 = self.trees[rv]
del self.trees[rv]
new_tree = nx.compose(subtree1, subtree2)
new_tree.add_edge(u, v)
self.trees[ru] = new_tree
print(list(new_tree.nodes()))
assert nx.is_connected(new_tree)
assert nx.is_tree(new_tree)
示例4: question4
def question4(T, node1, node2):
BSTG=nx.from_numpy_matrix(np.matrix(T))
if (nx.is_tree(BSTG) & check_sum(T)):
path = nx.all_simple_paths(BSTG, node1, node2)
return min(list(path)[0])
else:
print ("This graph is not a binary search tree")
return None
示例5: inspectlabels
def inspectlabels(milestone,sent):
for h,d in sent.edges():
if type(sent[h][d]['deprel']) == str:
pass
else:
print(milestone,"ARLAMA",sent)
if not nx.is_tree(sent):
print("not tree")
示例6: test_default_flow_function_karate_club_graph
def test_default_flow_function_karate_club_graph(self):
G = nx.karate_club_graph()
nx.set_edge_attributes(G, 1, 'capacity')
T = nx.gomory_hu_tree(G)
assert_true(nx.is_tree(T))
for u, v in combinations(G, 2):
cut_value, edge = self.minimum_edge_weight(T, u, v)
assert_equal(nx.minimum_cut_value(G, u, v),
cut_value)
示例7: test_karate_club_graph_cutset
def test_karate_club_graph_cutset(self):
G = nx.karate_club_graph()
nx.set_edge_attributes(G, 1, 'capacity')
T = nx.gomory_hu_tree(G)
assert_true(nx.is_tree(T))
u, v = 0, 33
cut_value, edge = self.minimum_edge_weight(T, u, v)
cutset = self.compute_cutset(G, T, edge)
assert_equal(cut_value, len(cutset))
示例8: span_makes_subtree
def span_makes_subtree(self, initidx, endidx):
G = nx.DiGraph()
span_nodes = list(range(initidx,endidx+1))
span_words = [self.node[x]["form"] for x in span_nodes]
G.add_nodes_from(span_nodes)
for h,d in self.edges():
if h in span_nodes and d in span_nodes:
G.add_edge(h,d)
return nx.is_tree(G)
示例9: test_davis_southern_women_graph
def test_davis_southern_women_graph(self):
G = nx.davis_southern_women_graph()
nx.set_edge_attributes(G, 'capacity', 1)
for flow_func in flow_funcs:
T = nx.gomory_hu_tree(G, flow_func=flow_func)
assert_true(nx.is_tree(T))
for u, v in combinations(G, 2):
cut_value, edge = self.minimum_edge_weight(T, u, v)
assert_equal(nx.minimum_cut_value(G, u, v),
cut_value)
示例10: test_florentine_families_graph
def test_florentine_families_graph(self):
G = nx.florentine_families_graph()
nx.set_edge_attributes(G, 1, 'capacity')
for flow_func in flow_funcs:
T = nx.gomory_hu_tree(G, flow_func=flow_func)
assert_true(nx.is_tree(T))
for u, v in combinations(G, 2):
cut_value, edge = self.minimum_edge_weight(T, u, v)
assert_equal(nx.minimum_cut_value(G, u, v),
cut_value)
示例11: _graphml2nx
def _graphml2nx(fname):
g=nx.DiGraph()
def _attrdict(node):
attrs=node.attributes()
return {key:attrs[key].value for key in attrs if key not in attr_blacklist}
parser=GraphMLParser()
imported_graph=parser.parse(fname)
edges=[(edge.node1.id, edge.node2.id) for edge in imported_graph.edges()]
nodes=[(node.id, _attrdict(node)) for node in imported_graph.nodes()]
g.add_edges_from(edges)
g.add_nodes_from(nodes)
assert(nx.is_tree(g))
assert(nx.is_directed(g))
return g
示例12: default
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, np.generic):
return obj.item()
elif isinstance(obj, nx.Graph) or isinstance(obj, nx.DiGraph):
if nx.is_tree(obj):
# NOTE: the root must be the first node added. otherwise it won't work
return json_graph.tree_data(obj, obj.nodes_iter().next())
else:
return json_graph.node_link_data(obj)
elif isinstance(obj, pd.DataFrame):
return obj.to_dict(orient='records')
return json.JSONEncoder.default(self, obj)
示例13: default
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, np.generic):
return obj.item()
elif isinstance(obj, nx.Graph) or isinstance(obj, nx.DiGraph):
if nx.is_tree(obj) and 'root' in obj.graph:
# NOTE: there must be a root graph attribute, or the root must be the first node added.
# otherwise it won't work
return json_graph.tree_data(obj, obj.graph['root'])
else:
return json_graph.node_link_data(obj)
elif isinstance(obj, pd.DataFrame):
return obj.to_dict(orient='records')
return json.JSONEncoder.default(self, obj)
示例14: test_wikipedia_example
def test_wikipedia_example(self):
# Example from https://en.wikipedia.org/wiki/Gomory%E2%80%93Hu_tree
G = nx.Graph()
G.add_weighted_edges_from((
(0, 1, 1), (0, 2, 7), (1, 2, 1),
(1, 3, 3), (1, 4, 2), (2, 4, 4),
(3, 4, 1), (3, 5, 6), (4, 5, 2),
))
for flow_func in flow_funcs:
T = nx.gomory_hu_tree(G, capacity='weight', flow_func=flow_func)
assert_true(nx.is_tree(T))
for u, v in combinations(G, 2):
cut_value, edge = self.minimum_edge_weight(T, u, v)
assert_equal(nx.minimum_cut_value(G, u, v, capacity='weight'),
cut_value)
示例15: check_spanning_tree
def check_spanning_tree(tree):
'''Check that a graph is a spanning tree.
Parameters
----------
tree : networkx graph
'''
nodes = np.array(tree.nodes())
n_nodes = len(nodes)
edges = np.array(tree.edges())
n_edges = len(edges)
# check number of edges
if not n_nodes - 1 == n_edges:
raise ValueError('Incorrect number of edes in graph.')
# check that graph is a tree
if not nx.is_tree(tree):
raise TypeError('Must input a tree.')