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

Method adjacency_matrix

python/dgl/graph_index.py:736–791  ·  view source on GitHub ↗

Return the adjacency matrix representation of this graph. By default, a row of returned adjacency matrix represents the destination of an edge and the column represents the source. When transpose is True, a row represents the source and a column represents a destina

(self, transpose, ctx)

Source from the content-addressed store, hash-verified

734 return utils.toindex(order), utils.toindex(rev_order)
735
736 def adjacency_matrix(self, transpose, ctx):
737 """Return the adjacency matrix representation of this graph.
738
739 By default, a row of returned adjacency matrix represents the destination
740 of an edge and the column represents the source.
741
742 When transpose is True, a row represents the source and a column represents
743 a destination.
744
745 Parameters
746 ----------
747 transpose : bool
748 A flag to transpose the returned adjacency matrix.
749 ctx : context
750 The context of the returned matrix.
751
752 Returns
753 -------
754 SparseTensor
755 The adjacency matrix.
756 utils.Index
757 A index for data shuffling due to sparse format change. Return None
758 if shuffle is not required.
759 """
760 if not isinstance(transpose, bool):
761 raise DGLError(
762 'Expect bool value for "transpose" arg,'
763 " but got %s." % (type(transpose))
764 )
765 fmt = F.get_preferred_sparse_format()
766 rst = _CAPI_DGLGraphGetAdj(self, transpose, fmt)
767 if fmt == "csr":
768 indptr = F.copy_to(utils.toindex(rst(0)).tousertensor(), ctx)
769 indices = F.copy_to(utils.toindex(rst(1)).tousertensor(), ctx)
770 shuffle = utils.toindex(rst(2))
771 dat = F.ones(indices.shape, dtype=F.float32, ctx=ctx)
772 spmat = F.sparse_matrix(
773 dat,
774 ("csr", indices, indptr),
775 (self.num_nodes(), self.num_nodes()),
776 )[0]
777 return spmat, shuffle
778 elif fmt == "coo":
779 ## FIXME(minjie): data type
780 idx = F.copy_to(utils.toindex(rst(0)).tousertensor(), ctx)
781 m = self.num_edges()
782 idx = F.reshape(idx, (2, m))
783 dat = F.ones((m,), dtype=F.float32, ctx=ctx)
784 n = self.num_nodes()
785 adj, shuffle_idx = F.sparse_matrix(dat, ("coo", idx), (n, n))
786 shuffle_idx = (
787 utils.toindex(shuffle_idx) if shuffle_idx is not None else None
788 )
789 return adj, shuffle_idx
790 else:
791 raise Exception("unknown format")
792
793 def incidence_matrix(self, typestr, ctx):

Callers

nothing calls this directly

Calls 5

num_nodesMethod · 0.95
num_edgesMethod · 0.95
DGLErrorClass · 0.85
tousertensorMethod · 0.80
copy_toMethod · 0.45

Tested by

no test coverage detected