gratr19 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/README +335 -0
  2. data/examples/graph_self.rb +54 -0
  3. data/examples/module_graph.jpg +0 -0
  4. data/examples/module_graph.rb +12 -0
  5. data/examples/self_graph.jpg +0 -0
  6. data/examples/visualize.jpg +0 -0
  7. data/examples/visualize.rb +8 -0
  8. data/install.rb +49 -0
  9. data/lib/gratr.rb +42 -0
  10. data/lib/gratr/adjacency_graph.rb +230 -0
  11. data/lib/gratr/base.rb +34 -0
  12. data/lib/gratr/biconnected.rb +116 -0
  13. data/lib/gratr/chinese_postman.rb +123 -0
  14. data/lib/gratr/common.rb +74 -0
  15. data/lib/gratr/comparability.rb +92 -0
  16. data/lib/gratr/digraph.rb +115 -0
  17. data/lib/gratr/digraph_distance.rb +185 -0
  18. data/lib/gratr/dot.rb +90 -0
  19. data/lib/gratr/edge.rb +145 -0
  20. data/lib/gratr/graph.rb +314 -0
  21. data/lib/gratr/graph_api.rb +82 -0
  22. data/lib/gratr/import.rb +44 -0
  23. data/lib/gratr/labels.rb +103 -0
  24. data/lib/gratr/maximum_flow.rb +107 -0
  25. data/lib/gratr/rdot.rb +332 -0
  26. data/lib/gratr/search.rb +422 -0
  27. data/lib/gratr/strong_components.rb +127 -0
  28. data/lib/gratr/undirected_graph.rb +153 -0
  29. data/lib/gratr/version.rb +6 -0
  30. data/lib/priority-queue/benchmark/dijkstra.rb +171 -0
  31. data/lib/priority-queue/compare_comments.rb +49 -0
  32. data/lib/priority-queue/ext/priority_queue/CPriorityQueue/extconf.rb +2 -0
  33. data/lib/priority-queue/lib/priority_queue.rb +14 -0
  34. data/lib/priority-queue/lib/priority_queue/c_priority_queue.rb +1 -0
  35. data/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb +46 -0
  36. data/lib/priority-queue/lib/priority_queue/ruby_priority_queue.rb +525 -0
  37. data/lib/priority-queue/setup.rb +1551 -0
  38. data/lib/priority-queue/test/priority_queue_test.rb +371 -0
  39. data/tests/TestBiconnected.rb +53 -0
  40. data/tests/TestChinesePostman.rb +53 -0
  41. data/tests/TestComplement.rb +54 -0
  42. data/tests/TestDigraph.rb +333 -0
  43. data/tests/TestDigraphDistance.rb +138 -0
  44. data/tests/TestDot.rb +75 -0
  45. data/tests/TestEdge.rb +171 -0
  46. data/tests/TestInspection.rb +57 -0
  47. data/tests/TestMultiEdge.rb +57 -0
  48. data/tests/TestNeighborhood.rb +64 -0
  49. data/tests/TestProperties.rb +160 -0
  50. data/tests/TestSearch.rb +277 -0
  51. data/tests/TestStrongComponents.rb +85 -0
  52. data/tests/TestTriagulated.rb +137 -0
  53. data/tests/TestUndirectedGraph.rb +219 -0
  54. metadata +152 -0
@@ -0,0 +1,90 @@
1
+ #--
2
+ # Copyright (c) 2006 Shawn Patrick Garbett
3
+ # Copyright (c) 2002,2004,2005 by Horst Duchene
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without modification,
6
+ # are permitted provided that the following conditions are met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright notice(s),
9
+ # this list of conditions and the following disclaimer.
10
+ # * Redistributions in binary form must reproduce the above copyright notice,
11
+ # this list of conditions and the following disclaimer in the documentation
12
+ # and/or other materials provided with the distribution.
13
+ # * Neither the name of the Shawn Garbett nor the names of its contributors
14
+ # may be used to endorse or promote products derived from this software
15
+ # without specific prior written permission.
16
+ #
17
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ #++
28
+ #
29
+ # Minimal Dot support, based on Dave Thomas's dot module (included in rdoc).
30
+ # rdot.rb is a modified version which also contains support for undirected
31
+ # graphs.
32
+
33
+ require 'gratr/rdot'
34
+
35
+ module GRATR
36
+ module Graph
37
+
38
+ # Return a DOT::DOTDigraph for directed graphs or a DOT::DOTSubgraph for an
39
+ # undirected Graph. _params_ can contain any graph property specified in
40
+ # rdot.rb. If an edge or vertex label is a kind of Hash then the keys
41
+ # which match +dot+ properties will be used as well.
42
+ def to_dot_graph (params = {})
43
+ params['name'] ||= self.class.name.gsub(/:/,'_')
44
+ fontsize = params['fontsize'] ? params['fontsize'] : '8'
45
+ graph = (directed? ? DOT::DOTDigraph : DOT::DOTSubgraph).new(params)
46
+ edge_klass = directed? ? DOT::DOTDirectedArc : DOT::DOTArc
47
+ vertices.each do |v|
48
+ name = v.to_s
49
+ params = {'name' => '"'+name+'"',
50
+ 'fontsize' => fontsize,
51
+ 'label' => name}
52
+ v_label = vertex_label(v)
53
+ params.merge!(v_label) if v_label and v_label.kind_of? Hash
54
+ graph << DOT::DOTNode.new(params)
55
+ end
56
+ edges.each do |e|
57
+ params = {'from' => '"'+ e.source.to_s + '"',
58
+ 'to' => '"'+ e.target.to_s + '"',
59
+ 'fontsize' => fontsize }
60
+ e_label = edge_label(e)
61
+ params.merge!(e_label) if e_label and e_label.kind_of? Hash
62
+ graph << edge_klass.new(params)
63
+ end
64
+ graph
65
+ end
66
+
67
+ # Output the dot format as a string
68
+ def to_dot (params={}) to_dot_graph(params).to_s; end
69
+
70
+ # Call +dotty+ for the graph which is written to the file 'graph.dot'
71
+ # in the # current directory.
72
+ def dotty (params = {}, dotfile = 'graph.dot')
73
+ File.open(dotfile, 'w') {|f| f << to_dot(params) }
74
+ system('dotty', dotfile)
75
+ end
76
+
77
+ # Use +dot+ to create a graphical representation of the graph. Returns the
78
+ # filename of the graphics file.
79
+ def write_to_graphic_file (fmt='png', dotfile='graph')
80
+ src = dotfile + '.dot'
81
+ dot = dotfile + '.' + fmt
82
+
83
+ File.open(src, 'w') {|f| f << self.to_dot << "\n"}
84
+
85
+ system( "dot -T#{fmt} #{src} -o #{dot}" )
86
+ dot
87
+ end
88
+
89
+ end # module Graph
90
+ end # module GRATR
@@ -0,0 +1,145 @@
1
+ #--
2
+ # Copyright (c) 2006 Shawn Patrick Garbett
3
+ # Copyright (c) 2002,2004,2005 by Horst Duchene
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without modification,
6
+ # are permitted provided that the following conditions are met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright notice(s),
9
+ # this list of conditions and the following disclaimer.
10
+ # * Redistributions in binary form must reproduce the above copyright notice,
11
+ # this list of conditions and the following disclaimer in the documentation
12
+ # and/or other materials provided with the distribution.
13
+ # * Neither the name of the Shawn Garbett nor the names of its contributors
14
+ # may be used to endorse or promote products derived from this software
15
+ # without specific prior written permission.
16
+ #
17
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ #++
28
+
29
+
30
+ module GRATR
31
+
32
+ # Arc includes classes for representing egdes of directed and
33
+ # undirected graphs. There is no need for a Vertex class, because any ruby
34
+ # object can be a vertex of a graph.
35
+ #
36
+ # Arc's base is a Struct with a :source, a :target and a :label
37
+ Struct.new("ArcBase",:source, :target, :label)
38
+
39
+ class Arc < Struct::ArcBase
40
+
41
+ def initialize(p_source,p_target,p_label=nil)
42
+ super(p_source, p_target, p_label)
43
+ end
44
+
45
+ # Ignore labels for equality
46
+ def eql?(other) self.class == other.class and target==other.target and source==other.source; end
47
+
48
+ # Alias for eql?
49
+ alias == eql?
50
+
51
+ # Returns (v,u) if self == (u,v).
52
+ def reverse() self.class.new(target, source, label); end
53
+
54
+ # Sort support
55
+ def <=>(rhs) [source,target] <=> [rhs.source,rhs.target]; end
56
+
57
+ # Arc.new[1,2].to_s => "(1-2 'label')"
58
+ def to_s
59
+ l = label ? " '#{label.to_s}'" : ''
60
+ "(#{source}-#{target}#{l})"
61
+ end
62
+
63
+ # Hash is defined in such a way that label is not
64
+ # part of the hash value
65
+ def hash() source.hash ^ (target.hash+1); end
66
+
67
+ # Shortcut constructor. Instead of Arc.new(1,2) one can use Arc[1,2]
68
+ def self.[](p_source, p_target, p_label=nil)
69
+ new(p_source, p_target, p_label)
70
+ end
71
+
72
+ def inspect() "#{self.class.to_s}[#{source.inspect},#{target.inspect},#{label.inspect}]"; end
73
+
74
+ end
75
+
76
+ # An undirected edge is simply an undirected pair (source, target) used in
77
+ # undirected graphs. Edge[u,v] == Edge[v,u]
78
+ class Edge < Arc
79
+
80
+ # Equality allows for the swapping of source and target
81
+ def eql?(other) super or (self.class == other.class and target==other.source and source==other.target); end
82
+
83
+ # Alias for eql?
84
+ alias == eql?
85
+
86
+ # Hash is defined such that source and target can be reversed and the
87
+ # hash value will be the same
88
+ #
89
+ # This will cause problems with self loops
90
+ def hash() source.hash ^ target.hash; end
91
+
92
+ # Sort support
93
+ def <=>(rhs)
94
+ [[source,target].max,[source,target].min] <=>
95
+ [[rhs.source,rhs.target].max,[rhs.source,rhs.target].min]
96
+ end
97
+
98
+ # Edge[1,2].to_s == "(1=2 'label)"
99
+ def to_s
100
+ l = label ? " '#{label.to_s}'" : ''
101
+ s = source.to_s
102
+ t = target.to_s
103
+ "(#{[s,t].min}=#{[s,t].max}#{l})"
104
+ end
105
+
106
+ end
107
+
108
+ # This module provides for internal numbering of edges for differentiating between mutliple edges
109
+ module ArcNumber
110
+
111
+ attr_accessor :number # Used to differentiate between mutli-edges
112
+
113
+ def initialize(p_source,p_target,p_number,p_label=nil)
114
+ self.number = p_number
115
+ super(p_source, p_target, p_label)
116
+ end
117
+
118
+ # Returns (v,u) if self == (u,v).
119
+ def reverse() self.class.new(target, source, number, label); end
120
+ def hash() super ^ number.hash; end
121
+ def to_s() super + "[#{number}]"; end
122
+ def <=>(rhs) (result = super(rhs)) == 0 ? number <=> rhs.number : result; end
123
+ def inspect() "#{self.class.to_s}[#{source.inspect},#{target.inspect},#{number.inspect},#{label.inspect}]"; end
124
+ def eql?(rhs) super(rhs) and (rhs.number.nil? or number.nil? or number == rhs.number); end
125
+ def ==(rhs) eql?(rhs); end
126
+
127
+ # Shortcut constructor. Instead of Arc.new(1,2) one can use Arc[1,2]
128
+ def self.included(cl)
129
+
130
+ def cl.[](p_source, p_target, p_number=nil, p_label=nil)
131
+ new(p_source, p_target, p_number, p_label)
132
+ end
133
+ end
134
+
135
+ end
136
+
137
+ class MultiArc < Arc
138
+ include ArcNumber
139
+ end
140
+
141
+ class MultiEdge < Edge
142
+ include ArcNumber
143
+ end
144
+
145
+ end
@@ -0,0 +1,314 @@
1
+ #--
2
+ # Copyright (c) 2006 Shawn Patrick Garbett
3
+ # Copyright (c) 2002,2004,2005 by Horst Duchene
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without modification,
6
+ # are permitted provided that the following conditions are met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright notice(s),
9
+ # this list of conditions and the following disclaimer.
10
+ # * Redistributions in binary form must reproduce the above copyright notice,
11
+ # this list of conditions and the following disclaimer in the documentation
12
+ # and/or other materials provided with the distribution.
13
+ # * Neither the name of the Shawn Garbett nor the names of its contributors
14
+ # may be used to endorse or promote products derived from this software
15
+ # without specific prior written permission.
16
+ #
17
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ #++
28
+
29
+
30
+ require 'gratr/edge'
31
+ require 'gratr/labels'
32
+ require 'gratr/graph_api'
33
+
34
+ module GRATR
35
+ # Using the functions required by the GraphAPI, it implements all the
36
+ # basic functions of a Graph class by using only functions in GraphAPI.
37
+ # An actual implementation still needs to be done, as in Digraph or
38
+ # UndirectedGraph.
39
+ module Graph
40
+ include Enumerable
41
+ include Labels
42
+ include GraphAPI
43
+
44
+ alias remove_arc! remove_edge!
45
+ alias add_arc! add_edge!
46
+ alias arcs edges
47
+ alias arc_class edge_class
48
+
49
+ # Non destructive version of add_vertex!, returns modified copy of Graph
50
+ def add_vertex(v, l=nil) x=self.class.new(self); x.add_vertex!(v,l); end
51
+
52
+ # Non destructive version add_edge!, returns modified copy of Graph
53
+ def add_edge(u, v=nil, l=nil) x=self.class.new(self); x.add_edge!(u,v,l); end
54
+ alias add_arc add_edge
55
+
56
+ # Non destructive version of remove_vertex!, returns modified copy of Graph
57
+ def remove_vertex(v) x=self.class.new(self); x.remove_vertex!(v); end
58
+
59
+ # Non destructive version of remove_edge!, returns modified copy of Graph
60
+ def remove_edge(u,v=nil) x=self.class.new(self); x.remove_edge!(u,v); end
61
+ alias remove_arc remove_edge
62
+
63
+ # Return Array of adjacent portions of the Graph
64
+ # x can either be a vertex an edge.
65
+ # options specifies parameters about the adjacency search
66
+ # :type can be either :edges or :vertices (default).
67
+ # :direction can be :in, :out(default) or :all.
68
+ #
69
+ # Note: It is probably more efficently done in implementation.
70
+ def adjacent(x, options={})
71
+ d = directed? ? (options[:direction] || :out) : :all
72
+
73
+ # Discharge the easy ones first
74
+ return [x.source] if x.kind_of? Arc and options[:type] == :vertices and d == :in
75
+ return [x.target] if x.kind_of? Arc and options[:type] == :vertices and d == :out
76
+ return [x.source, x.target] if x.kind_of? Arc and options[:type] != :edges and d == :all
77
+
78
+ (options[:type] == :edges ? edges : to_a).select {|u| adjacent?(x,u,d)}
79
+ end
80
+
81
+ # Add all objects in _a_ to the vertex set.
82
+ def add_vertices!(*a) a.each {|v| add_vertex! v}; self; end
83
+
84
+ # See add_vertices!
85
+
86
+ def add_vertices(*a) x=self.class.new(self); x.add_vertices(*a); self; end
87
+
88
+ # Add all edges in the _edges_ Enumerable to the edge set. Elements of the
89
+ # Enumerable can be both two-element arrays or instances of DirectedArc or
90
+ # UnDirectedArc.
91
+ def add_edges!(*args) args.each { |edge| add_edge!(edge) }; self; end
92
+ alias add_arc! add_edges!
93
+
94
+ # See add_edge!
95
+ def add_edges(*a) x=self.class.new(self); x.add_edges!(*a); self; end
96
+ alias add_arcs add_edges
97
+
98
+ # Remove all vertices specified by the Enumerable a from the graph by
99
+ # calling remove_vertex!.
100
+ def remove_vertices!(*a) a.each { |v| remove_vertex! v }; end
101
+
102
+ # See remove_vertices!
103
+ def remove_vertices(*a) x=self.class.new(self); x.remove_vertices(*a); end
104
+
105
+ # Remove all vertices edges by the Enumerable a from the graph by
106
+ # calling remove_edge!
107
+ def remove_edges!(*a) a.each { |e| remove_edges! e }; end
108
+ alias remove_arc! remove_edges!
109
+
110
+ # See remove_edges
111
+ def remove_edges(*a) x=self.class.new(self); x.remove_edges(*a); end
112
+ alias remove_arcs remove_edges
113
+
114
+ # Execute given block for each vertex, provides for methods in Enumerable
115
+ def each(&block) vertices.each(&block); end
116
+
117
+ # Returns true if _v_ is a vertex of the graph.
118
+ # This is a default implementation that is of O(n) average complexity.
119
+ # If a subclass uses a hash to store vertices, then this can be
120
+ # made into an O(1) average complexity operation.
121
+ def vertex?(v) vertices.include?(v); end
122
+
123
+ # Returns true if u or (u,v) is an Arc of the graph.
124
+ def edge?(*arg) edges.include?(edge_convert(*args)); end
125
+ alias arc? edge?
126
+
127
+ # Tests two objects to see if they are adjacent.
128
+ # direction parameter specifies direction of adjacency, :in, :out, or :all(default)
129
+ # All denotes that if there is any adjacency, then it will return true.
130
+ # Note that the default is different than adjacent where one is primarily concerned with finding
131
+ # all adjacent objects in a graph to a given object. Here the concern is primarily on seeing
132
+ # if two objects touch. For vertexes, any edge between the two will usually do, but the direction
133
+ # can be specified if need be.
134
+ def adjacent?(source, target, direction=:all)
135
+ if source.kind_of? GRATR::Arc
136
+ raise NoArcError unless edge? source
137
+ if target.kind_of? GRATR::Arc
138
+ raise NoArcError unless edge? target
139
+ (direction != :out and source.source == target.target) or (direction != :in and source.target == target.source)
140
+ else
141
+ raise NoVertexError unless vertex? target
142
+ (direction != :out and source.source == target) or (direction != :in and source.target == target)
143
+ end
144
+ else
145
+ raise NoVertexError unless vertex? source
146
+ if target.kind_of? GRATR::Arc
147
+ raise NoArcError unless edge? target
148
+ (direction != :out and source == target.target) or (direction != :in and source == target.source)
149
+ else
150
+ raise NoVertexError unless vertex? target
151
+ (direction != :out and edge?(target,source)) or (direction != :in and edge?(source,target))
152
+ end
153
+ end
154
+ end
155
+
156
+ # Returns true if the graph has no vertex, i.e. num_vertices == 0.
157
+ def empty?() vertices.size.zero?; end
158
+
159
+ # Returns true if the given object is a vertex or Arc in the Graph.
160
+ #
161
+ def include?(x) x.kind_of?(GRATR::Arc) ? edge?(x) : vertex?(x); end
162
+
163
+ # Returns the neighboorhood of the given vertex (or Arc)
164
+ # This is equivalent to adjacent, but bases type on the type of object.
165
+ # direction can be :all, :in, or :out
166
+ def neighborhood(x, direction = :all)
167
+ adjacent(x, :direction => direction, :type => ((x.kind_of? GRATR::Arc) ? :edges : :vertices ))
168
+ end
169
+
170
+ # Union of all neighborhoods of vertices (or edges) in the Enumerable x minus the contents of x
171
+ # Definition taken from Jorgen Bang-Jensen, Gregory Gutin, _Digraphs: Theory, Algorithms and Applications_, pg 4
172
+ def set_neighborhood(x, direction = :all)
173
+ x.inject(Set.new) {|a,v| a.merge(neighborhood(v,direction))}.reject {|v2| x.include?(v2)}
174
+ end
175
+
176
+ # Union of all set_neighborhoods reachable in p edges
177
+ # Definition taken from Jorgen Bang-Jensen, Gregory Gutin, _Digraphs: Theory, Algorithms and Applications_, pg 46
178
+ def closed_pth_neighborhood(w,p,direction=:all)
179
+ if p <= 0
180
+ w
181
+ elsif p == 1
182
+ (w + set_neighborhood(w,direction)).uniq
183
+ else
184
+ n = set_neighborhood(w, direction)
185
+ (w + n + closed_pth_neighborhood(n,p-1,direction)).uniq
186
+ end
187
+ end
188
+
189
+ # Returns the neighboorhoods reachable in p steps from every vertex (or edge)
190
+ # in the Enumerable x
191
+ # Definition taken from Jorgen Bang-Jensen, Gregory Gutin, _Digraphs: Theory, Algorithms and Applications_, pg 46
192
+ def open_pth_neighborhood(x, p, direction=:all)
193
+ if p <= 0
194
+ x
195
+ elsif p == 1
196
+ set_neighborhood(x,direction)
197
+ else
198
+ set_neighborhood(open_pth_neighborhood(x, p-1, direction),direction) - closed_pth_neighborhood(x,p-1,direction)
199
+ end
200
+ end
201
+
202
+ # Returns the number of out-edges (for directed graphs) or the number of
203
+ # incident edges (for undirected graphs) of vertex _v_.
204
+ def out_degree(v) adjacent(v, :direction => :out).size; end
205
+
206
+ # Returns the number of in-edges (for directed graphs) or the number of
207
+ # incident edges (for undirected graphs) of vertex _v_.
208
+ def in_degree(v) adjacent(v, :direction => :in ).size; end
209
+
210
+ # Returns the sum of the number in and out edges for a vertex
211
+ def degree(v) in_degree(v) + out_degree(v); end
212
+
213
+ # Minimum in-degree
214
+ def min_in_degree() to_a.map {|v| in_degree(v)}.min; end
215
+
216
+ # Minimum out-degree
217
+ def min_out_degree() to_a.map {|v| out_degree(v)}.min; end
218
+
219
+ # Minimum degree of all vertexes
220
+ def min_degree() [min_in_degree, min_out_degree].min; end
221
+
222
+ # Maximum in-degree
223
+ def max_in_degree() vertices.map {|v| in_degree(v)}.max; end
224
+
225
+ # Maximum out-degree
226
+ def max_out_degree() vertices.map {|v| out_degree(v)}.max; end
227
+
228
+ # Minimum degree of all vertexes
229
+ def max_degree() [max_in_degree, max_out_degree].max; end
230
+
231
+ # Regular
232
+ def regular?() min_degree == max_degree; end
233
+
234
+ # Returns the number of vertices.
235
+ def size() vertices.size; end
236
+
237
+ # Synonym for size.
238
+ def num_vertices() vertices.size; end
239
+
240
+ # Returns the number of edges.
241
+ def num_edges() edges.size; end
242
+
243
+ # Utility method to show a string representation of the edges of the graph.
244
+ def to_s() edges.to_s; end
245
+
246
+ # Equality is defined to be same set of edges and directed?
247
+ def eql?(g)
248
+ return false unless g.kind_of? GRATR::Graph
249
+
250
+ (g.directed? == self.directed?) and
251
+ (vertices.sort == g.vertices.sort) and
252
+ (g.edges.sort == edges.sort)
253
+ end
254
+
255
+ # Synonym for eql?
256
+ def ==(rhs) eql?(rhs); end
257
+
258
+ # Merge another graph into this one
259
+ def merge(other)
260
+ other.vertices.each {|v| add_vertex!(v) }
261
+ other.edges.each {|e| add_edge!(e) }
262
+ other.edges.each {|e| add_edge!(e.reverse) } if directed? and !other.directed?
263
+ self
264
+ end
265
+
266
+ # A synonym for merge, that doesn't modify the current graph
267
+ def +(other)
268
+ result = self.class.new(self)
269
+ case other
270
+ when GRATR::Graph : result.merge(other)
271
+ when GRATR::Arc : result.add_edge!(other)
272
+ else result.add_vertex!(other)
273
+ end
274
+ end
275
+
276
+ # Remove all vertices in the specified right hand side graph
277
+ def -(other)
278
+ case other
279
+ when GRATR::Graph : induced_subgraph(vertices - other.vertices)
280
+ when GRATR::Arc : self.class.new(self).remove_edge!(other)
281
+ else self.class.new(self).remove_vertex!(other)
282
+ end
283
+ end
284
+
285
+ # A synonym for add_edge!
286
+ def <<(edge) add_edge!(edge); end
287
+
288
+ # Return the complement of the current graph
289
+ def complement
290
+ vertices.inject(self.class.new) do |a,v|
291
+ a.add_vertex!(v)
292
+ vertices.each {|v2| a.add_edge!(v,v2) unless edge?(v,v2) }; a
293
+ end
294
+ end
295
+
296
+ # Given an array of vertices return the induced subgraph
297
+ def induced_subgraph(v)
298
+ edges.inject(self.class.new) do |a,e|
299
+ ( v.include?(e.source) and v.include?(e.target) ) ? (a << e) : a
300
+ end;
301
+ end
302
+
303
+ def inspect
304
+ l = vertices.select {|v| self[v]}.map {|u| "vertex_label_set(#{u.inspect},#{self[u].inspect})"}.join('.')
305
+ self.class.to_s + '[' + edges.map {|e| e.inspect}.join(', ') + ']' + (l ? '.'+l : '')
306
+ end
307
+
308
+ private
309
+ def edge_convert(*args) args[0].kind_of?(GRATR::Arc) ? args[0] : edge_class[*args]; end
310
+
311
+
312
+ end # Graph
313
+
314
+ end # GRATR