hash_graph 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
data/lib/hash_graph.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'tsort'
2
+ require 'set'
2
3
 
3
4
  module HashGraph
4
5
  # a simple directed graph representation for TSort,
@@ -86,5 +87,28 @@ module HashGraph
86
87
  UndirectedGraph::new_lower_hash(h,a_vertex)
87
88
  end
88
89
  end
90
+
91
+ # returns graph components
92
+ def components()
93
+ seen = Set.new
94
+ comps = []
95
+ keys.each do |vertex|
96
+ if !seen.include?(vertex)
97
+ seen.add(vertex)
98
+ comps << explore_component(seen, Set.new([vertex]), vertex).to_a
99
+ end
100
+ end
101
+ comps
102
+ end
103
+
104
+ def explore_component(seen, component, vertex)
105
+ self[vertex].keys.each do |conn_v|
106
+ explore_component(seen.add(conn_v),
107
+ component.add(conn_v),
108
+ conn_v) if !seen.include?(conn_v)
109
+ end
110
+ component
111
+ end
112
+
89
113
  end
90
114
  end
@@ -81,6 +81,15 @@ describe HashGraph do
81
81
  h[:foo][:bar]=1
82
82
  h.should == {:foo=>{:bar=>1}, :bar=>{:foo=>1}}
83
83
  end
84
+
85
+ it "should extract components from an UndirectedGraph" do
86
+ h=UndirectedGraph.new
87
+ h[:foo][:bar]=1
88
+ h[:foo][:baz]=1
89
+ h[:boo][:woo]=1
90
+ h.components.map(&:to_set).to_set.should == [[:foo, :bar, :baz].to_set,
91
+ [:boo, :woo].to_set].to_set
92
+ end
84
93
  end
85
94
 
86
95
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mccraig mccraig of the clan mccraig
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-19 00:00:00 +00:00
12
+ date: 2010-02-23 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency