dgl.graphbolt.add_reverse_edgesο
- dgl.graphbolt.add_reverse_edges(edges: Dict[str, Tuple[Tensor, Tensor]] | Tuple[Tensor, Tensor], reverse_etypes_mapping: Dict[str, str] | None = None)[source]ο
This function finds the reverse edges of the given edges and returns the composition of them. In a homogeneous graph, reverse edges have inverted source and destination node IDs. While in a heterogeneous graph, reversing also involves swapping node IDs and their types. This function could be used before exclude_edges function to help find targeting edges. Note: The found reverse edges may not really exists in the original graph. And repeat edges could be added becasue reverse edges may already exists in the edges.
- Parameters:
edges (Union[Dict[str, Tuple[torch.Tensor, torch.Tensor]],) β Tuple[torch.Tensor, torch.Tensor]] - If sampled subgraph is homogeneous, then edges should be a pair of of tensors. - If sampled subgraph is heterogeneous, then edges should be a dictionary of edge types and the corresponding edges to exclude.
reverse_etypes_mapping (Dict[str, str], optional) β The mapping from the original edge types to their reverse edge types.
- Returns:
Tuple[torch.Tensor, torch.Tensor]] The node pairs contain both the original edges and their reverse counterparts.
- Return type:
Union[Dict[str, Tuple[torch.Tensor, torch.Tensor]],
Examples
>>> edges = {"A:r:B": (torch.tensor([0, 1]), torch.tensor([1, 2]))} >>> print(gb.add_reverse_edges(edges, {"A:r:B": "B:rr:A"})) {'A:r:B': (tensor([0, 1]), tensor([1, 2])), 'B:rr:A': (tensor([1, 2]), tensor([0, 1]))}
>>> edges = (torch.tensor([0, 1]), torch.tensor([2, 1])) >>> print(gb.add_reverse_edges(edges)) (tensor([0, 1, 2, 1]), tensor([2, 1, 0, 1]))