Ask Your Question
0

How to determine if two colorings of a graph are the same?

asked 1 year ago

licheng gravatar image

updated 1 year ago

A coloring of a graph G with vertex set V is the partitioning of V into so called color classes in such a way that no two vertices of the same class are adjacent. A k-coloring contains exactly k color classes.

We shall think of a k-coloring of G as a map ψ:V1,2,,k such that ψ1(i),i=1,2,,k, are the color classes of G. Naturally two maps, ψ1 and ψ2, represent the same k-coloring if and only if ψ1=ψ2π for some permutation π of 1,2,,k. (See details in B. Bollobás Uniquely colorable graphs[J]. Journal of Combinatorial Theory, Series B, 1978, 25: 54-61.)

My goal is: given a graph with two k-colorings, I want to determine if they are same. I am unsure which function to use. I also want to determine how many different k-colorings of a graph in this sense.

For example,

from sage.graphs.graph_coloring import all_graph_colorings
G = Graph([('a','b'),('a','d'),('b','c'),('c','d'),('a','c')])
for C in all_graph_colorings(G, 3):
     print(C)


{0: ['a'], 1: ['b', 'd'], 2: ['c']}
{0: ['a'], 1: ['c'], 2: ['b', 'd']}
{0: ['b', 'd'], 1: ['a'], 2: ['c']}
{0: ['c'], 1: ['a'], 2: ['b', 'd']}
{0: ['b', 'd'], 1: ['c'], 2: ['a']}
{0: ['c'], 1: ['b', 'd'], 2: ['a']}

We obtained six 3-colorings, and are they the same by definition of same k-coloring?

After that, I would like to obtain all uniquely colorable simple graphs up to 10 vertices. See https://oeis.org/A369223 corresponding to their counts.

Additional information: The chromatic number of G, denoted by χ(G), is the minimal k for which G has a k-coloring. A graph with exactly one k-coloring is called uniquely k-colorable. It is obvious that if G is uniquely k-colorable then χ(G)=k or n, so we shall say simply that G is uniquely colorable if it is uniquely χ(G) colorable.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 1 year ago

Max Alekseyev gravatar image

updated 1 year ago

You can represent colorings as (unordered) set partitions of vertices:

from sage.graphs.graph_coloring import all_graph_colorings
G = Graph([('a','b'),('a','d'),('b','c'),('c','d'),('a','c')])
unique_colorings = set( map(SetPartition, all_graph_colorings(G, 3, color_classes=True)) )
for C in unique_colorings:
    print(C)

This tells us that there is just one partition:

{{'a'}, {'b', 'd'}, {'c'}}
Preview: (hide)
link

Comments

Nice. Further, I would like to write a function to determine if a graph is uniquely colorable (i.e. has exactly one (unordered) set partition), it feels a bit wasteful to iterate over all colorings when the graph is not uniquely colorable. I noticed that Python actually doesn't support nested sets, like {{'a'}, {'b', 'd'}, {'c'}}. It shows TypeError: unhashable type: 'set' How to do.

licheng gravatar imagelicheng ( 1 year ago )

Hashable sets are provided by Python's frozenset or by Sage's Set types. Alternatively, you can create set partitions from lists/tuples - like SetPartition( [{'a'}, {'b', 'd'}, {'c'}] ).

Max Alekseyev gravatar imageMax Alekseyev ( 1 year ago )

Thank you. I try it but not good for me. Would you write a function? My rough idea is to write it like this( the following is just an algorithm, and is not guaranteed to right due to the operator SetPartition(coloring) in Z)

def unique_ch(g, k):
    if g.chromatic_number()!=k:
       return False
    flag = True
    Z = []
    cl = all_graph_colorings(g, k, color_classes=True)

    for i, coloring in enumerate(cl): # may wrong 
        if i == 0:
           Z.append(SetPartition(coloring))
        else:
             if SetPartition(coloring) in Z:
                flag = False
                break
    return flag

Thank you in advance.

licheng gravatar imagelicheng ( 1 year ago )

I've already provided code for computing unique_colorings - just check len(unique_colorings) == 1 to make sure that there is one and only coloring.

Max Alekseyev gravatar imageMax Alekseyev ( 1 year ago )

I know. But it always iterates over all colorings in your code. If G that is not k-coloring, we may quickly test it avoiding all colorings. I want to write a correct code to replace coloring in enumerate(cl) in my codes

licheng gravatar imagelicheng ( 1 year ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 1 year ago

Seen: 269 times

Last updated: Feb 20 '24