hash_graph 0.1.0 → 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.
- data/README.rdoc +5 -0
- data/VERSION +1 -1
- data/lib/hash_graph.rb +17 -0
- data/spec/hash_graph_spec.rb +34 -0
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -10,6 +10,11 @@ b in the Hash g, so all nodes can be identified by g.keys
|
|
10
10
|
|
11
11
|
HashGraph::DirectedGraph also includes TSort for Tarjan's algorithm
|
12
12
|
|
13
|
+
== Install
|
14
|
+
|
15
|
+
gem source --add http://gemcutter.org
|
16
|
+
gem install hash_graph
|
17
|
+
|
13
18
|
== Note on Patches/Pull Requests
|
14
19
|
|
15
20
|
* Fork the project.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/hash_graph.rb
CHANGED
@@ -35,6 +35,23 @@ module HashGraph
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
+
# convert to an UndirectedGraph, with a proc which takes weights
|
39
|
+
# (a,b,weight_a_b,weight_b_a) as parameters [weights may be nil, indicating no edge],
|
40
|
+
# and returns weight_a_b or nil for the undirected graph edge [ which will be the same
|
41
|
+
# as weight_b_a ]
|
42
|
+
def to_undirected_graph(&proc)
|
43
|
+
inject(UndirectedGraph.new()) do |ug,(a,targets)|
|
44
|
+
targets.each do |(b,weight_a_b)|
|
45
|
+
if !ug[a][b]
|
46
|
+
weight_b_a = self[b][a]
|
47
|
+
undw = proc.call(a,b,weight_a_b, weight_b_a)
|
48
|
+
ug[a][b]=undw if undw
|
49
|
+
end
|
50
|
+
end
|
51
|
+
ug
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
38
55
|
alias tsort_each_node each_key
|
39
56
|
|
40
57
|
def tsort_each_child(node,&block)
|
data/spec/hash_graph_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'set'
|
2
3
|
|
3
4
|
describe HashGraph do
|
4
5
|
module HashGraph
|
@@ -27,6 +28,39 @@ describe HashGraph do
|
|
27
28
|
h[:bar][:baz] = 5
|
28
29
|
h.should == {:foo=>{:bar=>1}, :bar=>{:baz=>5}, :baz=>{}}
|
29
30
|
end
|
31
|
+
|
32
|
+
describe "to_undirected_graph" do
|
33
|
+
it "should convert a directed graph with bi-directional edges to an undirected graph" do
|
34
|
+
h = DirectedGraph.new
|
35
|
+
h[:foo][:bar] = 1
|
36
|
+
h[:bar][:foo] = 3
|
37
|
+
h.to_undirected_graph do |a,b,wab,wba|
|
38
|
+
[a,b].to_set.should == [:foo,:bar].to_set
|
39
|
+
[wab,wba].to_set.should == [1,3].to_set
|
40
|
+
wab+wba
|
41
|
+
end.should=={:foo=>{:bar=>4}, :bar=>{:foo=>4}}
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should convert a directed graph with asymmetric edges to an undirected graph" do
|
45
|
+
h = DirectedGraph.new
|
46
|
+
h[:foo][:bar] = 1
|
47
|
+
h.to_undirected_graph do |a,b,wab,wba|
|
48
|
+
[a,b].should == [:foo,:bar]
|
49
|
+
[wab,wba].should == [1,nil]
|
50
|
+
wab
|
51
|
+
end.should=={:foo=>{:bar=>1}, :bar=>{:foo=>1}}
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should not create undirected edges if the proc returns nil" do
|
55
|
+
h = DirectedGraph.new
|
56
|
+
h[:foo][:bar] = 1
|
57
|
+
h.to_undirected_graph do |a,b,wab,wba|
|
58
|
+
[a,b].should == [:foo,:bar]
|
59
|
+
[wab,wba].should == [1,nil]
|
60
|
+
nil
|
61
|
+
end.should=={}
|
62
|
+
end
|
63
|
+
end
|
30
64
|
end
|
31
65
|
|
32
66
|
describe UndirectedGraph do
|