From 5d14773ec385efd43111de779579c0d540dc9d94 Mon Sep 17 00:00:00 2001 From: Hamlet Date: Wed, 10 Nov 2010 05:21:49 +0100 Subject: [PATCH] added weight support for dijstra get shortes and for fast greedy algorithm --- Rakefile.rb | 14 +++++++------- ext/cIGraph.h | 3 ++- ext/cIGraph_community.c | 12 +++++++++--- ext/cIGraph_dijkstra.c | 15 ++++++++++++--- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/Rakefile.rb b/Rakefile.rb index bd631b9..6ff562d 100644 --- a/Rakefile.rb +++ b/Rakefile.rb @@ -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'], diff --git a/ext/cIGraph.h b/ext/cIGraph.h index 093fa47..493c2a6 100644 --- a/ext/cIGraph.h +++ b/ext/cIGraph.h @@ -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, diff --git a/ext/cIGraph_community.c b/ext/cIGraph_community.c index 4ef183b..fdb6702 100644 --- a/ext/cIGraph_community.c +++ b/ext/cIGraph_community.c @@ -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 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; diff --git a/ext/cIGraph_dijkstra.c b/ext/cIGraph_dijkstra.c index 90f7d99..484272d 100644 --- a/ext/cIGraph_dijkstra.c +++ b/ext/cIGraph_dijkstra.c @@ -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 0 ? &wghts : NULL,pmode); for(i=0; i