directed_graph 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +52 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/directed_graph.gemspec +27 -0
- data/example/simple_directed_graph.png +0 -0
- data/lib/directed_graph/edge.rb +17 -0
- data/lib/directed_graph/graph.rb +40 -0
- data/lib/directed_graph/job_runner.rb +30 -0
- data/lib/directed_graph/version.rb +3 -0
- data/lib/directed_graph.rb +7 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 19be1d44522c23cea508cee9c7355fddbf94f2b7
|
4
|
+
data.tar.gz: 3113b3809f3bc9871456410a5a698403739ec631
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cf44cda6bb1e05a6e40adf34ca14b58c2ca49f5d935b28ab10b8451365b1ca354c74e8c6b1d57f5aa2ea61ca8eddd6e8858c52e768707e19d305a3a7ad046fe3
|
7
|
+
data.tar.gz: 0a7178c9650e3d2908cb3b2185248c9cbbfbad2e0c88f75d695e8ed7111305aa6a8909e9f8fb3b4a745c6d11f31c8543bef187b83793a6da2be12c014e8fbe17
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 MrPowers
|
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,52 @@
|
|
1
|
+
# directed_graph
|
2
|
+
|
3
|
+
The directed_graph gem is useful for modeling directed, acyclic, unweighted graphs. It provides methods to topologically sort graphs, find the shortest path between two vertices, and render the graphs and json for client-side graphing.
|
4
|
+
|
5
|
+
The following graph will be used to demonstrate the functionality of the directed_graph gem.
|
6
|
+
|
7
|
+

|
8
|
+
|
9
|
+
The `Graph` class should be instantiated with an array of edges:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
# The first argument is the origin_vertex
|
13
|
+
# and the second is the destination_vertex
|
14
|
+
@ra = Edge.new("root", "a")
|
15
|
+
@ab = Edge.new("a", "b")
|
16
|
+
@bc = Edge.new("b", "c")
|
17
|
+
@bd = Edge.new("b", "d")
|
18
|
+
@ae = Edge.new("a", "e")
|
19
|
+
@de = Edge.new("d", "e")
|
20
|
+
@edges = [@ra, @ab, @bc, @bd, @ae, @de]
|
21
|
+
@graph = Graph.new(@edges)
|
22
|
+
```
|
23
|
+
|
24
|
+
The `@graph` object can be used to get a topological sorted array of the vertices:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
@graph.sorted_vertices # ["root", "a", "b", "d", "e", "c"]
|
28
|
+
```
|
29
|
+
|
30
|
+
## Installation
|
31
|
+
|
32
|
+
Add this line to your application's Gemfile:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
gem 'directed_graph'
|
36
|
+
```
|
37
|
+
|
38
|
+
Require the gem in your code:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
require 'directed_graph'
|
42
|
+
```
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/MrPowers/directed_graph.
|
47
|
+
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
52
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "directed_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
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'directed_graph/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "directed_graph"
|
8
|
+
spec.version = DirectedGraph::VERSION
|
9
|
+
spec.authors = ["MrPowers"]
|
10
|
+
spec.email = ["matthewkevinpowers@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Modeling directed acyclic graphs}
|
13
|
+
spec.description = %q{Topological sorting, shortest path, and json exports for directed acyclic graphs}
|
14
|
+
spec.homepage = "https://github.com/MrPowers/directed_graph"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
|
26
|
+
spec.add_dependency 'pry'
|
27
|
+
end
|
Binary file
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DirectedGraph
|
2
|
+
|
3
|
+
class Edge
|
4
|
+
|
5
|
+
attr_reader :origin_vertex, :destination_vertex, :value
|
6
|
+
|
7
|
+
def initialize(origin_vertex, destination_vertex, value = nil)
|
8
|
+
@origin_vertex = origin_vertex
|
9
|
+
@destination_vertex = destination_vertex
|
10
|
+
@value = value
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module DirectedGraph
|
2
|
+
|
3
|
+
class Graph
|
4
|
+
|
5
|
+
attr_reader :edges
|
6
|
+
|
7
|
+
def initialize(edges)
|
8
|
+
@edges = edges
|
9
|
+
end
|
10
|
+
|
11
|
+
def vertices
|
12
|
+
r = []
|
13
|
+
edges.each {|e| r.push(e.origin_vertex, e.destination_vertex)}
|
14
|
+
r.uniq
|
15
|
+
end
|
16
|
+
|
17
|
+
def ordered_edges
|
18
|
+
sorted_vertices.inject([]) do |memo, v|
|
19
|
+
edge = edges.select {|e| e.destination_vertex == v}
|
20
|
+
memo.push(*edge) if edge
|
21
|
+
memo
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def sorted_vertices
|
26
|
+
JobRunner.sorted_vertices(vertices_and_children)
|
27
|
+
end
|
28
|
+
|
29
|
+
def children(vertex)
|
30
|
+
edges.select {|e| e.origin_vertex == vertex}.map{|e| e.destination_vertex}
|
31
|
+
end
|
32
|
+
|
33
|
+
def vertices_and_children
|
34
|
+
vertices.map {|v| [v, children(v)]}
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'tsort'
|
2
|
+
|
3
|
+
module DirectedGraph; class JobRunner
|
4
|
+
|
5
|
+
def self.sorted_vertices(vertices_and_children)
|
6
|
+
runner = JobRunner.new
|
7
|
+
vertices_and_children.each { |v, c| runner.add(v, c) }
|
8
|
+
runner.tsort.reverse
|
9
|
+
end
|
10
|
+
|
11
|
+
include TSort
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@jobs = Hash.new{|h,k| h[k] = []}
|
15
|
+
end
|
16
|
+
|
17
|
+
def add(name, dependencies=[])
|
18
|
+
@jobs[name] = dependencies
|
19
|
+
end
|
20
|
+
|
21
|
+
def tsort_each_node(&block)
|
22
|
+
@jobs.each_key(&block)
|
23
|
+
end
|
24
|
+
|
25
|
+
def tsort_each_child(node, &block)
|
26
|
+
@jobs[node].each(&block) if @jobs.has_key?(node)
|
27
|
+
end
|
28
|
+
|
29
|
+
end; end
|
30
|
+
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: directed_graph
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- MrPowers
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-04 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Topological sorting, shortest path, and json exports for directed acyclic
|
70
|
+
graphs
|
71
|
+
email:
|
72
|
+
- matthewkevinpowers@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- directed_graph.gemspec
|
87
|
+
- example/simple_directed_graph.png
|
88
|
+
- lib/directed_graph.rb
|
89
|
+
- lib/directed_graph/edge.rb
|
90
|
+
- lib/directed_graph/graph.rb
|
91
|
+
- lib/directed_graph/job_runner.rb
|
92
|
+
- lib/directed_graph/version.rb
|
93
|
+
homepage: https://github.com/MrPowers/directed_graph
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
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.4.5
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Modeling directed acyclic graphs
|
117
|
+
test_files: []
|