gratr 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/Grater.xcf +0 -0
  2. data/README +328 -0
  3. data/Rakefile +220 -0
  4. data/examples/graph_self.rb +54 -0
  5. data/examples/module_graph.jpg +0 -0
  6. data/examples/module_graph.rb +12 -0
  7. data/examples/self_graph.jpg +0 -0
  8. data/examples/visualize.jpg +0 -0
  9. data/examples/visualize.rb +8 -0
  10. data/install.rb +49 -0
  11. data/lib/gratr.rb +33 -0
  12. data/lib/gratr/adjacency_graph.rb +230 -0
  13. data/lib/gratr/base.rb +34 -0
  14. data/lib/gratr/biconnected.rb +116 -0
  15. data/lib/gratr/chinese_postman.rb +123 -0
  16. data/lib/gratr/common.rb +73 -0
  17. data/lib/gratr/comparability.rb +92 -0
  18. data/lib/gratr/digraph.rb +113 -0
  19. data/lib/gratr/digraph_distance.rb +185 -0
  20. data/lib/gratr/dot.rb +90 -0
  21. data/lib/gratr/edge.rb +145 -0
  22. data/lib/gratr/graph.rb +315 -0
  23. data/lib/gratr/graph_api.rb +82 -0
  24. data/lib/gratr/import.rb +44 -0
  25. data/lib/gratr/labels.rb +103 -0
  26. data/lib/gratr/maximum_flow.rb +107 -0
  27. data/lib/gratr/rdot.rb +326 -0
  28. data/lib/gratr/search.rb +409 -0
  29. data/lib/gratr/strong_components.rb +127 -0
  30. data/lib/gratr/undirected_graph.rb +153 -0
  31. data/tests/TestBiconnected.rb +53 -0
  32. data/tests/TestChinesePostman.rb +53 -0
  33. data/tests/TestComplement.rb +54 -0
  34. data/tests/TestDigraph.rb +333 -0
  35. data/tests/TestDigraphDistance.rb +138 -0
  36. data/tests/TestEdge.rb +171 -0
  37. data/tests/TestInspection.rb +57 -0
  38. data/tests/TestMultiEdge.rb +57 -0
  39. data/tests/TestNeighborhood.rb +64 -0
  40. data/tests/TestProperties.rb +160 -0
  41. data/tests/TestSearch.rb +257 -0
  42. data/tests/TestStrongComponents.rb +85 -0
  43. data/tests/TestTriagulated.rb +137 -0
  44. data/tests/TestUndirectedGraph.rb +219 -0
  45. metadata +92 -0
@@ -0,0 +1,123 @@
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/digraph_distance'
30
+
31
+ module GRATR
32
+ module Graph
33
+ module ChinesePostman
34
+
35
+ # Returns the shortest walk that traverses all arcs at least
36
+ # once, returning to the specified start node.
37
+ def closed_chinese_postman_tour(start, weight=nil, zero=0)
38
+ cost, path, delta = floyd_warshall(weight, zero)
39
+ return nil unless cp_valid_least_cost? cost, zero
40
+ positive, negative = cp_unbalanced(delta)
41
+ f = cp_find_feasible(delta, positive, negative, zero)
42
+ while cp_improve(f, positive, negative, cost, zero); end
43
+ cp_euler_circuit(start, f, path)
44
+ end
45
+
46
+ private
47
+
48
+ def cp_euler_circuit(start, f, path) # :nodoc:
49
+ circuit = [u=v=start]
50
+ bridge_taken = Hash.new {|h,k| h[k] = Hash.new}
51
+ until v.nil?
52
+ if v=f[u].keys.detect {|k| f[u][k] > 0}
53
+ f[u][v] -= 1
54
+ circuit << (u = path[u][v]) while u != v
55
+ else
56
+ unless bridge_taken[u][bridge = path[u][start]]
57
+ v = vertices.detect {|v1| v1 != bridge && edge?(u,v1) && !bridge_taken[u][v1]} || bridge
58
+ bridge_taken[u][v] = true
59
+ circuit << v
60
+ end
61
+ end
62
+ u=v
63
+ end; circuit
64
+ end
65
+
66
+ def cp_cancel_cycle(cost, path, f, start, zero) # :nodoc:
67
+ u = start; k = nil
68
+ begin
69
+ v = path[u][start]
70
+ k = f[v][u] if cost[u][v] < zero and (k.nil? || k > f[v][u])
71
+ end until (u=v) != start
72
+ u = start
73
+ begin
74
+ v = path[u][start]
75
+ cost[u][v] < zero ? f[v][u] -= k : f[u][v] += k
76
+ end until (u=v) != start
77
+ true # This routine always returns true to make cp_improve easier
78
+ end
79
+
80
+ def cp_improve(f, positive, negative, cost, zero) # :nodoc:
81
+ residual = self.class.new
82
+ negative.each do |u|
83
+ positive.each do |v|
84
+ residual.add_edge!(u,v,cost[u][v])
85
+ residual.add_edge!(v,u,-cost[u][v]) if f[u][v] != 0
86
+ end
87
+ end
88
+ r_cost, r_path, r_delta = residual.floyd_warshall(nil, zero)
89
+ i = residual.vertices.detect {|v| r_cost[v][v] and r_cost[v][v] < zero}
90
+ i ? cp_cancel_cycle(r_cost, r_path, f, i) : false
91
+ end
92
+
93
+ def cp_find_feasible(delta, positive, negative, zero) # :nodoc:
94
+ f = Hash.new {|h,k| h[k] = Hash.new}
95
+ negative.each do |i|
96
+ positive.each do |j|
97
+ f[i][j] = -delta[i] < delta[j] ? -delta[i] : delta[j]
98
+ delta[i] += f[i][j]
99
+ delta[j] -= f[i][j]
100
+ end
101
+ end; f
102
+ end
103
+
104
+ def cp_valid_least_cost?(c, zero) # :nodoc:
105
+ vertices.each do |i|
106
+ vertices.each do |j|
107
+ return false unless c[i][j] and c[i][j] >= zero
108
+ end
109
+ end; true
110
+ end
111
+
112
+ def cp_unbalanced(delta) # :nodoc:
113
+ negative = []; positive = []
114
+ vertices.each do |v|
115
+ negative << v if delta[v] < 0
116
+ positive << v if delta[v] > 0
117
+ end; [positive, negative]
118
+ end
119
+
120
+ end # Chinese Postman
121
+ end # Graph
122
+ end # GRATR
123
+
@@ -0,0 +1,73 @@
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/edge'
30
+ require 'gratr/graph'
31
+
32
+ module GRATR
33
+ # This class defines a cycle graph of size n
34
+ # This is easily done by using the base Graph
35
+ # class and implemeting the minimum methods needed to
36
+ # make it work. This is a good example to look
37
+ # at for making one's own graph classes
38
+ class Cycle
39
+
40
+ def initialize(n) @size = n; end
41
+ def directed?() false; end
42
+ def vertices() (1..@size).to_a; end
43
+ def vertex?(v) v > 0 and v <= @size; end
44
+ def edge?(u,v=nil)
45
+ u, v = [u.source, v.target] if u.kind_of? GRATR::Arc
46
+ vertex?(u) && vertex?(v) && ((v-u == 1) or (u==@size && v=1))
47
+ end
48
+ def edges() Array.new(@size) {|i| GRATR::Edge[i+1, (i+1)==@size ? 1 : i+2]}; end
49
+ end
50
+
51
+ # This class defines a complete graph of size n
52
+ # This is easily done by using the base Graph
53
+ # class and implemeting the minimum methods needed to
54
+ # make it work. This is a good example to look
55
+ # at for making one's own graph classes
56
+ class Complete < Cycle
57
+ def initialize(n) @size = n; @edges = nil; end
58
+ def edges
59
+ return @edges if @edges # Cache edges
60
+ @edges = []
61
+ @size.times do |u|
62
+ @size.times {|v| @edges << GRATR::Edge[u+1, v+1]}
63
+ end; @edges
64
+ end
65
+ def edge?(u,v=nil)
66
+ u, v = [u.source, v.target] if u.kind_of? GRATR::Arc
67
+ vertex?(u) && vertex?(v)
68
+ end
69
+ end # Complete
70
+
71
+
72
+
73
+ 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,113 @@
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
+ return new(self) unless directed?
71
+ edges.inject(self.class.new) {|a,e| a << e.reverse}
72
+ end
73
+
74
+ # Return true if the Graph is oriented.
75
+ def oriented?
76
+ e = edges
77
+ re = e.map {|x| x.reverse}
78
+ not e.any? {|x| re.include?(x)}
79
+ end
80
+
81
+ # Balanced is when the out edge count is equal to the in edge count
82
+ def balanced?(v) out_degree(v) == in_degree(v); end
83
+
84
+ # Returns out_degree(v) - in_degree(v)
85
+ def delta(v) out_degree(v) - in_degree(v); end
86
+
87
+ end
88
+
89
+ # DirectedGraph is just an alias for Digraph should one desire
90
+ DirectedGraph = Digraph
91
+
92
+ # This is a Digraph that allows for parallel edges, but does not
93
+ # allow loops
94
+ class DirectedPseudoGraph < Digraph
95
+ def initialize(*params)
96
+ raise ArgumentError if params.any? do |p|
97
+ !(p.kind_of? GRATR::Graph or p.kind_of? Array)
98
+ end
99
+ super(:parallel_edges, *params)
100
+ end
101
+ end
102
+
103
+ # This is a Digraph that allows for parallel edges and loops
104
+ class DirectedMultiGraph < Digraph
105
+ def initialize(*params)
106
+ raise ArgumentError if params.any? do |p|
107
+ !(p.kind_of? GRATR::Graph or p.kind_of? Array)
108
+ end
109
+ super(:parallel_edges, :loops, *params)
110
+ end
111
+ end
112
+
113
+ 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