@staticmethod
def merge_nodes(G, nodes, new_node, **attr):
    """
        Merges the selected `nodes` of the graph G into one `new_node`,
        meaning that all the edges that pointed to or from one of these
        `nodes` will point to or from the `new_node`.
        attr_dict and `**attr` are defined as in `G.add_node`.
        """
    G.add_node(new_node, **attr)
    newG = G.copy()
    for n1, n2, data in newG.edges(data=True):
        if n1 in nodes:
            G.add_edge(new_node, n2, **data)
        elif n2 in nodes:
            G.add_edge(n1, new_node, **data)
    for n in nodes:
        G.remove_node(n)
