summaryrefslogtreecommitdiffstats
path: root/02-graph-coloring/CP-solver.rb
blob: c157c076633d1f7e25badcdd62b143142c468a22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
129
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
174
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#! /usr/bin/ruby

class Node
   #
   def initialize i, a
      @i = i      # index
      @a = [a]    # adjacents
      @c = nil    # colour
      @f = 0      # finished adjacents
      @l = nil
   end
   #
   attr_reader :i, :a
   attr_accessor :c, :f
   #
   def add a
      @a << a
   end
   #
   def to_s
      "#{@i} #{@c.to_s} " + @a.to_s
   end
   #
   def l
      return @l if not @l.nil?
      @l = @a.length
   end
   #
   def blocked?
      (l == @f)
   end
   #
end

class Step
   #
   def initialize n, c
      @n = n   # nodes
      @c = c   # max color
      @k = nil # current
      @f = []  # failed
      @p = n.select { |n| n.c.nil? } # possible
      # puts @p.inspect
      # @p[0].c = 666
      # puts @p[0].to_s
   end
   attr_reader :c, :n
   def done?
      @p.empty?
   end
   #
   def sort ar
      ar.sort { |a,b|
         r = b.l <=> a.l
         if r == 0
            b.f <=> a.f
         else
            r
         end
      }
   end
   #
   def chose_node
      b = @p.select { |n| n.blocked? }
      if not b.empty?
         return sort(b).shift
      end
      sort(@p).shift
   end
   #
   def test c
      @k.a.each do |i|
         if @n[i].c == c
            return false
         end
      end
      true
   end
   #
   def set_color c
      @k.c = c
      @k.a.each do |i|
         @n[i].f += 1
      end
   end
   #
   def try
      while not @p.empty?
         if @k.nil?
            @k = chose_node
            @p.delete @k
         end
         # puts "try #{@k.to_s}"
         co = nil
         cs = (@k.c.nil? ? 0 : @k.c )
         (cs..@c).each do |c|
            if test c
               co = c
               break
            end
         end
         if co.nil?
            @f << @k
            @k = nil
         else
            set_color co
            break
         end
      end
      # @n.each do |n| puts n.inspect end
      # puts @k.inspect
      # puts 'OK'
      true
   end
   #
   def next
      # TODO @k might be nil !!
      Step.new(@n.inject([]) {|a,n| a << n.clone}, ( (@k.c < @c) ? @c: @c+1))
   end
   #
end

class Graph
   #
   def initialize p, n, e
      @path = p
      @n = n
      @e = e
      @nodes = {}
      @edges = []
      @steps = []
      @c = 0
   end
   #
   def add e
      @edges << e

      a, b = *e
      a = a.to_i
      b = b.to_i

      n = @nodes[a]
      if n.nil?
         @nodes[a] = Node.new a, b
      else
         n.add b
      end

      n = @nodes[b.to_i]
      if n.nil?
         @nodes[b] = Node.new b, a
      else
         n.add a
      end
   end
   #
   def solve
      s = Step.new(@nodes.values.inject([]) {|a,n| a << n.clone}, @c)
      @steps << s
      while true
         if @steps.empty?
            puts 'unsolvable'
            return
         end
         if @steps[-1].try
            @steps << @steps[-1].next
            if @steps[-1].done?
               puts 'success'
               return
            end
         else
            @steps.pop
         end
      end
   end
   #
   def to_s
      r = "#{@p}: N:#{@n} E:#{@e}\n"
      @nodes.sort_by { |k,v| k } .each do |k,v|
         r += " #{v.to_s}\n"
      end
      r
   end
   #
   def draw
      puts "colors : #{@steps[-1].c}"
      require 'paleta'
      color = Paleta::Color.new(:hex, "0000ff")
      colors = Paleta::Palette.generate(:type => :complementary, :from => :color, :color => color, :size => @steps[-1].c).to_array(:rgb)
      df = @path+'.dot'
      pf = @path+'.png'
      open(df,'w') do |f|
         f << "graph {\n"
         f << "node [style=filled];\n"
         @steps[-1].n.each do |n|
            f << " #{n.i} [color=\"#{"#%02X%02X%02X" % colors[n.c]}\"]\n"
         end
         @edges.each do |a,b|
            f << " #{a} -- #{b}\n"
         end
         f << "}\n"
      end
      system "dot -Tpng -o '#{pf}' '#{df}'"
   end
   #
end

def read_graph path
   puts "read #{path}…"
   g = nil
   open(path).read.each_line do |line|
      if g.nil?
         g = Graph.new path, *line.split
         next
      end
      g.add line.split
   end
   puts "done."
   g
end

ARGV.each do |path|

   g = read_graph path
   g.solve
   puts g
   g.draw
end