directed_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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19be1d44522c23cea508cee9c7355fddbf94f2b7
4
- data.tar.gz: 3113b3809f3bc9871456410a5a698403739ec631
3
+ metadata.gz: 4fc02a4eccc4014ced95f5e9733f9562bf5760f2
4
+ data.tar.gz: 4263efeaa3fe670468c03a5f8600b890b7cae100
5
5
  SHA512:
6
- metadata.gz: cf44cda6bb1e05a6e40adf34ca14b58c2ca49f5d935b28ab10b8451365b1ca354c74e8c6b1d57f5aa2ea61ca8eddd6e8858c52e768707e19d305a3a7ad046fe3
7
- data.tar.gz: 0a7178c9650e3d2908cb3b2185248c9cbbfbad2e0c88f75d695e8ed7111305aa6a8909e9f8fb3b4a745c6d11f31c8543bef187b83793a6da2be12c014e8fbe17
6
+ metadata.gz: 9637fa8cab2d210e408a27af1d9d2c7d28c3c7436dc2d3a6ab696d9f0efece71a4b4ef9b869214728556d8d0c8f3fe072f58e25fd83a30b22c15ecd520fabb87
7
+ data.tar.gz: 6b7791dfac072c0a2f3c305347f225cb89f52f9b4b65afda24af5c16c5e20c6919afc8aac084835a04b8f646542902004edb2140d1c9338bec2b5fc44bcd191a
data/README.md CHANGED
@@ -27,6 +27,24 @@ The `@graph` object can be used to get a topological sorted array of the vertice
27
27
  @graph.sorted_vertices # ["root", "a", "b", "d", "e", "c"]
28
28
  ```
29
29
 
30
+ Here is an [awesome blog post](https://endofline.wordpress.com/2010/12/22/ruby-standard-library-tsort/) on topological sorting in Ruby with the `TSort` module.
31
+
32
+ The `@graph` object can also be used to calculate the shortest path between two vertices:
33
+
34
+ ```ruby
35
+ @graph.shortest_path("root", "e")) # %w|root a e|
36
+ @graph.shortest_path("root", "blah")) # nil because the "blah" vertex doesn't exist
37
+ @graph.shortest_path("d", "a")) # nil because the graph is directed and can't be traversed in the wrong direction
38
+ ```
39
+
40
+ The `@graph` object can be used to calculate the longest path between two vertices:
41
+
42
+ ```ruby
43
+ @graph.longest_path("root", "e")) # %w|root a b d e|
44
+ @graph.longest_path("a", "c")) # %w|a b c|
45
+ @graph.longest_path("d", "a")) # returns [] when the path doesn't exist
46
+ ```
47
+
30
48
  ## Installation
31
49
 
32
50
  Add this line to your application's Gemfile:
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rspec"
25
25
 
26
26
  spec.add_dependency 'pry'
27
+ spec.add_dependency 'simple-graph'
27
28
  end
@@ -8,6 +8,12 @@ module DirectedGraph
8
8
  @edges = edges
9
9
  end
10
10
 
11
+ def vertices_longest_path_to_root(root_vertex)
12
+ vertices.map do |v|
13
+ [v, longest_path(root_vertex, v)]
14
+ end
15
+ end
16
+
11
17
  def vertices
12
18
  r = []
13
19
  edges.each {|e| r.push(e.origin_vertex, e.destination_vertex)}
@@ -26,14 +32,42 @@ module DirectedGraph
26
32
  JobRunner.sorted_vertices(vertices_and_children)
27
33
  end
28
34
 
35
+ def shortest_path(origin_vertex, destination_vertex)
36
+ simple_graph.shortest_path(origin_vertex, destination_vertex)
37
+ end
38
+
39
+ def longest_path(origin_vertex, destination_vertex, result = [])
40
+ return [destination_vertex] + result if origin_vertex == destination_vertex
41
+ parents(destination_vertex).map do |v|
42
+ longest_path(origin_vertex, v, [destination_vertex] + result)
43
+ end.inject([]) {|m, arr| m = arr if arr.length > m.length; m}
44
+ end
45
+
46
+ private
47
+
29
48
  def children(vertex)
30
49
  edges.select {|e| e.origin_vertex == vertex}.map{|e| e.destination_vertex}
31
50
  end
32
51
 
52
+ def parents(vertex)
53
+ edges.select {|e| e.destination_vertex == vertex}.map{|e| e.origin_vertex}
54
+ end
55
+
33
56
  def vertices_and_children
34
57
  vertices.map {|v| [v, children(v)]}
35
58
  end
36
59
 
60
+ def simple_graph
61
+ graph = SimpleGraph::Graph.new
62
+ vertices.each do |v|
63
+ graph.add_vertex(v)
64
+ end
65
+ edges.each do |e|
66
+ graph.add_edge(e.origin_vertex, e.destination_vertex)
67
+ end
68
+ graph
69
+ end
70
+
37
71
  end
38
72
 
39
73
  end
@@ -1,3 +1,3 @@
1
1
  module DirectedGraph
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,3 +1,5 @@
1
+ require 'simple-graph'
2
+
1
3
  require "directed_graph/version"
2
4
  require_relative "./directed_graph/job_runner.rb"
3
5
  require_relative "./directed_graph/edge.rb"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: directed_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - MrPowers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-04 00:00:00.000000000 Z
11
+ date: 2015-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simple-graph
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: Topological sorting, shortest path, and json exports for directed acyclic
70
84
  graphs
71
85
  email: