hierarchical_graph 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a6c1ce470102ca17f36f40fe0f861c221c18237b46fa07d066baed98895501c8
4
- data.tar.gz: '08cf95bf13e8217262973a222cb213de9559777170253460acfb6b9856316f1d'
3
+ metadata.gz: b06e425f1dbfef479fc43a6c77574d7f39eea429e6b7c2f772cab22fa9a6c0b8
4
+ data.tar.gz: 7c7a7b5c3ac89569b47f6c2acf4a40a95da52ea0d397c668fb369c15be0f0c60
5
5
  SHA512:
6
- metadata.gz: 8e14241d62a3ffac4fa02fcf4c0ed37f7456658510a0e26ef17c63155dd5f9368fbe21dedd03c52182d86189523ef98df0cd4e1018c770f971e2b2e2077c1258
7
- data.tar.gz: 97ed130cabcd32d51266c9fe5bcf7c606c11577746c87a5ba2e169b756b4abd7f9002def04e9bc0218ca8e7df838a0c9e843c634e90fde72ce6cbb771cebbf8b
6
+ metadata.gz: 2d4dafcb3372f016ce1e6b79ec3b6a5346d410d54fa156d2c66bae8f7c33963c51d64ced1591e7e1755ad699da7e62c36556b907c5cdf831b2b3d60df965932d
7
+ data.tar.gz: 6a5d195ef7d30f9885734a5d156ef742a457e911d7d5a7e8edf7584d01cee192b44533af5043ad4e153255316975bf2b028a241716c8a682bc2a7397f319cae5
@@ -102,6 +102,26 @@ class HierarchicalGraph
102
102
  end.uniq(&:id)
103
103
  end
104
104
 
105
+ def subgraph_of(ids)
106
+ ids.each { |id| validate_present! id }
107
+
108
+ HierarchicalGraph.new.tap do |subgraph|
109
+ ids.each do |id|
110
+ subgraph.add_node id, nodes[id].data
111
+ end
112
+
113
+ subgraph.each do |node|
114
+ children_of(node.id).each do |child|
115
+ subgraph.add_relation parent_id: node.id, child_id: child.id unless subgraph[child.id].nil?
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ def descendants_subgraph_from(id)
122
+ subgraph_of [id] + descendants_of(id).map(&:id)
123
+ end
124
+
105
125
  def to_s
106
126
  "<#{self.class.name} nodes:[#{map(&:to_s).join(', ')}]>"
107
127
  end
@@ -17,6 +17,10 @@ class HierarchicalGraph
17
17
  data[key] = value
18
18
  end
19
19
 
20
+ def root?
21
+ parents.empty?
22
+ end
23
+
20
24
  def parents
21
25
  graph.parents_of id
22
26
  end
@@ -33,8 +37,8 @@ class HierarchicalGraph
33
37
  graph.descendants_of id
34
38
  end
35
39
 
36
- def root?
37
- parents.empty?
40
+ def descendants_subgraph
41
+ graph.descendants_subgraph_from id
38
42
  end
39
43
 
40
44
  def to_s
@@ -1,3 +1,3 @@
1
1
  class HierarchicalGraph
2
- VERSION = '1.0.2'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -253,6 +253,68 @@ describe HierarchicalGraph do
253
253
  graph.to_s.must_equal '<HierarchicalGraph nodes:[<HierarchicalGraph::Node 1 parents:[] children:[2]>, <HierarchicalGraph::Node 2 parents:[1] children:[]>]>'
254
254
  end
255
255
 
256
+ it 'Subgraph' do
257
+ graph = HierarchicalGraph.new
258
+ graph.add_node 1
259
+ graph.add_node 2
260
+ graph.add_node 3
261
+ graph.add_node 4
262
+ graph.add_node 5
263
+ graph.add_node 6
264
+ graph.add_relation parent_id: 1, child_id: 2
265
+ graph.add_relation parent_id: 1, child_id: 3
266
+ graph.add_relation parent_id: 2, child_id: 4
267
+ graph.add_relation parent_id: 3, child_id: 4
268
+ graph.add_relation parent_id: 3, child_id: 5
269
+ graph.add_relation parent_id: 4, child_id: 5
270
+ graph.add_relation parent_id: 5, child_id: 6
271
+
272
+ subgraph = graph.subgraph_of [3, 4, 5]
273
+
274
+ subgraph.map(&:id).must_equal [3, 4, 5]
275
+
276
+ subgraph[3].parents.must_be_empty
277
+ subgraph[3].children.map(&:id).must_equal [4, 5]
278
+
279
+ subgraph[4].parents.map(&:id).must_equal [3]
280
+ subgraph[4].children.map(&:id).must_equal [5]
281
+
282
+ subgraph[5].parents.map(&:id).must_equal [3, 4]
283
+ subgraph[5].children.must_be_empty
284
+ end
285
+
286
+ it 'Descendants Subgraph' do
287
+ graph = HierarchicalGraph.new
288
+ graph.add_node 1
289
+ graph.add_node 2
290
+ graph.add_node 3
291
+ graph.add_node 4
292
+ graph.add_node 5
293
+ graph.add_node 6
294
+ graph.add_relation parent_id: 1, child_id: 2
295
+ graph.add_relation parent_id: 1, child_id: 3
296
+ graph.add_relation parent_id: 3, child_id: 4
297
+ graph.add_relation parent_id: 3, child_id: 5
298
+ graph.add_relation parent_id: 4, child_id: 5
299
+ graph.add_relation parent_id: 5, child_id: 6
300
+
301
+ subgraph = graph.descendants_subgraph_from 3
302
+
303
+ subgraph.map(&:id).must_equal [3, 4, 5, 6]
304
+
305
+ subgraph[3].parents.must_be_empty
306
+ subgraph[3].children.map(&:id).must_equal [4, 5]
307
+
308
+ subgraph[4].parents.map(&:id).must_equal [3]
309
+ subgraph[4].children.map(&:id).must_equal [5]
310
+
311
+ subgraph[5].parents.map(&:id).must_equal [3, 4]
312
+ subgraph[5].children.map(&:id).must_equal [6]
313
+
314
+ subgraph[6].parents.map(&:id).must_equal [5]
315
+ subgraph[6].children.must_be_empty
316
+ end
317
+
256
318
  describe 'Node' do
257
319
 
258
320
  let :graph do
@@ -312,6 +374,16 @@ describe HierarchicalGraph do
312
374
  graph[2][:code].must_equal 'node_2'
313
375
  end
314
376
 
377
+ it 'Descendants Subgraph' do
378
+ node = graph[3]
379
+ subgraph = node.descendants_subgraph
380
+
381
+ subgraph.roots.map(&:id).must_equal [3]
382
+ subgraph[3].children.map(&:id).must_equal [4]
383
+ subgraph[4].children.map(&:id).must_equal [5]
384
+ subgraph[5].children.must_be_empty
385
+ end
386
+
315
387
  end
316
388
 
317
389
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hierarchical_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-29 00:00:00.000000000 Z
11
+ date: 2021-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake