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)
| 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) |