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 +4 -4
- data/README.md +21 -13
- data/lib/directed_graph.rb +1 -0
- data/lib/directed_graph/graph.rb +1 -1
- data/lib/directed_graph/version.rb +1 -1
- data/lib/directed_graph/vertex.rb +16 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f3a68ea4ee1b57dc00898a6fc2a7d22911aea8ff
|
4
|
+
data.tar.gz: cd9fe2d655d4672152a786f5204e97b9023af6cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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:
|
15
|
-
@ab = Edge.new(origin_vertex:
|
16
|
-
@bc = Edge.new(origin_vertex:
|
17
|
-
@bd = Edge.new(origin_vertex:
|
18
|
-
@ae = Edge.new(origin_vertex:
|
19
|
-
@de = Edge.new(origin_vertex:
|
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 # [
|
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(
|
36
|
-
@graph.shortest_path(
|
37
|
-
@graph.shortest_path(
|
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(
|
44
|
-
@graph.longest_path(
|
45
|
-
@graph.longest_path(
|
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
|
data/lib/directed_graph.rb
CHANGED
data/lib/directed_graph/graph.rb
CHANGED
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.
|
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-
|
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
|