MultiDiGraph.has_edge#

MultiDiGraph.has_edge(u, v, key=None)#

Returns True if the graph has an edge between nodes u and v.

This is the same as v in G[u] or key in G[u][v] without KeyError exceptions.

Parameters:
u, vnodes

Nodes can be, for example, strings or numbers.

keyhashable identifier, optional (default=None)

If specified return True only if the edge with key is found.

Returns:
edge_indbool

True if edge is in the graph, False otherwise.

Examples

Can be called either using two nodes u, v, an edge tuple (u, v), or an edge tuple (u, v, key).

>>> G = nx.MultiGraph()  # or MultiDiGraph
>>> nx.add_path(G, [0, 1, 2, 3])
>>> G.has_edge(0, 1)  # using two nodes
True
>>> e = (0, 1)
>>> G.has_edge(*e)  #  e is a 2-tuple (u, v)
True
>>> G.add_edge(0, 1, key="a")
'a'
>>> G.has_edge(0, 1, key="a")  # specify key
True
>>> G.has_edge(1, 0, key="a")  # edges aren't directed
True
>>> e = (0, 1, "a")
>>> G.has_edge(*e)  # e is a 3-tuple (u, v, 'a')
True

The following syntax are equivalent:

>>> G.has_edge(0, 1)
True
>>> 1 in G[0]  # though this gives :exc:`KeyError` if 0 not in G
True
>>> 0 in G[1]  # other order; also gives :exc:`KeyError` if 0 not in G
True