Diameter of disconnected components of a graph

1,465 views
Skip to first unread message

Deepa Nair

unread,
Apr 22, 2008, 8:37:25 AM4/22/08
to networkx...@googlegroups.com

Hi,
    Please tell me what went wrong in the following code:

g=XGraph(multiedges=false,selfloops=true)
#I have added some edges
c=networkx.number_connected_components(obj.g) # Gave the value 118
k=networkx.connected_components(g)
for i in k:
      d=diameter(i)

But I get the following error.
File "C:\Python25\Lib\site-packages\networkx\distance.py", line 60, in diameter
    e=eccentricity(G,with_labels=True)
  File "C:\Python25\Lib\site-packages\networkx\distance.py", line 28, in eccentricity
    nodes=G.nodes()
AttributeError: 'list' object has no attribute 'nodes'


Another question, is there any facility in networkx to check whether
a node is included in any cycle. Is your cycle_graph() function can do
any purpose here..


THanks and regards,
deepa

Aric Hagberg

unread,
Apr 22, 2008, 8:46:04 AM4/22/08
to networkx...@googlegroups.com
On Tue, Apr 22, 2008 at 06:07:25PM +0530, Deepa Nair wrote:
> Hi,
> Please tell me what went wrong in the following code:
>
> g=XGraph(multiedges=false,selfloops=true)
> #I have added some edges
> c=networkx.number_connected_components(obj.g) # Gave the value 118
> k=networkx.connected_components(g)

Use networkx.connected_component_subgraphs here

> for i in k:
> d=diameter(i)
>
> But I get the following error.
> File "C:\Python25\Lib\site-packages\networkx\distance.py", line 60, in
> diameter
> e=eccentricity(G,with_labels=True)
> File "C:\Python25\Lib\site-packages\networkx\distance.py", line 28, in
> eccentricity
> nodes=G.nodes()
> AttributeError: 'list' object has no attribute 'nodes'
>
>
> Another question, is there any facility in networkx to check whether
> a node is included in any cycle. Is your cycle_graph() function can do
> any purpose here..

There are no cycle detection or counting algorithms. If you write
some we'd be glad to include them.

Aric

Deepa Nair

unread,
Apr 23, 2008, 2:03:30 AM4/23/08
to networkx...@googlegroups.com
Hi,
   Thanks for the reply.

Iam trying to write the code to find out cycles.
One more doubt regarding the diameter like:
I have a graph of about 700 nodes and
networkx.number_connected_components gave the value 16.
This means that there are 16 distinct connected components.
I want to find out the diameter for each of the components
and then took its average.

I used as u said in the previous reply..But the results i
got was amazing and iam doubted whether i have used it in the right way..

Here is the code

k=networkx.connected_component_subgraphs(obj.g)
for i in k:
 print "\nNodes in component :",i.nodes() #line 2
        p=p+diameter(i)

Line 2 give lists which consists of only 2 or 0 nodes. Iam sure the graph consists
of connected components which will have more nodes than this.
Pls help if i went wrong anywhere...



 

Christopher Ellison

unread,
Apr 23, 2008, 12:14:34 PM4/23/08
to networkx...@googlegroups.com
Deepa Nair wrote the following on 04/22/2008 11:03 PM:
> Here is the code
>
> k=networkx.connected_component_subgraphs(obj.g)
> for i in k:
> print "\nNodes in component :",i.nodes() #line 2
> p=p+diameter(i)
>
> Line 2 give lists which consists of only 2 or 0 nodes. Iam sure the
> graph consists
> of connected components which will have more nodes than this.

Can you provide this graph...or a smaller graph which demonstrates the
same problem? I am in agreement that a connected component should not
have 0 nodes.

Chris

Deepa Nair

unread,
Apr 24, 2008, 3:58:57 AM4/24/08
to networkx...@googlegroups.com
Hi,
     I have attached the text file which consists of the edge list. Each row shows each edge..
thanks

 
Hpylorippi.txt

Deepa Nair

unread,
Apr 24, 2008, 4:01:43 AM4/24/08
to networkx...@googlegroups.com
Hi,
Here is the python code to form the graph from the above file.
infile=open('Hpylorippi.txt','r')
row=infile.readline()
while row:         
            words=row.split()
            try:
                a=words[0]
                b=words[1]
                self.g.add_edge(a,b)
                print "Adding edge a=",a,"b=",b
            except:
                print "exception "
            row=infile.readline()   
        infile.close()

 

Pieter Swart

unread,
Apr 26, 2008, 1:51:45 PM4/26/08
to networkx-discuss
Deepa,

here is how I would read the graph
(with edges in the file Hpylorippi.txt in the working directory)

import networkx as nx
G = nx.read_edgelist("Hpylorippi.txt", nodetype=str, delimiter='
')
# the length of the delimiter is important
Gcc = nx.connected_component_subgraphs(G)
# this is a list of graphs, Gcc[0] is the most important here
[ len(g) for g in Gcc ]
# this gives the number of nodes in each connected component
[710, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1]
# let's inspect one of the isolated nodes
Gcc[10].nodes()
# Out[15]: ['HP0553']
# and inspecting your data file you will see that this
# corresponds to adding a self-loop HP0553 ---- HP0553

hth
Pieter Swart
Reply all
Reply to author
Forward
0 new messages