rgraph 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rgraph/graph.rb +26 -7
- data/lib/rgraph/version.rb +1 -1
- data/spec/rgraph/graph_spec.rb +15 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8b930efb8ed11c31dacaee01435737d719d91dd
|
4
|
+
data.tar.gz: 86ba6f3a3c0985a29da1c0819c4fd99af9803c5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d073c066a225bc4bb874739bffcec43715410d8ef8b1cddececc47807650c8edf12bcf515216e466a51a8c6e1ceb08ace176c48f5f34d1c988807edb8abe7ad7
|
7
|
+
data.tar.gz: 0d3490d9524aca751f01cc92ddeb7f9bc95e5117d46d9644197221ecb5c33432025b0debf83d470f734816f37a0daf5844c58b58b06da1fc15e3d37274fecced
|
data/lib/rgraph/graph.rb
CHANGED
@@ -100,20 +100,26 @@ class Graph
|
|
100
100
|
|
101
101
|
def shortest_paths(args = {})
|
102
102
|
multiples = args[:multiples] || false
|
103
|
+
giant = args[:giant] || false
|
104
|
+
|
103
105
|
@shortest_paths = []
|
104
106
|
|
105
107
|
distances
|
108
|
+
giant_component
|
109
|
+
|
110
|
+
nodes = giant ? @giant_component : @nodes
|
106
111
|
|
107
|
-
0.upto(
|
108
|
-
(i + 1).upto(
|
109
|
-
@shortest_paths += shortest_path(i, j, multiples)
|
112
|
+
0.upto(nodes.size - 1).each do |i|
|
113
|
+
(i + 1).upto(nodes.size - 1).each do |j|
|
114
|
+
@shortest_paths += shortest_path(i, j, multiples, giant)
|
110
115
|
end
|
111
116
|
end
|
112
117
|
|
113
118
|
@shortest_paths
|
114
119
|
end
|
115
120
|
|
116
|
-
def shortest_path(i, j, multiples = false)
|
121
|
+
def shortest_path(i, j, multiples = false, giant = false)
|
122
|
+
nodes = giant ? @giant_component : @nodes
|
117
123
|
|
118
124
|
return [] if @distance[i][j] == @infinity
|
119
125
|
|
@@ -141,8 +147,9 @@ class Graph
|
|
141
147
|
out
|
142
148
|
end
|
143
149
|
|
144
|
-
def between(i)
|
145
|
-
|
150
|
+
def between(i, args = {})
|
151
|
+
giant = args[:giant] || false
|
152
|
+
shortest_paths(multiples: true, giant: giant)
|
146
153
|
|
147
154
|
n = @shortest_paths.select{|c| c[1..-2].include?(i)}.size.to_f
|
148
155
|
m = @shortest_paths.size.to_f
|
@@ -151,11 +158,12 @@ class Graph
|
|
151
158
|
|
152
159
|
def betweenness(args = {})
|
153
160
|
cumulative = args[:cumulative] || false
|
161
|
+
giant = args[:giant] || false
|
154
162
|
|
155
163
|
#If we ask cumulative, normalized must be true
|
156
164
|
normalized = args[:normalized] || cumulative || false
|
157
165
|
|
158
|
-
bts = 0.upto(@nodes.size - 1).map { |i| between(i) }
|
166
|
+
bts = 0.upto(@nodes.size - 1).map { |i| between(i, args) }
|
159
167
|
|
160
168
|
if normalized
|
161
169
|
max = bts.max
|
@@ -227,6 +235,17 @@ class Graph
|
|
227
235
|
@components
|
228
236
|
end
|
229
237
|
|
238
|
+
def giant_component
|
239
|
+
components unless @components
|
240
|
+
|
241
|
+
@giant_component = []
|
242
|
+
@components.each do |c|
|
243
|
+
@giant_component = c if c.size > @giant_component.size
|
244
|
+
end
|
245
|
+
|
246
|
+
@giant_component
|
247
|
+
end
|
248
|
+
|
230
249
|
private
|
231
250
|
|
232
251
|
def step_page_rank(d = 0.85, e = 0.01)
|
data/lib/rgraph/version.rb
CHANGED
data/spec/rgraph/graph_spec.rb
CHANGED
@@ -158,6 +158,17 @@ describe Graph do
|
|
158
158
|
expect(g.shortest_paths.count).to eq(6)
|
159
159
|
expect(g.shortest_paths(multiples: true).count).to eq(8)
|
160
160
|
end
|
161
|
+
it "calculates the betweenness of the giant component" do
|
162
|
+
g = Graph.new_from_string("source,target\n1,2\n2,3\n3,4\n5,6")
|
163
|
+
expect(g.between(0, giant: true)).to eq(0 / 6.0)
|
164
|
+
expect(g.between(1, giant: true)).to eq(2 / 6.0)
|
165
|
+
expect(g.between(2, giant: true)).to eq(2 / 6.0)
|
166
|
+
expect(g.between(3, giant: true)).to eq(0 / 6.0)
|
167
|
+
expect(g.between(4, giant: true)).to eq(0.0)
|
168
|
+
expect(g.between(5, giant: true)).to eq(0.0)
|
169
|
+
expect(g.betweenness(giant: true)).to eq([0, 1 / 3.0, 1 / 3.0, 0, 0, 0])
|
170
|
+
end
|
171
|
+
|
161
172
|
it "calculates the betweenness of a single node" do
|
162
173
|
expect(subject.between(0)).to eq(0 / 18.0)
|
163
174
|
expect(subject.between(1)).to eq(2 / 18.0)
|
@@ -199,6 +210,10 @@ describe Graph do
|
|
199
210
|
g = Graph.new_from_string("source,target\n1,2\n3,4")
|
200
211
|
expect(g.components).to eq([g.nodes[0..1], g.nodes[2..3]])
|
201
212
|
end
|
213
|
+
it "sets the giant component" do
|
214
|
+
g = Graph.new_from_string("source,target\n1,2\n2,3\n4,5")
|
215
|
+
expect(g.giant_component.size).to eq(3)
|
216
|
+
end
|
202
217
|
it "calculates clustering of a single node" do
|
203
218
|
nodes = subject.nodes.sort{|a,b| a.id <=> b.id}
|
204
219
|
|