rgraphum 0.0.1.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. data/.gitignore +26 -0
  2. data/GLOSSARIES.md +108 -0
  3. data/GREMLIN.md +1398 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +136 -0
  7. data/Rakefile +16 -0
  8. data/bin/.irbrc +41 -0
  9. data/bin/rgraphum_console +61 -0
  10. data/bin/rgraphum_runner +57 -0
  11. data/examples/ba_model/make.rb +19 -0
  12. data/examples/ba_model/make_dummy_twitter_rt_data.rb +0 -0
  13. data/examples/basic/check_modularity.rb +27 -0
  14. data/examples/basic/make_graph.rb +12 -0
  15. data/examples/parser/dot.rb +28 -0
  16. data/examples/sis_model/lifegame.rb +161 -0
  17. data/graph_struct.jpg +0 -0
  18. data/lib/rgraphum/analyzer/linear_regression.rb +31 -0
  19. data/lib/rgraphum/analyzer/meme_tracker.rb +296 -0
  20. data/lib/rgraphum/analyzer/twitter/rt_at_mark.rb +45 -0
  21. data/lib/rgraphum/analyzer.rb +8 -0
  22. data/lib/rgraphum/cluster.rb +67 -0
  23. data/lib/rgraphum/communities.rb +65 -0
  24. data/lib/rgraphum/community.rb +86 -0
  25. data/lib/rgraphum/cosine_similarity_matrix.rb +40 -0
  26. data/lib/rgraphum/edge.rb +194 -0
  27. data/lib/rgraphum/edges.rb +161 -0
  28. data/lib/rgraphum/ext/cosine_similarity_matrix.rb +79 -0
  29. data/lib/rgraphum/ext/linear_regression.rb +22 -0
  30. data/lib/rgraphum/ext/tf_idf.rb +52 -0
  31. data/lib/rgraphum/graph/gremlin.rb +193 -0
  32. data/lib/rgraphum/graph/math/clustering_coefficient.rb +53 -0
  33. data/lib/rgraphum/graph/math/community_detection.rb +141 -0
  34. data/lib/rgraphum/graph/math/degree_distribution.rb +50 -0
  35. data/lib/rgraphum/graph/math/dijkstra.rb +331 -0
  36. data/lib/rgraphum/graph/math.rb +45 -0
  37. data/lib/rgraphum/graph.rb +267 -0
  38. data/lib/rgraphum/importer.rb +97 -0
  39. data/lib/rgraphum/marshal.rb +26 -0
  40. data/lib/rgraphum/motifs.rb +8 -0
  41. data/lib/rgraphum/parsers/flare.rb +42 -0
  42. data/lib/rgraphum/parsers/gephi.rb +193 -0
  43. data/lib/rgraphum/parsers/graphviz.rb +78 -0
  44. data/lib/rgraphum/parsers/miserables.rb +54 -0
  45. data/lib/rgraphum/parsers.rb +32 -0
  46. data/lib/rgraphum/path.rb +37 -0
  47. data/lib/rgraphum/query.rb +130 -0
  48. data/lib/rgraphum/rgraphum_array.rb +159 -0
  49. data/lib/rgraphum/rgraphum_array_dividers.rb +43 -0
  50. data/lib/rgraphum/rgraphum_random.rb +5 -0
  51. data/lib/rgraphum/simulator/ba_model.rb +140 -0
  52. data/lib/rgraphum/simulator/sir_model.rb +178 -0
  53. data/lib/rgraphum/simulator/sis_model.rb +158 -0
  54. data/lib/rgraphum/simulator.rb +29 -0
  55. data/lib/rgraphum/statistic/power_law.rb +9 -0
  56. data/lib/rgraphum/t.rb +12 -0
  57. data/lib/rgraphum/tf_idf.rb +27 -0
  58. data/lib/rgraphum/version.rb +3 -0
  59. data/lib/rgraphum/vertex.rb +354 -0
  60. data/lib/rgraphum/vertices.rb +97 -0
  61. data/lib/rgraphum.rb +38 -0
  62. data/performance/add-vertices-edges.rb +20 -0
  63. data/performance/add-vertices.rb +12 -0
  64. data/performance/build-graph.rb +19 -0
  65. data/performance/delete-graph.rb +24 -0
  66. data/performance/delete-vertices.rb +25 -0
  67. data/performance/refer-graph.rb +23 -0
  68. data/rgraphum.gemspec +30 -0
  69. data/test/lib/rgraphum/analyzer/linear_regression_test.rb +20 -0
  70. data/test/lib/rgraphum/analyzer/meme_tracker_test.rb +383 -0
  71. data/test/lib/rgraphum/analyzer/twitter/rt_at_mark_test.rb +120 -0
  72. data/test/lib/rgraphum/array_test.rb +95 -0
  73. data/test/lib/rgraphum/bubble_test.rb +7 -0
  74. data/test/lib/rgraphum/communities_test.rb +53 -0
  75. data/test/lib/rgraphum/cosine_similarity_test.rb +18 -0
  76. data/test/lib/rgraphum/edge_test.rb +89 -0
  77. data/test/lib/rgraphum/edges_test.rb +178 -0
  78. data/test/lib/rgraphum/graph_builder_test.rb +64 -0
  79. data/test/lib/rgraphum/graph_dup_test.rb +199 -0
  80. data/test/lib/rgraphum/graph_plus_test.rb +80 -0
  81. data/test/lib/rgraphum/graph_test.rb +512 -0
  82. data/test/lib/rgraphum/gremlin_test.rb +145 -0
  83. data/test/lib/rgraphum/importers/idg_json_edges.json +20 -0
  84. data/test/lib/rgraphum/importers/idg_json_test.rb +207 -0
  85. data/test/lib/rgraphum/importers/idg_json_vertices.json +46 -0
  86. data/test/lib/rgraphum/math/average_distance_matrix_test.rb +142 -0
  87. data/test/lib/rgraphum/math/clustering_coefficient_test.rb +219 -0
  88. data/test/lib/rgraphum/math/community_test.rb +78 -0
  89. data/test/lib/rgraphum/math/degree_distribution_test.rb +40 -0
  90. data/test/lib/rgraphum/math/dijkstra_test.rb +146 -0
  91. data/test/lib/rgraphum/math/modularity_test.rb +154 -0
  92. data/test/lib/rgraphum/math/quick_average_distance_matrix_test.rb +84 -0
  93. data/test/lib/rgraphum/path_test.rb +44 -0
  94. data/test/lib/rgraphum/query/enumerable_test.rb +42 -0
  95. data/test/lib/rgraphum/query/where_operators_test.rb +75 -0
  96. data/test/lib/rgraphum/query/where_test.rb +59 -0
  97. data/test/lib/rgraphum/simulator/ba_model_test.rb +75 -0
  98. data/test/lib/rgraphum/simulator/sir_model_test.rb +513 -0
  99. data/test/lib/rgraphum/simulator/sis_model_test.rb +478 -0
  100. data/test/lib/rgraphum/simulator_test.rb +22 -0
  101. data/test/lib/rgraphum/tf_idf_test.rb +30 -0
  102. data/test/lib/rgraphum/vertex_test.rb +50 -0
  103. data/test/lib/rgraphum/vertices_test.rb +180 -0
  104. data/test/test_helper.rb +98 -0
  105. data/tmp/.gitkeep +0 -0
  106. metadata +254 -0
@@ -0,0 +1,207 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rgraphum'
4
+ require 'test_helper'
5
+
6
+ class RgraphumImporterIdgJsonTest < MiniTest::Test
7
+ def test_parse_vertex
8
+ vertices_json_str = <<-EOT
9
+ {
10
+ "result": [{
11
+ "@type": "d", "@rid": "#-2:0", "@version": 0,
12
+ "rid": "#6:0",
13
+ "x": 999.99999,
14
+ "y": 999.99999,
15
+ "r": 99.999999,
16
+ "rgb": 99999999,
17
+ "screen_name": "person_1",
18
+ "actor_type": 0,
19
+ "c_id": 0,
20
+ "c_is_parent": 0,
21
+ "c_r": 99.999999,
22
+ "c_rgb": 9999999,
23
+ "@fieldTypes": "x=f,y=f,r=f,c_r=f"
24
+ }
25
+ ]
26
+ }
27
+ EOT
28
+
29
+ graph = Rgraphum::Graph.parse(format: :idg_json, vertices: vertices_json_str)
30
+ assert_equal 1, graph.vertices.size
31
+ assert_equal 0, graph.edges.size
32
+
33
+ vertex = graph.vertices[0]
34
+ assert_equal "person_1", vertex.label
35
+ end
36
+
37
+ def test_parse_vertices
38
+ vertices_json_str = <<-EOT
39
+ {
40
+ "result": [{
41
+ "@type": "d", "@rid": "#-2:0", "@version": 0,
42
+ "rid": "#6:0",
43
+ "x": 999.99999,
44
+ "y": 999.99999,
45
+ "r": 99.999999,
46
+ "rgb": 99999999,
47
+ "screen_name": "person_a",
48
+ "actor_type": 0,
49
+ "c_id": 0,
50
+ "c_is_parent": 0,
51
+ "c_r": 99.999999,
52
+ "c_rgb": 9999999,
53
+ "@fieldTypes": "x=f,y=f,r=f,c_r=f"
54
+ }, {
55
+ "@type": "d", "@rid": "#-2:1", "@version": 0,
56
+ "rid": "#6:1",
57
+ "x": 999.99999,
58
+ "y": 999.99999,
59
+ "r": 99.99999,
60
+ "rgb": 99999999,
61
+ "screen_name": "person_b",
62
+ "actor_type": 0,
63
+ "c_id": 0,
64
+ "c_is_parent": 1,
65
+ "c_r": 99.999999,
66
+ "c_rgb": 9999999,
67
+ "@fieldTypes": "x=f,y=f,r=f,c_r=f"
68
+ }, {
69
+ "@type": "d", "@rid": "#-2:2", "@version": 0,
70
+ "rid": "#6:2",
71
+ "x": 999.99999,
72
+ "y": 999.99999,
73
+ "r": 99.999999,
74
+ "rgb": 99999999,
75
+ "screen_name": "person_c",
76
+ "actor_type": 0,
77
+ "c_id": 0,
78
+ "c_is_parent": 0,
79
+ "c_r": 99.999999,
80
+ "c_rgb": 9999999,
81
+ "@fieldTypes": "x=f,y=f,r=f,c_r=f"
82
+ }
83
+ ]
84
+ }
85
+ EOT
86
+
87
+ graph = Rgraphum::Graph.parse(format: :idg_json, vertices: vertices_json_str)
88
+ assert_equal 3, graph.vertices.size
89
+ assert_equal 0, graph.edges.size
90
+
91
+ assert_equal "person_a", graph.vertices[0].label
92
+ assert_equal "person_b", graph.vertices[1].label
93
+ assert_equal "person_c", graph.vertices[2].label
94
+ end
95
+
96
+ def test_parse_vertices_and_edges
97
+ vertices_json_str = <<-EOT
98
+ {
99
+ "result": [{
100
+ "@type": "d", "@rid": "#-2:0", "@version": 0,
101
+ "rid": "#6:0",
102
+ "x": 999.99999,
103
+ "y": 999.99999,
104
+ "r": 99.999999,
105
+ "rgb": 99999999,
106
+ "screen_name": "person_a",
107
+ "actor_type": 0,
108
+ "c_id": 0,
109
+ "c_is_parent": 0,
110
+ "c_r": 99.999999,
111
+ "c_rgb": 9999999,
112
+ "@fieldTypes": "x=f,y=f,r=f,c_r=f"
113
+ }, {
114
+ "@type": "d", "@rid": "#-2:1", "@version": 0,
115
+ "rid": "#6:1",
116
+ "x": 999.99999,
117
+ "y": 999.99999,
118
+ "r": 99.99999,
119
+ "rgb": 99999999,
120
+ "screen_name": "person_b",
121
+ "actor_type": 0,
122
+ "c_id": 0,
123
+ "c_is_parent": 1,
124
+ "c_r": 99.999999,
125
+ "c_rgb": 9999999,
126
+ "@fieldTypes": "x=f,y=f,r=f,c_r=f"
127
+ }, {
128
+ "@type": "d", "@rid": "#-2:2", "@version": 0,
129
+ "rid": "#6:2",
130
+ "x": 999.99999,
131
+ "y": 999.99999,
132
+ "r": 99.999999,
133
+ "rgb": 99999999,
134
+ "screen_name": "person_c",
135
+ "actor_type": 0,
136
+ "c_id": 0,
137
+ "c_is_parent": 0,
138
+ "c_r": 99.999999,
139
+ "c_rgb": 9999999,
140
+ "@fieldTypes": "x=f,y=f,r=f,c_r=f"
141
+ }
142
+ ]
143
+ }
144
+ EOT
145
+
146
+ edges_json_str = <<-EOT
147
+ {
148
+ "result": [{
149
+ "@type": "d", "@rid": "#-2:0", "@version": 0,
150
+ "in": "#6:0",
151
+ "out": "#6:1",
152
+ "statusIds": "999999999999999999",
153
+ "weight": 1.0,
154
+ "created_at": 1373554121,
155
+ "@fieldTypes": "weight=f,created_at=l"
156
+ }, {
157
+ "@type": "d", "@rid": "#-2:1", "@version": 0,
158
+ "in": "#6:2",
159
+ "out": "#6:1",
160
+ "statusIds": "999999999999999999",
161
+ "weight": 1.0,
162
+ "created_at": 1373553637,
163
+ "@fieldTypes": "weight=f,created_at=l"
164
+ }
165
+ ]
166
+ }
167
+ EOT
168
+
169
+ Rgraphum::Edge.instance_eval {
170
+ field :created_at # FIXME
171
+ }
172
+
173
+ graph = Rgraphum::Graph.parse(format: :idg_json, vertices: vertices_json_str, edges: edges_json_str)
174
+ assert_equal 3, graph.vertices.size
175
+ assert_equal 2, graph.edges.size
176
+
177
+ assert_equal "person_a", graph.vertices[0].label
178
+ assert_equal "person_b", graph.vertices[1].label
179
+ assert_equal "person_c", graph.vertices[2].label
180
+
181
+ assert_equal "person_a", graph.edges[0].source.label
182
+ assert_equal "person_b", graph.edges[0].target.label
183
+ assert_equal "2013-07-11 23:48:41 JST", graph.edges[0].created_at.strftime("%Y-%m-%d %H:%M:%S %Z")
184
+
185
+ assert_equal "person_c", graph.edges[1].source.label
186
+ assert_equal "person_b", graph.edges[1].target.label
187
+ assert_equal "2013-07-11 23:40:37 JST", graph.edges[1].created_at.strftime("%Y-%m-%d %H:%M:%S %Z")
188
+ end
189
+
190
+ def test_load_vertices_and_edges
191
+ v_path = File.join(File.dirname(__FILE__), "idg_json_vertices.json")
192
+ e_path = File.join(File.dirname(__FILE__), "idg_json_edges.json")
193
+
194
+ graph = Rgraphum::Graph.load(format: :idg_json, vertices: v_path, edges: e_path)
195
+ assert_equal 3, graph.vertices.size
196
+ assert_equal 2, graph.edges.size
197
+
198
+ assert_equal "person_a", graph.vertices[0].label
199
+ assert_equal "person_b", graph.vertices[1].label
200
+ assert_equal "person_c", graph.vertices[2].label
201
+
202
+ assert_equal "person_a", graph.edges[0].source.label
203
+ assert_equal "person_b", graph.edges[0].target.label
204
+ assert_equal "person_c", graph.edges[1].source.label
205
+ assert_equal "person_b", graph.edges[1].target.label
206
+ end
207
+ end
@@ -0,0 +1,46 @@
1
+ {
2
+ "result": [{
3
+ "@type": "d", "@rid": "#-2:0", "@version": 0,
4
+ "rid": "#6:0",
5
+ "x": 999.99999,
6
+ "y": 999.99999,
7
+ "r": 99.999999,
8
+ "rgb": 99999999,
9
+ "screen_name": "person_a",
10
+ "actor_type": 0,
11
+ "c_id": 0,
12
+ "c_is_parent": 0,
13
+ "c_r": 99.999999,
14
+ "c_rgb": 9999999,
15
+ "@fieldTypes": "x=f,y=f,r=f,c_r=f"
16
+ }, {
17
+ "@type": "d", "@rid": "#-2:1", "@version": 0,
18
+ "rid": "#6:1",
19
+ "x": 999.99999,
20
+ "y": 999.99999,
21
+ "r": 99.99999,
22
+ "rgb": 99999999,
23
+ "screen_name": "person_b",
24
+ "actor_type": 0,
25
+ "c_id": 0,
26
+ "c_is_parent": 1,
27
+ "c_r": 99.999999,
28
+ "c_rgb": 9999999,
29
+ "@fieldTypes": "x=f,y=f,r=f,c_r=f"
30
+ }, {
31
+ "@type": "d", "@rid": "#-2:2", "@version": 0,
32
+ "rid": "#6:2",
33
+ "x": 999.99999,
34
+ "y": 999.99999,
35
+ "r": 99.999999,
36
+ "rgb": 99999999,
37
+ "screen_name": "person_c",
38
+ "actor_type": 0,
39
+ "c_id": 0,
40
+ "c_is_parent": 0,
41
+ "c_r": 99.999999,
42
+ "c_rgb": 9999999,
43
+ "@fieldTypes": "x=f,y=f,r=f,c_r=f"
44
+ }
45
+ ]
46
+ }
@@ -0,0 +1,142 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rgraphum'
4
+ require 'test_helper'
5
+
6
+ class RgraphumMathAverageDistanceMatrixTest < MiniTest::Test
7
+ def setup
8
+ @graph = Rgraphum::Graph.new
9
+ # @graph.vertices = [
10
+ # { label: "A" },
11
+ # { label: "B" },
12
+ # { label: "C" },
13
+ # { label: "D" },
14
+ # { label: "G" },
15
+ # ]
16
+ @a = @graph.vertices.build(label: "A")
17
+ @b = @graph.vertices.build(label: "B")
18
+ @c = @graph.vertices.build(label: "C")
19
+ @d = @graph.vertices.build(label: "D")
20
+ @g = @graph.vertices.build(label: "G")
21
+ end
22
+
23
+ # A --(5)-- B --(1)-- G
24
+ # | \ | /
25
+ #(1) (4) (2) (4)
26
+ # | \ | /
27
+ # C --(5)-- D
28
+ #
29
+ def test_average_distance_1
30
+ @graph.edges.build(source: @a, target: @b, weight: 5) # A->B
31
+ @graph.edges.build(source: @a, target: @c, weight: 1) # A->C
32
+ @graph.edges.build(source: @a, target: @d, weight: 4) # A->D
33
+ @graph.edges.build(source: @b, target: @d, weight: 2) # B->D
34
+ @graph.edges.build(source: @c, target: @d, weight: 5) # C->D
35
+ @graph.edges.build(source: @b, target: @g, weight: 1) # B->G
36
+ @graph.edges.build(source: @d, target: @g, weight: 4) # D->G
37
+
38
+ average_distance = @graph.average_distance
39
+
40
+ n = @graph.vertices.size
41
+ minimum_distance_matrix = [
42
+ # A B C D G
43
+ [ 0, 5, 1, 4, 6 ], # A
44
+ [ 5, 0, 6, 2, 1 ], # B
45
+ [ 1, 6, 0, 5, 7 ], # C
46
+ [ 4, 2, 5, 0, 3 ], # D
47
+ [ 6, 1, 7, 3, 0 ], # G
48
+ ]
49
+ expected = Rational(minimum_distance_matrix.flatten.inject(&:+), n * (n - 1))
50
+
51
+ assert_equal expected, average_distance
52
+ end
53
+
54
+ # A --(1)-- B --(4)-- G
55
+ # | \ | /
56
+ #(2) (4) (2) (1)
57
+ # | \ | /
58
+ # C --(3)-- D
59
+ #
60
+ def test_average_distance_2
61
+ @graph.edges.build(source: @a, target: @b, weight: 1) # A->B
62
+ @graph.edges.build(source: @a, target: @c, weight: 2) # A->C
63
+ @graph.edges.build(source: @a, target: @d, weight: 4) # A->D
64
+ @graph.edges.build(source: @b, target: @d, weight: 2) # B->D
65
+ @graph.edges.build(source: @c, target: @d, weight: 3) # C->D
66
+ @graph.edges.build(source: @b, target: @g, weight: 4) # B->G
67
+ @graph.edges.build(source: @d, target: @g, weight: 1) # D->G
68
+
69
+ average_distance = @graph.average_distance
70
+
71
+ n = @graph.vertices.size
72
+ minimum_distance_matrix = [
73
+ # A B C D G
74
+ [ 0, 1, 2, 3, 4 ], # A
75
+ [ 1, 0, 3, 2, 3 ], # B
76
+ [ 2, 3, 0, 3, 4 ], # C
77
+ [ 3, 2, 3, 0, 1 ], # D
78
+ [ 4, 3, 4, 1, 0 ], # G
79
+ ]
80
+ expected = Rational(minimum_distance_matrix.flatten.inject(&:+), n * (n - 1))
81
+
82
+ assert_equal expected, average_distance
83
+ end
84
+
85
+ # A --(5)-- B --(1)-- G
86
+ # | \ | /
87
+ #(1) (4) (2) (4)
88
+ # | \ | /
89
+ # C --(5)-- D
90
+ #
91
+ def test_minimum_distance_matrix_1
92
+ @graph.edges.build(source: @a, target: @b, weight: 5) # A->B
93
+ @graph.edges.build(source: @a, target: @c, weight: 1) # A->C
94
+ @graph.edges.build(source: @a, target: @d, weight: 4) # A->D
95
+ @graph.edges.build(source: @b, target: @d, weight: 2) # B->D
96
+ @graph.edges.build(source: @c, target: @d, weight: 5) # C->D
97
+ @graph.edges.build(source: @b, target: @g, weight: 1) # B->G
98
+ @graph.edges.build(source: @d, target: @g, weight: 4) # D->G
99
+
100
+ matrix = @graph.minimum_distance_matrix
101
+
102
+ expected = [
103
+ # A B C D G
104
+ [ 0, 5, 1, 4, 6 ], # A
105
+ [ 5, 0, 6, 2, 1 ], # B
106
+ [ 1, 6, 0, 5, 7 ], # C
107
+ [ 4, 2, 5, 0, 3 ], # D
108
+ [ 6, 1, 7, 3, 0 ], # G
109
+ ]
110
+
111
+ assert_equal expected, matrix
112
+ end
113
+
114
+ # A --(1)-- B --(4)-- G
115
+ # | \ | /
116
+ #(2) (4) (2) (1)
117
+ # | \ | /
118
+ # C --(3)-- D
119
+ #
120
+ def test_minimum_distance_matrix_2
121
+ @graph.edges.build(source: @a, target: @b, weight: 1) # A->B
122
+ @graph.edges.build(source: @a, target: @c, weight: 2) # A->C
123
+ @graph.edges.build(source: @a, target: @d, weight: 4) # A->D
124
+ @graph.edges.build(source: @b, target: @d, weight: 2) # B->D
125
+ @graph.edges.build(source: @c, target: @d, weight: 3) # C->D
126
+ @graph.edges.build(source: @b, target: @g, weight: 4) # B->G
127
+ @graph.edges.build(source: @d, target: @g, weight: 1) # D->G
128
+
129
+ matrix = @graph.minimum_distance_matrix
130
+
131
+ expected = [
132
+ # A B C D G
133
+ [ 0, 1, 2, 3, 4 ], # A
134
+ [ 1, 0, 3, 2, 3 ], # B
135
+ [ 2, 3, 0, 3, 4 ], # C
136
+ [ 3, 2, 3, 0, 1 ], # D
137
+ [ 4, 3, 4, 1, 0 ], # G
138
+ ]
139
+
140
+ assert_equal expected, matrix
141
+ end
142
+ end
@@ -0,0 +1,219 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rgraphum'
4
+ require 'test_helper'
5
+
6
+ class RgraphumMathClusteringCoefficientTest < MiniTest::Test
7
+ def setup
8
+ end
9
+
10
+ def test_local_clustering_coefficient_with_complete_graph
11
+ # B---C
12
+ # |\ /|
13
+ # | A |
14
+ # \ | /
15
+ # D
16
+ graph = Rgraphum::Graph.new
17
+ a = graph.vertices.build(label: "A")
18
+ b = graph.vertices.build(label: "B")
19
+ c = graph.vertices.build(label: "C")
20
+ d = graph.vertices.build(label: "D")
21
+
22
+ graph.edges.build(source: a, target: b)
23
+ graph.edges.build(source: a, target: c)
24
+ graph.edges.build(source: a, target: d)
25
+
26
+ graph.edges.build(source: b, target: c)
27
+ graph.edges.build(source: c, target: d)
28
+ graph.edges.build(source: d, target: b)
29
+
30
+ # B - C B\ /C
31
+ # \ / | A A |
32
+ # A D/ \D
33
+ assert_equal "3/3".to_r, a.clustering_coefficient
34
+ # B - C B\ B - C
35
+ # \ / | A \ /
36
+ # A D/ D
37
+ assert_equal "3/3".to_r, b.clustering_coefficient
38
+ # B - C /C B - C
39
+ # \ / A | \ /
40
+ # A \D D
41
+ assert_equal "3/3".to_r, c.clustering_coefficient
42
+ # B\ /C B - C
43
+ # | A A | \ /
44
+ # D/ \D D
45
+ assert_equal "3/3".to_r, d.clustering_coefficient
46
+ end
47
+
48
+ def test_local_clustering_coefficient_with_non_complete_graph_1
49
+ # B---C
50
+ # \ /
51
+ # A
52
+ # |
53
+ # D
54
+ graph = Rgraphum::Graph.new
55
+ a = graph.vertices.build(label: "A")
56
+ b = graph.vertices.build(label: "B")
57
+ c = graph.vertices.build(label: "C")
58
+ d = graph.vertices.build(label: "D")
59
+
60
+ graph.edges.build(source: a, target: b)
61
+ graph.edges.build(source: a, target: c)
62
+ graph.edges.build(source: a, target: d)
63
+
64
+ graph.edges.build(source: b, target: c)
65
+
66
+ # B - C B\ /C
67
+ # \ / A A
68
+ # A D/ \D
69
+ assert_equal "1/3".to_r, a.clustering_coefficient
70
+ # B - C
71
+ # \ /
72
+ # A
73
+ assert_equal "1/1".to_r, b.clustering_coefficient
74
+ # B - C
75
+ # \ /
76
+ # A
77
+ assert_equal "1/1".to_r, c.clustering_coefficient
78
+ #
79
+ #
80
+ #
81
+ assert_equal "0".to_r, d.clustering_coefficient
82
+ end
83
+
84
+ def test_local_clustering_coefficient_with_non_complete_graph_2
85
+ # B---C
86
+ # \ /|
87
+ # A |
88
+ # |/
89
+ # D
90
+ graph = Rgraphum::Graph.new
91
+ a = graph.vertices.build(label: "A")
92
+ b = graph.vertices.build(label: "B")
93
+ c = graph.vertices.build(label: "C")
94
+ d = graph.vertices.build(label: "D")
95
+
96
+ graph.edges.build(source: a, target: b)
97
+ graph.edges.build(source: a, target: c)
98
+ graph.edges.build(source: a, target: d)
99
+
100
+ graph.edges.build(source: b, target: c)
101
+ graph.edges.build(source: c, target: d)
102
+
103
+ # B - C B\ /C
104
+ # \ / A A |
105
+ # A D/ \D
106
+ assert_equal "2/3".to_r, a.clustering_coefficient
107
+ # B - C
108
+ # \ /
109
+ # A
110
+ assert_equal "1/1".to_r, b.clustering_coefficient
111
+ # B - C /C B - C
112
+ # / A | \ /
113
+ # A \D D
114
+ assert_equal "2/3".to_r, c.clustering_coefficient
115
+ # /C
116
+ # A |
117
+ # \D
118
+ assert_equal "1/1".to_r, d.clustering_coefficient
119
+ end
120
+
121
+ def test_global_clustering_coefficient_with_complete_graph
122
+ # B---C
123
+ # |\ /|
124
+ # | A |
125
+ # \ | /
126
+ # D
127
+ graph = Rgraphum::Graph.new
128
+ a = graph.vertices.build(label: "A")
129
+ b = graph.vertices.build(label: "B")
130
+ c = graph.vertices.build(label: "C")
131
+ d = graph.vertices.build(label: "D")
132
+
133
+ graph.edges.build(source: a, target: b)
134
+ graph.edges.build(source: a, target: c)
135
+ graph.edges.build(source: a, target: d)
136
+
137
+ graph.edges.build(source: b, target: c)
138
+ graph.edges.build(source: c, target: d)
139
+ graph.edges.build(source: d, target: b)
140
+
141
+ # assert_equal "3/3".to_r, a.clustering_coefficient
142
+ # assert_equal "3/3".to_r, b.clustering_coefficient
143
+ # assert_equal "3/3".to_r, c.clustering_coefficient
144
+ # assert_equal "3/3".to_r, d.clustering_coefficient
145
+
146
+ # N: 4
147
+ # N
148
+ # 1/N * Σ Ci
149
+ # i=0
150
+ #
151
+ # 1/4 * (3/3 + 3/3 + 3/3 + 3/3) = 1
152
+ assert_equal "1".to_r, graph.clustering_coefficient
153
+ end
154
+
155
+ def test_global_clustering_coefficient_with_non_complete_graph_1
156
+ # B---C
157
+ # \ /
158
+ # A
159
+ # |
160
+ # D
161
+ graph = Rgraphum::Graph.new
162
+ a = graph.vertices.build(label: "A")
163
+ b = graph.vertices.build(label: "B")
164
+ c = graph.vertices.build(label: "C")
165
+ d = graph.vertices.build(label: "D")
166
+
167
+ graph.edges.build(source: a, target: b)
168
+ graph.edges.build(source: a, target: c)
169
+ graph.edges.build(source: a, target: d)
170
+
171
+ graph.edges.build(source: b, target: c)
172
+
173
+ # assert_equal "1/3".to_r, a.clustering_coefficient
174
+ # assert_equal "1/1".to_r, b.clustering_coefficient
175
+ # assert_equal "1/1".to_r, c.clustering_coefficient
176
+ # assert_equal "0".to_r, d.clustering_coefficient
177
+
178
+ # N: 4
179
+ # N
180
+ # 1/N * Σ Ci
181
+ # i=0
182
+ #
183
+ # 1/4 * (1/3 + 1/1 + 1/1 + 0) = 1/4 * (7/3) = 7/12
184
+ assert_equal "7/12".to_r, graph.clustering_coefficient
185
+ end
186
+
187
+ def test_global_clustering_coefficient_with_non_complete_graph_2
188
+ # B---C
189
+ # \ /|
190
+ # A |
191
+ # |/
192
+ # D
193
+ graph = Rgraphum::Graph.new
194
+ a = graph.vertices.build(label: "A")
195
+ b = graph.vertices.build(label: "B")
196
+ c = graph.vertices.build(label: "C")
197
+ d = graph.vertices.build(label: "D")
198
+
199
+ graph.edges.build(source: a, target: b)
200
+ graph.edges.build(source: a, target: c)
201
+ graph.edges.build(source: a, target: d)
202
+
203
+ graph.edges.build(source: b, target: c)
204
+ graph.edges.build(source: c, target: d)
205
+
206
+ # assert_equal "2/3".to_r, a.clustering_coefficient
207
+ # assert_equal "1/1".to_r, b.clustering_coefficient
208
+ # assert_equal "2/3".to_r, c.clustering_coefficient
209
+ # assert_equal "1/1".to_r, d.clustering_coefficient
210
+
211
+ # N: 4
212
+ # N
213
+ # 1/N * Σ Ci
214
+ # i=0
215
+ #
216
+ # 1/4 * (2/3 + 1/1 + 2/3 + 1/1) = 1/4 * (4/3 + 2/1) = 1/4 * (10/3) = 10/12 = 5/6
217
+ assert_equal "5/6".to_r, graph.clustering_coefficient
218
+ end
219
+ end
@@ -0,0 +1,78 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'test_helper'
4
+ require 'rgraphum'
5
+
6
+ class RgraphumMathCommunityTest < MiniTest::Test
7
+ def setup
8
+ # 1 - 2
9
+ # \ /
10
+ # 3
11
+ # / \
12
+ # 4 - 5
13
+
14
+ @graph = Rgraphum::Graph.new
15
+ @graph.vertices = [
16
+ { id: 1, label: "A", community_id: 1 },
17
+ { id: 2, label: "B", community_id: 1 },
18
+ { id: 3, label: "C" },
19
+ { id: 4, label: "D" },
20
+ { id: 5, label: "E" },
21
+ ]
22
+ @graph.edges = [
23
+ { id: 0, source: 1, target: 2, weight: 1 },
24
+ { id: 1, source: 1, target: 3, weight: 1 },
25
+ { id: 2, source: 2, target: 3, weight: 1 },
26
+ { id: 3, source: 3, target: 4, weight: 1 },
27
+ { id: 4, source: 3, target: 5, weight: 1 },
28
+ { id: 5, source: 4, target: 5, weight: 1 },
29
+ ]
30
+ end
31
+
32
+ def test_community
33
+ # 1 - 2
34
+ # \ /
35
+ # 3
36
+ # / \
37
+ # 4 - 5
38
+ # (1-2) communityy
39
+
40
+ @graph.id_aspect!
41
+ community = @graph.communities[0]
42
+
43
+ assert community
44
+ assert_instance_of Rgraphum::Community, community
45
+ assert_equal 1, community.id
46
+ expected = [
47
+ { id: 0, source: 1, target: 2, weight: 1 },
48
+ { id: 1, source: 1, target: 3, weight: 1 },
49
+ { id: 2, source: 2, target: 3, weight: 1 },
50
+ ]
51
+ rg_assert_equal expected, community.edges
52
+
53
+ rg_assert_equal [{ id: 0, source: 1, target: 2, weight: 1 }], community.inter_edges
54
+ expected = [
55
+ { id: 1, source: 1, target: 3, weight: 1 },
56
+ { id: 2, source: 2, target: 3, weight: 1 },
57
+ ]
58
+ rg_assert_equal expected, community.edges_from(@graph.vertices.where(id: 3).first)
59
+ assert_empty community.edges_from(@graph.vertices.where(id: 4).first)
60
+
61
+ @graph.edges.where(id: 0).first.weight = 4
62
+ @graph.edges.where(id: 1).first.weight = 1
63
+ @graph.edges.where(id: 2).first.weight = 2
64
+ @graph.edges.where(id: 3).first.weight = 3
65
+ @graph.edges.where(id: 4).first.weight = 5
66
+ @graph.edges.where(id: 5).first.weight = 6
67
+
68
+ assert_equal 11, community.degree_weight
69
+ assert_equal 4, community.sigma_in
70
+
71
+ assert_equal 11, @graph.vertices.where(id: 3).first.sigma_tot
72
+
73
+ @graph.real_aspect!
74
+ rmcd = Rgraphum::Graph::Math::CommunityDetection.new(@graph)
75
+ delta_q = rmcd.delta_q(community,@graph.vertices.where(id: 3).first)
76
+ assert_in_delta 0.00566893, delta_q, 0.00000001
77
+ end
78
+ end