igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[igraph] the R "igraph" library - converting graphs from graphNEL to igr


From: Coghlan, Avril
Subject: [igraph] the R "igraph" library - converting graphs from graphNEL to igraph format
Date: Sat, 6 Feb 2010 18:28:19 -0000

Dear Gabor,

Thank you for your very helpful reply.

That is great that there are already functions to convert between graphNEL and 
igraph format graphs.
I'm just a beginner at R, so am sure the functions that the igraph developers 
have written are much better than I could write.

However, I think I might have found one small problem with the 
igraph.from.graphNEL function that is at:
http://bazaar.launchpad.net/~igraph/igraph/0.6-main/annotate/head:/interfaces/R/igraph/R/conversion.R

I find that in order to make it work for directed graphs, I need to change the 
line:
    g <- graph.adjlist(al, mode=mode, duplicate=TRUE)
to be:
    g <- graph.adjlist(al, directed=TRUE, duplicate=TRUE)
I think this is because the graph.adjlist() function takes the argument 
"directed" rather than argument "mode".
Is this correct?

For example, if I type:
> library("graph")
> library("igraph")
> V <- LETTERS[1:4] # Make a graphNEL graph
> edL <- vector("list", length=4)
> names(edL) <- V
> for(i in 1:4)
      edL[[i]] <- list(edges=5-i, weights=runif(1))
> gR <- new("graphNEL", nodes=V, edgeL=edL)
> igraph.from.graphNEL(gR)
I get the error message:
   Error in graph.adjlist(al, mode = mode, duplicate = TRUE) : 
       unused argument(s) (mode = mode)

This error message goes away if I change the line in function 
igraph.to.graphNEL() as described above.
I'm not sure if this is a bug, but thought I'd tell you just in case.

thanks for your help, I appreciate it a lot.
Avril

Avril Coghlan
University College Cork, Ireland

-----Original Message-----
From: address@hidden [mailto:address@hidden On Behalf Of Gábor Csárdi
Sent: 19 January 2010 19:46
To: Coghlan, Avril
Subject: Re: the R "igraph" library

Dear Avril,

thanks for your efforts! Actually we happen to have such functions in
the development version of igraph. You can see the development version
here:
https://code.launchpad.net/~igraph/igraph/0.6-main
or you can get a nightly build from it here:
http://code.google.com/p/igraph/downloads/list

You can see the conversion functions here:
http://bazaar.launchpad.net/~igraph/igraph/0.6-main/annotate/head:/interfaces/R/igraph/R/conversion.R
They are called igraph.from.graphNEL and igraph.to.graphNEL

If you think your version is better in some respect, then please tell us!

Best Regards,
Gabor

On Tue, Jan 19, 2010 at 4:02 PM, Coghlan, Avril <address@hidden> wrote:
> Dear Dr Csardi,
>
>
>
> I have been using the R "igraph" library and finding it very useful.
>
>
>
> I have written a function for converting a graph that is in the graphNEL
> object format used by the R "graph" library, into the graph object format
> used by the igraph library (see below). As input it takes a graph in the
> graphNEL format used by the "graph" library, and as output it returns a
> graph in the format used by the "igraph" library.
>
>
>
> This function is useful to me, as I like to use functions in both the graph
> and igraph libraries.
>
>
>
> I am wondering would you consider including this function in the "igraph"
> library, as it might also be useful to others?
>
>
>
> Regards,
>
> Avril Coghlan
>
> University College Cork, Ireland
>
>
>
> # Function to convert a graphNEL graph object to a graph object for the
> igraph library
>
> makeigraphgraph <- function(mygraph)
>
> {
>
>    # Find the number of nodes in the input graph
>
>    nodes <- nodes(mygraph)
>
>    numnodes <- length(nodes)
>
>
>
>    # Record the vertex number for each vertex name
>
>    myvector <- vector()
>
>    for (i in 1:numnodes)
>
>    {
>
>       node <- nodes[i] # "node" is the vertex name, i is the vertex number
>
>       myvector[`node`] <- i  # Add named element to myvector
>
>    }
>
>
>
>    # Create a graph in the "igraph" library format, with numnodes nodes:
>
>    newgraph <- graph.empty(n=numnodes,directed=FALSE)
>
>    # First record which edges we have seen already in the "mymatrix" matrix,
>
>    # so that we don't add any edge twice:
>
>    mymatrix <- matrix(nrow=numnodes,ncol=numnodes)
>
>    for (i in 1:numnodes)
>
>    {
>
>       for (j in 1:numnodes)
>
>       {
>
>          mymatrix[i,j] = 0
>
>          mymatrix[j,i] = 0
>
>       }
>
>    }
>
>    # Now add edges to the graph "newgraph":
>
>    for (i in 1:numnodes)
>
>    {
>
>       node <- nodes[i] # "node" is the vertex name, i is the vertex number
>
>       # Find the nodes that this node is joined to:
>
>       neighbours <- adj(mygraph, node)
>
>       neighbours <- neighbours[[1]] # Get the list of neighbours
>
>       numneighbours <- length(neighbours)
>
>       if (numneighbours >= 1) # If this node "node" has some edges to other
> nodes
>
>       {
>
>          for (j in 1:numneighbours)
>
>          {
>
>             neighbour <- neighbours[j]
>
>             # Get the vertex number
>
>             neighbourindex <- myvector[neighbour]
>
>             # Add a node in the new graph "newgraph" between vertices (i-1)
> and (neighbourindex-1)
>
>             # In graph "newgraph", the vertices are counted from 0 upwards.
>
>             indexi <- i
>
>             indexj <- neighbourindex
>
>             # If we have not seen this edge already:
>
>             if (mymatrix[indexi,indexj] == 0 && mymatrix[indexj,indexi] ==
> 0)
>
>             {
>
>                mymatrix[indexi,indexj] <- 1
>
>                mymatrix[indexj,indexi] <- 1
>
>                # Add edges to the graph "newgraph"
>
>                newgraph <- add.edges(newgraph, c(i-1, neighbourindex-1))
>
>             }
>
>          }
>
>       }
>
>    }
>
>    # Set the names of the vertices in graph "newgraph":
>
>    newgraph <- set.vertex.attribute(newgraph, "name", value=nodes)
>
>
>
>    return(newgraph)
>
> }
>
>



-- 
Gabor Csardi <address@hidden>     UNIL DGM




reply via email to

[Prev in Thread] Current Thread [Next in Thread]