igraph-help
[Top][All Lists]

## Re: [igraph] Question on importing edgelists

 From: Gabor Csardi Subject: Re: [igraph] Question on importing edgelists Date: Tue, 5 Feb 2008 15:18:00 +0100 User-agent: Mutt/1.5.13 (2006-08-11)

```Richard,

i don't know whether you're using R/Python/C, here are two possible
solutions:

1) If you can be sure that in reality there is a single component
in your graph, and that has at least two vertices, then you can
read the graph with the isolates first and then remove the isolates.
Here is a way in R, preserving the original vertex ids as an attribute:

1 5
1 2
2 3
2 6
3 7
1 10

> V(g)\$name <- seq(vcount(g))-1
> cl <- clusters(g)
> g2 <- subgraph(g, which(cl\$membership==which.max(cl\$csize)-1)-1)
> V(g2)
Vertex sequence:
[1]  1  2  3  5  6  7 10

This can be done in Python/C as well, without much trouble.

2) In R, read the file as a matrix of strings, and use graph.edgelist().
This function uses symbolic vertex names if the input matrix contains
strings:

> el <- matrix(scan("/tmp/b.txt", character(0)), nc=2, byrow=TRUE)
> g <- graph.edgelist(el)
> V(g)
Vertex sequence:
[1] "1"  "5"  "2"  "3"  "6"  "7"  "10"

Note that the vertex ids are not necessarily assigned the same way
for the two solutions. 2) works if your graph has multiple components.

Gabor

On Mon, Feb 04, 2008 at 06:37:15PM -0500, Richard Geddes wrote:
> I have an edge list text file, in which edges are defined as a white
> space separated pairs of integers, each integer representing a node... I
> think this is standard... however, the node numbers in my dataset are
> not contiguous... ie for a graph consisting of nodes {0 .. n}  some
> nodes k are missing such that (0 < k < n).   It looks like when I import
> this type of dataset, the "missing" node numbers are included as single
> node components... the output of the igraph_clusters routine seems to
> report this.  Is there a work around or a routine to re-sequence the
> node numbers in igraph?
>
> Thanks
> Richard
>
>
> _______________________________________________
> igraph-help mailing list