filter all nodes with only one edge

1,639 views
Skip to first unread message

Hugo Koopmans

unread,
Feb 16, 2011, 3:36:24 PM2/16/11
to networkx-discuss
Hi all,

I am new to networkX and have the following question:
I have created a network graph and now I would like to filter out all
nodes with only one edge.
So I keep the network of nodes(people) who have more then one friend
in the network...

thank you for a reply

hugo

Aric Hagberg

unread,
Feb 16, 2011, 3:45:18 PM2/16/11
to networkx...@googlegroups.com

Here is the "one-liner" way ([17])

In [15]: G=nx.fast_gnp_random_graph(10,0.1)

In [16]: G.degree()
Out[16]: {0: 4, 1: 3, 2: 3, 3: 2, 4: 1, 5: 2, 6: 4, 7: 2, 8: 1, 9: 2}

In [17]: G.remove_nodes_from((n for n,d in G.degree_iter() if d==1))

In [18]: G.degree()
Out[18]: {0: 4, 1: 2, 2: 2, 3: 2, 5: 2, 6: 4, 7: 2, 9: 2}

Aric

Amitabh Sharma

unread,
Feb 16, 2011, 4:09:11 PM2/16/11
to networkx...@googlegroups.com
Hi Aric,
In the similar line, I would like to ask you how to retain the edges having link with two friends i.e. n1 is connected to friend1 and also with friend2. How to do this in networkx.

Amitabh

> --
> You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
> To post to this group, send email to networkx...@googlegroups.com.
> To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/networkx-discuss?hl=en.
>

Christopher Ellison

unread,
Feb 16, 2011, 4:45:30 PM2/16/11
to networkx...@googlegroups.com
Amitabh Sharma wrote the following on 02/16/2011 01:09 PM:
> Hi Aric, In the similar line, I would like to ask you how to retain
> the edges having link with two friends i.e. n1 is connected to
> friend1 and also with friend2. How to do this in networkx.
>

Hi Amitabh,

It's nearly identical to what Aric showed you. Previously you wanted to
remove all nodes which had degree equal to 1. Now you want to remove
all nodes which have degree not equal to 2. So replace,

d==1

with

d!=2

Chris

Dan Schult

unread,
Feb 16, 2011, 4:46:28 PM2/16/11
to networkx...@googlegroups.com
A one liner version could be:

G.remove_nodes_from((n for n in G if n not in G[friend1] or n not in G[friend2]))

But its probably better to do something like:

bothnbrs=set([friend1,friend2]) ^ (set(G[friend1]) & set(G[friend2]))
H=G.subgraph(bothnbrs)

Dan

Amitabh Sharma

unread,
Feb 16, 2011, 4:56:54 PM2/16/11
to networkx...@googlegroups.com
Thanks Dan!
It worked, I have to just check it in the graph..soo easy :)

cheers

Hugo Koopmans

unread,
Feb 17, 2011, 2:34:58 AM2/17/11
to networkx...@googlegroups.com
Thank you guys for this quick and excellent response!
much appriciated

hugo
Vriendelijke groet / Kind regards,

Hugo Koopmans

managing partner

DIKW Consulting
___________________________________
data > information > knowledge > wisdom

[w] www.dikw.com
[m] +31 6 4310 6780
Reply all
Reply to author
Forward
0 new messages