在网络图中获得直接边缘连接(Getting direct edge connections in a network graph)

编程入门 行业动态 更新时间:2024-10-22 19:26:30
在网络图中获得直接边缘连接(Getting direct edge connections in a network graph)

我有以下网络图:

import networkx as nx net = nx.Graph() node_list = ["Gur","Qing","Samantha","Jorge","Lakshmi","Jack","John","Jill"] edge_list = [("Gur","Qing",{"relation":"work"}), ("Gur","Jorge", {"relation":"family"}), ("Samantha","Qing", {"relation":"family"}), ("Jack","Qing", {"relation":"work"}), ("Jorge","Lakshmi", {"relation":"work"}), ("Jorge","Samantha",{"relation":"family"}), ("Samantha","John", {"relation":"family"}), ("Lakshmi","Jack", {"relation":"family"}), ("Jack","Jill", {"relation":"charity"}), ("Jill","John",{"relation":"family"})] net.add_nodes_from(node_list) net.add_edges_from(edge_list)

我想构建一个给定网络,节点名称和关系类型的函数,返回给定人员直接连接的人员列表。

这是我到目前为止的功能:

def get_relations(graph,node,relationship): if relationship == 'charity': charity = [ (v) for (u,v,d) in net.edges( data = True) if d["relation"]=="charity"] return list(set(charity)) else: if relationship == 'work': work = [ (v) for (u,v,d) in net.edges( data = True) if d["relation"]=="work"] return list(set(work)) else: if relationship == 'family': family = [(v) for (u,v,d) in net.edges( data = True) if d["relation"]=="family"] return list(set(family)) else: return None

像这样调用函数:

get_connections(net, 'John', 'family')

产生这个输出:

['Gur', 'Samantha', 'John', 'Lakshmi']

这不是我需要的,我希望它只返回与John直接相关的人,或者任何节点,直接路径,而不是间接路径。

以相同的方式调用函数应该将其作为正确的输出:

['John', 'Jill', 'Samantha', 'Qing', 'Jorge', 'Gur']

代码尝试:

def get_relations(graph,node,relationship): if relationship not in {'charity', 'work', 'family'}: return None relation_net = nx.Graph([(u,v,d) for (u, v, d) in net.edges( data = True) if d["relation"] == relationship]) relation_subnet = nx.Graph([(node,v,d) for (u, v, d) in relation_net.edges( data = True) if d["relation"] == relationship]) return list(set(relation_subnet.nodes))

但是,这仍然会返回错误的结果。

i have the folowwing network graph:

import networkx as nx net = nx.Graph() node_list = ["Gur","Qing","Samantha","Jorge","Lakshmi","Jack","John","Jill"] edge_list = [("Gur","Qing",{"relation":"work"}), ("Gur","Jorge", {"relation":"family"}), ("Samantha","Qing", {"relation":"family"}), ("Jack","Qing", {"relation":"work"}), ("Jorge","Lakshmi", {"relation":"work"}), ("Jorge","Samantha",{"relation":"family"}), ("Samantha","John", {"relation":"family"}), ("Lakshmi","Jack", {"relation":"family"}), ("Jack","Jill", {"relation":"charity"}), ("Jill","John",{"relation":"family"})] net.add_nodes_from(node_list) net.add_edges_from(edge_list)

I want to build a function that given a network, a node name, and a relationship type, returns a list of the people to which the given person is directly connected.

This is the function i got so far:

def get_relations(graph,node,relationship): if relationship == 'charity': charity = [ (v) for (u,v,d) in net.edges( data = True) if d["relation"]=="charity"] return list(set(charity)) else: if relationship == 'work': work = [ (v) for (u,v,d) in net.edges( data = True) if d["relation"]=="work"] return list(set(work)) else: if relationship == 'family': family = [(v) for (u,v,d) in net.edges( data = True) if d["relation"]=="family"] return list(set(family)) else: return None

calling the function like this:

get_connections(net, 'John', 'family')

yields this output:

['Gur', 'Samantha', 'John', 'Lakshmi']

This is not what i need though, i would like it to return only those people which are directly connected to John, or whatever the node is, with a direct path, not indirect ones.

Calling the function in the same way should yield this as the correct output:

['John', 'Jill', 'Samantha', 'Qing', 'Jorge', 'Gur']

Code attempt:

def get_relations(graph,node,relationship): if relationship not in {'charity', 'work', 'family'}: return None relation_net = nx.Graph([(u,v,d) for (u, v, d) in net.edges( data = True) if d["relation"] == relationship]) relation_subnet = nx.Graph([(node,v,d) for (u, v, d) in relation_net.edges( data = True) if d["relation"] == relationship]) return list(set(relation_subnet.nodes))

However, this still returns the wrong results.

最满意答案

根据PMende的指导,您的编码尝试非常接近。 问题在于你的第二次提取,当你试图只采取连接到约翰的那些边:

relation_subnet = nx.Graph([(node,v,d) for (u, v, d) in relation_net.edges( data = True) if d["relation"] == relationship])

(u,v,d)的选择(节点,v,d)是错误的,条件错误。 在第一个条款中

(node,v,d) for (u, v, d) in ...

对于relation_net每个节点,您可以获取src,dst和arc值...但是然后丢弃src并强制John进入该位置。 这仅在John已经恰好成为源时才有效; 否则,您刚刚创建了一个新的子网边缘,例如

("John","Jack", {"relation":"family"}) # Lakshmi removed

相反,您需要将包含John的边缘保持在任一位置。

其次,你的“关系”过滤器第二次没有做任何事情; 你已经第一次包括了每个家庭关系。 相反,你需要收集与John一起出现在家庭边缘的每个人。

relation_subnet = nx.Graph([(u,v,d) for (u, v, d) in relation_net.edges( data = True) if u == node or v == node])

这正确地产生了

['Samantha', 'Jill', 'John']

萨曼莎和吉尔是约翰的唯一直系家庭。 如果你想要这个列表上的连接闭包,那么你需要迭代这个过程,将所有新的family添加到Jill和/或Samantha,重复这个,直到没有添加新节点。

你能从那里拿走吗?

Your coding attempt per PMende's guidance is very close. The problem is in your second extraction, when you try to take only those edges connected to John:

relation_subnet = nx.Graph([(node,v,d) for (u, v, d) in relation_net.edges( data = True) if d["relation"] == relationship])

The selection (node,v,d) for (u, v, d) is wrong, and the condition is wrong. In the first clause

(node,v,d) for (u, v, d) in ...

For each node in relation_net, you take the src, dst, and arc values ... but then discard the src and force John into that position. This is valid only when John already happens to be the source; otherwise, you just created a new subnet edge, such as

("John","Jack", {"relation":"family"}) # Lakshmi removed

Instead, you need to keep those edges which include John in either position.

Second, your "relation" filter does nothing the second time; you already included every family relationship the first time. Instead, you need to collect everyone who appears in a family edge with John.

relation_subnet = nx.Graph([(u,v,d) for (u, v, d) in relation_net.edges( data = True) if u == node or v == node])

This correctly yields

['Samantha', 'Jill', 'John']

Samantha and Jill are the only direct-link family of John's. If you want the concatenation closure on this list, then you need to iterate on the process, adding everyone new who is family to Jill and/or Samantha, repeating that until no new nodes are added.

Can you take it from there?

更多推荐

本文发布于:2023-08-07 12:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图中   边缘   网络   direct   graph

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!