Skip to content

Instantly share code, notes, and snippets.

@mapio
Last active January 20, 2016 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapio/c44c029a1c1a5ff1ab59 to your computer and use it in GitHub Desktop.
Save mapio/c44c029a1c1a5ff1ab59 to your computer and use it in GitHub Desktop.
The Hasse diagram of the divisibility graph
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
def divisibility_graph( n ):
E = set()
for dst in range( 30 ):
for src in range( 2, dst ):
if dst % src == 0:
E.add( ( src, dst ) )
return E
def hasse_diagram( E ):
E2 = set()
for e0 in E:
for e1 in E:
if e0[1] == e1[0]:
E2.add( ( e0[0], e1[1] ) )
return E - E2
def to_dot( E ):
res = [ 'digraph G { rankdir = LR; ' ]
res.extend( [ '{}->{};'.format( *e ) for e in E ] )
res.append( '}' )
return '\n'.join( res )
if __name__ == '__main__':
from sys import argv
E = divisibility_graph( int( argv[ 1 ] ) )
H = hasse_diagram( E )
print to_dot( H )
@mapio
Copy link
Author

mapio commented Jan 20, 2016

This code generates the Hasse diagram of the divisibility graph in Graphviz format. You can run it as

 python hasse_divisibility.py 30 | dot -Tsvg >h.svg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment