rbgraph 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 31b24e4c2c99bcbb6f05cc1b4eb7204e33f8f1e0
4
- data.tar.gz: ef21129413405b2af54baed48820688e44c0b692
3
+ metadata.gz: bf0651e43311d3e309fc16dc6a295cbaa6a22d01
4
+ data.tar.gz: 74de94cc9f37c30a21e0dd71932362502e67fb92
5
5
  SHA512:
6
- metadata.gz: fe770bb5fee84e7ad1f0a6aad1da05fcac31c5549356a0abc368bc2f658ccbd64722c1beccada0473a6dc4800cc829ff799c96cc96a41adf1f10947e901fcc94
7
- data.tar.gz: 0a32f8f6c07ed88bbc936f53b10f1f9f5b06434c27522186d474263fd157bf33a450ec4843e4a70356ee3a203cad52f01b9ec15ff2a0c79b90199032f6adc0e4
6
+ metadata.gz: c10c70096241736953e7dc983f897b6e72749280a79f02830b999b783ba4c46dd6b0b16b46c35ad89ba46b4cbea905bb1735300381861689188b268725a09cef
7
+ data.tar.gz: 4d7543bae183a0f6b17bf9e06bc86746376c8e80cdd7fa884ff36e413718257835844e1a44230e09b5739b326bd7b471eeb469352ca2252b9c25b0833cffcb2a
data/README.md CHANGED
@@ -93,5 +93,15 @@ graph.edges["1==2"].to_json
93
93
  # => {"id":"1==2","directed":false,"weight":14}
94
94
  ```
95
95
 
96
+ Version 0.2.0+
97
+
98
+ Can now find connected components in a directed graph, without respecting the direction
99
+ ```ruby
100
+ graph = Rbgraph::DirectedGraph.new()
101
+ ... # add nodes and edges
102
+ t = Rbgraph::Traverser::BfsTraverser.new(graph)
103
+ c = t.connected_components(respect_direction: false)
104
+ ```
105
+
96
106
  ### Disclaimer
97
107
  This project is written on a need to use basis for inclusion to other projects I'm working on for now, so completion is not an immediate goal.
@@ -8,9 +8,7 @@ module Rbgraph
8
8
 
9
9
  def connect_nodes(node1, node2, edge)
10
10
  node1.connect_to(node2, edge)
11
- if node2.edges[edge.id].nil?
12
- node2.edges[edge.id] ||= edge
13
- end
11
+ node2.edges[edge.id] ||= edge
14
12
  end
15
13
 
16
14
  end
data/lib/rbgraph/node.rb CHANGED
@@ -35,10 +35,8 @@ module Rbgraph
35
35
  end
36
36
 
37
37
  def connect_to(node, edge)
38
- if neighbors[node.id].nil? && edges[edge.id].nil?
39
- neighbors[node.id] ||= node
40
- edges[edge.id] ||= edge
41
- end
38
+ neighbors[node.id] ||= node
39
+ edges[edge.id] ||= edge
42
40
  self
43
41
  end
44
42
 
@@ -15,19 +15,19 @@ module Rbgraph
15
15
  self.unvisited_nodes = Set.new [*graph.nodes.values]
16
16
  end
17
17
 
18
- def connected_components
18
+ def connected_components(options = {})
19
19
  self.visited_nodes = Set.new
20
20
  self.queue = Queue.new
21
21
 
22
22
  while !unvisited_nodes.empty? do
23
23
  root = unvisited_nodes.to_a.first
24
24
  unvisited_nodes.delete(unvisited_nodes.to_a.first)
25
- self.connected_subgraphs << bfs_from_root(root)
25
+ self.connected_subgraphs << bfs_from_root(root, options)
26
26
  end
27
27
  connected_subgraphs
28
28
  end
29
29
 
30
- def bfs_from_root(root)
30
+ def bfs_from_root(root, options = {})
31
31
  subgraph = graph.class.new
32
32
  visited_nodes.add(root)
33
33
  queue.enq(root)
@@ -37,6 +37,7 @@ module Rbgraph
37
37
  yield(t) if block_given? # do sth on current node
38
38
  subgraph.nodes[t.id] ||= t
39
39
  t.edges.each do |eid, edge|
40
+ next if graph.directed? && !edge.out_for?(t) unless options[:respect_direction] == false
40
41
  neighbor = edge.other_node(t)
41
42
  subgraph.edges[eid] ||= edge
42
43
  subgraph.nodes[neighbor.id] ||= neighbor
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbgraph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Lamprianidis