Return the number of edges in the graph. Parameters ---------- etype : str or (str, str, str), optional The type name of the edges. The allowed type name formats are: * ``(str, str, str)`` for source node type, edge type and destination node type.
(self, etype=None)
| 2683 | return self.num_edges(etype) |
| 2684 | |
| 2685 | def num_edges(self, etype=None): |
| 2686 | """Return the number of edges in the graph. |
| 2687 | |
| 2688 | Parameters |
| 2689 | ---------- |
| 2690 | etype : str or (str, str, str), optional |
| 2691 | The type name of the edges. The allowed type name formats are: |
| 2692 | |
| 2693 | * ``(str, str, str)`` for source node type, edge type and destination node type. |
| 2694 | * or one ``str`` edge type name if the name can uniquely identify a |
| 2695 | triplet format in the graph. |
| 2696 | |
| 2697 | If not provided, return the total number of edges regardless of the types |
| 2698 | in the graph. |
| 2699 | |
| 2700 | Returns |
| 2701 | ------- |
| 2702 | int |
| 2703 | The number of edges. |
| 2704 | |
| 2705 | Examples |
| 2706 | -------- |
| 2707 | |
| 2708 | The following example uses PyTorch backend. |
| 2709 | |
| 2710 | >>> import dgl |
| 2711 | >>> import torch |
| 2712 | |
| 2713 | Create a graph with three canonical edge types. |
| 2714 | |
| 2715 | >>> g = dgl.heterograph({ |
| 2716 | ... ('user', 'follows', 'user'): (torch.tensor([0, 1]), torch.tensor([1, 2])), |
| 2717 | ... ('user', 'follows', 'game'): (torch.tensor([0, 1, 2]), torch.tensor([1, 2, 3])), |
| 2718 | ... ('user', 'plays', 'game'): (torch.tensor([1, 3]), torch.tensor([2, 3])) |
| 2719 | ... }) |
| 2720 | |
| 2721 | Query for the number of edges. |
| 2722 | |
| 2723 | >>> g.num_edges('plays') |
| 2724 | 2 |
| 2725 | >>> g.num_edges() |
| 2726 | 7 |
| 2727 | |
| 2728 | Use a canonical edge type instead when there is ambiguity for an edge type. |
| 2729 | |
| 2730 | >>> g.num_edges(('user', 'follows', 'user')) |
| 2731 | 2 |
| 2732 | >>> g.num_edges(('user', 'follows', 'game')) |
| 2733 | 3 |
| 2734 | """ |
| 2735 | if etype is None: |
| 2736 | return sum( |
| 2737 | [ |
| 2738 | self._graph.num_edges(etid) |
| 2739 | for etid in range(len(self.canonical_etypes)) |
| 2740 | ] |
| 2741 | ) |
| 2742 | else: |