library(igraph)
data <-
"AS3 AS6 AS10 AS1
AS56 AS23 AS36 AS1
AS123 AS568 AS2 AS45 AS36 AS1
AS79 AS1"

paths <- readLines(textConnection(data))
edges <- lapply(strsplit(paths, " "), function(x) {
  if (length(x)==2) {
    x
  } else {
    c(x[1], rep(x[2:(length(x)-1)], each=2), x[length(x)])
  }
})

G <- graph.edgelist(matrix(unlist(edges), ncol=2, byrow=TRUE))
str(G)
# IGRAPH DN-- 12 12 --
# + attr: name (v/c)
# + edges (vertex names):
#  [1] AS3  ->AS6   AS6  ->AS10  AS10 ->AS1   AS56 ->AS23  AS23 ->AS36
#  [6] AS36 ->AS1   AS123->AS568 AS568->AS2   AS2  ->AS45  AS45 ->AS36
# [11] AS36 ->AS1   AS79 ->AS1

G.


On Tue, Mar 11, 2014 at 10:34 AM, Song Li <[email protected]> wrote:

>  I installed igraph in R, and i am also a beginner in R, could you give
> me a detail example for my question?
>
> Thank you very much
>
> Sky li
>
> Hi,
>
>  in R? Python? C?
>
>  Anyway, first create a long vector that contains the edges of your
> graph, and then use the regular igraph constructors, e.g. graph() in R.
>
>  Gabor
>
>
> On Tue, Mar 11, 2014 at 10:25 AM, Song Li <[email protected]> wrote:
>
>> Hi everyone,
>>
>> I'm a beginner in igraph. I have one simple question:
>>
>> there is a file route.txt as below:
>>
>> AS3 AS6 AS10 AS1
>> AS56 AS23 AS36 AS1
>> AS123 AS568 AS2 AS45 AS36 AS1
>> AS79 AS1
>>
>> AS* represents a network node, and each line represents a route,for
>> example: AS3->AS6->AS10->AS1 (line 1)
>>
>> how to create a tree graph from this file?
>>
>> Thanks!
>>
>>
>> Sky li
>>
>> _______________________________________________
>> igraph-help mailing list
>> [email protected]
>> https://lists.nongnu.org/mailman/listinfo/igraph-help
>>
>
>
>
_______________________________________________
igraph-help mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/igraph-help

Reply via email to