edgegraph.traversal.helpers.neighbors#
- edgegraph.traversal.helpers.neighbors(vert, direction_sensitive=0, unknown_handling=2, filterfunc=None)#
Identify the neighbors of a given vertex.
This function checks the edges associated with the given vertex, identifies the vertices on the other end of those edges, and returns them. It respects edge directionality if/when necessary, and can handle arbitrary edge types given they are subclasses of either
DirectedEdgeorUnDirectedEdge.For example, with the given graph:
the function would operate as:
>>> neighbors(v1) [v2, v3] >>> neighbors(v1, direction_sensitive=DIR_SENS_FORWARD) [v2, v3, v4] >>> neighbors(v4) [v1] >>> neighbors(v4, direction_sensitive=DIR_SENS_ANY) [v1, v3]
If supplied, the
filterfuncargument should be to a callable object (function or otherwise) that will return eitherTrueorFalse. This function is used to determine if a given vertex should be included in the returned neighbors. It must have the following signature:- edgegraph.traversal.helpers.filterfunc(e, v2)
Determines if a given vertex (
v2) should be included in the neighbors ofv. Becausev2may be reachable fromv1via multiple edges, the edge currently being considered is given as well.- Parameters:
e – The edge connecting
v1tov2.v2 – The vertex under consideration.
- Returns:
Whether or not
v2should be considered a neighbor ofv, when reached viae.
For example, one may wish to only consider vertices if a given attribute meets some criteria:
>>> neighbors(v1) [v2, v3] >>> neighbors(v1, filterfunc=lambda e, v2: v2.i >= 3) [v3]
Note
The
filterfuncparameter operates in addition to thedirection_sensitiveparameter!- Parameters:
vert (Vertex) – The vertex to identify neighbors of.
direction_sensitive (int) – How to handle directional edges as they are encountered.
DIR_SENS_FORWARDwill indicates “normal” usages, where edges will only be followed outwards from the given vertex.DIR_SENS_ANYwill follow any edge, regardless of whether it points to or from this vertex.DIR_SENS_BACKWARDwill follow only edges inbound to this vertex.unknown_handling (int) – What to do with edges whose class is not recognized (not a subclass / instance of either
DirectedEdgeorUnDirectedEdge). Options areLNK_UNKNOWN_NONNEIGHBORto treat unknown edges as non-neighbors,LNK_UNKNOWN_NEIGHBORto treat unknown edges as neighbors, orLNK_UNKNOWN_ERRORto raise aNotImplementedErrorif such an edge is encountered.filterfunc (Callable) – Callable object to determine whether the given edge / vertex should be included in the neighbors output.
- Raises:
NotImplementedError – if kwarg
unknown_handlingis set toLNK_UNKNOWN_ERRORand an unknown edge class is enountered.- Returns:
A list of
Vertexobjects representing neighbors of the specified vertex.- Return type: