Return whether the graph is a bidirected graph. A graph is bidirected if for any edge :math:`(u, v)` in :math:`G` with weight :math:`w`, there exists an edge :math:`(v, u)` in :math:`G` with the same weight.
(g)
| 1292 | |
| 1293 | |
| 1294 | def is_bidirected(g): |
| 1295 | """Return whether the graph is a bidirected graph. |
| 1296 | |
| 1297 | A graph is bidirected if for any edge :math:`(u, v)` in :math:`G` with weight :math:`w`, |
| 1298 | there exists an edge :math:`(v, u)` in :math:`G` with the same weight. |
| 1299 | """ |
| 1300 | src, dst = g.edges() |
| 1301 | num_nodes = g.num_nodes() |
| 1302 | |
| 1303 | # Sort first by src then dst |
| 1304 | idx_src_dst = src * num_nodes + dst |
| 1305 | perm_src_dst = F.argsort(idx_src_dst, dim=0, descending=False) |
| 1306 | src1, dst1 = src[perm_src_dst], dst[perm_src_dst] |
| 1307 | |
| 1308 | # Sort first by dst then src |
| 1309 | idx_dst_src = dst * num_nodes + src |
| 1310 | perm_dst_src = F.argsort(idx_dst_src, dim=0, descending=False) |
| 1311 | src2, dst2 = src[perm_dst_src], dst[perm_dst_src] |
| 1312 | |
| 1313 | return F.allclose(src1, dst2) and F.allclose(src2, dst1) |
| 1314 | |
| 1315 | |
| 1316 | # pylint: disable=C0103 |