Skip to content

edge_exist

Signature/Parameters

def edge_exist(self, edge, edges = None)

Check whether an edge is present in the graph (or a supplied edge list).

Parameters:

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

Edge specification to check for existence. The method canonicalizes the representation so that undirected and bidirected edges are insensitive to node order.

required
edges list or None

Specific list of edges to search. When None, the method looks up the corresponding edge collection from the instance.

None

Returns:

Type Description
bool

True when the edge is found, otherwise False.

Examples:

>>> G = DAG(graph="X -> Y")
>>> G.edge_exist(("X", "Y"))
True
>>> G.edge_exist({"X", "Y"})
False
Source code in causalinf/gcm.py
def edge_exist(self, edge, edges=None):
    """
    Check whether an edge is present in the graph (or a supplied edge list).

    Parameters
    ----------
    edge : tuple[str, str] or tuple[tuple[str, str], tuple[str, str]] or set[str]
        Edge specification to check for existence. The method canonicalizes
        the representation so that undirected and bidirected edges are
        insensitive to node order.
    edges : list or None, optional
        Specific list of edges to search. When ``None``, the method looks up
        the corresponding edge collection from the instance.

    Returns
    -------
    bool
        ``True`` when the edge is found, otherwise ``False``.

    Examples
    --------
    >>> G = DAG(graph="X -> Y")
    >>> G.edge_exist(("X", "Y"))
    True
    >>> G.edge_exist({"X", "Y"})
    False
    """
    if edges is None:
        edge_type = self.__edge_type__(edge)
        edges = self.__getattribute__(edge_type)
    edges = [edges] if not isinstance(edges, list) else edges
    edge = self.__edge_frozen_format__(edge)
    edges_in_list = {self.__edge_frozen_format__(e) for e in edges}
    return edge in edges_in_list