diff --git a/ext/cIGraph_community.c b/ext/cIGraph_community.c index fdb6702..a32a466 100644 --- a/ext/cIGraph_community.c +++ b/ext/cIGraph_community.c @@ -43,7 +43,7 @@ VALUE cIGraph_modularity(VALUE self, VALUE groups){ } /* call-seq: - * graph.community_to_membership(merge,steps) -> Array + * graph.community_to_membership(merge,steps,nodes) -> Array * * Create membership vector from community structure dendrogram This function * creates a membership vector from a community structure dendrogram. @@ -458,10 +458,10 @@ igraph_arpack_options_init(&arpack_opt); * */ + VALUE cIGraph_community_walktrap(VALUE self, VALUE weights, VALUE steps){ igraph_t *graph; - igraph_vector_t weights_vec; igraph_vector_t modularity; igraph_matrix_t *merges = malloc(sizeof(igraph_matrix_t)); @@ -473,8 +473,8 @@ VALUE cIGraph_community_walktrap(VALUE self, VALUE weights, VALUE steps){ Data_Get_Struct(self, igraph_t, graph); igraph_matrix_init(merges,0,0); - igraph_vector_init(&weights_vec,0); igraph_vector_init(&modularity,0); + igraph_vector_init(&weights_vec,RARRAY_LEN(weights)); for(i=0;i Array + * graph.community_fastgreedy(weights) -> Array * * Finding community structure by greedy optimization of modularity. * This function implements the fast greedy modularity optimization algorithm diff --git a/test/tc_community.rb b/test/tc_community.rb index 2bf5615..523f955 100644 --- a/test/tc_community.rb +++ b/test/tc_community.rb @@ -11,7 +11,7 @@ class TestGraph < Test::Unit::TestCase groups,mod,temp = g.community_spinglass([],25,false,1,0.01,0.99,IGraph::SPINCOMM_UPDATE_SIMPLE,1.0) assert_in_delta 0.25, mod, 0.15 assert_in_delta 0.200, temp, 0.100 - assert_equal [['A','B','C','D','E','F']], groups + assert_equal [['A','B','C'],['D','E','F']], groups.sort commun,coh,adh = g.community_spinglass_single([],'A',25,IGraph::SPINCOMM_UPDATE_SIMPLE,1.0) assert_in_delta 1.25, coh, 0.001 assert_in_delta(-2.5, adh, 0.100) @@ -63,10 +63,94 @@ class TestGraph < Test::Unit::TestCase def test_fastgreedy g = IGraph.new(['A','B','B','C','A','C','C','D','D','E','E','F','D','F'],false) - merges,mod = g.community_fastgreedy + merges,mod = g.community_fastgreedy([]) groups = g.community_to_membership(merges,4,6) assert_equal [['A','B','C'],['D','E','F']], groups.sort assert_in_delta 0.19, mod[3], 0.1 end + + + def test_extend_weights + graph = IGraph.new(['A1','B1','A1','C1','B1','C1','C1','D1',\ + 'A2','B2','A2','C2','A2','D2','B2','C2','B2','D2','C2','D2','D2','E2',\ + 'A3','B3','A3','C3','A3','D3','A3','E3','B3','C3','B3','D3','B3','E3','C3','D3','C3','E3','D3','E3',\ + 'A1','A2','B2','B3'],false) + + weights = [5,7,7,5,\ + 6,6,5,7,5,6,8,\ + 8,4,3,5,4,6,7,8,3,7,\ + 3,3] + merges,mod = graph.community_walktrap(weights,50) + max_idx = mod.index(mod.max) + res = graph.community_to_membership(merges,max_idx,graph.vcount()).sort + assert_equal [["A1", "B1", "C1", "D1"], ["A2", "B2", "C2", "D2", "E2"], ["A3", "B3", "C3", "D3", "E3"]], res + + merges,mod = graph.community_walktrap([],50) + max_idx = mod.index(mod.max) + res = graph.community_to_membership(merges,max_idx,graph.vcount()).sort + assert_equal [["A1", "B1", "C1", "D1"], ["A2", "B2", "C2", "D2", "E2"], ["A3", "B3", "C3", "D3", "E3"]], res + + end + + def test_extend_fastgreedy + graph = IGraph.new(['A1','B1','A1','C1','B1','C1','C1','D1',\ + 'A2','B2','A2','C2','A2','D2','B2','C2','B2','D2','C2','D2','D2','E2',\ + 'A3','B3','A3','C3','A3','D3','A3','E3','B3','C3','B3','D3','B3','E3','C3','D3','C3','E3','D3','E3',\ + 'A1','A2','B2','B3'],false) + + weights = [5,7,7,5,\ + 6,6,5,7,5,6,8,\ + 8,4,3,5,4,6,7,8,3,7,\ + 3,3] + + merges,mod = graph.community_fastgreedy(weights) + max_idx = mod.index(mod.max) + res = graph.community_to_membership(merges,max_idx,graph.vcount()).sort + assert_equal [["A1", "B1", "C1", "D1"], ["A2", "B2", "C2", "D2", "E2"], ["A3", "B3", "C3", "D3", "E3"]], res + + merges,mod = graph.community_fastgreedy([]) + max_idx = mod.index(mod.max) + res = graph.community_to_membership(merges,max_idx,graph.vcount()).sort + assert_equal [["A1", "B1", "C1", "D1"], ["A2", "B2", "C2", "D2", "E2"], ["A3", "B3", "C3", "D3", "E3"]], res + + + end + + def test_extend_betweenness + graph = IGraph.new(['A1','B1','A1','C1','B1','C1','C1','D1',\ + 'A2','B2','A2','C2','A2','D2','B2','C2','B2','D2','C2','D2','D2','E2',\ + 'A3','B3','A3','C3','A3','D3','A3','E3','B3','C3','B3','D3','B3','E3','C3','D3','C3','E3','D3','E3',\ + 'A1','A2','B2','B3'],false) + + weights = [5,7,7,5,\ + 6,6,5,7,5,6,8,\ + 8,4,3,5,4,6,7,8,3,7,\ + 3,3] + + + merges,result,edge_betw,bridges = graph.community_edge_betweenness(false) + + #max_idx = mod.index(mod.max) + res = graph.community_to_membership(merges,11,graph.vcount()).sort + #puts graph.modularity(edge_betw).to_s + assert_equal [["A1", "B1", "C1", "D1"], ["A2", "B2", "C2", "D2", "E2"], ["A3", "B3", "C3", "D3", "E3"]], res + + end + + def test_extend_eigenvector + graph = IGraph.new(['A1','B1','A1','C1','B1','C1','C1','D1',\ + 'A2','B2','A2','C2','A2','D2','B2','C2','B2','D2','C2','D2','D2','E2',\ + 'A3','B3','A3','C3','A3','D3','A3','E3','B3','C3','B3','D3','B3','E3','C3','D3','C3','E3','D3','E3',\ + 'A1','A2','B2','B3'],false) + + weights = [5,7,7,5,\ + 6,6,5,7,5,6,8,\ + 8,4,3,5,4,6,7,8,3,7,\ + 3,3] + + res = graph.community_leading_eigenvector(10)[0].sort + assert_equal [["A1", "B1", "C1", "D1"], ["A2", "B2", "C2", "D2", "E2"], ["A3", "B3", "C3", "D3", "E3"]], res + end + end