[Nauty] Fw: Filters graphs with diameters greater than 2 and less than 4

lczhangmath lczhangmath at 163.com
Sun Oct 1 00:16:05 AEST 2023



Dear Keith,


Thank you very much, even if you need to note that your translate doesn't really work! (note that \X07). Anyway, the topic about python call problem should even end. I'm currently more concerned about my previous email: Filters graphs with diameters greater than 2 and less than 4. Until Professor Brendan doesn't reply,  python call is a choose (SageMath is better), and maybe need a little more depth on how to implement more filtering in C. Thank you again. 


Licheng.
Best wishes.











在 2023-09-30 21:52:29,keith.briggs at bt.com 写道:

This should be the last email on this topic, as most people are not interested. The re and functools methods are good.  The right way to use translate is:


>>> x='\a\b\c\n'
>>> d={'\a':r'\a','\b':r'\b','\c':r'\c',r'\n':'\n'}
>>> x.translate(d)
'\x07\x08\\c\n'



Keith 


From: lczhangmath <lczhangmath at 163.com>
Sent: Saturday, September 30, 2023 14:26
To: Briggs,KM,Keith,TUD2 R <keith.briggs at bt.com>
Cc: nauty at anu.edu.au <nauty at anu.edu.au>
Subject: Re:Re: [Nauty] Fw: Filters graphs with diameters greater than 2 and less than 4
 


Hello,


Thank you for your reminder. Your first way is nice.  I tried your translate function, but it didn't work for me. If you have specific steps, please let me know. Anyway, I have two other methods that seem pythonic. 


One is to use regular expressions.
```
import re
import networkx  as nx
char_map = {
    '\a': r'\a',
    '\b': r'\b',
    '\c': r'\c',
    '\f': r'\f',
    '\n': r'\n',
    '\r': r'\r',
    '\t': r'\t',
    '\v': r'\v',
    '\'': r'\'',
    '\"': r'\"'
}


pattern = re.compile('|'.join(re.escape(key) for key in char_map.keys()))
line = 'ihChWC@?gE_@?@?A_ at g?@??C??j?@l??E??Ao??\???m??@I??DF??E`O??GM??@?g??S\@o?@g at O??G?w??C?I??D?@o?@g?D???_?M??@??I??D??FK?E_?@Q??G??N??@???CPCOaG\a??'
line1 = pattern.sub(lambda x: char_map[x.group(0)], line)
G = nx.from_graph6_bytes(line1.encode())
print(G)
```
Another way to write it.


```
import functools
import networkx  as nx
char_map = [
    ('\a', r'\a'),
    ('\b', r'\b'),
    ('\c', r'\c'),
    ('\f', r'\f'),
    ('\n', r'\n'),
    ('\r', r'\r'),
    ('\t', r'\t'),
    ('\v', r'\v'),
    ('\'', r'\''),
    ('\"', r'\"')
]
def replace_chars(s, mapping):
    return functools.reduce(lambda s, replacement: s.replace(*replacement), mapping, s)
line = 'ihChWC@?gE_@?@?A_ at g?@??C??j?@l??E??Ao??\???m??@I??DF??E`O??GM??@?g??S\@o?@g at O??G?w??C?I??D?@o?@g?D???_?M??@??I??D??FK?E_?@Q??G??N??@???CPCOaG\a??'
line1 = replace_chars(line, char_map)
G = nx.from_graph6_bytes(line1.encode())
print(G)
```




By the way, I made some improvements to the code before, such as passing the filter through a pipe directly from geng instead of exporting it to a file (using universal_newlines=True).


import subprocess
import sys
import networkx  as nx
import functools
escape_dict={'\a':r'\a',
             '\b':r'\b',
             '\c':r'\c',
             '\f':r'\f',
             '\n':r'\n',
             '\r':r'\r',
             '\t':r'\t',
             '\v':r'\v',
             '\'':r'\'',
             '\"':r'\"'}


process = subprocess.Popen(['geng', '19', '22', '-b'], stdout=subprocess.PIPE, universal_newlines=True)
count = 0
for line in process.stdout:
    line=line.strip()
    #Eliminate the impact of escape characters
    line=''.join((escape_dict[x] if x in escape_dict else x) for x in line)
    G = nx.from_graph6_bytes(line.encode())    
    if  len(nx.max_weight_matching(G))==4:
        count+=1
        sg=nx.to_graph6_bytes(G)
        cleaned_g6string = sg[10:-1].decode()
        print(cleaned_g6string)
print(count,"graphs written to stdout")


One of the more interesting things is when does geng produce graphs with \ characters. I've been looking to check if my code is working.


Best wishes.
Licheng.

















More information about the Nauty mailing list