"sgl-kernel/python/vscode:/vscode.git/clone" did not exist on "b6d0ce9f7839bd2e42a915529673fc4797829ae6"
subgraph.cc 5.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*!
 *  Copyright (c) 2020 by Contributors
 * \file graph/subgraph.cc
 * \brief Functions for extracting subgraphs.
 */
#include "./heterograph.h"
using namespace dgl::runtime;

namespace dgl {

11
12
13
HeteroSubgraph InEdgeGraphRelabelNodes(
    const HeteroGraphPtr graph, const std::vector<IdArray>& vids) {
  CHECK_EQ(vids.size(), graph->NumVertexTypes())
14
15
      << "Invalid input: the input list size must be the same as the number of "
         "vertex types.";
16
  std::vector<IdArray> eids(graph->NumEdgeTypes());
17
  DGLContext ctx = aten::GetContextOf(vids);
18
19
20
21
  for (dgl_type_t etype = 0; etype < graph->NumEdgeTypes(); ++etype) {
    auto pair = graph->meta_graph()->FindEdge(etype);
    const dgl_type_t dst_vtype = pair.second;
    if (aten::IsNullArray(vids[dst_vtype])) {
22
      eids[etype] = IdArray::Empty({0}, graph->DataType(), ctx);
23
24
25
26
27
28
29
30
31
32
    } else {
      const auto& earr = graph->InEdges(etype, {vids[dst_vtype]});
      eids[etype] = earr.id;
    }
  }
  return graph->EdgeSubgraph(eids, false);
}

HeteroSubgraph InEdgeGraphNoRelabelNodes(
    const HeteroGraphPtr graph, const std::vector<IdArray>& vids) {
33
34
  // TODO(mufei): This should also use EdgeSubgraph once it is supported for CSR
  // graphs
35
  CHECK_EQ(vids.size(), graph->NumVertexTypes())
36
37
      << "Invalid input: the input list size must be the same as the number of "
         "vertex types.";
38
39
  std::vector<HeteroGraphPtr> subrels(graph->NumEdgeTypes());
  std::vector<IdArray> induced_edges(graph->NumEdgeTypes());
40
  DGLContext ctx = aten::GetContextOf(vids);
41
42
43
44
45
46
47
48
  for (dgl_type_t etype = 0; etype < graph->NumEdgeTypes(); ++etype) {
    auto pair = graph->meta_graph()->FindEdge(etype);
    const dgl_type_t src_vtype = pair.first;
    const dgl_type_t dst_vtype = pair.second;
    auto relgraph = graph->GetRelationGraph(etype);
    if (aten::IsNullArray(vids[dst_vtype])) {
      // create a placeholder graph
      subrels[etype] = UnitGraph::Empty(
49
50
51
52
          relgraph->NumVertexTypes(), graph->NumVertices(src_vtype),
          graph->NumVertices(dst_vtype), graph->DataType(), ctx);
      induced_edges[etype] =
          IdArray::Empty({0}, graph->DataType(), graph->Context());
53
54
55
    } else {
      const auto& earr = graph->InEdges(etype, {vids[dst_vtype]});
      subrels[etype] = UnitGraph::CreateFromCOO(
56
57
          relgraph->NumVertexTypes(), graph->NumVertices(src_vtype),
          graph->NumVertices(dst_vtype), earr.src, earr.dst);
58
59
60
61
      induced_edges[etype] = earr.id;
    }
  }
  HeteroSubgraph ret;
62
63
  ret.graph = CreateHeteroGraph(
      graph->meta_graph(), subrels, graph->NumVerticesPerType());
64
65
66
67
  ret.induced_edges = std::move(induced_edges);
  return ret;
}

68
HeteroSubgraph InEdgeGraph(
69
70
    const HeteroGraphPtr graph, const std::vector<IdArray>& vids,
    bool relabel_nodes) {
71
72
73
74
75
76
77
78
79
80
  if (relabel_nodes) {
    return InEdgeGraphRelabelNodes(graph, vids);
  } else {
    return InEdgeGraphNoRelabelNodes(graph, vids);
  }
}

HeteroSubgraph OutEdgeGraphRelabelNodes(
    const HeteroGraphPtr graph, const std::vector<IdArray>& vids) {
  CHECK_EQ(vids.size(), graph->NumVertexTypes())
81
82
      << "Invalid input: the input list size must be the same as the number of "
         "vertex types.";
83
  std::vector<IdArray> eids(graph->NumEdgeTypes());
84
  DGLContext ctx = aten::GetContextOf(vids);
85
86
87
88
  for (dgl_type_t etype = 0; etype < graph->NumEdgeTypes(); ++etype) {
    auto pair = graph->meta_graph()->FindEdge(etype);
    const dgl_type_t src_vtype = pair.first;
    if (aten::IsNullArray(vids[src_vtype])) {
89
      eids[etype] = IdArray::Empty({0}, graph->DataType(), ctx);
90
91
92
93
94
95
96
97
98
99
    } else {
      const auto& earr = graph->OutEdges(etype, {vids[src_vtype]});
      eids[etype] = earr.id;
    }
  }
  return graph->EdgeSubgraph(eids, false);
}

HeteroSubgraph OutEdgeGraphNoRelabelNodes(
    const HeteroGraphPtr graph, const std::vector<IdArray>& vids) {
100
101
  // TODO(mufei): This should also use EdgeSubgraph once it is supported for CSR
  // graphs
102
  CHECK_EQ(vids.size(), graph->NumVertexTypes())
103
104
      << "Invalid input: the input list size must be the same as the number of "
         "vertex types.";
105
106
  std::vector<HeteroGraphPtr> subrels(graph->NumEdgeTypes());
  std::vector<IdArray> induced_edges(graph->NumEdgeTypes());
107
  DGLContext ctx = aten::GetContextOf(vids);
108
109
110
111
112
113
114
115
  for (dgl_type_t etype = 0; etype < graph->NumEdgeTypes(); ++etype) {
    auto pair = graph->meta_graph()->FindEdge(etype);
    const dgl_type_t src_vtype = pair.first;
    const dgl_type_t dst_vtype = pair.second;
    auto relgraph = graph->GetRelationGraph(etype);
    if (aten::IsNullArray(vids[src_vtype])) {
      // create a placeholder graph
      subrels[etype] = UnitGraph::Empty(
116
117
118
119
          relgraph->NumVertexTypes(), graph->NumVertices(src_vtype),
          graph->NumVertices(dst_vtype), graph->DataType(), ctx);
      induced_edges[etype] =
          IdArray::Empty({0}, graph->DataType(), graph->Context());
120
121
122
    } else {
      const auto& earr = graph->OutEdges(etype, {vids[src_vtype]});
      subrels[etype] = UnitGraph::CreateFromCOO(
123
124
          relgraph->NumVertexTypes(), graph->NumVertices(src_vtype),
          graph->NumVertices(dst_vtype), earr.src, earr.dst);
125
126
127
128
      induced_edges[etype] = earr.id;
    }
  }
  HeteroSubgraph ret;
129
130
  ret.graph = CreateHeteroGraph(
      graph->meta_graph(), subrels, graph->NumVerticesPerType());
131
132
133
134
  ret.induced_edges = std::move(induced_edges);
  return ret;
}

135
HeteroSubgraph OutEdgeGraph(
136
137
    const HeteroGraphPtr graph, const std::vector<IdArray>& vids,
    bool relabel_nodes) {
138
139
140
141
142
143
144
  if (relabel_nodes) {
    return OutEdgeGraphRelabelNodes(graph, vids);
  } else {
    return OutEdgeGraphNoRelabelNodes(graph, vids);
  }
}

145
}  // namespace dgl