directed_graph 0.3.0 → 0.4.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: 0adbde84d2c5293373788a5ce73f444fa41d3129
4
- data.tar.gz: 691938d500c03589376637969b320ade01699fee
3
+ metadata.gz: f3a68ea4ee1b57dc00898a6fc2a7d22911aea8ff
4
+ data.tar.gz: cd9fe2d655d4672152a786f5204e97b9023af6cc
5
5
  SHA512:
6
- metadata.gz: 843edf49bae2d53557d2c749b1caaae3a523502f505d4306905dc43285b8f8a10047a39424617bb9edf9d97fe1e58456f9ebf1a11f6efa03ee62cebf9a68963f
7
- data.tar.gz: df3e337f5f782951bab9974b6ade178ce10692fab50c2a0641a6efa1a38ee7a2f5f5859d116e88793a6c993a9ba9891a72cc78a709b6b3b48d1c9bcdf59dad65
6
+ metadata.gz: 46e9b7620b61262d8dd27f81b922968e1151a552e635bfc97fd41a7bb196f22c47e2910bf24563d632032e2fb607eccb4449c4da2de76796898f8d0e0d96bf12
7
+ data.tar.gz: 6515c50de7baa3fb797389ac632d764ef6739d198b88c0be0d20657fd60e0e79dd09c8ee0300b3fd0301cc181b0f08e644f58c56e45e24b9c6310f8eb9ac4a18
data/README.md CHANGED
@@ -9,14 +9,22 @@ The following graph will be used to demonstrate the functionality of the directe
9
9
  The `Graph` class should be instantiated with an array of edges:
10
10
 
11
11
  ```ruby
12
+ # create the vertices
13
+ @root = Vertex.new(name: "root")
14
+ @a = Vertex.new(name: "a")
15
+ @b = Vertex.new(name: "b")
16
+ @c = Vertex.new(name: "c")
17
+ @d = Vertex.new(name: "d")
18
+ @e = Vertex.new(name: "e")
19
+
12
20
  # The first argument is the origin_vertex
13
21
  # and the second is the destination_vertex
14
- @ra = Edge.new(origin_vertex: "root", destination_vertex: "a")
15
- @ab = Edge.new(origin_vertex: "a", destination_vertex: "b")
16
- @bc = Edge.new(origin_vertex: "b", destination_vertex: "c")
17
- @bd = Edge.new(origin_vertex: "b", destination_vertex: "d")
18
- @ae = Edge.new(origin_vertex: "a", destination_vertex: "e")
19
- @de = Edge.new(origin_vertex: "d", destination_vertex: "e")
22
+ @ra = Edge.new(origin_vertex: @root, destination_vertex: @a)
23
+ @ab = Edge.new(origin_vertex: @a, destination_vertex: @b)
24
+ @bc = Edge.new(origin_vertex: @b, destination_vertex: @c)
25
+ @bd = Edge.new(origin_vertex: @b, destination_vertex: @d)
26
+ @ae = Edge.new(origin_vertex: @a, destination_vertex: @e)
27
+ @de = Edge.new(origin_vertex: @d, destination_vertex: @e)
20
28
  @edges = [@ra, @ab, @bc, @bd, @ae, @de]
21
29
  @graph = Graph.new(@edges)
22
30
  ```
@@ -24,7 +32,7 @@ The `Graph` class should be instantiated with an array of edges:
24
32
  The `@graph` object can be used to get a topological sorted array of the vertices:
25
33
 
26
34
  ```ruby
27
- @graph.sorted_vertices # ["root", "a", "b", "d", "e", "c"]
35
+ @graph.sorted_vertices # [@root, @a, @b, @d, @e, @c]
28
36
  ```
29
37
 
30
38
  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.
@@ -32,17 +40,17 @@ Here is an [awesome blog post](https://endofline.wordpress.com/2010/12/22/ruby-s
32
40
  The `@graph` object can also be used to calculate the shortest path between two vertices:
33
41
 
34
42
  ```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
43
+ @graph.shortest_path(@root, @e) # [@root, @a, @e]
44
+ @graph.shortest_path(@root, Vertex.new(name: "blah")) # nil because the "blah" vertex doesn't exist
45
+ @graph.shortest_path(@d, @a) # nil because the graph is directed and can't be traversed in the wrong direction
38
46
  ```
39
47
 
40
48
  The `@graph` object can be used to calculate the longest path between two vertices:
41
49
 
42
50
  ```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
51
+ @graph.longest_path(@root, @e) # [@root, @a, @b, @d, @e]
52
+ @graph.longest_path(@a, @c) # [@a, @b, @c]
53
+ @graph.longest_path(@d, @a) # returns [] when the path doesn't exist
46
54
  ```
47
55
 
48
56
  ## Installation
@@ -2,6 +2,7 @@ require 'simple-graph'
2
2
 
3
3
  require "directed_graph/version"
4
4
  require_relative "./directed_graph/job_runner.rb"
5
+ require_relative "./directed_graph/vertex.rb"
5
6
  require_relative "./directed_graph/edge.rb"
6
7
  require_relative "./directed_graph/graph.rb"
7
8
 
@@ -11,7 +11,7 @@ module DirectedGraph
11
11
  def vertices
12
12
  r = []
13
13
  edges.each {|e| r.push(e.origin_vertex, e.destination_vertex)}
14
- r.uniq
14
+ r.uniq {|e| e.object_id}
15
15
  end
16
16
 
17
17
  def ordered_edges
@@ -1,3 +1,3 @@
1
1
  module DirectedGraph
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -0,0 +1,16 @@
1
+ module DirectedGraph
2
+
3
+ class Vertex
4
+
5
+ attr_reader :name
6
+ attr_accessor :data
7
+
8
+ def initialize(args)
9
+ @name = args.fetch(:name)
10
+ @data = args.fetch(:data, {})
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+
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.3.0
4
+ version: 0.4.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-12 00:00:00.000000000 Z
11
+ date: 2015-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -104,6 +104,7 @@ files:
104
104
  - lib/directed_graph/graph.rb
105
105
  - lib/directed_graph/job_runner.rb
106
106
  - lib/directed_graph/version.rb
107
+ - lib/directed_graph/vertex.rb
107
108
  homepage: https://github.com/MrPowers/directed_graph
108
109
  licenses:
109
110
  - MIT