weighted_graph 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 41020baf8d67aa652af02d9f7a5068c297a3cb8a
4
+ data.tar.gz: f8e571e67392eae819f7275ff7c4ad3ffa421e52
5
+ SHA512:
6
+ metadata.gz: 991a975216c7a5395bd4af8d4a26edb4ce7f7bb839d0cf71762975a4f97d3530a3e4d34332e195bb7b8599b953ca6b22e989ca69d098f63f8a99c087f93085f7
7
+ data.tar.gz: 98de78724c26e51f49f7dbf1b9747508667f0150ddc814f241cd23f303d19a99265bc011626870bb6f3cfb84f59eed37661015a3dc7b216cff06bbb59b8360d7
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop ADDED
@@ -0,0 +1,2 @@
1
+ --rails
2
+ --display-cop-names
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ Style/BlockLength:
2
+ Exclude:
3
+ - 'Rakefile'
4
+ - '**/*.rake'
5
+ - 'spec/**/*.rb'
6
+
7
+ Metrics/ModuleLength:
8
+ Exclude:
9
+ - 'Rakefile'
10
+ - '**/*.rake'
11
+ - 'spec/**/*.rb'
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ cache: bundler
6
+
7
+ before_install: gem install bundler -v 1.14.6
8
+ before_script: bundle install
9
+ script: bundle exec rake spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in weighted_graph.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Mark Sayson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # weighted_graph
2
+
3
+ weighted_graph is a simple Ruby library for working on graphs with weighted edges.
4
+
5
+ ## WeightedGraph::Graph API
6
+
7
+ ```ruby
8
+ # Add directed edge (source, destination) to the graph with given weight
9
+ add_edge(source, destination, weight)
10
+
11
+ # Add undirected edge (vertex_a, vertex_b) to the graph with given weight
12
+ add_undirected_edge(vertex_a, vertex_b, weight)
13
+
14
+ # Remove directed edge (source, destination) from the graph
15
+ remove_edge(source, destination)
16
+
17
+ # Remove undirected edge (vertex_a, vertex_b) from the graph
18
+ remove_undirected_edge(vertex_a, vertex_b)
19
+
20
+ # Return true iff the graph contains directed edge (source, destination)
21
+ contains_edge?(source, destination)
22
+
23
+ # Returns the weight of directed edge (source, destination),
24
+ # or returns Float::INFINITY if no such edge exists
25
+ get_edge_weight(source, destination)
26
+
27
+ # Returns the set of vertices v_i where edge (source, v_i) is in the graph
28
+ get_adjacent_vertices(source)
29
+ ```
30
+
31
+ <!-- TODO: Add installation section after publishing the gem.
32
+ ## Installation
33
+
34
+ Add this line to your application's Gemfile:
35
+
36
+ ```ruby
37
+ gem 'weighted_graph'
38
+ ```
39
+
40
+ Or install it yourself as:
41
+
42
+ $ gem install weighted_graph -->
43
+
44
+ ## Development
45
+
46
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/msayson/weighted_graph.
51
+
52
+ ## License
53
+
54
+ The weighted_graph library is open source and available under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'weighted_graph'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ require 'weighted_graph/version'
2
+ require_relative './weighted_graph/graph'
3
+
4
+ # A simple graph with weighted edges
5
+ module WeightedGraph
6
+ end
@@ -0,0 +1,62 @@
1
+ require 'set'
2
+
3
+ # A simple graph with weighted edges
4
+ module WeightedGraph
5
+ # Graph API
6
+ class Graph
7
+ # Initialize a graph with an optional adjacency list
8
+ def initialize(edges = Hash.new(0))
9
+ @edges = edges
10
+ end
11
+
12
+ # Add directed edge (source, destination) to the graph with given weight
13
+ def add_edge(source, destination, weight)
14
+ if @edges.key?(source)
15
+ @edges[source][destination] = weight
16
+ else
17
+ @edges[source] = { destination => weight }
18
+ end
19
+ end
20
+
21
+ # Add undirected edge (vertex_a, vertex_b) to the graph with given weight
22
+ def add_undirected_edge(vertex_a, vertex_b, weight)
23
+ add_edge(vertex_a, vertex_b, weight)
24
+ add_edge(vertex_b, vertex_a, weight)
25
+ end
26
+
27
+ # Remove directed edge (source, destination) from the graph
28
+ def remove_edge(source, destination)
29
+ @edges[source].delete(destination) if @edges.key?(source)
30
+ end
31
+
32
+ # Remove undirected edge (vertex_a, vertex_b) from the graph
33
+ def remove_undirected_edge(vertex_a, vertex_b)
34
+ remove_edge(vertex_a, vertex_b)
35
+ remove_edge(vertex_b, vertex_a)
36
+ end
37
+
38
+ # Return true iff the graph contains directed edge (source, destination)
39
+ def contains_edge?(source, destination)
40
+ @edges.key?(source) && @edges[source].key?(destination)
41
+ end
42
+
43
+ # Returns the weight of directed edge (source, destination),
44
+ # or returns Float::INFINITY if no such edge exists
45
+ def get_edge_weight(source, destination)
46
+ if contains_edge?(source, destination)
47
+ @edges[source][destination]
48
+ else
49
+ Float::INFINITY
50
+ end
51
+ end
52
+
53
+ # Returns the set of vertices v_i where edge (source, v_i) is in the graph
54
+ def get_adjacent_vertices(source)
55
+ adjacent_array = []
56
+ if @edges.key?(source)
57
+ adjacent_array = @edges[source].map { |dst, _weight| dst }
58
+ end
59
+ Set.new(adjacent_array)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module WeightedGraph
2
+ VERSION = '0.1.1'.freeze
3
+ end
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'weighted_graph/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'weighted_graph'
9
+ spec.version = WeightedGraph::VERSION
10
+ spec.authors = ['Mark Sayson']
11
+ spec.email = ['masayson@gmail.com']
12
+
13
+ spec.summary = 'A simple weighted graph implementation.'
14
+ spec.description = 'A simple weighted graph implementation that stores ' \
15
+ 'edges in an adjacency list.'
16
+ spec.homepage = 'https://github.com/msayson/weighted_graph'
17
+ spec.license = 'MIT'
18
+
19
+ # Prevent pushing this gem to RubyGems.org.
20
+ # To allow pushes either set the 'allowed_push_host' to allow pushing to
21
+ # a single host or delete this section to allow pushing to any host.
22
+ if spec.respond_to?(:metadata)
23
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/gems/weighted_graph'
24
+ else
25
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
26
+ 'public gem pushes.'
27
+ end
28
+
29
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
30
+ f.match(%r{^(test|spec|features)/})
31
+ end
32
+ spec.bindir = 'exe'
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ['lib']
35
+
36
+ spec.add_development_dependency 'bundler', '~> 1.14'
37
+ spec.add_development_dependency 'pry', '~> 0.10'
38
+ spec.add_development_dependency 'rake', '~> 10.0'
39
+ spec.add_development_dependency 'rspec', '~> 3.0'
40
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weighted_graph
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Mark Sayson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-27 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: A simple weighted graph implementation that stores edges in an adjacency
70
+ list.
71
+ email:
72
+ - masayson@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".rubocop"
80
+ - ".rubocop.yml"
81
+ - ".travis.yml"
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/weighted_graph.rb
89
+ - lib/weighted_graph/graph.rb
90
+ - lib/weighted_graph/version.rb
91
+ - weighted_graph.gemspec
92
+ homepage: https://github.com/msayson/weighted_graph
93
+ licenses:
94
+ - MIT
95
+ metadata:
96
+ allowed_push_host: https://rubygems.org/gems/weighted_graph
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.6.11
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: A simple weighted graph implementation.
117
+ test_files: []