4lexik
4lexik

Reputation: 1

Python Welsh-Powell graph coloring

I have implemented algorithm for Welsh-Powell graph coloring. Task is that you have txt document with lines of 2 numbers, in which the first one represent vertex and the second one represent its neighbor. Looks like this:

1 2
1 4
1 5
1 8
1 9
1 11
2 3
2 4
2 5
2 6
2 7
2 8
2 10
2 11
2 12
3 4
3 5
3 6
3 7
3 8
3 9
3 11
4 6
4 9
4 10
4 11
4 12
5 6
5 7
5 10
5 11
5 12
6 7
6 10
6 11
6 12
7 8
7 10
7 11
7 12
8 10
8 11
8 12
9 10
9 12
10 12

Output should be also txt document with two numbers in line, where the first one is vertex and the second one is number of color like this:

1 2
2 1
3 2
4 3
5 3
6 4
7 5
8 3
9 1
10 2
11 6
12 6

The number of used colors is not important, as it shouldnt be more than all vertexes.

This is 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

And this is 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//: I received a solution for problem with input, but I dont have expected output. Instead of expected output above, i received this:

1 2
2 1
3 2
4 4
5 6
6 5
7 4
8 5
9 1
10 3
11 3
12 2

// I should use "python main.py ./input.txt" to input txt document, but after running the program it says:

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

I˙m not so good at programming and all of this is really hard for me to understand so I really appreciate some help to make this functional.

Upvotes: 0

Views: 1020

Answers (2)

younes chafi
younes chafi

Reputation: 1

change this line

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

to this one

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

Upvotes: 0

Pascal
Pascal

Reputation: 1256

For me it is working. And it also gives your desired output

1 1
2 2
3 1
4 2

I think it is about how you run the script. Because when I run python .\main.py I also get

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

you have to run it like this: python .\main.py .\input.txt (on windows)

The script is missing the input file.

Upvotes: 0

Related Questions