mgraph 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ec4492a401e3b784164bc9c65e63469f1a9b3c4c
4
+ data.tar.gz: 9c724aa9dad2b3591853dbb6218377d64f1194dc
5
+ SHA512:
6
+ metadata.gz: fb012711dd8631c904aaa9c80d3609073417579ae2d474aa52636f7e32b2ffdf1e7df69cb7ba892f350329a50c8289278487ed58c72facc76de9dab81949a668
7
+ data.tar.gz: 69a3e584931b7fa9f9cc27b4d48e6de10c8d642034dc5017d72b36a8be716f1fbe2ba0bfbfef354ea6cd2702ebef32c74cf7c5af07219352d3edf069595a0b39
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - jruby-19mode
6
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mgraph.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Andrew Marshall <http://johnandrewmarshall.com/>
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # MGraph
2
+
3
+ [![Build Status](https://secure.travis-ci.org/amarshall/mgraph.png?branch=master)](https://travis-ci.org/amarshall/mgraph)
4
+ [![Code Climate rating](https://codeclimate.com/github/amarshall/mgraph.png)](https://codeclimate.com/github/amarshall/mgraph)
5
+ [![Gem Version](https://badge.fury.io/rb/mgraph.png)](https://rubygems.org/gems/mgraph)
6
+
7
+ A modern graph data structure library. Still very much a work-in-progress and in-flux.
8
+
9
+ ## Installation
10
+
11
+ Install as usual: `gem install mgraph` or add `gem 'mgraph'` to your Gemfile
12
+
13
+ ## Usage
14
+
15
+ TODO
16
+
17
+ ## Contributing
18
+
19
+ Contributions are welcome. Please be sure that your pull requests are atomic so they can be considered and accepted separately.
20
+
21
+ 1. Fork it
22
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
23
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
24
+ 4. Push to the branch (`git push origin my-new-feature`)
25
+ 5. Create new Pull Request
26
+
27
+ ## Credits & License
28
+
29
+ Copyright © 2013 J. Andrew Marshall. License is available in the LICENSE file.
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Run specs'
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.ruby_opts = '-w' unless ENV['TRAVIS']
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,5 @@
1
+ require 'mgraph/version'
2
+
3
+ module MGraph
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,28 @@
1
+ module MGraph
2
+ class Builder
3
+ def initialize neighbor_retriever
4
+ @neighbor_retriever = neighbor_retriever
5
+ freeze
6
+ end
7
+
8
+ def build root
9
+ graph = Graph.new
10
+ traverse_and_build graph, root
11
+ end
12
+
13
+ private
14
+
15
+ def traverse_and_build graph, vertex
16
+ neighbors = @neighbor_retriever.(vertex)
17
+
18
+ neighbors.reject do |neighbor|
19
+ graph.has_edge? vertex, neighbor
20
+ end.each do |neighbor|
21
+ graph.add_edge vertex, neighbor
22
+ traverse_and_build graph, neighbor
23
+ end
24
+
25
+ graph
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,21 @@
1
+ require 'set'
2
+
3
+ module MGraph
4
+ class Edge
5
+ attr_reader :vertices
6
+
7
+ def initialize a, b
8
+ @vertices = [a, b].to_set.freeze
9
+ freeze
10
+ end
11
+
12
+ def == other
13
+ @vertices == other.vertices
14
+ end
15
+ alias_method :eql?, :==
16
+
17
+ def hash
18
+ self.class.hash ^ @vertices.hash
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,57 @@
1
+ require 'mgraph/edge'
2
+ require 'mgraph/vertex_edge_table'
3
+
4
+ require 'set'
5
+
6
+ module MGraph
7
+ class Graph
8
+ def initialize graph_store = VertexEdgeTable.new
9
+ @graph_store = graph_store
10
+ end
11
+
12
+ def add_edge v1, v2
13
+ edge = Edge.new v1, v2
14
+ @graph_store.add_edge edge
15
+ edge
16
+ end
17
+
18
+ def breadth_first_traverse starting_vertex
19
+ return enum_for(:breadth_first_traverse, starting_vertex) unless block_given?
20
+
21
+ queue = [starting_vertex, *neighbors(starting_vertex)]
22
+ visited = []
23
+ while vertex = queue.shift
24
+ yield vertex
25
+ visited << vertex
26
+ queue += neighbors(vertex).to_a - visited
27
+ queue = queue.uniq
28
+ end
29
+ end
30
+
31
+ def each_vertex
32
+ return vertices.each unless block_given?
33
+ vertices.each { |vertex| yield vertex }
34
+ end
35
+
36
+ def edges
37
+ @graph_store.edges
38
+ end
39
+
40
+ def has_edge? v1, v2
41
+ edge = Edge.new v1, v2
42
+ edges.include? edge
43
+ end
44
+
45
+ def has_vertex? vertex
46
+ vertices.include? vertex
47
+ end
48
+
49
+ def neighbors vertex
50
+ @graph_store.neighbors vertex
51
+ end
52
+
53
+ def vertices
54
+ @graph_store.vertices
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module MGraph
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,36 @@
1
+ module MGraph
2
+ class VertexEdgeTable
3
+ def initialize
4
+ @vertex_edges = {}
5
+ end
6
+
7
+ def << edge
8
+ add_edge(edge)
9
+ self
10
+ end
11
+
12
+ def add_edge edge
13
+ edge.vertices.each do |vertex|
14
+ (@vertex_edges[vertex] ||= Set.new) << edge
15
+ end
16
+ end
17
+
18
+ def edges
19
+ @vertex_edges.each_value.reduce(Set.new, :+).freeze
20
+ end
21
+
22
+ def incident_edges vertex
23
+ @vertex_edges.fetch(vertex, Set.new)
24
+ end
25
+
26
+ def neighbors vertex
27
+ adjacencies = incident_edges(vertex).map(&:vertices).reduce(:+) || Set.new
28
+ adjacencies - [vertex]
29
+ end
30
+
31
+ def vertices
32
+ @vertex_edges.keys.to_set
33
+ end
34
+ end
35
+ private_constant :VertexEdgeTable
36
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mgraph/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mgraph'
8
+ spec.version = MGraph::VERSION
9
+ spec.authors = ['Andrew Marshall']
10
+ spec.email = ['andrew@johnandrewmarshall.com']
11
+ spec.description = %q{A modern graph data structure library.}
12
+ spec.summary = %q{A modern graph data structure library.}
13
+ spec.homepage = 'http://johnandrewmarshall.com/projects/mgraph'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.required_ruby_version = '>= 1.9.3'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec'
26
+ end
@@ -0,0 +1,46 @@
1
+ require 'mgraph/builder'
2
+
3
+ describe MGraph::Builder do
4
+ it "builds a graph given a starting object and a way to get its neighbors" do
5
+ neighbor_get = ->(vertex) { vertex.neighbors }
6
+ builder = MGraph::Builder.new(neighbor_get)
7
+ l2_1_neighbors = [double(neighbors: [])]
8
+ l2_2_neighbors = [double(neighbors: [])]
9
+ root_neighbors = [
10
+ double(neighbors: l2_1_neighbors),
11
+ double(neighbors: l2_2_neighbors),
12
+ ]
13
+ root = double neighbors: root_neighbors
14
+
15
+ graph = builder.build root
16
+
17
+ expect(graph.vertices).to eq [
18
+ root,
19
+ *root_neighbors,
20
+ *l2_1_neighbors,
21
+ *l2_2_neighbors,
22
+ ].to_set
23
+ end
24
+
25
+ it "can handle circular graphs" do
26
+ neighbor_get = ->(vertex) { vertex.neighbors }
27
+ builder = MGraph::Builder.new(neighbor_get)
28
+ root = double
29
+ l2_1_neighbors = [double(neighbors: [root])]
30
+ l2_2_neighbors = [double(neighbors: [])]
31
+ root_neighbors = [
32
+ double(neighbors: l2_1_neighbors),
33
+ double(neighbors: l2_2_neighbors),
34
+ ]
35
+ root.stub(:neighbors).and_return root_neighbors
36
+
37
+ graph = builder.build root
38
+
39
+ expect(graph.vertices).to eq [
40
+ root,
41
+ *root_neighbors,
42
+ *l2_1_neighbors,
43
+ *l2_2_neighbors,
44
+ ].to_set
45
+ end
46
+ end
@@ -0,0 +1,63 @@
1
+ require 'mgraph/edge'
2
+
3
+ describe MGraph::Edge do
4
+ it "contains an undirected pair of objects" do
5
+ v1, v2 = double, double
6
+ edge = MGraph::Edge.new v1, v2
7
+ expect(edge.vertices).to eq [v1, v2].to_set
8
+ end
9
+
10
+ it "does not allow modification of its vertices" do
11
+ v1, v2 = double, double
12
+ edge = MGraph::Edge.new v1, v2
13
+ expect { edge.vertices << double }.to raise_error(/frozen/i)
14
+ end
15
+
16
+ describe "equality" do
17
+ it "is `equal` to itself" do
18
+ edge = MGraph::Edge.new double, double
19
+
20
+ expect(edge == edge).to eq true
21
+ expect(edge.eql?(edge)).to eq true
22
+ expect(edge.equal?(edge)).to eq true
23
+ end
24
+
25
+ it "is equal to another Edge with the same vertices" do
26
+ v1, v2 = double, double
27
+ a = MGraph::Edge.new v1, v2
28
+ b = MGraph::Edge.new v2, v1
29
+
30
+ expect(a == b).to eq true
31
+ expect(a.eql?(b)).to eq true
32
+ expect(a.equal?(b)).to eq false
33
+ end
34
+
35
+ it "is not equal to another Edge with different vertices" do
36
+ v1, v2, v3 = double, double, double
37
+ a = MGraph::Edge.new v1, v2
38
+ b = MGraph::Edge.new v2, v3
39
+
40
+ expect(a == b).to eq false
41
+ expect(a.eql?(b)).to eq false
42
+ expect(a.equal?(b)).to eq false
43
+ end
44
+ end
45
+
46
+ describe "#hash" do
47
+ it "is the same for Edges with the same vertices" do
48
+ v1, v2 = double, double
49
+ a = MGraph::Edge.new v1, v2
50
+ b = MGraph::Edge.new v2, v1
51
+
52
+ expect(a.hash).to eq b.hash
53
+ end
54
+
55
+ it "is different for Edges with different vertices" do
56
+ v1, v2, v3 = double, double, double
57
+ a = MGraph::Edge.new v1, v2
58
+ b = MGraph::Edge.new v2, v3
59
+
60
+ expect(a.hash).to_not eq b.hash
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,157 @@
1
+ require 'mgraph/graph'
2
+
3
+ describe MGraph::Graph do
4
+ describe "#add_edge" do
5
+ it "accepts edges as two vertices, returning the Edge" do
6
+ v1, v2, = double, double
7
+ edge = double vertices: [v1, v2]
8
+ graph = MGraph::Graph.new
9
+ MGraph::Edge.stub(:new).with(v1, v2).and_return edge
10
+
11
+ returned = graph.add_edge v1, v2
12
+
13
+ expect(returned).to eq edge
14
+ end
15
+ end
16
+
17
+ describe "#breadth_first_traverse" do
18
+ it "yields the vertices in breadth-first order, starting at the given vertex" do
19
+ graph = MGraph::Graph.new
20
+ graph.add_edge 1, 2
21
+ graph.add_edge 1, 3
22
+ graph.add_edge 2, 4
23
+ graph.add_edge 2, 5
24
+ graph.add_edge 3, 5
25
+ graph.add_edge 4, 5
26
+
27
+ yielded = []
28
+ graph.breadth_first_traverse(1) { |vertex| yielded << vertex }
29
+
30
+ expect(yielded).to eq [1, 2, 3, 4, 5]
31
+ end
32
+
33
+ it "returns an Enumerator when no block is given" do
34
+ graph = MGraph::Graph.new
35
+ enumerator = graph.breadth_first_traverse(1)
36
+ graph.add_edge 1, 2
37
+ graph.add_edge 1, 3
38
+ graph.add_edge 2, 4
39
+
40
+ graph.breadth_first_traverse(1)
41
+
42
+ expect(enumerator).to be_a Enumerator
43
+ expect(enumerator.to_a).to eq [1, 2, 3, 4]
44
+ end
45
+ end
46
+
47
+ describe "#each_vertex" do
48
+ it "yields each vertex to the given block" do
49
+ vertices = [double, double, double, double]
50
+ graph = MGraph::Graph.new
51
+ vertices.each_cons(2) { |v1, v2| graph.add_edge v1, v2 }
52
+
53
+ yielded = []
54
+ graph.each_vertex { |v| yielded << v }
55
+
56
+ expect(yielded).to eq vertices
57
+ end
58
+
59
+ it "returns an Enumerator when no block is given" do
60
+ vertices = [double, double, double, double]
61
+ graph = MGraph::Graph.new
62
+ vertices.each_cons(2) { |v1, v2| graph.add_edge v1, v2 }
63
+
64
+ enumerator = graph.each_vertex
65
+
66
+ expect(enumerator).to be_a Enumerator
67
+ expect(enumerator.to_a).to eq vertices
68
+ end
69
+ end
70
+
71
+ describe "#edges" do
72
+ it "returns all the edges" do
73
+ v1, v2, v3, v4 = double, double
74
+ edge_1, edge_2 = double(vertices: [v1, v2]), double(vertices: [v3, v4])
75
+ graph = MGraph::Graph.new
76
+ MGraph::Edge.stub(:new).with(v1, v2).and_return edge_1
77
+ MGraph::Edge.stub(:new).with(v3, v4).and_return edge_2
78
+
79
+ graph.add_edge v1, v2
80
+ graph.add_edge v3, v4
81
+
82
+ expect(graph.edges).to eq [edge_1, edge_2].to_set
83
+ end
84
+
85
+ it "does not allow modification of the edges" do
86
+ graph = MGraph::Graph.new
87
+ expect{ graph.edges << double }.to raise_error(/frozen/i)
88
+ end
89
+ end
90
+
91
+ describe "#has_edge?" do
92
+ it "returns true if there is an edge between the given vertices" do
93
+ v1, v2, = double, double
94
+ graph = MGraph::Graph.new
95
+ graph.add_edge v1, v2
96
+
97
+ expect(graph.has_edge?(v1, v2)).to eq true
98
+ expect(graph.has_edge?(v2, v1)).to eq true
99
+ end
100
+
101
+ it "returns false if there is not an edge between the given vertices" do
102
+ v1, v2, = double, double
103
+ graph = MGraph::Graph.new
104
+ graph.add_edge v1, v2
105
+
106
+ expect(graph.has_edge?(v1, double)).to eq false
107
+ expect(graph.has_edge?(double, v2)).to eq false
108
+ end
109
+ end
110
+
111
+ describe "#has_vertex?" do
112
+ it "returns true if the vertex has been added" do
113
+ v1, v2, = double, double
114
+ graph = MGraph::Graph.new
115
+ graph.add_edge v1, v2
116
+
117
+ expect(graph.has_vertex?(v1)).to eq true
118
+ expect(graph.has_vertex?(v2)).to eq true
119
+ end
120
+
121
+ it "returns false if the vertex has not been added" do
122
+ v1, v2, = double, double
123
+ graph = MGraph::Graph.new
124
+ graph.add_edge v1, v2
125
+
126
+ expect(graph.has_vertex?(double)).to eq false
127
+ end
128
+ end
129
+
130
+ describe "#neighbors" do
131
+ it "returns an empty set when the vertex doesn't exist" do
132
+ graph = MGraph::Graph.new
133
+ expect(graph.neighbors(double)).to eq Set.new
134
+ end
135
+
136
+ it "returns the neighbors of a given vertex" do
137
+ neighbors = [double, double, double]
138
+ not_neighbors = [double, double, double]
139
+ vertex = [double]
140
+ graph = MGraph::Graph.new
141
+ neighbors.each { |neighbor| graph.add_edge vertex, neighbor }
142
+ neighbors.zip(not_neighbors) { |v1, v2| graph.add_edge v1, v2 }
143
+
144
+ expect(graph.neighbors(vertex)).to eq neighbors.to_set
145
+ end
146
+ end
147
+
148
+ describe "#vertices" do
149
+ it "returns a set with all the added vertices" do
150
+ vertices = [double, double, double, double]
151
+ graph = MGraph::Graph.new
152
+ vertices.each_cons(2) { |v1, v2| graph.add_edge v1, v2 }
153
+
154
+ expect(graph.vertices).to eq vertices.to_set
155
+ end
156
+ end
157
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mgraph
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Marshall
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A modern graph data structure library.
56
+ email:
57
+ - andrew@johnandrewmarshall.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/mgraph.rb
69
+ - lib/mgraph/builder.rb
70
+ - lib/mgraph/edge.rb
71
+ - lib/mgraph/graph.rb
72
+ - lib/mgraph/version.rb
73
+ - lib/mgraph/vertex_edge_table.rb
74
+ - mgraph.gemspec
75
+ - spec/mgraph/builder_spec.rb
76
+ - spec/mgraph/edge_spec.rb
77
+ - spec/mgraph/graph_spec.rb
78
+ homepage: http://johnandrewmarshall.com/projects/mgraph
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: 1.9.3
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.0.5
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A modern graph data structure library.
102
+ test_files:
103
+ - spec/mgraph/builder_spec.rb
104
+ - spec/mgraph/edge_spec.rb
105
+ - spec/mgraph/graph_spec.rb
106
+ has_rdoc: