rgl 0.5.9 → 0.5.10
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 +2 -2
- data/README.md +11 -11
- data/examples/canvas.rb +1 -1
- data/examples/graph.dot +971 -0
- data/examples/insel_der_tausend_gefahren.rb +1 -1
- data/examples/north/g.10.0.graphml +1 -1
- data/examples/north/g.10.1.graphml +1 -1
- data/examples/north/g.10.11.graphml +1 -1
- data/examples/north/g.10.12.graphml +1 -1
- data/examples/north/g.10.13.graphml +1 -1
- data/examples/north/g.10.14.graphml +1 -1
- data/examples/north/g.10.15.graphml +1 -1
- data/examples/north/g.10.16.graphml +1 -1
- data/examples/north/g.10.17.graphml +1 -1
- data/examples/north/g.10.19.graphml +1 -1
- data/examples/north/g.10.2.graphml +1 -1
- data/examples/north/g.10.20.graphml +1 -1
- data/examples/north/g.10.22.graphml +1 -1
- data/examples/north/g.10.24.graphml +1 -1
- data/examples/north/g.10.25.graphml +1 -1
- data/examples/north/g.10.27.graphml +1 -1
- data/examples/north/g.10.28.graphml +1 -1
- data/examples/north/g.10.29.graphml +1 -1
- data/examples/north/g.10.3.graphml +1 -1
- data/examples/north/g.10.30.graphml +1 -1
- data/examples/north/g.10.31.graphml +1 -1
- data/examples/north/g.10.34.graphml +1 -1
- data/examples/north/g.10.37.graphml +1 -1
- data/examples/north/g.10.38.graphml +1 -1
- data/examples/north/g.10.39.graphml +1 -1
- data/examples/north/g.10.4.graphml +1 -1
- data/examples/north/g.10.40.graphml +1 -1
- data/examples/north/g.10.41.graphml +1 -1
- data/examples/north/g.10.42.graphml +1 -1
- data/examples/north/g.10.45.graphml +1 -1
- data/examples/north/g.10.46.graphml +1 -1
- data/examples/north/g.10.5.graphml +1 -1
- data/examples/north/g.10.50.graphml +1 -1
- data/examples/north/g.10.56.graphml +1 -1
- data/examples/north/g.10.57.graphml +1 -1
- data/examples/north/g.10.58.graphml +1 -1
- data/examples/north/g.10.6.graphml +1 -1
- data/examples/north/g.10.60.graphml +1 -1
- data/examples/north/g.10.61.graphml +1 -1
- data/examples/north/g.10.62.graphml +1 -1
- data/examples/north/g.10.68.graphml +1 -1
- data/examples/north/g.10.69.graphml +1 -1
- data/examples/north/g.10.7.graphml +1 -1
- data/examples/north/g.10.70.graphml +1 -1
- data/examples/north/g.10.71.graphml +1 -1
- data/examples/north/g.10.72.graphml +1 -1
- data/examples/north/g.10.74.graphml +1 -1
- data/examples/north/g.10.75.graphml +1 -1
- data/examples/north/g.10.78.graphml +1 -1
- data/examples/north/g.10.79.graphml +1 -1
- data/examples/north/g.10.8.graphml +1 -1
- data/examples/north/g.10.80.graphml +1 -1
- data/examples/north/g.10.82.graphml +1 -1
- data/examples/north/g.10.83.graphml +1 -1
- data/examples/north/g.10.85.graphml +1 -1
- data/examples/north/g.10.86.graphml +1 -1
- data/examples/north/g.10.88.graphml +1 -1
- data/examples/north/g.10.89.graphml +1 -1
- data/examples/north/g.10.9.graphml +1 -1
- data/examples/north/g.10.90.graphml +1 -1
- data/examples/north/g.10.91.graphml +1 -1
- data/examples/north/g.10.92.graphml +1 -1
- data/examples/north/g.10.93.graphml +1 -1
- data/examples/north/g.10.94.graphml +1 -1
- data/examples/north/g.12.8.graphml +1 -1
- data/examples/north/g.14.9.graphml +1 -1
- data/examples/north.rb +1 -1
- data/examples/rdep-rgl.rb +3 -3
- data/lib/rgl/adjacency.rb +43 -51
- data/lib/rgl/base.rb +71 -60
- data/lib/rgl/bellman_ford.rb +4 -3
- data/lib/rgl/bidirectional.rb +2 -2
- data/lib/rgl/bipartite.rb +1 -1
- data/lib/rgl/condensation.rb +3 -3
- data/lib/rgl/connected_components.rb +6 -6
- data/lib/rgl/dot.rb +5 -7
- data/lib/rgl/edmonds_karp.rb +7 -2
- data/lib/rgl/graph_iterator.rb +4 -2
- data/lib/rgl/graph_visitor.rb +16 -14
- data/lib/rgl/graph_wrapper.rb +3 -2
- data/lib/rgl/graphxml.rb +4 -4
- data/lib/rgl/implicit.rb +35 -35
- data/lib/rgl/mutable.rb +7 -7
- data/lib/rgl/path_builder.rb +3 -2
- data/lib/rgl/prim.rb +3 -1
- data/lib/rgl/rdot.rb +7 -7
- data/lib/rgl/topsort.rb +8 -7
- data/lib/rgl/transitivity.rb +6 -6
- data/lib/rgl/traversal.rb +47 -47
- data/test/traversal_test.rb +1 -1
- metadata +4 -3
data/lib/rgl/traversal.rb
CHANGED
@@ -1,41 +1,45 @@
|
|
1
1
|
# traversal.rb
|
2
2
|
#
|
3
|
-
# This file defines the basic graph traversal algorithm for DFS and BFS search.
|
4
|
-
# They are implemented as an RGL::GraphIterator, which is a Stream of vertices
|
5
|
-
# of a given graph. The streams are not reversable.
|
6
|
-
#
|
7
|
-
# Beside being an iterator in the sense of the Stream mixin, RGL::BFSIterator
|
8
|
-
# and RGL::DFSIterator follow the BGL
|
9
|
-
# Visitor[http://www.boost.org/libs/graph/doc/visitor_concepts.html] Concepts
|
10
|
-
# in a slightly modified fashion (especially for the RGL::DFSIterator).
|
11
|
-
|
12
3
|
require 'rgl/base'
|
13
4
|
require 'rgl/graph_visitor'
|
14
5
|
require 'rgl/graph_iterator'
|
15
6
|
|
16
7
|
module RGL
|
17
8
|
|
18
|
-
#
|
19
|
-
#
|
9
|
+
# File +traversal.rb+ defines the basic graph traversal algorithm for DFS and
|
10
|
+
# BFS search. They are implemented as a {GraphIterator}, which is a +Stream+
|
11
|
+
# of vertices of a given graph. The streams are not reversable.
|
12
|
+
#
|
13
|
+
# Beside being an iterator in the sense of the Stream mixin, {BFSIterator} and
|
14
|
+
# {DFSIterator} follow the
|
15
|
+
# {https://www.boost.org/libs/graph/doc/visitor_concepts.html BGL Visitor
|
16
|
+
# Concepts} in a slightly modified fashion (especially for the
|
17
|
+
# {DFSIterator}).
|
18
|
+
#
|
19
|
+
# A {BFSIterator} can be used to traverse a graph from a given start vertex in
|
20
|
+
# breath first search order. Since the iterator also mixins the {GraphVisitor},
|
20
21
|
# it provides all event points defined there.
|
21
22
|
#
|
22
|
-
# The vertices which are not yet visited are held in the queue
|
23
|
+
# The vertices which are not yet visited are held in the queue +@waiting+.
|
23
24
|
# During the traversal, vertices are *colored* using the colors :GRAY
|
24
25
|
# (when waiting) and :BLACK when finished. All other vertices are :WHITE.
|
25
26
|
#
|
26
|
-
# For more
|
27
|
-
#
|
27
|
+
# For more see the BGL {BFS Visitor
|
28
|
+
# Concept}[https://www.boost.org/libs/graph/doc/BFSVisitor.html].
|
28
29
|
#
|
29
|
-
# See the implementation of bfs_search_tree_from for an example usage.
|
30
|
+
# See the implementation of {Graph#bfs_search_tree_from} for an example usage.
|
30
31
|
#
|
32
|
+
# @see Graph#bfs_iterator
|
33
|
+
# @see Graph#bfs_search_tree_from
|
31
34
|
class BFSIterator
|
32
35
|
|
33
36
|
include GraphIterator
|
34
37
|
include GraphVisitor
|
35
38
|
|
39
|
+
# @return the vertex where the search starts
|
36
40
|
attr_accessor :start_vertex
|
37
41
|
|
38
|
-
# Create a new BFSIterator on _graph_, starting at vertex _start_.
|
42
|
+
# Create a new {BFSIterator} on _graph_, starting at vertex _start_.
|
39
43
|
#
|
40
44
|
def initialize(graph, start = graph.detect { |x| true })
|
41
45
|
super(graph)
|
@@ -43,9 +47,9 @@ module RGL
|
|
43
47
|
set_to_begin
|
44
48
|
end
|
45
49
|
|
46
|
-
# Returns true if
|
50
|
+
# Returns true if +@color_map+ has only one entry (for the start vertex).
|
47
51
|
#
|
48
|
-
def at_beginning?
|
52
|
+
def at_beginning?
|
49
53
|
@color_map.size == 1
|
50
54
|
end
|
51
55
|
|
@@ -66,7 +70,8 @@ module RGL
|
|
66
70
|
self
|
67
71
|
end
|
68
72
|
|
69
|
-
|
73
|
+
# @private
|
74
|
+
def basic_forward
|
70
75
|
u = next_vertex
|
71
76
|
handle_examine_vertex(u)
|
72
77
|
|
@@ -92,7 +97,7 @@ module RGL
|
|
92
97
|
|
93
98
|
protected
|
94
99
|
|
95
|
-
def next_vertex
|
100
|
+
def next_vertex
|
96
101
|
# waiting is a queue
|
97
102
|
@waiting.shift
|
98
103
|
end
|
@@ -101,16 +106,15 @@ module RGL
|
|
101
106
|
|
102
107
|
module Graph
|
103
108
|
|
104
|
-
#
|
105
|
-
|
109
|
+
# @return [BFSIterator] starting at vertex _v_.
|
106
110
|
def bfs_iterator(v = self.detect { |x| true })
|
107
111
|
BFSIterator.new(self, v)
|
108
112
|
end
|
109
113
|
|
110
|
-
#
|
111
|
-
# starting at _v_. This method uses the tree_edge_event of BFSIterator
|
114
|
+
# This method uses the +tree_edge_event+ of BFSIterator
|
112
115
|
# to record all tree edges of the search tree in the result.
|
113
|
-
|
116
|
+
#
|
117
|
+
# @return [DirectedAdjacencyGraph] which represents a BFS search tree starting at _v_.
|
114
118
|
def bfs_search_tree_from(v)
|
115
119
|
require 'rgl/adjacency'
|
116
120
|
bfs = bfs_iterator(v)
|
@@ -127,11 +131,12 @@ module RGL
|
|
127
131
|
end # module Graph
|
128
132
|
|
129
133
|
# Iterator for a depth first search, starting at a given vertex. The only
|
130
|
-
# difference from BFSIterator is that
|
134
|
+
# difference from {BFSIterator} is that +@waiting+ is a stack, instead of a queue.
|
131
135
|
#
|
132
|
-
# Note that this is different from DFSVisitor, which is used in the recursive
|
133
|
-
# version for depth first search (see depth_first_search).
|
136
|
+
# Note that this is different from {DFSVisitor}, which is used in the recursive
|
137
|
+
# version for depth first search (see {Graph#depth_first_search}).
|
134
138
|
#
|
139
|
+
# @see Graph#dfs_iterator
|
135
140
|
class DFSIterator < BFSIterator
|
136
141
|
|
137
142
|
def next_vertex
|
@@ -141,17 +146,13 @@ module RGL
|
|
141
146
|
|
142
147
|
end # class DFSIterator
|
143
148
|
|
144
|
-
# A DFSVisitor is needed by the depth_first_search and
|
145
|
-
# methods of a graph. Besides the
|
146
|
-
# an additional eventpoint start_vertex
|
147
|
-
# depth_first_search starts a new subtree of the depth first
|
148
|
-
# defined by the search.
|
149
|
-
#
|
150
|
-
# Note that the discover_vertex event defined in the BGL
|
151
|
-
# DFSVisitor[http://www.boost.org/libs/graph/doc/DFSVisitor.html] is not
|
152
|
-
# this is also defined in the common mixin GraphVisitor of DFSVisitor,
|
153
|
-
# DFSIterator, and BFSIterator.
|
149
|
+
# A DFSVisitor is needed by the {Graph#depth_first_search} and
|
150
|
+
# {Graph#depth_first_visit} methods of a graph. Besides the eventpoints of
|
151
|
+
# {GraphVisitor}, it provides an additional eventpoint +start_vertex+, which is
|
152
|
+
# called when a +depth_first_search+ starts a new subtree of the depth first
|
153
|
+
# forest that is defined by the search.
|
154
154
|
#
|
155
|
+
# @see DFSIterator
|
155
156
|
class DFSVisitor
|
156
157
|
|
157
158
|
include GraphVisitor
|
@@ -162,20 +163,19 @@ module RGL
|
|
162
163
|
|
163
164
|
module Graph
|
164
165
|
|
165
|
-
#
|
166
|
-
|
166
|
+
# @return [DFSIterator] staring at vertex _v_.
|
167
167
|
def dfs_iterator(v = self.detect { |x| true })
|
168
168
|
DFSIterator.new(self, v)
|
169
169
|
end
|
170
170
|
|
171
|
-
# Do a recursive DFS search on the whole graph. If a block is passed,
|
172
|
-
#
|
173
|
-
#
|
171
|
+
# Do a recursive DFS search on the whole graph. If a block is passed, it is
|
172
|
+
# called on each +finish_vertex+ event. See {Graph#strongly_connected_components}
|
173
|
+
# for an example usage.
|
174
174
|
#
|
175
175
|
# Note that this traversal does not garantee, that roots are at the top of
|
176
176
|
# each spanning subtree induced by the DFS search on a directed graph (see
|
177
177
|
# also the discussion in issue #20[https://github.com/monora/rgl/issues/20]).
|
178
|
-
#
|
178
|
+
# @see DFSVisitor
|
179
179
|
def depth_first_search(vis = DFSVisitor.new(self), &b)
|
180
180
|
each_vertex do |u|
|
181
181
|
unless vis.finished_vertex?(u)
|
@@ -186,8 +186,8 @@ module RGL
|
|
186
186
|
end
|
187
187
|
|
188
188
|
# Start a depth first search at vertex _u_. The block _b_ is called on
|
189
|
-
# each finish_vertex event.
|
190
|
-
#
|
189
|
+
# each +finish_vertex+ event.
|
190
|
+
# @see DFSVisitor
|
191
191
|
def depth_first_visit(u, vis = DFSVisitor.new(self), &b)
|
192
192
|
vis.color_map[u] = :GRAY
|
193
193
|
vis.handle_examine_vertex(u)
|
@@ -197,7 +197,7 @@ module RGL
|
|
197
197
|
|
198
198
|
if vis.follow_edge?(u, v) # (u,v) is a tree edge
|
199
199
|
vis.handle_tree_edge(u, v) # also discovers v
|
200
|
-
|
200
|
+
# color of v was :WHITE. Will be marked :GRAY in recursion
|
201
201
|
depth_first_visit(v, vis, &b)
|
202
202
|
else # (u,v) is a non tree edge
|
203
203
|
if vis.color_map[v] == :GRAY
|
data/test/traversal_test.rb
CHANGED
@@ -204,7 +204,7 @@ END
|
|
204
204
|
|
205
205
|
def test_depth_first_search_with_parens
|
206
206
|
@dg.add_edge 10, 11
|
207
|
-
# We must ensure, that the order of the traversal is not
|
207
|
+
# We must ensure, that the order of the traversal is not dependent on the
|
208
208
|
# order of the each iterator in the hash map of the adjacency graph. Therefor we
|
209
209
|
# wrap the graph with an implicit graph that simply ensures a sort order on
|
210
210
|
# the vertices.
|
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.10
|
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:
|
12
|
+
date: 2023-01-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: stream
|
@@ -114,6 +114,7 @@ files:
|
|
114
114
|
- Rakefile
|
115
115
|
- examples/canvas.rb
|
116
116
|
- examples/examples.rb
|
117
|
+
- examples/graph.dot
|
117
118
|
- examples/insel_der_tausend_gefahren.rb
|
118
119
|
- examples/north.rb
|
119
120
|
- examples/north/Graph.log
|
@@ -258,7 +259,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
258
259
|
- !ruby/object:Gem::Version
|
259
260
|
version: '0'
|
260
261
|
requirements: []
|
261
|
-
rubygems_version: 3.
|
262
|
+
rubygems_version: 3.4.1
|
262
263
|
signing_key:
|
263
264
|
specification_version: 4
|
264
265
|
summary: Ruby Graph Library
|