igraph-help
[Top][All Lists]
Advanced

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

Re: [igraph] create a (weighted) graph in C from txt files


From: Gábor Csárdi
Subject: Re: [igraph] create a (weighted) graph in C from txt files
Date: Mon, 29 Nov 2010 21:48:49 +0100

Hi Francois,

On Mon, Nov 29, 2010 at 5:56 PM, Francois Bastardie <address@hidden> wrote:
> Dear all ,
>
>
>
> I am a R user of the igraph library which I found very useful, thank you
> very much to the authors.
>
>
>
> I would like now to switch to C/C++ because I would like to speed up my
> entire code which actually includes the igraph library only as a subroutine.
> However I am not very proficient in C/C++ and then I would like to ask you
> if you can help me for the beginning. I managed to compile igraph in a .dll
> using the microsoft visual C++ software and then I launched some examples
> embedded  with the library.
>
>
>
> Here it is: I would like to translate these following lines from R to some
> equivalent lines in C++. It is about a spatial network of nodes:
>
>
>
>   # BUILD THE IGRAPH OBJECT IN R
>
>  library(igraph)
>
> coord <- matrix(c(10, 9, 2, 4, 7,8, 45,34), ncol=2) # fake
>
> graph <- matrix(c(1,2,34,2,3,35,3,1,37, 4,1,30),ncol=3, byrow=TRUE) #fake
>
>  vertices    <- data.frame(name=as.character(unique(graph[,1])),
> x=coord[,1], y=coord[,2])  # name of the node, longitude, latitude
>
>  edges        <- data.frame(from=c(graph[,1]),
>
>                         to=c(graph[,2]),
>
>                         dist.km=graph[,3]) # name of the departure node,
> name of the arrival node, distance in km
>
>  g <- graph.data.frame(edges, directed=FALSE, vertices=vertices)
>
>
>
> I do not want to enter this by hand actually because I have more than 15000
> nodes in my actual data...
>
>
>
> My first guess is that I have to use the following C method to get back a
> graph with weighted edges (i.e. the distances between nodes)?

Well, you don't have to, there are several other file formats for
storing graphs. But this is one possibility.

> int igraph_read_graph_ncol(igraph_t *graph, FILE *instream,
>
>                           igraph_strvector_t *predefnames,
>
>                           igraph_bool_t names, igraph_bool_t weights,
> igraph_bool_t directed);
>
>
>
> In addition, I´m always struggling with the import/export of the files and
> that was one of the (many) reasons why I mainly used R for my work because
> with R we do not have to bother very much for importing and creating
> objects.
>
> Could you please give me some clues/examples to do that i.e. how to import
> the coordinates of the nodes and the distances between nodes in C/C++, and
> then create the graph I want?

You can read them the normal C/C++ way from your files, and then
create the igraph graph via igraph_create and add the coordinates as
attributes if you like, see
http://igraph.sourceforge.net/doc/html/ch09s02.html
http://igraph.sourceforge.net/doc/html/index.html

Alternatively, if you don't have many graphs to work with, you can
read them into R, and save them in some file format like GraphML or
GML, and then read them in from C.

Best,
Gabor

> Thank you very much,
>
> Best regards,
>
> Francois
>
>
>
>
>
>
>
> _______________________________________________
> igraph-help mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/igraph-help
>
>



-- 
Gabor Csardi <address@hidden>     UNIL DGM



reply via email to

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