subgraph.cc 5.57 KB
Newer Older
sangwzh's avatar
sangwzh committed
1
// !!! This is a file automatically generated by hipify!!!
2
/**
3
 *  Copyright (c) 2020 by Contributors
4
5
 * @file graph/subgraph.cc
 * @brief Functions for extracting subgraphs.
6
 */
sangwzh's avatar
sangwzh committed
7
#include "heterograph.h"
8
9
10
11
using namespace dgl::runtime;

namespace dgl {

12
13
14
HeteroSubgraph InEdgeGraphRelabelNodes(
    const HeteroGraphPtr graph, const std::vector<IdArray>& vids) {
  CHECK_EQ(vids.size(), graph->NumVertexTypes())
15
16
      << "Invalid input: the input list size must be the same as the number of "
         "vertex types.";
17
  std::vector<IdArray> eids(graph->NumEdgeTypes());
18
  DGLContext ctx = aten::GetContextOf(vids);
19
20
21
22
  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])) {
23
      eids[etype] = IdArray::Empty({0}, graph->DataType(), ctx);
24
25
26
27
28
29
30
31
32
33
    } 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) {
34
35
  // TODO(mufei): This should also use EdgeSubgraph once it is supported for CSR
  // graphs
36
  CHECK_EQ(vids.size(), graph->NumVertexTypes())
37
38
      << "Invalid input: the input list size must be the same as the number of "
         "vertex types.";
39
40
  std::vector<HeteroGraphPtr> subrels(graph->NumEdgeTypes());
  std::vector<IdArray> induced_edges(graph->NumEdgeTypes());
41
  DGLContext ctx = aten::GetContextOf(vids);
42
43
44
45
46
47
48
49
  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(
50
51
52
53
          relgraph->NumVertexTypes(), graph->NumVertices(src_vtype),
          graph->NumVertices(dst_vtype), graph->DataType(), ctx);
      induced_edges[etype] =
          IdArray::Empty({0}, graph->DataType(), graph->Context());
54
55
56
    } else {
      const auto& earr = graph->InEdges(etype, {vids[dst_vtype]});
      subrels[etype] = UnitGraph::CreateFromCOO(
57
58
          relgraph->NumVertexTypes(), graph->NumVertices(src_vtype),
          graph->NumVertices(dst_vtype), earr.src, earr.dst);
59
60
61
62
      induced_edges[etype] = earr.id;
    }
  }
  HeteroSubgraph ret;
63
64
  ret.graph = CreateHeteroGraph(
      graph->meta_graph(), subrels, graph->NumVerticesPerType());
65
66
67
68
  ret.induced_edges = std::move(induced_edges);
  return ret;
}

69
HeteroSubgraph InEdgeGraph(
70
71
    const HeteroGraphPtr graph, const std::vector<IdArray>& vids,
    bool relabel_nodes) {
72
73
74
75
76
77
78
79
80
81
  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())
82
83
      << "Invalid input: the input list size must be the same as the number of "
         "vertex types.";
84
  std::vector<IdArray> eids(graph->NumEdgeTypes());
85
  DGLContext ctx = aten::GetContextOf(vids);
86
87
88
89
  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])) {
90
      eids[etype] = IdArray::Empty({0}, graph->DataType(), ctx);
91
92
93
94
95
96
97
98
99
100
    } 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) {
101
102
  // TODO(mufei): This should also use EdgeSubgraph once it is supported for CSR
  // graphs
103
  CHECK_EQ(vids.size(), graph->NumVertexTypes())
104
105
      << "Invalid input: the input list size must be the same as the number of "
         "vertex types.";
106
107
  std::vector<HeteroGraphPtr> subrels(graph->NumEdgeTypes());
  std::vector<IdArray> induced_edges(graph->NumEdgeTypes());
108
  DGLContext ctx = aten::GetContextOf(vids);
109
110
111
112
113
114
115
116
  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(
117
118
119
120
          relgraph->NumVertexTypes(), graph->NumVertices(src_vtype),
          graph->NumVertices(dst_vtype), graph->DataType(), ctx);
      induced_edges[etype] =
          IdArray::Empty({0}, graph->DataType(), graph->Context());
121
122
123
    } else {
      const auto& earr = graph->OutEdges(etype, {vids[src_vtype]});
      subrels[etype] = UnitGraph::CreateFromCOO(
124
125
          relgraph->NumVertexTypes(), graph->NumVertices(src_vtype),
          graph->NumVertices(dst_vtype), earr.src, earr.dst);
126
127
128
129
      induced_edges[etype] = earr.id;
    }
  }
  HeteroSubgraph ret;
130
131
  ret.graph = CreateHeteroGraph(
      graph->meta_graph(), subrels, graph->NumVerticesPerType());
132
133
134
135
  ret.induced_edges = std::move(induced_edges);
  return ret;
}

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

146
}  // namespace dgl