def to_graphviz ( graph ) : ret = [ 'digraph g {' ] vertices = [ ] node_ids = dict ( [ ( name , 'node' + idx ) for ( idx , name ) in enumerate ( list ( graph ) ) ] ) for node in list ( graph ) : ret . append ( '  "%s" [label="%s"];' % ( node_ids [ node ] , node ) ) for target in graph [ node ] : vertices . append ( '  "%s" -> "%s";' % ( node_ids [ node ] , node_ids [ target ] ) ) ret += vertices ret . append ( '}' ) return '\n' . join ( ret )
python3 dict to graphviz nodes
def to_graphviz(graph):
    """

    :param graph:
    :return:
    """
    ret = ['digraph g {']
    vertices = []

    node_ids = dict([(name, 'node' + idx) for (idx, name) in enumerate(list(graph))])

    for node in list(graph):
        ret.append('  "%s" [label="%s"];' % (node_ids[node], node))
        for target in graph[node]:
            vertices.append('  "%s" -> "%s";' % (node_ids[node], node_ids[target]))

    ret += vertices
    ret.append('}')
    return '\n'.join(ret)