简体   繁体   English

如何使用networkx图根据中间节点的属性值找到2个节点之间的路径?

[英]How to find a path between 2 nodes based on in-between node's attribute's value using networkx graph?

I can search thru a tree and get shortest path between nodes using just simple:我可以通过一棵树搜索并使用简单的方法获得节点之间的最短路径:

nx.shortest_path(G, source=, target=)

But how can I choose a path going thru a node with particular attribute's value?但是我怎样才能选择一条通过具有特定属性值的节点的路径呢?

I have simple graph with nodes我有带节点的简单图

G = nx.Graph()
for token in document:
    G.add_node(token.orth_, item = token.i, tag = token.tag_, dep = token.dep_)

and edges:和边缘:

for token in document:    
    for child in token.children:
        G.add_edge(token.orth_, child.orth_, pitem = token.i, citem = child.i,
                   ptag = token.tag_, pdep = token.dep_, ctag = child.tag_, cdep = child.dep_)

Can I find simple solution because now i'm struggling to build complicated function.我能找到简单的解决方案吗,因为现在我正在努力构建复杂的功能。

EDIT编辑

The idea is to have a function like this: (sketchy)这个想法是有一个这样的功能:(粗略)

def getPathByNode(betw_word, betw_attr, src_word, src_attr, trg_word, trg_attr):
    nx.shortest_path(G, source=src, source_attr=src_attr, target=trg, target_attr=trg_attr, through=betw_word, through_attr=betw_attr)
    ....

But of course not all parameters must be passed.但当然不是所有的参数都必须传递。 As inputs I'd take for example:作为输入,我将举例:

source_attr = {'dep_': 'ROOT'}
target_attr = {'tag_': 'NN'}

through = "of" or through = "from" or through_attr = {'tag_': 'IN'} through = "of" or through = "from" or through_attr = {'tag_': 'IN'}

And et cetera.等等。 I'm currently trying to build recursion starting from the middle ( through='from' ) and searchning for neighhbors but the same situatuion - missing attributes.我目前正在尝试从中间( through='from' )开始构建递归并搜索邻居,但情况相同 - 缺少属性。

for i in G.neighbors("from"):
    print(i)

i is just a string.我只是一个字符串。

A simple solution would be computing all paths from source to target.一个简单的解决方案是计算从源到目标的所有路径。 Then just filter out all paths without a node that has the desired condition, and choose the shortest path among this set of paths.然后过滤掉所有没有满足条件的节点的路径,在这组路径中选择最短的路径。 Assuming you have an undirected and unweighted graph, something like this should work:假设你有一个无向和未加权的图,这样的事情应该可以工作:

import networkx as nx

# Generate a sample graph:
G = nx.barabasi_albert_graph(10, 3, seed=42)
print(G.edges())

def check_attribute(G, node):
    # Say the condition is node id is 3:
    return node == 3

valid_paths = []
for path in nx.all_simple_paths(G, source=0, target=7):
    cond = False
    for node in path:
        if check_attribute(G, node):
            cond = True
            valid_paths.append(path)
            break

lengths = [len(path) for path in valid_paths]
shortest_path = valid_paths[lengths.index(min(lengths))]
print('valid paths: {}'.format(valid_paths))
print('shortest_path: {}'.format(shortest_path))

暂无
暂无

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

相关问题 创建一个networkx加权图并找到权重最小的2个节点之间的路径 - Create a networkx weighted graph and find the path between 2 nodes with the smallest weight 根据节点属性对 NetworkX 图的节点进行排序 - Sort nodes of a NetworkX graph based on a node attribute 如何根据图的模块化改变networkx图中节点的颜色 - How to change color of nodes in networkx graph based on graph's modularity 如何过滤具有特定属性的节点并使用networkx图保留路径 - How to filter nodes with specifc attribute and preserve the path using networkx graph 根据节点的属性NetworkX将图划分为太阳图 - partition graph into sungraphs based on node's attribute NetworkX 是否有可能找到一个起始节点和多个目的节点之间的最短路径? 使用 Networkx 或 OSMnx - is it possible to find the shortest path between an origin node and multiple destination nodes? Using Networkx or OSMnx 如何在图中的两个节点之间找到相似性(NetworkX / Python) - How to find similarity between two nodes in graph (NetworkX/ Python) Networkx图:查找给定节点集中的任何节点与另一组节点之间是否存在路径 - Networkx graph: finding if path exists between any node in a given set of nodes and another set of nodes 在Python Networkx中的两个节点之间添加按节点的边属性 - Adding an edge by node attribute between two nodes in Python Networkx Python networkx - 在 DAG 中找到 2 个节点之间的最重路径 - Python networkx - find heaviest path in DAG between 2 nodes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM