rgl 0.5.4 → 0.5.6
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/ChangeLog +7 -0
- data/README.md +6 -0
- data/lib/rgl/base.rb +1 -1
- data/lib/rgl/path.rb +17 -0
- data/lib/rgl/traversal.rb +4 -1
- data/test/path_test.rb +52 -0
- data/test/traversal_test.rb +26 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6cc432ed6e3560ca66d241be508defa12cd832a205df40f6f06e50ae187cb35f
|
4
|
+
data.tar.gz: 4f31192830a5a78b1d42c1a150a0f5b649750abb440ea06df689fa3dfc4c9621
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 568056f6ad3038dda236d889b09e78007767103692c514d5627a4efe0e610797e91eda827b58880c5714e357d964db7cf2040b238fbf79286108410b35592aeb
|
7
|
+
data.tar.gz: 3e1f01edc45fc73f094dd1d89cca164e6a2792cee0e555ab3b330f7c8cb981a5d79d2ec6003a6e9f8a39f6ec8d651b8d3d36645415a7156b6ab8e72cad4d41bb
|
data/ChangeLog
CHANGED
data/README.md
CHANGED
@@ -171,6 +171,12 @@ Add inverse edge (4-2) to directed graph:
|
|
171
171
|
irb> dg.remove_edge 4,2
|
172
172
|
true
|
173
173
|
|
174
|
+
Check whether a path exists between vertices 1 and 5
|
175
|
+
|
176
|
+
irb> require 'rgl/path'
|
177
|
+
irb> dg.path?(1, 5)
|
178
|
+
true
|
179
|
+
|
174
180
|
*Topological sort* is implemented as an iterator:
|
175
181
|
|
176
182
|
require 'rgl/topsort'
|
data/lib/rgl/base.rb
CHANGED
data/lib/rgl/path.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rgl/traversal'
|
4
|
+
|
5
|
+
module RGL
|
6
|
+
module Graph
|
7
|
+
# Checks whether a path exists between _source_ and _target_ vertices
|
8
|
+
# in the graph.
|
9
|
+
#
|
10
|
+
def path?(source, target)
|
11
|
+
return false unless has_vertex?(source)
|
12
|
+
|
13
|
+
bfs_iterator = bfs_iterator(source)
|
14
|
+
bfs_iterator.include?(target)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/rgl/traversal.rb
CHANGED
@@ -58,9 +58,12 @@ module RGL
|
|
58
58
|
# Reset the iterator to the initial state (i.e. at_beginning? == true).
|
59
59
|
#
|
60
60
|
def set_to_begin
|
61
|
+
# Reset color_map
|
62
|
+
@color_map = Hash.new(:WHITE)
|
61
63
|
color_map[@start_vertex] = :GRAY
|
62
|
-
@waiting = [@start_vertex]
|
64
|
+
@waiting = [@start_vertex] # a queue
|
63
65
|
handle_tree_edge(nil, @start_vertex) # discovers start vertex
|
66
|
+
self
|
64
67
|
end
|
65
68
|
|
66
69
|
def basic_forward # :nodoc:
|
data/test/path_test.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
require 'rgl/adjacency'
|
6
|
+
require 'rgl/path'
|
7
|
+
|
8
|
+
class TestPath < Test::Unit::TestCase
|
9
|
+
include RGL
|
10
|
+
|
11
|
+
def setup
|
12
|
+
edges = [[1, 2], [2, 3], [2, 4], [4, 5], [6, 4], [1, 6]]
|
13
|
+
@directed_graph, @undirected_graph =
|
14
|
+
[DirectedAdjacencyGraph, AdjacencyGraph].map do |klass|
|
15
|
+
graph = klass.new
|
16
|
+
graph.add_edges(*edges)
|
17
|
+
graph
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_path_for_directed_graph
|
22
|
+
assert(@directed_graph.path?(1, 5))
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_path_for_undirected_graph
|
26
|
+
assert(@undirected_graph.path?(1, 5))
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_inverse_path_for_directed_graph
|
30
|
+
assert_equal(@directed_graph.path?(3, 1), false)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_inverse_path_for_undirected_graph
|
34
|
+
assert(@undirected_graph.path?(3, 1))
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_path_for_directed_graph_wrong_source
|
38
|
+
assert_equal(@directed_graph.path?(0, 1), false)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_path_for_undirected_graph_wrong_source
|
42
|
+
assert_equal(@undirected_graph.path?(0, 1), false)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_path_for_directed_graph_wrong_target
|
46
|
+
assert_equal(@directed_graph.path?(4, 0), false)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_path_for_undirected_graph_wrong_target
|
50
|
+
assert_equal(@undirected_graph.path?(4, 0), false)
|
51
|
+
end
|
52
|
+
end
|
data/test/traversal_test.rb
CHANGED
@@ -218,4 +218,30 @@ END
|
|
218
218
|
dg.depth_first_search(vis) { |x| }
|
219
219
|
assert_equal("(1 (2 (3 3)(4 (5 5) 4) 2)(6 6) 1)(10 (11 11) 10)", a)
|
220
220
|
end
|
221
|
+
|
222
|
+
def test_bfs_stream_protocol
|
223
|
+
it = @dg.bfs_iterator(1)
|
224
|
+
assert_true(it.at_beginning?)
|
225
|
+
|
226
|
+
it.set_to_end()
|
227
|
+
assert_true(it.at_end?)
|
228
|
+
|
229
|
+
it.set_to_begin()
|
230
|
+
assert_true(it.at_beginning?)
|
231
|
+
|
232
|
+
assert_equal(it.to_a(), [1, 2, 6, 3, 4, 5])
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_dfs_stream_protocol
|
236
|
+
it = @dg.dfs_iterator(1)
|
237
|
+
assert_true(it.at_beginning?)
|
238
|
+
|
239
|
+
it.set_to_end()
|
240
|
+
assert_true(it.at_end?)
|
241
|
+
|
242
|
+
it.set_to_begin()
|
243
|
+
assert_true(it.at_beginning?)
|
244
|
+
|
245
|
+
assert_equal(it.to_a(), [1, 6, 4, 5, 2, 3])
|
246
|
+
end
|
221
247
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rgl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Horst Duchene
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire: rgl/base
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-
|
12
|
+
date: 2019-08-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: stream
|
@@ -183,6 +183,7 @@ files:
|
|
183
183
|
- lib/rgl/graphxml.rb
|
184
184
|
- lib/rgl/implicit.rb
|
185
185
|
- lib/rgl/mutable.rb
|
186
|
+
- lib/rgl/path.rb
|
186
187
|
- lib/rgl/path_builder.rb
|
187
188
|
- lib/rgl/prim.rb
|
188
189
|
- lib/rgl/rdot.rb
|
@@ -205,6 +206,7 @@ files:
|
|
205
206
|
- test/graph_test.rb
|
206
207
|
- test/graph_xml_test.rb
|
207
208
|
- test/implicit_test.rb
|
209
|
+
- test/path_test.rb
|
208
210
|
- test/prim_test.rb
|
209
211
|
- test/rdot_test.rb
|
210
212
|
- test/test_helper.rb
|
@@ -234,7 +236,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
234
236
|
- !ruby/object:Gem::Version
|
235
237
|
version: '0'
|
236
238
|
requirements: []
|
237
|
-
rubygems_version: 3.0.
|
239
|
+
rubygems_version: 3.0.6
|
238
240
|
signing_key:
|
239
241
|
specification_version: 4
|
240
242
|
summary: Ruby Graph Library
|