Note

This is the documentation for the current state of the development branch of rustworkx. The documentation or APIs here can change prior to being released.

rustworkx.PyDAG.predecessors#

PyDAG.predecessors(node, /)#

Return a list of data of all node predecessors in a directed graph

A predecessor is defined as a node that has a directed edge pointing to the specified node. In a multigraph, where two nodes can be connected by multiple edges, each predecessor node is returned only once.

>>> G = rx.PyDiGraph()
>>> G.add_nodes_from(["A", "B", "C", "D", "E"])
NodeIndices[0, 1, 2, 3, 4]
>>> G.extend_from_edge_list([(0, 3), (1, 3), (2, 3), (3, 4)])
>>> G.predecessors(3)  # predecessors of the 'D' node
['C', 'B', 'A']
>>> G.predecessors(10) # predecessors of an non-existing node
[]

See also

To filter the predecessors by the attributes of the connecting edge, see find_predecessors_by_edge().

See also successors().

For undirected graphs, see neighbors().

To get beyond the nearest predecessors, see ancestors().

Parameters:

node (int) – The index of the node to get the predecessors for

Returns:

A list of the node data of all node’s predecessors

Return type:

list[S]