Miguel 2488
Miguel 2488

Reputation: 1440

Extracting edge information from network in python

i have the following network:

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)

In this network every person is a node and in this nodes are all connected with each other based in a type of relationship. The relationships that connect the nodes are the edges in this case.

What i need to do is extract the relationship information contained in the edges in order to create a function that given a person name and a relationship type, tells to which other people is connected based on the the specified relationship type.

I'm using the networkx package in python to perform this task. Since i'm totally new to networks this confuses me a bit so i will appreciate any suggestions on this.

Thanks in advance

Upvotes: 1

Views: 640

Answers (1)

mspiller
mspiller

Reputation: 3857

What I would do is to create a new graph only containing the edges that match the given "source", e.g. for "family":

family = nx.Graph([(u,v,d) for u,v,d in net.edges(data=True) if d["source"]=="family"])

You can then use

list(nx.bfs_tree(family, "Gur"))

To get the complete family of Gur

Upvotes: 1

Related Questions