本文整理匯總了Python中networkx.eulerian_circuit方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.eulerian_circuit方法的具體用法?Python networkx.eulerian_circuit怎麽用?Python networkx.eulerian_circuit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.eulerian_circuit方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: run_diffusion_process
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import eulerian_circuit [as 別名]
def run_diffusion_process(self, node):
"""
Generating a diffusion tree from a given source node.
Linearizing it with an Eulerian tour.
:param node: Source of diffusion.
:return euler: Eulerian linear node sequence.
"""
infected = [node]
sub_graph = nx.DiGraph()
sub_graph.add_node(node)
infected_counter = 1
while infected_counter < self.number_of_nodes:
end_point = random.sample(infected, 1)[0]
nebs = [node for node in self.graph.neighbors(end_point)]
sample = random.sample(nebs, 1)[0]
if sample not in infected:
infected_counter = infected_counter + 1
infected = infected + [sample]
sub_graph.add_edges_from([(end_point, sample), (sample, end_point)])
if infected_counter == self.number_of_nodes:
break
euler = [str(u) for u, v in nx.eulerian_circuit(sub_graph, infected[0])]
if len(euler) == 0:
euler = [str(u) for u, v in nx.eulerian_circuit(graph, infected[0])]
return euler
示例2: test_eulerian_circuit_cycle
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import eulerian_circuit [as 別名]
def test_eulerian_circuit_cycle(self):
G=nx.cycle_graph(4)
edges=list(eulerian_circuit(G,source=0))
nodes=[u for u,v in edges]
assert_equal(nodes,[0,3,2,1])
assert_equal(edges,[(0,3),(3,2),(2,1),(1,0)])
edges=list(eulerian_circuit(G,source=1))
nodes=[u for u,v in edges]
assert_equal(nodes,[1,2,3,0])
assert_equal(edges,[(1,2),(2,3),(3,0),(0,1)])
G=nx.complete_graph(3)
edges=list(eulerian_circuit(G,source=0))
nodes=[u for u,v in edges]
assert_equal(nodes,[0,2,1])
assert_equal(edges,[(0,2),(2,1),(1,0)])
edges=list(eulerian_circuit(G,source=1))
nodes=[u for u,v in edges]
assert_equal(nodes,[1,2,0])
assert_equal(edges,[(1,2),(2,0),(0,1)])
示例3: test_eulerian_circuit_cycle
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import eulerian_circuit [as 別名]
def test_eulerian_circuit_cycle(self):
G = nx.cycle_graph(4)
edges = list(eulerian_circuit(G, source=0))
nodes = [u for u, v in edges]
assert_equal(nodes, [0, 3, 2, 1])
assert_equal(edges, [(0, 3), (3, 2), (2, 1), (1, 0)])
edges = list(eulerian_circuit(G, source=1))
nodes = [u for u, v in edges]
assert_equal(nodes, [1, 2, 3, 0])
assert_equal(edges, [(1, 2), (2, 3), (3, 0), (0, 1)])
G = nx.complete_graph(3)
edges = list(eulerian_circuit(G, source=0))
nodes = [u for u, v in edges]
assert_equal(nodes, [0, 2, 1])
assert_equal(edges, [(0, 2), (2, 1), (1, 0)])
edges = list(eulerian_circuit(G, source=1))
nodes = [u for u, v in edges]
assert_equal(nodes, [1, 2, 0])
assert_equal(edges, [(1, 2), (2, 0), (0, 1)])
示例4: _run_diffusion_process
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import eulerian_circuit [as 別名]
def _run_diffusion_process(self, node):
"""
Generating a diffusion tree from a given source node and linearizing it
with a directed Eulerian tour.
Arg types:
* **node** *(int)* - The source node of the diffusion.
Return types:
* **euler** *(list of strings)* - The list of nodes in the walk.
"""
infected = [node]
sub_graph = nx.DiGraph()
sub_graph.add_node(node)
infected_counter = 1
while infected_counter < self.diffusion_cover:
end_point = random.sample(infected, 1)[0]
nebs = [node for node in self.graph.neighbors(end_point)]
sample = random.choice(nebs)
if sample not in infected:
infected_counter = infected_counter + 1
infected = infected + [sample]
sub_graph.add_edges_from([(end_point, sample), (sample, end_point)])
if infected_counter == self.diffusion_cover:
break
euler = [str(u) for u, v in nx.eulerian_circuit(sub_graph, infected[0])]
return euler
示例5: test_eulerian_circuit_digraph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import eulerian_circuit [as 別名]
def test_eulerian_circuit_digraph(self):
G=nx.DiGraph()
G.add_cycle([0,1,2,3])
edges=list(eulerian_circuit(G,source=0))
nodes=[u for u,v in edges]
assert_equal(nodes,[0,1,2,3])
assert_equal(edges,[(0,1),(1,2),(2,3),(3,0)])
edges=list(eulerian_circuit(G,source=1))
nodes=[u for u,v in edges]
assert_equal(nodes,[1,2,3,0])
assert_equal(edges,[(1,2),(2,3),(3,0),(0,1)])
示例6: test_eulerian_circuit_multigraph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import eulerian_circuit [as 別名]
def test_eulerian_circuit_multigraph(self):
G=nx.MultiGraph()
G.add_cycle([0,1,2,3])
G.add_edge(1,2)
G.add_edge(1,2)
edges=list(eulerian_circuit(G,source=0))
nodes=[u for u,v in edges]
assert_equal(nodes,[0,3,2,1,2,1])
assert_equal(edges,[(0,3),(3,2),(2,1),(1,2),(2,1),(1,0)])
示例7: test_not_eulerian
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import eulerian_circuit [as 別名]
def test_not_eulerian(self):
f=list(eulerian_circuit(nx.complete_graph(4)))
示例8: test_eulerian_circuit_digraph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import eulerian_circuit [as 別名]
def test_eulerian_circuit_digraph(self):
G = nx.DiGraph()
nx.add_cycle(G, [0, 1, 2, 3])
edges = list(eulerian_circuit(G, source=0))
nodes = [u for u, v in edges]
assert_equal(nodes, [0, 1, 2, 3])
assert_equal(edges, [(0, 1), (1, 2), (2, 3), (3, 0)])
edges = list(eulerian_circuit(G, source=1))
nodes = [u for u, v in edges]
assert_equal(nodes, [1, 2, 3, 0])
assert_equal(edges, [(1, 2), (2, 3), (3, 0), (0, 1)])
示例9: test_multigraph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import eulerian_circuit [as 別名]
def test_multigraph(self):
G = nx.MultiGraph()
nx.add_cycle(G, [0, 1, 2, 3])
G.add_edge(1, 2)
G.add_edge(1, 2)
edges = list(eulerian_circuit(G, source=0))
nodes = [u for u, v in edges]
assert_equal(nodes, [0, 3, 2, 1, 2, 1])
assert_equal(edges, [(0, 3), (3, 2), (2, 1), (1, 2), (2, 1), (1, 0)])
示例10: test_multigraph_with_keys
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import eulerian_circuit [as 別名]
def test_multigraph_with_keys(self):
G = nx.MultiGraph()
nx.add_cycle(G, [0, 1, 2, 3])
G.add_edge(1, 2)
G.add_edge(1, 2)
edges = list(eulerian_circuit(G, source=0, keys=True))
nodes = [u for u, v, k in edges]
assert_equal(nodes, [0, 3, 2, 1, 2, 1])
assert_equal(edges[:2], [(0, 3, 0), (3, 2, 0)])
assert_count_equal(edges[2:5], [(2, 1, 0), (1, 2, 1), (2, 1, 2)])
assert_equal(edges[5:], [(1, 0, 0)])
示例11: test_not_eulerian
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import eulerian_circuit [as 別名]
def test_not_eulerian(self):
f = list(eulerian_circuit(nx.complete_graph(4)))
示例12: create_eulerian_circuit
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import eulerian_circuit [as 別名]
def create_eulerian_circuit(graph_augmented, graph_original, start_node=None):
"""
networkx.eulerian_circuit only returns the order in which we hit each node. It does not return the attributes of the
edges needed to complete the circuit. This is necessary for the postman problem where we need to keep track of which
edges have been covered already when multiple edges exist between two nodes.
We also need to annotate the edges added to make the eulerian to follow the actual shortest path trails (not
the direct shortest path pairings between the odd nodes for which there might not be a direct trail)
Args:
graph_augmented (networkx graph): graph w links between odd degree nodes created from `add_augmenting_path_to_graph`.
graph_original (networkx graph): orginal graph created from `create_networkx_graph_from_edgelist`
start_node (str): name of starting (and ending) node for CPP solution.
Returns:
networkx graph (`graph_original`) augmented with edges directly between the odd nodes
"""
euler_circuit = list(nx.eulerian_circuit(graph_augmented, source=start_node, keys=True))
assert len(graph_augmented.edges()) == len(euler_circuit), 'graph and euler_circuit do not have equal number of edges.'
for edge in euler_circuit:
aug_path = nx.shortest_path(graph_original, edge[0], edge[1], weight='distance')
edge_attr = graph_augmented[edge[0]][edge[1]][edge[2]]
if not edge_attr.get('augmented'):
yield edge + (edge_attr,)
else:
for edge_aug in list(zip(aug_path[:-1], aug_path[1:])):
# find edge with shortest distance (if there are two parallel edges between the same nodes)
edge_aug_dict = graph_original[edge_aug[0]][edge_aug[1]]
edge_key = min(edge_aug_dict.keys(), key=(lambda k: edge_aug_dict[k]['distance'])) # index with min distance
edge_aug_shortest = edge_aug_dict[edge_key]
edge_aug_shortest['augmented'] = True
edge_aug_shortest['id'] = edge_aug_dict[edge_key]['id']
yield edge_aug + (edge_key, edge_aug_shortest, )