Skip to content

edge_remove

Signature/Parameters

def edge_remove(self, edge)

Remove an existing edge from the graph when present.

Parameters:

Name Type Description Default
edge tuple[str, str] or tuple[tuple[str, str], tuple[str, str]] or set[str]

Edge specification matching one of the accepted formats. The check is insensitive to direction for bidirected and undirected edges.

required

Returns:

Type Description
DAG

A new DAG instance with the edge removed when the edge exists; otherwise the current instance is returned unchanged.

Examples:

>>> G = DAG(graph="X -> Y")
>>> G = G.edge_remove(("X", "Y"))
>>> ("X", "Y") in G.directed
False
Source code in causalinf/gcm.py
def edge_remove(self, edge):
    """
    Remove an existing edge from the graph when present.

    Parameters
    ----------
    edge : tuple[str, str] or tuple[tuple[str, str], tuple[str, str]] or set[str]
        Edge specification matching one of the accepted formats. The check
        is insensitive to direction for bidirected and undirected edges.

    Returns
    -------
    DAG
        A new `DAG` instance with the edge removed when the edge exists;
        otherwise the current instance is returned unchanged.

    Examples
    --------
    >>> G = DAG(graph="X -> Y")
    >>> G = G.edge_remove(("X", "Y"))
    >>> ("X", "Y") in G.directed
    False
    """
    removed = False
    graph = self.__graph_list__.copy()

    if edge in self.__graph_list__:
        graph.remove(edge)
        removed = True
    elif self.__edge_type__(edge)=='bidirected':
        edge = (edge[1], edge[0])
        if edge in self.__graph_list__:
            graph.remove(edge)
            removed = True

    if removed:
        return self.__rebuild_graph__(graph)
    else:
        return  self