How do I make the colour of nodes a and f different?

92 Views Asked by At

This is a simple graph colouring problem where I've used a predefined graph. The output gives me the same colour for nodes 'a' and 'f'. How do I make them different?

#initialising colours
colours = ['Green', 'Red', 'Yellow', 'Black', 'White']

#initialising vertices
vertices = ['a', 'b', 'c', 'd', 'e','f']

#dictionary to store neighbours. Adajacent sides are treated as values to the key vertice
neighbours = {}
neighbours['a'] = ['b', 'c','d','e']
neighbours['b'] = ['a', 'f', 'd']
neighbours['c'] = ['a', 'f', 'd']
neighbours['d'] = ['a', 'f', 'b']
neighbours['e'] = ['a', 'c', 'f']
neighbours['f'] = ['b', 'c', 'd', 'e']

dictionary to store the final output

colours_of_vertices = {}

a function that checks whether neighbouring vertices have the same colour or not

def promising(vertice, colour):
    for neighbour in neighbours.get(vertice): 
        colour_of_neighbour = colours_of_vertices.get(neighbour)
        if colour_of_neighbour == colour:
            return False

    return True

a function that assigns the colour to the vertices

def get_colour_for_vertice(vertice):
    for colour in colours:
        if promising(vertice, colour):
            return colour

driver program

def main():
    for vertice in vertices:
        colours_of_vertices[vertice] = get_colour_for_vertice(vertice)

    print (colours_of_vertices)


main()
0

There are 0 best solutions below