【问题标题】:Create particular node order for graph coloring problem为图形着色问题创建特定的节点顺序
【发布时间】:2019-08-15 19:35:24
【问题描述】:

我很难用算法来创建我将为图表着色的顺序。 让我们考虑下图:

import networkx as nx
from matplotlib import pyplot as plt

nodes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
edges = [(1, 2), (2, 3), (3, 4), (4, 5), (1, 5), (5, 6), (6, 10), 
         (6, 7), (4, 7), (3, 8), (7, 8), (8, 9), (8, 11)]

# Create the graph
G = nx.Graph()
# Add edges
G.add_edges_from(edges)
# Plot
nx.draw(G, with_labels=True, font_size = 16)
plt.show()

我想有多个起点,称为initial_nodes,并在相邻节点周围创建订单。对于上图,我们将起始节点视为节点27

顺序是:

# Step 1: - Initial nodes
order = [2, 7]
# Step 2: - Nodes adjacent to the initial nodes
order = [2, 7, 1, 3, 4, 6, 8]
# Step 3: - Nodes adjacent to the nodes added in the previous step
# If they are not already present in the order...
order = [2, 7, 1, 3, 4, 6, 8, 5, 10, 9, 11]

我觉得递归方法应该很好用,但我不知道如何写下来。有什么想法吗?

编辑:所有问题都描述了一点further

当前创建订单的算法:

def create_node_order(graph, initial_nodes):
    """
    Create the node order.
    """
    # Initialization
    order = initial_nodes
    next_nodes = list()

    for node in initial_nodes:
        for adj_node in graph.adj[node].keys():
            if adj_node not in order:
                order.append(adj_node)
                next_nodes.append(adj_node)

    while len(next_nodes) != 0:

        for node in next_nodes:
            for adj_node in graph.adj[node].keys():
                if adj_node not in order:
                    order.append(adj_node)
                    next_nodes.append(adj_node)

            next_nodes.remove(node)

    return order

【问题讨论】:

  • 目标是达到任何着色还是需要找到最小着色?我知道没有证据表明这种方法会产生最佳着色。通常着色的顺序非常重要,我知道的一些方法需要回溯,这意味着“顺序”对它们毫无意义。
  • @EtienneOtt 我不是图论专家......从我读到的内容来看,没有算法可以有效地(小于指数时间)达到最佳着色。我还不知道回溯到底是什么。据我所见,它是沿着一条路走下去,然后再往上走以校正颜色,以实现更好的全局着色。目前,我不想使用回溯,我想尝试一种贪婪的方法。我不会描述整个问题,但我上面描述的顺序是根据我的数据和我试图实现的颜色明智地选择的。
  • @EtienneOtt 我只是想不出一个算法来有效地创建这个订单......虽然我觉得递归方法是正确的选择,但我不知道如何写下来.

标签: python networkx graph-theory graph-coloring


【解决方案1】:

请注意,考虑到从某些起始节点“辐射”出“圆圈”的迭代方法,这不是产生最佳着色的要求,甚至被证明是可能的。鉴于我对所描述的算法和要求的理解,我会使用这样的东西:

伪代码:

// no need for more than four colors IFF the algorithm is optimal and the graph is planar, otherwise extend
colors = [red, blue, green, yellow]
// initialize
graph = SomeSuitableDataStructure(data)
queue = [graph(2), graph(7)] // starting nodes
for node in graph.nodes:
    node.visited = False
    node.color = Undefined

while not queue.empty():
    node = queue.pop()
    node.visited = True
    node.color = first_color_not_in([n.color for n in node.neighbors()])
    for neighbor in node.neighbors():
        if not neighbor.visited: queue.push_back(neighbor)

实现first_color_not_in() 和处理Undefined 颜色留给读者作为练习。

【讨论】:

  • 这是在做着色,而不仅仅是订单。我只是在寻找一种比我在编辑中添加的代码更有效的方式来生成订单的方法。我的问题实际上有点不同。每个节点都有一个可用替代值列表,目标是为每个节点选择一个,以使附近节点(链接)之间的偏差最小化。与着色问题相反,目标是使连接的节点具有相同或最接近的颜色......这就是为什么“圆圈”方法是有意义的,这就是为什么我只寻找顺序
  • 无论如何,确实,对于纯着色问题,您的方法很棒,而且确实,所需颜色的最大数量为 d+1,d 为度数。
  • 好吧,我不确定构造队列的迭代顺序和着色顺序之间的区别,因为对于大多数算法来说它是相同的,或者更准确地说,它是症结所在解决问题,找到一个就是找到另一个。
  • 顺便说一下,如果算法是最优的,它不会达到 4 种颜色的最大值。而是贪心算法在最坏的情况下需要 4 种颜色,具体取决于执行着色的顺序。
  • 我不确定您所说的队列顺序是什么意思,因为在您的算法和我正在编写的算法中,队列的顺序都是节点着色的顺序.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多