Recursively generated graphΒΆ

Daft is Python, so you can do anything Python can do. This graph is generated by recursive code.

import daft

def recurse(pgm, nodename, level, c):
    if level > 4:
        return nodename
    r = c // 2
    r1nodename = "r{0:02d}{1:04d}".format(level, r)
    if 2 * r == c:
        # print("adding {0}".format(r1nodename))
        pgm.add_node(
            r1nodename, r"reduce", 2 ** level * (r + 0.5) - 0.5, 3 - 0.7 * level, aspect=1.9
        )
    pgm.add_edge(nodename, r1nodename)
    if 2 * r == c:
        return recurse(pgm, r1nodename, level + 1, r)


pgm = daft.PGM()

pgm.add_node("query", r'\texttt{"kittens?"}', 3, 6.0, aspect=3.0, plot_params={"ec": "none"})
pgm.add_node("input", r"input", 7.5, 6.0, aspect=3.0)
pgm.add_edge("query", "input")

for c in range(16):
    nodename = "map {0:02d}".format(c)
    pgm.add_node(nodename, str(nodename), c, 3.0, aspect=1.9)
    pgm.add_edge("input", nodename)
    level = 1
    recurse(pgm, nodename, level, c)

pgm.add_node("output", r"output", 7.5, -1.0, aspect=3.0)
pgm.add_edge("r040000", "output")
pgm.add_node(
    "answer",
    r'\texttt{"http://dwh.gg/"}',
    12.0,
    -1.0,
    aspect=4.5,
    plot_params={"ec": "none"},
)
pgm.add_edge("output", "answer")

pgm.render()
<Axes:>
../../_images/recursive_2_1.png