简体   繁体   English

从度量> = 3的节点处拆分的图形中检索路径

[英]Retrieve paths from a graph split at nodes with a degree >= 3

When I add paths to a graph: 当我向图表添加路径时:

>>> graph = nx.MultiGraph()  # Needs to be MultiGraph/MultiDiGraph.
>>> graph.add_path([1,2,3,4,5])
>>> graph.add_path([2,6,7])
>>> graph.add_path([4,8,9,10,11])

What can I do to retrieve my paths split at nodes with a degree >= 3? 我可以做什么来检索在度数> = 3的节点上拆分的路径? So that I get: 所以我得到:

[[1, 2], [2, 6, 7], [2, 3, 4], [4, 8, 9, 10, 11], [4, 5]]

I assume you know your paths in advance. 我假设你事先知道你的路径。 If you actually start out with a graph and want to "strip" it into paths, let me know. 如果你真的开始使用图表并希望将其“剥离”到路径中,请告诉我。 I would suggest another approach then. 我会建议另一种方法。

Here is would I came up with, might not be the fastest or most elegant way, but it should work: 这是我想出来的,可能不是最快或最优雅的方式,但它应该工作:

import networkx as nx

graph = nx.MultiGraph()
paths = [[1,2,3,4,5], [2,6,7], [4,8,9,10,11]]

for path in paths:
    graph.add_path(path)

splitted_paths = []    

# generate list of nodes at which the paths are to be splitted
splitting_nodes = [node for node in graph if graph.degree(node)>=3]

for path in paths:
    # find the splitting nodes in the current path
    splitting_nodes_in_path = [node for node in splitting_nodes if node in path]
    for splitting_node in splitting_nodes_in_path:
        # get remaining path up to the current splitting node
        path_piece = path[:path.index(splitting_node)+1]
        if len(path_piece) > 1:
            splitted_paths.append(path_piece)
        # overwrite current path with the remaining path
        path = path[path.index(splitting_node):]
    # get the remaining piece from the last splitting node until the end of the current path
    if len(path) > 1:        
        splitted_paths.append(path)

print splitted_paths

Hope this helps! 希望这可以帮助!

EDIT 1: removed an unnecessary for loop and added some missing code lines 编辑1:删除了一个不必要的for循环并添加了一些丢失的代码行


EDIT 2: If you need to start out with a graph, like @marcus comment suggests, and want to have two paths in the given list of paths "glued together" if they are connected without a node of degree >= 3, you have to use a different approach. 编辑2:如果您需要从图表开始,如@marcus评论建议,并希望在给定的路径列表中有两个路径“粘在一起”,如果它们连接没有度数> = 3的节点,你有使用不同的方法。 I don't have the time to fully code what I have in mind to solve this, but here is a sketch of what I would try: 我没有时间完全编写我的想法来解决这个问题,但这里是我想要尝试的草图:

  • write a function cut(graph, node) that takes a Networkx graph and a nodelabel and replaces node by degree( node ) new nodes, each connected to one of node s neighbors (one has to think of a clever way to name the new nodes, so that it's clear in the end where they came from) 编写一个函数cut(graph, node) ,它接受Networkx图和nodelabel并逐个node替换nodenode )新节点,每个节点连接到一个node的邻居(一个人必须想到一个聪明的方法来命名新节点) ,所以最终他们来自哪里清楚)
  • apply cut() to all nodes of degree >= 3 and end up with a disconnected graph, where each component is one of the desired splitted paths cut()应用于度> = 3的所有节点,最后得到一个断开连接的图,其中每个组件都是所需的分割路径之一

The following Networkx functions could maybe be helpful for that: remove_node() , subgraph() . 以下Networkx函数可能对此有用: remove_node()remove_node() subgraph() all_simple_paths() , predecessor() and the Operators . all_simple_paths()predecessor()Operators

first: why do you need MultiGraph? 第一:你为什么需要MultiGraph? From what you are saying, a simple DiGraph would suffice; 根据你的说法,一个简单的DiGraph就足够了; MultiGraphs are only needed if you want two different connections from eg node 4 and 10, with different attributes. 只有在需要两个不同的连接时才需要MultiGraphs,例如节点4和10,具有不同的属性。

A lot of nx functions don't work with MultiGraphs, but if DiGraphs will work, then you could try weakly_connected_subcomponent_graphs . 很多nx函数不适用于MultiGraphs,但如果DiGraphs可以工作,那么你可以尝试使用weakly_connected_subcomponent_graphs I don't have time to completely code it up, but this should be the algorithm: 我没有时间对其进行完全编码,但这应该是算法:

1- Get all the nodes with degree >=3 (this is easy, and left as an exercise to the reader), along with any node connected to it (predecessors + successors). 1-获取程度> = 3的所有节点(这很容易,并留给读者练习),以及连接到它的任何节点(前辈+后继者)。 2- Remove the nodes from the graph. 2-从图中删除节点。 This should leave only nodes with degree <= 2. 3- Call weakly_connected_subcomponent_graphs. 这应该只留下度<= 2的节点.3-调用weakly_connected_subcomponent_graphs。 This will return the forest of remaining graphs that aren't connected to each other. 这将返回未相互连接的剩余图形的林。 4- But the nodes with degree >=3 aren't included; 4-但是不包括度> = 3的节点; you'll need to add them back. 你需要把它们加回去。 You have to add both nodes and edges; 你必须添加节点和边; you can either just get the node list from the weakly connected subtree, add in the connection nodes, and ask the original graph for a subgraph, or add the edges yourself. 你可以从弱连接的子树中获取节点列表,添加连接节点,并向原始图形询问子图,或者自己添加边。

This should be reasonably fast. 这应该相当快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM