Last active
July 23, 2020 22:22
-
-
Save mapio/70153b1750b3019d6764d5573135e7a0 to your computer and use it in GitHub Desktop.
Playing with my son Federico
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
from subprocess import Popen, PIPE | |
from sys import argv | |
first, last = map(int, argv[1:3]) | |
name = argv[3] | |
ops = [ | |
lambda x: x + 1, | |
lambda x: x * 2 | |
] | |
frontier = deque([first]) | |
seen = set() | |
graph = 'graph G {\nnode [shape=point]' | |
while frontier: | |
v = frontier.popleft() | |
if v in seen: continue | |
for op in ops: | |
u = op(v) | |
if u > last: continue | |
graph += '\t{} -- {}'.format(v, u) | |
frontier.append(u) | |
seen.add(v) | |
graph += '}' | |
sfdp = Popen(['sfdp', '-Tpng'], stdin = PIPE, stdout = PIPE) | |
with open('{}.png'.format(name), 'wb') as of: | |
of.write(sfdp.communicate(input = graph)[0]) |
Just works on python 2.7. And here it produces really small images, like 179x173 with the args 1 10000. Any idea why?
I guess you mean 179x173 pixels, the image size depends on the sfdp
tool, part of Graphviz. You can check the manual to see if you can specify a larger size, this Stack Overflow answer could be a good starting point.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the script used to generate the above PNG, that also appears in this tweet; it is based on Graphviz and assumes
sfdp
layout tool of such package to be in the path.The tool generates a graph G=(V, E) such that V = {
first
<= x <=last
} and E = {(x, y) : y = x + 1} U {(x, y) : y = 2x}To run it, just pass the
first
andlast
number to consider and the basename of the graph, as inand open
graph.png
.