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,74 @@
1
+ #--
2
+ # Copyright (c) 2006 Shawn Patrick Garbett
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without modification,
5
+ # are permitted provided that the following conditions are met:
6
+ #
7
+ # * Redistributions of source code must retain the above copyright notice(s),
8
+ # this list of conditions and the following disclaimer.
9
+ # * Redistributions in binary form must reproduce the above copyright notice,
10
+ # this list of conditions and the following disclaimer in the documentation
11
+ # and/or other materials provided with the distribution.
12
+ # * Neither the name of the Shawn Garbett nor the names of its contributors
13
+ # may be used to endorse or promote products derived from this software
14
+ # without specific prior written permission.
15
+ #
16
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ #++
27
+
28
+
29
+ require 'gratr/version'
30
+ require 'gratr/edge'
31
+ require 'gratr/graph'
32
+
33
+ module GRATR
34
+ # This class defines a cycle graph of size n
35
+ # This is easily done by using the base Graph
36
+ # class and implemeting the minimum methods needed to
37
+ # make it work. This is a good example to look
38
+ # at for making one's own graph classes
39
+ class Cycle
40
+
41
+ def initialize(n) @size = n; end
42
+ def directed?() false; end
43
+ def vertices() (1..@size).to_a; end
44
+ def vertex?(v) v > 0 and v <= @size; end
45
+ def edge?(u,v=nil)
46
+ u, v = [u.source, v.target] if u.kind_of? GRATR::Arc
47
+ vertex?(u) && vertex?(v) && ((v-u == 1) or (u==@size && v=1))
48
+ end
49
+ def edges() Array.new(@size) {|i| GRATR::Edge[i+1, (i+1)==@size ? 1 : i+2]}; end
50
+ end
51
+
52
+ # This class defines a complete graph of size n
53
+ # This is easily done by using the base Graph
54
+ # class and implemeting the minimum methods needed to
55
+ # make it work. This is a good example to look
56
+ # at for making one's own graph classes
57
+ class Complete < Cycle
58
+ def initialize(n) @size = n; @edges = nil; end
59
+ def edges
60
+ return @edges if @edges # Cache edges
61
+ @edges = []
62
+ @size.times do |u|
63
+ @size.times {|v| @edges << GRATR::Edge[u+1, v+1]}
64
+ end; @edges
65
+ end
66
+ def edge?(u,v=nil)
67
+ u, v = [u.source, v.target] if u.kind_of? GRATR::Arc
68
+ vertex?(u) && vertex?(v)
69
+ end
70
+ end # Complete
71
+
72
+
73
+
74
+ end # GRATR
@@ -0,0 +1,92 @@
1
+ #--
2
+ # Copyright (c) 2006 Shawn Patrick Garbett
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without modification,
5
+ # are permitted provided that the following conditions are met:
6
+ #
7
+ # * Redistributions of source code must retain the above copyright notice(s),
8
+ # this list of conditions and the following disclaimer.
9
+ # * Redistributions in binary form must reproduce the above copyright notice,
10
+ # this list of conditions and the following disclaimer in the documentation
11
+ # and/or other materials provided with the distribution.
12
+ # * Neither the name of the Shawn Garbett nor the names of its contributors
13
+ # may be used to endorse or promote products derived from this software
14
+ # without specific prior written permission.
15
+ #
16
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ #++
27
+
28
+ module GRATR
29
+ module Graph
30
+ module Comparability
31
+
32
+ # A comparability graph is an UndirectedGraph that has a transitive
33
+ # orientation. This returns a boolean that says if this graph
34
+ # is a comparability graph.
35
+ def comparability?() gamma_decomposition[1]; end
36
+
37
+ # Returns an array with two values, the first being a hash of edges
38
+ # with a number containing their class assignment, the second valud
39
+ # is a boolean which states whether or not the graph is a
40
+ # comparability graph
41
+ #
42
+ # Complexity in time O(d*|E|) where d is the maximum degree of a vertex
43
+ # Complexity in space O(|V|+|E|)
44
+ def gamma_decomposition
45
+ k = 0; comparability=true; classification={}
46
+ edges.map {|edge| [edge.source,edge.target]}.each do |e|
47
+ if classification[e].nil?
48
+ k += 1
49
+ classification[e] = k; classification[e.reverse] = -k
50
+ comparability &&= gratr_comparability_explore(e, k, classification)
51
+ end
52
+ end; [classification, comparability]
53
+ end
54
+
55
+ # Returns one of the possible transitive orientations of
56
+ # the UndirectedGraph as a Digraph
57
+ def transitive_orientation(digraph_class=Digraph)
58
+ raise NotImplementError
59
+ end
60
+
61
+ private
62
+
63
+ # Taken from Figure 5.10, on pg. 130 of Martin Golumbic's, _Algorithmic_Graph_
64
+ # _Theory_and_Perfect_Graphs.
65
+ def gratr_comparability_explore(edge, k, classification, space='')
66
+ ret = gratr_comparability_explore_inner(edge, k, classification, :forward, space)
67
+ gratr_comparability_explore_inner(edge.reverse, k, classification, :backward, space) && ret
68
+ end
69
+
70
+ def gratr_comparability_explore_inner(edge, k, classification, direction,space)
71
+ comparability = true
72
+ adj_target = adjacent(edge[1])
73
+ adjacent(edge[0]).select do |mt|
74
+ (classification[[edge[1],mt]] || k).abs < k or
75
+ not adj_target.any? {|adj_t| adj_t == mt}
76
+ end.each do |m|
77
+ e = (direction == :forward) ? [edge[0], m] : [m,edge[0]]
78
+ if classification[e].nil?
79
+ classification[e] = k
80
+ classification[e.reverse] = -k
81
+ comparability = gratr_comparability_explore(e, k, classification, ' '+space) && comparability
82
+ elsif classification[e] == -k
83
+ classification[e] = k
84
+ gratr_comparability_explore(e, k, classification, ' '+space)
85
+ comparability = false
86
+ end
87
+ end; comparability
88
+ end # gratr_comparability_explore_inner
89
+
90
+ end # Comparability
91
+ end # Graph
92
+ end # GRATR
@@ -0,0 +1,115 @@
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
+ require 'gratr/edge'
30
+ require 'gratr/graph'
31
+ require 'gratr/search'
32
+ require 'gratr/adjacency_graph'
33
+ require 'gratr/strong_components'
34
+ require 'gratr/digraph_distance'
35
+ require 'gratr/chinese_postman'
36
+
37
+ module GRATR
38
+ #
39
+ # Digraph is a directed graph which is a finite set of vertices
40
+ # and a finite set of edges connecting vertices. It cannot contain parallel
41
+ # edges going from the same source vertex to the same target. It also
42
+ # cannot contain loops, i.e. edges that go have the same vertex for source
43
+ # and target.
44
+ #
45
+ # DirectedPseudoGraph is a class that allows for parallel edges, and a
46
+ # DirectedMultiGraph is a class that allows for parallel edges and loops
47
+ # as well.
48
+ class Digraph
49
+ include AdjacencyGraph
50
+ include Graph::Search
51
+ include Graph::StrongComponents
52
+ include Graph::Distance
53
+ include Graph::ChinesePostman
54
+
55
+ def initialize(*params)
56
+ raise ArgumentError if params.any? do |p|
57
+ !(p.kind_of? GRATR::Graph or p.kind_of? Array)
58
+ end if self.class == GRATR::Digraph
59
+ super(*params)
60
+ end
61
+
62
+ # A directed graph is directed by definition
63
+ def directed?() true; end
64
+
65
+ # A digraph uses the Arc class for edges
66
+ def edge_class() @parallel_edges ? GRATR::MultiArc : GRATR::Arc; end
67
+
68
+ # Reverse all edges in a graph
69
+ def reversal
70
+ result = self.class.new
71
+ edges.inject(result) {|a,e| a << e.reverse}
72
+ vertices.each { |v| result.add_vertex!(v) unless result.vertex?(v) }
73
+ result
74
+ end
75
+
76
+ # Return true if the Graph is oriented.
77
+ def oriented?
78
+ e = edges
79
+ re = e.map {|x| x.reverse}
80
+ not e.any? {|x| re.include?(x)}
81
+ end
82
+
83
+ # Balanced is when the out edge count is equal to the in edge count
84
+ def balanced?(v) out_degree(v) == in_degree(v); end
85
+
86
+ # Returns out_degree(v) - in_degree(v)
87
+ def delta(v) out_degree(v) - in_degree(v); end
88
+
89
+ end
90
+
91
+ # DirectedGraph is just an alias for Digraph should one desire
92
+ DirectedGraph = Digraph
93
+
94
+ # This is a Digraph that allows for parallel edges, but does not
95
+ # allow loops
96
+ class DirectedPseudoGraph < Digraph
97
+ def initialize(*params)
98
+ raise ArgumentError if params.any? do |p|
99
+ !(p.kind_of? GRATR::Graph or p.kind_of? Array)
100
+ end
101
+ super(:parallel_edges, *params)
102
+ end
103
+ end
104
+
105
+ # This is a Digraph that allows for parallel edges and loops
106
+ class DirectedMultiGraph < Digraph
107
+ def initialize(*params)
108
+ raise ArgumentError if params.any? do |p|
109
+ !(p.kind_of? GRATR::Graph or p.kind_of? Array)
110
+ end
111
+ super(:parallel_edges, :loops, *params)
112
+ end
113
+ end
114
+
115
+ end
@@ -0,0 +1,185 @@
1
+ #--
2
+ # Copyright (c) 2006 Shawn Patrick Garbett
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without modification,
5
+ # are permitted provided that the following conditions are met:
6
+ #
7
+ # * Redistributions of source code must retain the above copyright notice(s),
8
+ # this list of conditions and the following disclaimer.
9
+ # * Redistributions in binary form must reproduce the above copyright notice,
10
+ # this list of conditions and the following disclaimer in the documentation
11
+ # and/or other materials provided with the distribution.
12
+ # * Neither the name of the Shawn Garbett nor the names of its contributors
13
+ # may be used to endorse or promote products derived from this software
14
+ # without specific prior written permission.
15
+ #
16
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ #++
27
+
28
+ module GRATR
29
+ module Graph
30
+ module Distance
31
+
32
+ # Shortest path from Jorgen Band-Jensen and Gregory Gutin,
33
+ # _DIGRAPHS:_Theory,_Algorithms_and_Applications, pg 53-54
34
+ #
35
+ # Requires that the graph be acyclic. If the graph is not
36
+ # acyclic, then see dijkstras_algorithm or bellman_ford_moore
37
+ # for possible solutions.
38
+ #
39
+ # start is the starting vertex
40
+ # weight can be a Proc, or anything else is accessed using the [] for the
41
+ # the label or it defaults to using
42
+ # the value stored in the label for the Arc. If it is a Proc it will
43
+ # pass the edge to the proc and use the resulting value.
44
+ # zero is used for math system with a different definition of zero
45
+ #
46
+ # Returns a hash with the key being a vertex and the value being the
47
+ # distance. A missing vertex from the hash is an infinite distance
48
+ #
49
+ # Complexity O(n+m)
50
+ def shortest_path(start, weight=nil, zero=0)
51
+ dist = {start => zero}; path = {}
52
+ topsort(start) do |vi|
53
+ next if vi == start
54
+ dist[vi],path[vi] = adjacent(vi, :direction => :in).map do |vj|
55
+ [dist[vj] + cost(vj,vi,weight), vj]
56
+ end.min {|a,b| a[0] <=> b[0]}
57
+ end;
58
+ dist.keys.size == vertices.size ? [dist,path] : nil
59
+ end # shortest_path
60
+
61
+ # Algorithm from Jorgen Band-Jensen and Gregory Gutin,
62
+ # _DIGRAPHS:_Theory,_Algorithms_and_Applications, pg 53-54
63
+ #
64
+ # Finds the distances from a given vertex s in a weighted digraph
65
+ # to the rest of the vertices, provided all the weights of arcs
66
+ # are non-negative. If negative arcs exist in the graph, two
67
+ # basic options exist, 1) modify all weights to be positive by
68
+ # using an offset, or 2) use the bellman_ford_moore algorithm.
69
+ # Also if the graph is acyclic, use the shortest_path algorithm.
70
+ #
71
+ # weight can be a Proc, or anything else is accessed using the [] for the
72
+ # the label or it defaults to using
73
+ # the value stored in the label for the Arc. If it is a Proc it will
74
+ # pass the edge to the proc and use the resulting value.
75
+ #
76
+ # zero is used for math system with a different definition of zero
77
+ #
78
+ # Returns a hash with the key being a vertex and the value being the
79
+ # distance. A missing vertex from the hash is an infinite distance
80
+ #
81
+ # O(n*log(n) + m) complexity
82
+ def dijkstras_algorithm(s, weight = nil, zero = 0)
83
+ q = vertices; distance = { s => zero }; path = {}
84
+ while not q.empty?
85
+ v = (q & distance.keys).inject(nil) {|a,k| (!a.nil?) && (distance[a] < distance[k]) ? a : k}
86
+ q.delete(v)
87
+ (q & adjacent(v)).each do |u|
88
+ c = cost(v,u,weight)
89
+ if distance[u].nil? or distance[u] > (c+distance[v])
90
+ distance[u] = c + distance[v]
91
+ path[u] = v
92
+ end
93
+ end
94
+ end; [distance, path]
95
+ end # dijkstras_algorithm
96
+
97
+ # Algorithm from Jorgen Band-Jensen and Gregory Gutin,
98
+ # _DIGRAPHS:_Theory,_Algorithms_and_Applications, pg 56-58
99
+ #
100
+ # Finds the distances from a given vertex s in a weighted digraph
101
+ # to the rest of the vertices, provided the graph has no negative cycle.
102
+ # If no negative weights exist, then dijkstras_algorithm is more
103
+ # efficient in time and space. Also if the graph is acyclic, use the
104
+ # shortest_path algorithm.
105
+ #
106
+ # weight can be a Proc, or anything else is accessed using the [] for the
107
+ # the label or it defaults to using
108
+ # the value stored in the label for the Arc. If it is a Proc it will
109
+ # pass the edge to the proc and use the resulting value.
110
+ #
111
+ # zero is used for math system with a different definition of zero
112
+ #
113
+ # Returns a hash with the key being a vertex and the value being the
114
+ # distance. A missing vertex from the hash is an infinite distance
115
+ #
116
+ # O(nm) complexity
117
+ def bellman_ford_moore(start, weight = nil, zero = 0)
118
+ distance = { start => zero }; path = {}
119
+ 2.upto(vertices.size) do
120
+ edges.each do |e|
121
+ u,v = e[0],e[1]
122
+ unless distance[u].nil?
123
+ c = cost(u, v, weight)+distance[u]
124
+ if distance[v].nil? or c < distance[v]
125
+ distance[v] = c
126
+ path[v] = u
127
+ end
128
+ end
129
+ end
130
+ end; [distance, path]
131
+ end # bellman_ford_moore
132
+
133
+ # This uses the Floyd-Warshall algorithm to efficiently find
134
+ # and record shortest paths at the same time as establishing
135
+ # the costs for all vertices in a graph.
136
+ # See, S.Skiena, "The Algorithm Design Manual",
137
+ # Springer Verlag, 1998 for more details.
138
+ #
139
+ # Returns a pair of matrices and a hash of delta values.
140
+ # The matrices will be indexed by two vertices and are
141
+ # implemented as a Hash of Hashes. The first matrix is the cost, the second
142
+ # matrix is the shortest path spanning tree. The delta (difference of number
143
+ # of in edges and out edges) is indexed by vertex.
144
+ #
145
+ # weight specifies how an edge weight is determined, if it's a
146
+ # Proc the Arc is passed to it, if it's nil it will just use
147
+ # the value in the label for the Arc, otherwise the weight is
148
+ # determined by applying the [] operator to the value in the
149
+ # label for the Arc
150
+ #
151
+ # zero defines the zero value in the math system used. Defaults
152
+ # of course, to 0. This allows for no assumptions to be made
153
+ # about the math system and fully functional duck typing.
154
+ #
155
+ # O(n^3) complexity in time.
156
+ def floyd_warshall(weight=nil, zero=0)
157
+ c = Hash.new {|h,k| h[k] = Hash.new}
158
+ path = Hash.new {|h,k| h[k] = Hash.new}
159
+ delta = Hash.new {|h,k| h[k] = 0}
160
+ edges.each do |e|
161
+ delta[e.source] += 1
162
+ delta[e.target] -= 1
163
+ path[e.source][e.target] = e.target
164
+ c[e.source][e.target] = cost(e, weight)
165
+ end
166
+ vertices.each do |k|
167
+ vertices.each do |i|
168
+ if c[i][k]
169
+ vertices.each do |j|
170
+ if c[k][j] &&
171
+ (c[i][j].nil? or c[i][j] > (c[i][k] + c[k][j]))
172
+ path[i][j] = path[i][k]
173
+ c[i][j] = c[i][k] + c[k][j]
174
+ return nil if i == j and c[i][j] < zero
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
180
+ [c, path, delta]
181
+ end # floyd_warshall
182
+
183
+ end # Distance
184
+ end # Graph
185
+ end # GRATR