Class: Dither::Cpp::Graph
- Inherits:
-
Object
- Object
- Dither::Cpp::Graph
- Defined in:
- lib/dither/chinese_postman_problem.rb
Defined Under Namespace
Classes: Edge
Instance Attribute Summary collapse
-
#c ⇒ Object
Returns the value of attribute c.
-
#cheapest_edge ⇒ Object
Returns the value of attribute cheapest_edge.
-
#defined ⇒ Object
Returns the value of attribute defined.
-
#degree ⇒ Object
Returns the value of attribute degree.
-
#edges ⇒ Object
Returns the value of attribute edges.
-
#f ⇒ Object
Returns the value of attribute f.
-
#initialized ⇒ Object
Returns the value of attribute initialized.
-
#label ⇒ Object
Returns the value of attribute label.
-
#n ⇒ Object
Returns the value of attribute n.
-
#neg ⇒ Object
Returns the value of attribute neg.
-
#origin ⇒ Object
Returns the value of attribute origin.
-
#path ⇒ Object
Returns the value of attribute path.
-
#pos ⇒ Object
Returns the value of attribute pos.
Class Method Summary collapse
Instance Method Summary collapse
- #add_edge(lab, u, v, cost) ⇒ Object
- #check_valid ⇒ Object
- #cpp ⇒ Object
- #find_feasible ⇒ Object
- #improvments ⇒ Object
-
#initialize(n) ⇒ Graph
constructor
A new instance of Graph.
- #initialized? ⇒ Boolean
- #least_cost_paths ⇒ Object
- #print(start) ⇒ Object
Constructor Details
#initialize(n) ⇒ Graph
Returns a new instance of Graph.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dither/chinese_postman_problem.rb', line 13 def initialize(n) @n = n @degree = Array.new(n).fill(0) @defined = Array.new(n).fill { |_| Array.new(n).fill(false) } @label = Array.new(n).fill { |_| Array.new(n).fill { |_| [] } } @c = Array.new(n).fill { |_| Array.new(n).fill(0.0) } @f = Array.new(n).fill { |_| Array.new(n).fill(0) } @edges = Array.new(n).fill { |_| Array.new(n).fill(0) } @cheapest_edge = Array.new(n).fill { |_| Array.new(n).fill(0) } @path = Array.new(n).fill { |_| Array.new(n).fill(0) } @initialized = true end |
Instance Attribute Details
#c ⇒ Object
Returns the value of attribute c.
9 10 11 |
# File 'lib/dither/chinese_postman_problem.rb', line 9 def c @c end |
#cheapest_edge ⇒ Object
Returns the value of attribute cheapest_edge.
9 10 11 |
# File 'lib/dither/chinese_postman_problem.rb', line 9 def cheapest_edge @cheapest_edge end |
#defined ⇒ Object
Returns the value of attribute defined.
9 10 11 |
# File 'lib/dither/chinese_postman_problem.rb', line 9 def defined @defined end |
#degree ⇒ Object
Returns the value of attribute degree.
9 10 11 |
# File 'lib/dither/chinese_postman_problem.rb', line 9 def degree @degree end |
#edges ⇒ Object
Returns the value of attribute edges.
9 10 11 |
# File 'lib/dither/chinese_postman_problem.rb', line 9 def edges @edges end |
#f ⇒ Object
Returns the value of attribute f.
9 10 11 |
# File 'lib/dither/chinese_postman_problem.rb', line 9 def f @f end |
#initialized ⇒ Object
Returns the value of attribute initialized.
9 10 11 |
# File 'lib/dither/chinese_postman_problem.rb', line 9 def initialized @initialized end |
#label ⇒ Object
Returns the value of attribute label.
9 10 11 |
# File 'lib/dither/chinese_postman_problem.rb', line 9 def label @label end |
#n ⇒ Object
Returns the value of attribute n.
9 10 11 |
# File 'lib/dither/chinese_postman_problem.rb', line 9 def n @n end |
#neg ⇒ Object
Returns the value of attribute neg.
9 10 11 |
# File 'lib/dither/chinese_postman_problem.rb', line 9 def neg @neg end |
#origin ⇒ Object
Returns the value of attribute origin.
9 10 11 |
# File 'lib/dither/chinese_postman_problem.rb', line 9 def origin @origin end |
#path ⇒ Object
Returns the value of attribute path.
9 10 11 |
# File 'lib/dither/chinese_postman_problem.rb', line 9 def path @path end |
#pos ⇒ Object
Returns the value of attribute pos.
9 10 11 |
# File 'lib/dither/chinese_postman_problem.rb', line 9 def pos @pos end |
Class Method Details
.create(raw_graph) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dither/chinese_postman_problem.rb', line 38 def self.create(raw_graph) vertices = [].to_set raw_graph[:edges].each do |edge| vertices << edge[:src_vertex] vertices << edge[:dst_vertex] end graph = Graph.new(vertices.length) raw_graph[:edges].each do |edge| graph.add_edge(edge[:name], edge[:src_vertex], edge[:dst_vertex], 1) end graph.origin = raw_graph[:origin] graph end |
Instance Method Details
#add_edge(lab, u, v, cost) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/dither/chinese_postman_problem.rb', line 54 def add_edge(lab, u, v, cost) @label[u][v] = [] unless defined[u][v] @label[u][v] << lab if !defined[u][v] || c[u][v] > cost @c[u][v] = cost @cheapest_edge[u][v] = edges[u][v] @defined[u][v] = true @path[u][v] = v end @edges[u][v] += 1 @degree[u] += 1 @degree[v] -= 1 self end |
#check_valid ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/dither/chinese_postman_problem.rb', line 69 def check_valid (0...n).each do |i| raise Dither::Error, 'negative cycle' if c[i][i] < 0 (0...n).each do |j| raise Dither::Error, 'not strongly connected' unless defined[i][j] end end end |
#cpp ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/dither/chinese_postman_problem.rb', line 28 def cpp initialized? least_cost_paths check_valid find_feasible while improvments do end print(origin) end |
#find_feasible ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/dither/chinese_postman_problem.rb', line 96 def find_feasible nn, np = 0, 0 (0...n).each do |i| if degree[i] < 0 nn += 1 elsif degree[i] > 0 np += 1 end end @neg = Array.new(nn) @pos = Array.new(np) nn, np = 0, 0 (0...n).each do |i| if degree[i] < 0 @neg[nn] = i nn += 1 elsif degree[i] > 0 @pos[np] = i np += 1 end end (0...nn).each do |u| i = neg[u] (0...np).each do |v| j = pos[v] @f[i][j] = -degree[i] < degree[j] ? -degree[i] : degree[j] @degree[i] += f[i][j] @degree[j] -= f[i][j] end end end |
#improvments ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/dither/chinese_postman_problem.rb', line 130 def improvments r = Graph.new(n) (0...neg.length).each do |u| i = neg[u] (0...pos.length).each do |v| j = pos[v] if edges[i][j] > 0 r.add_edge(nil, i, j, c[i][j]) end if f[i][j] != 0 r.add_edge(nil, j, i, -c[i][j]) end end end r.least_cost_paths (0...n).each do |i| if r.c[i][i] < 0 k = 0 kunset = true u = i begin v = r.path[u][i] if r.c[u][v] < 0 && (kunset || k > f[v][u]) k = f[v][u] kunset = false end u = v end while u != i u = i begin v = r.path[u][i] if r.c[u][v] < 0 @f[v][u] -= k else @f[u][v] += k end u = v end while u != i return true end end false end |
#initialized? ⇒ Boolean
209 210 211 212 213 |
# File 'lib/dither/chinese_postman_problem.rb', line 209 def initialized? raise Dither::Error, 'graph not initialized' unless initialized raise Dither::Error, 'origin not set' unless origin @initialized = false end |
#least_cost_paths ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/dither/chinese_postman_problem.rb', line 78 def least_cost_paths (0...n).each do |k| (0...n).each do |i| if defined[i][k] (0...n).each do |j| if defined[k][j] && (!defined[i][j] || c[i][j] > c[i][k]+c[k][j]) @defined[i][j] = true @path[i][j] = path[i][k] @c[i][j] = c[i][k] + c[k][j] # negative cycle return if i == j && c[i][j] < 0 end end end end end end |
#print(start) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/dither/chinese_postman_problem.rb', line 175 def print(start) result = [] v = start loop do skip = false u = v (0...n).each do |i| if f[u][i] > 0 v = i @f[u][v] -= 1 while u != v p = path[u][v] result << Edge.new(label[u][p][cheapest_edge[u][p]], u, p) u = p end skip = true end end next if skip v = -1 (0...n).each do |i| if edges[u][i] > 0 v = i if v == -1 || i != path[u][start] end end return result if v == -1 result << Edge.new(label[u][v][edges[u][v] - 1], u, v) @edges[u][v] -= 1 end result end |