我具有以下网络:
net = nx.Graph()
node_list = ["Gur","Qing","Samantha","Jorge","Lakshmi","Jack","John","Jill"]
edge_list = [("Gur","Qing",{"source":"work"}),
("Gur","Jorge", {"source":"family"}),
("Samantha","Qing", {"source":"family"}),
("Jack","Qing", {"source":"work"}),
("Jorge","Lakshmi", {"source":"work"}),
("Jorge","Samantha",{"source":"family"}),
("Samantha","John", {"source":"family"}),
("Lakshmi","Jack", {"source":"family"}),
("Jack","Jill", {"source":"charity"}),
("Jill","John",{"source":"family"})]
net.add_nodes_from(nodes)
net.add_edges_from(edges)
在此网络中,每个人都是一个节点,并且在该节点中,所有用户都基于一种关系彼此连接。在这种情况下,连接节点的关系就是边缘。
我需要做的是提取边缘中包含的关系信息,以创建给定人名和关系类型的函数,并根据指定的关系类型告诉其他人与之连接。
我正在python中使用networkx
包来执行此任务。由于我是网络的新手,这让我有些困惑,因此我将不胜感激任何建议。
预先感谢
答案 0 :(得分:1)
我要做的是创建一个仅包含与给定“源”匹配的边的新图,例如对于“家庭”:
family = nx.Graph([(u,v,d) for u,v,d in net.edges(data=True) if d["source"]=="family"])
然后您可以使用
list(nx.bfs_tree(family, "Gur"))
获得完整的古尔族