added test and refactored dijkstra

This commit is contained in:
Hamlet 2010-11-10 10:31:48 +01:00
parent 5d14773ec3
commit 19a42c8650
9 changed files with 139 additions and 2 deletions

View File

@ -0,0 +1,3 @@
<component name="ProjectDictionaryState">
<dictionary name="hamlet" />
</component>

5
.idea/encodings.xml Normal file
View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>

18
.idea/igraph2.iml Normal file
View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RUBY_MODULE" version="4">
<component name="GemRequirementsHolder" version="3">
<requirement>
<requirement>
<dependency name="test-unit" version="0" bound="GREATER_OR_EQUAL" git="false" path="false" doRequire="true" />
</requirement>
<source manual="true" />
</requirement>
</component>
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="[gem] test-unit (v2.1.1, /home/hamlet/.rvm/gems/ruby-1.9.2-p0/gems/test-unit-2.1.1)" level="application" />
</component>
</module>

8
.idea/misc.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DependencyValidationManager">
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Ruby SDK 1.9.2-p0" project-jdk-type="RUBY_SDK" />
</project>

9
.idea/modules.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/igraph2.iml" filepath="$PROJECT_DIR$/.idea/igraph2.iml" />
</modules>
</component>
</project>

8
.idea/vcs.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -340,6 +340,7 @@ void Init_igraph(){
rb_define_method(cIGraph_shortestpaths, "girth", cIGraph_girth, 0); /* in cIGraph_shortest_paths.c */
rb_define_method(cIGraph_shortestpaths, "dijkstra_shortest_paths", cIGraph_dijkstra_shortest_paths, 3); /* in cIGraph_dijkstra.c */
rb_define_method(cIGraph_shortestpaths, "get_dijkstra_shortest_paths", cIGraph_get_dijkstra_shortest_paths, 4); /* in cIGraph_dijkstra.c */
/* Functions for querying the neighborhood of vertices */
cIGraph_neighborhoodm = rb_define_module_under(cIGraph, "Neighborhood");
@ -554,7 +555,7 @@ void Init_igraph(){
rb_define_method(cIGraph_community, "community_leading_eigenvector_step", cIGraph_community_leading_eigenvector_step, 2); /* in cIGraph_community.c */ rb_define_method(cIGraph_community, "community_walktrap", cIGraph_community_walktrap, 2); /* in cIGraph_community.c */
rb_define_method(cIGraph_community, "community_edge_betweenness", cIGraph_community_edge_betweenness, 1); /* in cIGraph_community.c */
rb_define_method(cIGraph_community, "community_eb_get_merges", cIGraph_community_eb_get_merges, 1); /* in cIGraph_community.c */
rb_define_method(cIGraph_community, "community_fastgreedy", cIGraph_community_fastgreedy, 0); /* in cIGraph_community.c */
rb_define_method(cIGraph_community, "community_fastgreedy", cIGraph_community_fastgreedy, 1); /* in cIGraph_community.c */
rb_define_const(cIGraph, "VERSION", rb_str_new2("0.9.1"));

View File

@ -82,7 +82,7 @@ VALUE cIGraph_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE weights, VAL
*/
VALUE cIGraph_get_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE to, VALUE weights, VALUE mode){
igraph_t *graph;
igraph_t *graph;
igraph_integer_t from_vid;
igraph_vs_t to_vids;
@ -151,6 +151,79 @@ VALUE cIGraph_get_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE to, VALU
return matrix;
/*
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);
igraph_vector_ptr_t res;
igraph_vector_t *path_v;
int i;
int j;
VALUE path;
VALUE matrix = rb_ary_new();
int n_paths = 0;
Data_Get_Struct(self, igraph_t, graph);
n_paths = RARRAY_LEN(to);
//vector to hold the results of the calculations
igraph_vector_ptr_init(&res,0);
for(i=0;i<n_paths;i++)
{
path_v = malloc(sizeof(igraph_vector_t));
igraph_vector_init(path_v,0);
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);
//create vertex selector from the vecotr of ids
igraph_vs_vector(&to_vids,&to_vidv);
//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_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();
rb_ary_push(matrix,path);
path_v = VECTOR(res)[i];
for(j=0; j<igraph_vector_size(VECTOR(res)[i]); j++){
rb_ary_push(path,cIGraph_get_vertex_object(self,VECTOR(*path_v)[j]));
}
}
for(i=0;i<n_paths;i++){
igraph_vector_destroy(VECTOR(res)[i]);
free(VECTOR(res)[i]);
}
igraph_vector_destroy(&to_vidv);
igraph_vector_ptr_destroy(&res);
igraph_vs_destroy(&to_vids);
igraph_vector_destroy(&wghts);
return matrix;
*/
}
int igraph_dijkstra_shortest_paths(const igraph_t *graph,

View File

@ -8,4 +8,16 @@ class TestGraph < Test::Unit::TestCase
g = IGraph.new([1,2,3,4],false)
assert_equal [[0,1.5,Infinity,Infinity]], g.dijkstra_shortest_paths([1],[1.5,2.5],IGraph::OUT)
end
def test_get_dijkstra_shortest_paths
graph = IGraph.new(['A','B','C','D','B','D','A','C'],false)
weights2 = [6,5,6,5]
weights = [1,5,1,5]
m = graph.get_dijkstra_shortest_paths('A',['D'],weights,IGraph::ALL)
assert_equal ['A','B','D'], m[0]
m = graph.get_dijkstra_shortest_paths('A',['D'],weights2,IGraph::ALL)
assert_equal ['A','C','D'], m[0]
#assert_equal [], m[1]
end
end