question about ego_graph

272 views
Skip to first unread message

Jerry

unread,
Oct 15, 2010, 2:00:10 PM10/15/10
to networkx-discuss
Hi, I was working with the ego_graph function today and got some odd
behavior so I wanted to make sure that I understood what I was seeing.

I have an undirected, weighted graph (H) and wanted to find edges
within a certain distance (e.g. 50 units) of a particular node. I
found the ego_graph function and fed in the following parameters:

>>> q = nx.ego_graph(H, 'n509', radius = 50, undirected=True)

I was assuming that the radius was based on the edge weights. When I
looked at the results I found that I had a subgraph with 4870 nodes
and 5385 edges The answer I expected was 1 node and 1 edge.

>>> len(q)
4870
>>> len(q.edges())
5385

Then I tried the following:

>>> q = nx.ego_graph(H, 'n509', radius = 1, undirected=True)
>>> for i in q.edges(data=True):
print i


(u'n502', 'n509', {u'building': u'sidewalks', u'weight': 33,
u'lineslope': 0, u'floor': None, u'modcounti': 0, u'modified': False})
('n509', u'n557', {u'building': u'sidewalks', u'weight': 192,
u'lineslope': 5, u'floor': None, u'modcounti': 0, u'modified': False})
('n509', u'n274', {u'building': u'sidewalks', u'weight': 81,
u'lineslope': 9, u'floor': None, u'modcounti': 0, u'modified': False})
>>> nx.degree(H, 'n509')
3

So, it looks like it is working on n-order neighbors. Is this
correct? I thought it was based on edge-weights rather than neighbor
order - but when I run the following I get identical results for the
1st order neighbors:

>>> r = nx.neighbors(H, 'n509')
>>> r
[u'n502', u'n557', u'n274']

The graph is generated from a mongoDB collection which bring in the
unicode part - is it just not recognizing the weight parameter? Or
did I misunderstand the function? Or both...?

Thanks for any information.
Jerry

Drew Conway

unread,
Oct 15, 2010, 2:34:05 PM10/15/10
to networkx...@googlegroups.com
Hi Jerry,

The ego_graph function does work on n-order neighbors, rather than weights.  Note that the help file for ego_net defines radius as the following:

radius : integer, optional
Include all neighbors of distance<=radius from n

I am a bit unclear what you mean by "50 units" for a center node?  In your example it seems you want to include only those neighbors whose weights are above a certain threshold.

If this is the case than you want to do something akin to dichotomization. 

In your case, where H is the weighted graph and t is some threshold, you could do something like:

>>> H_thresh=nx.Graph([(a,b) for (a,b,c) in H.edges(data=True) if c['weight']>=t])

Note, in this case 'H_thresh' would drop the weight values from H.

This type of operation is common enough that it may be worth opening a ticket for.

--
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.



___________________________________
Drew Conway
Ph.D. Student
Department of Politics, New York University
@drewconway

Aric Hagberg

unread,
Oct 15, 2010, 2:40:56 PM10/15/10
to networkx...@googlegroups.com
On Fri, Oct 15, 2010 at 12:00 PM, Jerry <jerry...@gmail.com> wrote:
> Hi,  I was working with the ego_graph function today and got some odd
> behavior so I wanted to make sure that I understood what I was seeing.
>
> I have an undirected, weighted graph (H) and wanted to find edges
> within a certain distance (e.g. 50 units) of a particular node.  I
> found the ego_graph function and fed in the following parameters:
>
>>>> q = nx.ego_graph(H, 'n509', radius = 50, undirected=True)
>
> I was assuming that the radius was based on the edge weights.

That version of ego_graph doesn't consider edge weights as distances.

But it is pretty simple to add that.
I just uploaded a version with a weighted= True keyword option.
https://networkx.lanl.gov/trac/ticket/449

Aric

Jerry

unread,
Oct 15, 2010, 3:11:19 PM10/15/10
to networkx-discuss
Hi Drew,

The part that threw me off was the term distance in the radius
description. I didn't equate distance and neighbor order so that's
where the problem came from. It makes sense - I just read it wrong.
A caveat explaining how distance is defined might have helped but I
get it. I already had another way to find node/edges within/outside a
certain weighted distance using edge weights as distance but I was
going to compare this in terms of speed.

Thanks for the information,
Jerry

ps. you guys are doing great work. Networkx has helped me a ton.

On Oct 15, 1:34 pm, Drew Conway <agc...@nyu.edu> wrote:
> Hi Jerry,
>
> The ego_graph function does work on n-order neighbors, rather than weights.  Note that the help file for ego_net defines radius as the following:
>
> radius : integer, optional
> Include all neighbors of distance<=radius from nhttp://networkx.lanl.gov/reference/generated/networkx.generators.ego....
> > For more options, visit this group athttp://groups.google.com/group/networkx-discuss?hl=en.
>
> ___________________________________
> Drew Conway
> Ph.D. Student
> Department of Politics, New York University
> drew.con...@nyu.eduhttp://www.drewconway.com/
> @drewconway

Jann Poppinga

unread,
Jun 16, 2021, 8:02:25 AM6/16/21
to networkx-discuss
Hi Jerry,

I'm in the same boat as you were. I see it's been over ten years, but are you maybe still able to share how you did it?

Best,
Jann
Reply all
Reply to author
Forward
0 new messages