def save_graph_to_svg(G, labels, filename, backward=False):
    """ Generate a figure of a networkx's graph object and save it. """
    import networkx as nx
    if backward:
        G = G.reverse()
    pydot_graph = nx.drawing.nx_pydot.to_pydot(G)
    pydot_graph.set_rankdir('LR')
    pydot_graph.set_nodesep(0.5)
    for n in pydot_graph.get_nodes():
        name = n.get_name()
        n.set_style('rounded')
        n.set_style('filled')
        n.set_shape('box')
        n.set_fillcolor('#E5E5E5')
        n.set_width(0)
        n.set_height(0)
        n.set_label(labels[name.strip('"')])
    pydot_graph.write_svg(filename)
