着色特定类型的平面图

时间:2013-04-20 12:10:47

标签: algorithm planar-graph

我有一个特定类型的平面图,我发现搜索一个会为其顶点legally着色的算法很有意思。关于这种类型的图表,它非常容易和酷:

  

考虑任何树 T n> 2 顶点和 k 叶。让我们将 G(T)表示为由 T 构建的图表,将其叶子连接到 k - 循环,其方式为 G( T)是平面的。

我想出的问题是用 3 颜色着色 G(T)。显然, G(T)作为平面图, 4 - 可以,但我认为(没有证明)它几乎总是 3 - 由于其简单性而可归类。几乎总是意味着只有当 T star并且只有奇数的叶子时, G(T) 4 -colorable。

我正在寻找一些算法,或者可能证明我的假设很容易转化为算法。我会非常感谢任何帮助,提示。

如果我不够清楚,我会举一个例子:

令T为具有边E(T)= {{1,2},{2,3},{2,4},{4,5}}然后E(G(T))=的树集合的总和:E(T)和{{1,5},{5,3},{3,1}},因为我们将叶子1,5,3连接成一个循环。

1 个答案:

答案 0 :(得分:0)

local Edges_of_Tree = {{1,2}, {2,3}, {2,4}, {4,5}}
local Cycle_of_Leaves = {1,5,3}

local color1 = 'Red'
local color2 = 'Green'
local color3 = 'Blue'

local vert = {}     
local vert_arr = {}  

local function add_edge(v1, v2)
   assert(v1 ~= v2)
   if not vert[v1] then
      vert[v1] = {deg = 0, adj = {}}
      table.insert(vert_arr, v1)
   end
   vert[v1].deg = vert[v1].deg + 1
   assert(not vert[v1].adj[v2], 'multiple edges between '..v1..' and '..v2)
   vert[v1].adj[v2] = true
end

for _, edge in ipairs(Edges_of_Tree) do
   local v1, v2 = unpack(edge)
   add_edge(v1, v2)
   add_edge(v2, v1)
end

table.sort(vert_arr)

local leaf_ctr = 0
local root
for v, vv in pairs(vert) do
   if vv.deg == 1 then
      leaf_ctr = leaf_ctr + 1
   else
      root = v
   end
end
assert(#vert_arr > leaf_ctr + 1, 'tree is a star')
assert(leaf_ctr == #Cycle_of_Leaves, 'invalid Cycle_of_Leaves')
for _, v in ipairs(Cycle_of_Leaves) do
   assert(vert[v] and vert[v].deg == 1 and vert[v].color == nil, 
      'invalid Cycle_of_Leaves')
   vert[v].color = false
end

local function recursive_paint_inodes(v, color, prev_v)
   assert(vert[v].color == nil, 'a cycle in tree found')
   vert[v].color = color
   local next_color = (color1..color2):gsub(color, '')
   for next_v in pairs(vert[v].adj) do
      if next_v ~= prev_v and vert[next_v].deg > 1 then
         recursive_paint_inodes(next_v, next_color, v)
      end
   end
end
recursive_paint_inodes(root, color1)

local front
for i = 1, leaf_ctr do
   local vv = vert[Cycle_of_Leaves[i]]
   vv.next = Cycle_of_Leaves[i % leaf_ctr + 1]
   vv.prev = Cycle_of_Leaves[(i - 2) % leaf_ctr + 1]
   local parent = next(vv.adj)
   if parent ~= next(vert[vv.prev].adj) then
      assert(not vert[parent].conn_to_leaf, 'graph is non-planar')
      vert[parent].conn_to_leaf = true
      front = Cycle_of_Leaves[i]
   end
end

vert[next(vert[vert[front].prev].adj)].color = color3
vert[front].color = color3

local tricolor = color1..color2..color3
local leaf = front
for i = 1, leaf_ctr - 1 do
   local prev_color = vert[leaf].color
   leaf = vert[leaf].next
   local parent_color = vert[next(vert[leaf].adj)].color
   local enabled_colors = tricolor:gsub(prev_color, ''):gsub(parent_color, '')
   vert[leaf].color = enabled_colors:match(color1)
      or enabled_colors:match(color2) or color3
end

for _, v in ipairs(vert_arr) do
   print(v..' '..vert[v].color)
end

此代码以Lua编写 您可以在行动there中对其进行测试。