Python Welsh-Powell图着色

mzillmmw  于 2022-12-10  发布在  Python
关注(0)|答案(2)|浏览(93)

我已经实现了算法的Welsh-Powell图着色。任务是你有一个txt文件的行2个数字,其中第一个代表顶点和第二个代表它的邻居。看起来像这样:
输出也应该是txt文档,每行有两个数字,其中第一个是顶点,第二个是颜色数,如下所示:
使用的颜色数量并不重要,因为它不应超过所有顶点。
这里是graphColoring.py

def file_to_graph(filename):
    file1 = open(filename, 'r')
    lines = file1.readlines()
    graph = [[], []]

    for line in lines:
        if line == '':
            continue

        row = line.split(' ')

        from_vertex = int(row[0])
        to_vertex = int(row[1])

        found_from_vertex = -1
        found_to_vertex = -1

        for i in range(len(graph[0])):
            if graph[0][i][0] == from_vertex:
                graph[0][i][1] += 1
                found_from_vertex = i

            if graph[0][i][0] == to_vertex:
                graph[0][i][1] += 1
                found_to_vertex = i

        if found_from_vertex == -1:
            graph[0].append([from_vertex, 1])

        if found_to_vertex == -1:
            graph[0].append([to_vertex, 1])

        graph[1].append((from_vertex, to_vertex))

    return graph

def get_color(colored_vertexes, vertex):
    for colored_vertex in colored_vertexes:
        if colored_vertex[0] == vertex:
            return colored_vertex[1]
    return -1

def color_graph(graph, max_colors=-1):
    sorted_vertexes = sorted(graph[0],  key=lambda x: x[1])
    colored_vertexes = []
    for vertex_with_edges in reversed(sorted_vertexes):
        colored_vertexes.append([vertex_with_edges[0], 0])

    actual_color = 1

    while True:
        for vertex_with_edge_count in reversed(sorted_vertexes):
            color = 0
            colored_vertex_index = -1
            vertex_name = vertex_with_edge_count[0]

            for i in range(len(colored_vertexes)):
                if colored_vertexes[i][0] == vertex_name:
                    color = colored_vertexes[i][1]
                    colored_vertex_index = i
                    break

            if color != 0:
                continue

            color_of_neighbours = []

            for (from_vertex, to_vertex) in graph[1]:
                if from_vertex == vertex_name:
                    color_of_neighbours.append(get_color(colored_vertexes, to_vertex))

                if to_vertex == vertex_name:
                    color_of_neighbours.append(get_color(colored_vertexes, from_vertex))

            if actual_color not in color_of_neighbours:
                colored_vertexes[colored_vertex_index][1] = actual_color

        actual_color += 1
        is_colored = True
        for colored_vertex in colored_vertexes:
            if colored_vertex[1] == 0:
                is_colored = False
                break

        if is_colored:
            break

    return colored_vertexes

这里是main.py

from graphColoring import file_to_graph, color_graph
import sys

if __name__ == '__main__':
    graph = file_to_graph(sys.argv[1])
    colored_vertexes = color_graph(graph)
    for colored_vertex in sorted(colored_vertexes, key=lambda x: x[0]):
        print(str(colored_vertex[0]) + " " + str(colored_vertex[1]))

EDIT//:我收到了输入问题的解决方案,但没有预期的输出。我收到的不是上面预期的输出,而是:
//我应该使用“python main.py”来输入txt文档,但运行程序后它显示:

line 7, in <module>
    graph = file_to_graph(sys.argv[1])
                          ~~~~~~~~^^^
IndexError: list index out of range

我不是很擅长编程,所有这些对我来说真的很难理解,所以我真的很感激一些帮助,使这个功能。

3duebb1j

3duebb1j1#

对我来说,它是工作的。而且它也给出了你想要的输出
我认为这与如何运行脚本有关。因为当我运行python .\main.py时,

Traceback (most recent call last):
  File "C:\Users\fhdwnig\Desktop\main.py", line 5, in <module>
    graph = file_to_graph(sys.argv[1])
                          ~~~~~~~~^^^

您必须这样运行它:python .\main.py .\input.txt(在Windows上)
脚本缺少输入文件。

xlpyo6sf

xlpyo6sf2#

更改此行

sorted_vertexes = sorted(graph[0],  key=lambda x: x[1])

到这个

sorted_vertexes = sorted(sorted(graph[0][::-1],key=lambda x:x[0], reverse=True),  key=lambda x: x[1])

相关问题