Browse Source

added weight support for dijstra get shortes and for fast greedy algorithm

pull/4/head
Hamlet 15 years ago
parent
commit
5d14773ec3
4 changed files with 30 additions and 14 deletions
  1. +7
    -7
      Rakefile.rb
  2. +2
    -1
      ext/cIGraph.h
  3. +9
    -3
      ext/cIGraph_community.c
  4. +12
    -3
      ext/cIGraph_dijkstra.c

+ 7
- 7
Rakefile.rb View File

@@ -11,20 +11,20 @@ end
#rescue RuntimeError
#end

hoe = Hoe.new("igraph",IGraph::VERSION) do |p|
hoe = Hoe.spec("igraph") do |p|
p.author = "Alex Gutteridge"
p.email = "ag357@cam.ac.uk"
p.url = "http://igraph.rubyforge.org/"
p.description = p.paragraphs_of("README.txt",1..3)[0]
p.summary = p.paragraphs_of("README.txt",1)[0]
p.changes = p.paragraphs_of("History.txt",0..1).join("\n\n")
p.clean_globs = ["ext/*.o","ext/*.so","ext/Makefile","ext/mkmf.log","**/*~","email.txt","manual.{aux,log,out,toc,pdf}"]
p.rdoc_pattern = /(^ext\/.*\.c$|^README|^History|^License)/
#p.rdoc_pattern = /(^ext\/.*\.c$|^README|^History|^License)/
p.spec_extras = {
:extensions => ['ext/extconf.rb'],
:require_paths => ['test'],


+ 2
- 1
ext/cIGraph.h View File

@@ -117,6 +117,7 @@ VALUE cIGraph_diameter (VALUE self, VALUE directed, VALUE unconn);
VALUE cIGraph_girth (VALUE self);

VALUE cIGraph_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE weights, VALUE mode);
VALUE cIGraph_get_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE to, VALUE weights, VALUE mode);
int igraph_dijkstra_shortest_paths(const igraph_t *graph,
igraph_matrix_t *res,
const igraph_vs_t from,
@@ -313,7 +314,7 @@ VALUE cIGraph_community_edge_betweenness (VALUE self,
VALUE directed);
VALUE cIGraph_community_eb_get_merges (VALUE self,
VALUE edges);
VALUE cIGraph_community_fastgreedy (VALUE self);
VALUE cIGraph_community_fastgreedy (VALUE self,VALUE weights);

//Attributes
int cIGraph_attribute_init(igraph_t *graph,


+ 9
- 3
ext/cIGraph_community.c View File

@@ -625,10 +625,10 @@ VALUE cIGraph_community_eb_get_merges(VALUE self, VALUE edges){
*
*/

VALUE cIGraph_community_fastgreedy(VALUE self){
VALUE cIGraph_community_fastgreedy(VALUE self, VALUE weights){

igraph_t *graph;
igraph_vector_t weights_vec;
igraph_vector_t modularity;
igraph_matrix_t *merges = malloc(sizeof(igraph_matrix_t));

@@ -640,8 +640,13 @@ VALUE cIGraph_community_fastgreedy(VALUE self){

igraph_matrix_init(merges,0,0);
igraph_vector_init(&modularity,0);
igraph_vector_init(&weights_vec,RARRAY_LEN(weights));

for(i=0;i<RARRAY_LEN(weights);i++){
VECTOR(weights_vec)[i] = NUM2DBL(RARRAY_PTR(weights)[i]);
}

igraph_community_fastgreedy(graph,NULL,
igraph_community_fastgreedy(graph,igraph_vector_size(&weights_vec) > 0 ? &weights_vec : NULL,
merges,&modularity);

modularity_a = rb_ary_new();
@@ -655,6 +660,7 @@ VALUE cIGraph_community_fastgreedy(VALUE self){
modularity_a);

igraph_vector_destroy(&modularity);
igraph_vector_destroy(&weights_vec);

return res;



+ 12
- 3
ext/cIGraph_dijkstra.c View File

@@ -64,7 +64,7 @@ VALUE cIGraph_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE weights, VAL
igraph_vector_destroy(&vidv);
igraph_matrix_destroy(&res);
igraph_vs_destroy(&vids);
igraph_vector_destroy(&wghts);
return matrix;

}
@@ -80,13 +80,14 @@ VALUE cIGraph_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE weights, VAL
* calculated. IGraph::ALL the directed graph is considered as an undirected
* one for the computation.
*/
VALUE cIGraph_get_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE to, VALUE mode){
VALUE cIGraph_get_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE to, VALUE weights, VALUE mode){

igraph_t *graph;

igraph_integer_t from_vid;
igraph_vs_t to_vids;
igraph_vector_t to_vidv;
igraph_vector_t wghts;

igraph_neimode_t pmode = NUM2INT(mode);

@@ -111,6 +112,12 @@ VALUE cIGraph_get_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE to, VALU
igraph_vector_ptr_push_back(&res,path_v);
}

igraph_vector_init(&wghts,RARRAY_LEN(weights));

for(i=0;i<RARRAY_LEN(weights);i++){
VECTOR(wghts)[i] = NUM2DBL(RARRAY_PTR(weights)[i]);
}

//Convert an array of vertices to a vector of vertex ids
igraph_vector_init_int(&to_vidv,0);
cIGraph_vertex_arr_to_id_vec(self,to,&to_vidv);
@@ -120,7 +127,8 @@ VALUE cIGraph_get_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE to, VALU
//The id of the vertex from where we are counting
from_vid = cIGraph_get_vertex_id(self, from);

igraph_get_shortest_paths(graph,&res,from_vid,to_vids,pmode);
//igraph_get_shortest_paths(graph,&res,from_vid,to_vids,pmode);
igraph_get_shortest_paths_dijkstra(graph,&res,from_vid,to_vids,igraph_vector_size(&wghts) > 0 ? &wghts : NULL,pmode);

for(i=0; i<n_paths; i++){
path = rb_ary_new();
@@ -139,6 +147,7 @@ VALUE cIGraph_get_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE to, VALU
igraph_vector_destroy(&to_vidv);
igraph_vector_ptr_destroy(&res);
igraph_vs_destroy(&to_vids);
igraph_vector_destroy(&wghts);

return matrix;



Loading…
Cancel
Save