MCPcopy
hub / github.com/dmlc/dgl / predecessors

Method predecessors

python/dgl/heterograph.py:3030–3087  ·  view source on GitHub ↗

Return the predecessor(s) of a particular node with the specified edge type. Node ``u`` is a predecessor of node ``v`` if there is an edge ``(u, v)`` with type ``etype`` in the graph. Parameters ---------- v : int The node ID. If the graph has mu

(self, v, etype=None)

Source from the content-addressed store, hash-verified

3028 return F.astype(ret, F.bool)
3029
3030 def predecessors(self, v, etype=None):
3031 """Return the predecessor(s) of a particular node with the specified edge type.
3032
3033 Node ``u`` is a predecessor of node ``v`` if there is an edge ``(u, v)`` with type
3034 ``etype`` in the graph.
3035
3036 Parameters
3037 ----------
3038 v : int
3039 The node ID. If the graph has multiple edge types, the ID is for the destination
3040 type corresponding to the edge type.
3041 etype : str or (str, str, str), optional
3042 The type names of the edges. The allowed type name formats are:
3043
3044 * ``(str, str, str)`` for source node type, edge type and destination node type.
3045 * or one ``str`` edge type name if the name can uniquely identify a
3046 triplet format in the graph.
3047
3048 Can be omitted if the graph has only one type of edges.
3049
3050
3051 Returns
3052 -------
3053 Tensor
3054 The predecessors of :attr:`v` with the specified edge type.
3055
3056 Examples
3057 --------
3058 The following example uses PyTorch backend.
3059
3060 >>> import dgl
3061 >>> import torch
3062
3063 Create a homogeneous graph.
3064
3065 >>> g = dgl.graph((torch.tensor([0, 0, 1, 1]), torch.tensor([1, 1, 2, 3])))
3066
3067 Query for node 1.
3068
3069 >>> g.predecessors(1)
3070 tensor([0, 0])
3071
3072 For a graph of multiple edge types, it is required to specify the edge type in query.
3073
3074 >>> hg = dgl.heterograph({
3075 ... ('user', 'follows', 'user'): (torch.tensor([0, 1]), torch.tensor([1, 2])),
3076 ... ('user', 'plays', 'game'): (torch.tensor([3, 4]), torch.tensor([5, 6]))
3077 ... })
3078 >>> hg.predecessors(1, etype='follows')
3079 tensor([0])
3080
3081 See Also
3082 --------
3083 successors
3084 """
3085 if not self.has_nodes(v, self.to_canonical_etype(etype)[-1]):
3086 raise DGLError("Non-existing node ID {}".format(v))
3087 return self._graph.predecessors(self.get_etype_id(etype), v)

Callers 7

_testFunction · 0.45
_test_hypersparseFunction · 0.45
_test_queryFunction · 0.45
_test_oneFunction · 0.45
_test_csr_oneFunction · 0.45
check_metis_partitionFunction · 0.45
test_reorder_nodesFunction · 0.45

Calls 5

has_nodesMethod · 0.95
to_canonical_etypeMethod · 0.95
get_etype_idMethod · 0.95
DGLErrorClass · 0.85
formatMethod · 0.80

Tested by 7

_testFunction · 0.36
_test_hypersparseFunction · 0.36
_test_queryFunction · 0.36
_test_oneFunction · 0.36
_test_csr_oneFunction · 0.36
check_metis_partitionFunction · 0.36
test_reorder_nodesFunction · 0.36