本文整理匯總了Python中networkx.shortest_simple_paths方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.shortest_simple_paths方法的具體用法?Python networkx.shortest_simple_paths怎麽用?Python networkx.shortest_simple_paths使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.shortest_simple_paths方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: visualize_frags
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def visualize_frags(outdir, graphs, options):
from rpy2.robjects import r
utilities.ensure_dir(outdir)
for i, graph in enumerate(graphs):
r.pdf(os.path.join(outdir, "fragments.cluster_{}.pdf".format(i)))
for component in networkx.connected_components(graph):
subgraph = graph.subgraph(component)
ends = [node for node,degree in subgraph.degree_iter() if degree==1]
breakends = [node for node in list(networkx.shortest_simple_paths(subgraph, ends[0], ends[1]))[0]]
# breakends = [breakend_from_label(node) for node in breakends]
breakends = breakends[:-1:2] + breakends[-1:]
# print ")"*100, breakends
for sample, dataset in sorted(options.iter_10xdatasets()):
plot_frags(breakends, options, sample, dataset)
# plot_frags(breakpoints, options, sample, dataset)
r["dev.off"]()
示例2: k_shortest_paths
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def k_shortest_paths(self, source, target):
"""
如果源宿點是None,則返回len為1的None數組
:param source:
:param target:
:return:
"""
if source is None:
return [None]
generator = shortest_simple_paths(self, source, target, weight=self.weight)
rtn = []
index = 0
for i in generator:
index += 1
if index > self.k:
break
rtn.append(i)
return rtn
開發者ID:BoyuanYan,項目名稱:Actor-Critic-Based-Resource-Allocation-for-Multimodal-Optical-Networks,代碼行數:20,代碼來源:RwaNet.py
示例3: k_shortest_paths
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def k_shortest_paths(self, source, target):
"""
如果源宿點是None,則返回len為1的None數組
:param source:
:param target:
:return:
"""
if source is None:
return [None]
generator = shortest_simple_paths(self.net, source, target, weight=self.weight)
rtn = []
index = 0
for i in generator:
index += 1
if index > self.k:
break
rtn.append(i)
return rtn
開發者ID:BoyuanYan,項目名稱:Actor-Critic-Based-Resource-Allocation-for-Multimodal-Optical-Networks,代碼行數:20,代碼來源:Service.py
示例4: visualize_frag_cluster
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def visualize_frag_cluster(breakpoints, options):
"""
breakpoints are (chromx, x, chromy, y, orientation)
"""
graph = networkx.Graph()
for breakpoint in breakpoints:
chromx, x, chromy, y, orientation = breakpoint
graph.add_edge((chromx, x, orientation[0]), (chromy, y, orientation[1]))
ends = [node for node,degree in graph.degree_iter() if degree==1]
breakends = [node for node in list(networkx.shortest_simple_paths(graph, ends[0], ends[1]))[0]]
# breakends = [breakend_from_label(node) for node in breakends]
breakends = breakends[:-1:2] + breakends[-1:]
# print ")"*100, breakends
for sample, dataset in sorted(options.iter_10xdatasets()):
plot_frags(breakends, options, sample, dataset)
示例5: k_shortest_paths
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def k_shortest_paths(G, source, target, k, weight=None):
return list(islice(nx.shortest_simple_paths(G, source, target, weight=weight), k))
# DE PM! LO Ordena de menor a menos segun el weight
示例6: nodes_in_order
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def nodes_in_order(self):
ends = self.ends
return list(networkx.shortest_simple_paths(self.graph, ends[0], ends[1]))[0]
示例7: find_all_paths
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def find_all_paths(self, max_paths=10, sort=True, score=True):
""" Find all paths through DAG to End
Params:
max_paths (int :default:=10): Number of paths to find
If this is > 1000, all paths will be found
sort (bool) : If True (default), sort paths
in ascending order of length
"""
if self.roots:
self.lock_start()
if score:
self.score_graph()
# shortest_simple_paths is slow for >1000 paths
if max_paths <= 1000:
if score:
paths = list(six.moves.map(lambda x: x[1:-1],
islice(nx.shortest_simple_paths(
self.G, self.start, self.end, weight='weight'),
max_paths)))
scores = self.scorer.score_splits(paths)
path_scores = zip(paths, scores)
sorted_path_scores = sorted(path_scores, key=operator.itemgetter(1), reverse=True)
logger.debug("Sorted paths with scores:\n %s", sorted_path_scores)
# Strip the scores from the returned result, to be consistent with no-scoring option
sorted_paths, _ = zip(*sorted_path_scores)
return list(sorted_paths)
else:
paths = list(six.moves.map(lambda x: x[1:-1],
islice(nx.shortest_simple_paths(
self.G, self.start, self.end),
max_paths)))
return paths
else: # Fall back to all_simple_paths
ps = list(six.moves.map(lambda x: x[1:-1],
nx.all_simple_paths(self.G, self.start, self.end)))
# If we do not intend to display paths, no need to sort them
if sort:
ps.sort(key=lambda x: len(x))
return ps
示例8: _get_single_path
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def _get_single_path( # pylint: disable=too-many-arguments, too-many-locals
self,
graph: DiGraph,
source: Address,
target: Address,
value: PaymentAmount,
reachability_state: AddressReachabilityProtocol,
visited: Dict[ChannelID, float],
disallowed_paths: List[List[Address]],
fee_penalty: float,
) -> Optional[Path]:
# update edge weights
for node1, node2 in graph.edges():
edge = graph[node1][node2]
backwards_edge = graph[node2][node1]
edge["weight"] = self.edge_weight(
visited=visited,
view=edge["view"],
view_from_partner=backwards_edge["view"],
amount=value,
fee_penalty=fee_penalty,
)
# find next path
all_paths: Iterable[List[Address]] = nx.shortest_simple_paths(
G=graph, source=source, target=target, weight="weight"
)
try:
# skip duplicates and invalid paths
path = next(
p
for p in (Path(self.G, nodes, value, reachability_state) for nodes in all_paths)
if p.is_valid and p.nodes not in disallowed_paths
)
return path
except StopIteration:
return None
示例9: test_shortest_simple_paths
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def test_shortest_simple_paths():
G = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")
paths = nx.shortest_simple_paths(G, 1, 12)
assert_equal(next(paths), [1, 2, 3, 4, 8, 12])
assert_equal(next(paths), [1, 5, 6, 7, 8, 12])
assert_equal([len(path) for path in nx.shortest_simple_paths(G, 1, 12)],
sorted([len(path) for path in nx.all_simple_paths(G, 1, 12)]))
示例10: test_shortest_simple_paths_directed
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def test_shortest_simple_paths_directed():
G = nx.cycle_graph(7, create_using=nx.DiGraph())
paths = nx.shortest_simple_paths(G, 0, 3)
assert_equal([path for path in paths], [[0, 1, 2, 3]])
示例11: test_Greg_Bernstein
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def test_Greg_Bernstein():
g1 = nx.Graph()
g1.add_nodes_from(["N0", "N1", "N2", "N3", "N4"])
g1.add_edge("N4", "N1", weight=10.0, capacity=50, name="L5")
g1.add_edge("N4", "N0", weight=7.0, capacity=40, name="L4")
g1.add_edge("N0", "N1", weight=10.0, capacity=45, name="L1")
g1.add_edge("N3", "N0", weight=10.0, capacity=50, name="L0")
g1.add_edge("N2", "N3", weight=12.0, capacity=30, name="L2")
g1.add_edge("N1", "N2", weight=15.0, capacity=42, name="L3")
solution = [['N1', 'N0', 'N3'], ['N1', 'N2', 'N3'], ['N1', 'N4', 'N0', 'N3']]
result = list(nx.shortest_simple_paths(g1, 'N1', 'N3', weight='weight'))
assert_equal(result, solution)
示例12: test_weighted_shortest_simple_path
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def test_weighted_shortest_simple_path():
def cost_func(path):
return sum(G.edge[u][v]['weight'] for (u, v) in zip(path, path[1:]))
G = nx.complete_graph(5)
weight = {(u, v): random.randint(1, 100) for (u, v) in G.edges()}
nx.set_edge_attributes(G, 'weight', weight)
cost = 0
for path in nx.shortest_simple_paths(G, 0, 3, weight='weight'):
this_cost = cost_func(path)
assert_true(cost <= this_cost)
cost = this_cost
示例13: test_directed_weighted_shortest_simple_path
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def test_directed_weighted_shortest_simple_path():
def cost_func(path):
return sum(G.edge[u][v]['weight'] for (u, v) in zip(path, path[1:]))
G = nx.complete_graph(5)
G = G.to_directed()
weight = {(u, v): random.randint(1, 100) for (u, v) in G.edges()}
nx.set_edge_attributes(G, 'weight', weight)
cost = 0
for path in nx.shortest_simple_paths(G, 0, 3, weight='weight'):
this_cost = cost_func(path)
assert_true(cost <= this_cost)
cost = this_cost
示例14: test_ssp_source_missing
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def test_ssp_source_missing():
G = nx.Graph()
G.add_path([1,2,3])
paths = list(nx.shortest_simple_paths(G, 0, 3))
示例15: test_ssp_target_missing
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import shortest_simple_paths [as 別名]
def test_ssp_target_missing():
G = nx.Graph()
G.add_path([1,2,3])
paths = list(nx.shortest_simple_paths(G, 1, 4))