rbgraph 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -0
- data/lib/rbgraph/graphs/directed.rb +1 -3
- data/lib/rbgraph/node.rb +2 -4
- data/lib/rbgraph/traverser/bfs_traverser.rb +4 -3
- 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: bf0651e43311d3e309fc16dc6a295cbaa6a22d01
|
4
|
+
data.tar.gz: 74de94cc9f37c30a21e0dd71932362502e67fb92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
data/lib/rbgraph/node.rb
CHANGED
@@ -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
|