Skip to content

edge_add

Signature/Parameters

def edge_add(self, edge)

Add an edge to the graph if it is not already present.

Parameters:

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

Edge specification compatible with the formats accepted at initialization. Use a two-tuple for directed edges, a set with two nodes for undirected edges, or a pair of directed tuples for bidirected edges.

required

Returns:

Type Description
DAG

The current instance when the edge already exists; otherwise a new DAG instance containing the added edge.

Examples:

>>> G = DAG(graph="X -> Y")
>>> G = G.edge_add(("Y", "Z"))
>>> ("Y", "Z") in G.directed
True
Source code in causalinf/gcm.py
def edge_add(self, edge):
    """
    Add an edge to the graph if it is not already present.

    Parameters
    ----------
    edge : tuple[str, str] or tuple[tuple[str, str], tuple[str, str]] or set[str]
        Edge specification compatible with the formats accepted at
        initialization. Use a two-tuple for directed edges, a set with two
        nodes for undirected edges, or a pair of directed tuples for
        bidirected edges.

    Returns
    -------
    DAG
        The current instance when the edge already exists; otherwise a new
        `DAG` instance containing the added edge.

    Examples
    --------
    >>> G = DAG(graph="X -> Y")
    >>> G = G.edge_add(("Y", "Z"))
    >>> ("Y", "Z") in G.directed
    True
    """
    res = self
    if not self.edge_exist(edge):
        graph = self.__graph_list__.copy()
        graph.append(edge)
        res = self.__rebuild_graph__(graph)
    return res