Class: Dither::Cpp::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/dither/chinese_postman_problem.rb

Defined Under Namespace

Classes: Edge

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#cObject

Returns the value of attribute c.



9
10
11
# File 'lib/dither/chinese_postman_problem.rb', line 9

def c
  @c
end

#cheapest_edgeObject

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

#definedObject

Returns the value of attribute defined.



9
10
11
# File 'lib/dither/chinese_postman_problem.rb', line 9

def defined
  @defined
end

#degreeObject

Returns the value of attribute degree.



9
10
11
# File 'lib/dither/chinese_postman_problem.rb', line 9

def degree
  @degree
end

#edgesObject

Returns the value of attribute edges.



9
10
11
# File 'lib/dither/chinese_postman_problem.rb', line 9

def edges
  @edges
end

#fObject

Returns the value of attribute f.



9
10
11
# File 'lib/dither/chinese_postman_problem.rb', line 9

def f
  @f
end

#initializedObject

Returns the value of attribute initialized.



9
10
11
# File 'lib/dither/chinese_postman_problem.rb', line 9

def initialized
  @initialized
end

#labelObject

Returns the value of attribute label.



9
10
11
# File 'lib/dither/chinese_postman_problem.rb', line 9

def label
  @label
end

#nObject

Returns the value of attribute n.



9
10
11
# File 'lib/dither/chinese_postman_problem.rb', line 9

def n
  @n
end

#negObject

Returns the value of attribute neg.



9
10
11
# File 'lib/dither/chinese_postman_problem.rb', line 9

def neg
  @neg
end

#originObject

Returns the value of attribute origin.



9
10
11
# File 'lib/dither/chinese_postman_problem.rb', line 9

def origin
  @origin
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/dither/chinese_postman_problem.rb', line 9

def path
  @path
end

#posObject

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_validObject



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

#cppObject



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_feasibleObject



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

#improvmentsObject



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

Returns:

  • (Boolean)

Raises:



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_pathsObject



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


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