"sgl-kernel/vscode:/vscode.git/clone" did not exist on "d738ab52f86f65806fc6549270a704e34aae5a32"
traversal.h 9.38 KB
Newer Older
GaiYu0's avatar
GaiYu0 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
/*!
 *  Copyright (c) 2018 by Contributors
 * \file graph/traversal.h
 * \brief Graph traversal routines.
 *
 * Traversal routines generate frontiers. Frontiers can be node frontiers or edge
 * frontiers depending on the traversal function. Each frontier is a
 * list of nodes/edges (specified by their ids). An optional tag can be specified
 * for each node/edge (represented by an int value).
 */
#ifndef DGL_GRAPH_TRAVERSAL_H_
#define DGL_GRAPH_TRAVERSAL_H_

14
#include <dgl/graph_interface.h>
GaiYu0's avatar
GaiYu0 committed
15
16
17
18
19
20
21
22
23
24
#include <stack>
#include <tuple>
#include <vector>

namespace dgl {
namespace traverse {

/*!
 * \brief Traverse the graph in a breadth-first-search (BFS) order.
 *
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 * The queue object must suffice following interface:
 *   Members:
 *   void push(dgl_id_t);  // push one node
 *   dgl_id_t top();       // get the first node
 *   void pop();           // pop one node
 *   bool empty();         // return true if the queue is empty
 *   size_t size();        // return the size of the queue
 * For example, std::queue<dgl_id_t> is a valid queue type.
 *
 * The visit function must be compatible with following interface:
 *   void (*visit)(dgl_id_t );
 *
 * The frontier function must be compatible with following interface:
 *   void (*make_frontier)(void);
 *
 * \param graph The graph.
GaiYu0's avatar
GaiYu0 committed
41
42
 * \param sources Source nodes.
 * \param reversed If true, BFS follows the in-edge direction
43
44
45
 * \param queue The queue used to do bfs.
 * \param visit The function to call when a node is visited.
 * \param make_frontier The function to indicate that a new froniter can be made;
GaiYu0's avatar
GaiYu0 committed
46
 */
47
template<typename Queue, typename VisitFn, typename FrontierFn>
48
void BFSNodes(const GraphInterface& graph,
GaiYu0's avatar
GaiYu0 committed
49
50
              IdArray source,
              bool reversed,
51
              Queue* queue,
GaiYu0's avatar
GaiYu0 committed
52
53
54
55
56
57
58
              VisitFn visit,
              FrontierFn make_frontier) {
  const int64_t len = source->shape[0];
  const int64_t* src_data = static_cast<int64_t*>(source->data);

  std::vector<bool> visited(graph.NumVertices());
  for (int64_t i = 0; i < len; ++i) {
59
60
61
62
    const dgl_id_t u = src_data[i];
    visited[u] = true;
    visit(u);
    queue->push(u);
GaiYu0's avatar
GaiYu0 committed
63
  }
64
  make_frontier();
GaiYu0's avatar
GaiYu0 committed
65

66
  const auto neighbor_iter = reversed? &GraphInterface::PredVec : &GraphInterface::SuccVec;
67
68
69
70
71
  while (!queue->empty()) {
    const size_t size = queue->size();
    for (size_t i = 0; i < size; ++i) {
      const dgl_id_t u = queue->top();
      queue->pop();
GaiYu0's avatar
GaiYu0 committed
72
73
74
      for (auto v : (graph.*neighbor_iter)(u)) {
        if (!visited[v]) {
          visited[v] = true;
75
76
          visit(v);
          queue->push(v);
GaiYu0's avatar
GaiYu0 committed
77
78
79
        }
      }
    }
80
    make_frontier();
GaiYu0's avatar
GaiYu0 committed
81
82
83
  }
}

Gan Quan's avatar
Gan Quan committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*!
 * \brief Traverse the graph in a breadth-first-search (BFS) order, returning
 *        the edges of the BFS tree.
 *
 * The queue object must suffice following interface:
 *   Members:
 *   void push(dgl_id_t);  // push one node
 *   dgl_id_t top();       // get the first node
 *   void pop();           // pop one node
 *   bool empty();         // return true if the queue is empty
 *   size_t size();        // return the size of the queue
 * For example, std::queue<dgl_id_t> is a valid queue type.
 *
 * The visit function must be compatible with following interface:
 *   void (*visit)(dgl_id_t );
 *
 * The frontier function must be compatible with following interface:
 *   void (*make_frontier)(void);
 *
 * \param graph The graph.
 * \param sources Source nodes.
 * \param reversed If true, BFS follows the in-edge direction
 * \param queue The queue used to do bfs.
 * \param visit The function to call when a node is visited.
 *        The argument would be edge ID.
 * \param make_frontier The function to indicate that a new frontier can be made;
 */
template<typename Queue, typename VisitFn, typename FrontierFn>
112
void BFSEdges(const GraphInterface& graph,
Gan Quan's avatar
Gan Quan committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
              IdArray source,
              bool reversed,
              Queue* queue,
              VisitFn visit,
              FrontierFn make_frontier) {
  const int64_t len = source->shape[0];
  const int64_t* src_data = static_cast<int64_t*>(source->data);

  std::vector<bool> visited(graph.NumVertices());
  for (int64_t i = 0; i < len; ++i) {
    const dgl_id_t u = src_data[i];
    visited[u] = true;
    queue->push(u);
  }
  make_frontier();

129
  const auto neighbor_iter = reversed? &GraphInterface::InEdgeVec : &GraphInterface::OutEdgeVec;
Gan Quan's avatar
Gan Quan committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  while (!queue->empty()) {
    const size_t size = queue->size();
    for (size_t i = 0; i < size; ++i) {
      const dgl_id_t u = queue->top();
      queue->pop();
      for (auto e : (graph.*neighbor_iter)(u)) {
        const auto uv = graph.FindEdge(e);
        const dgl_id_t v = (reversed ? uv.first : uv.second);
        if (!visited[v]) {
          visited[v] = true;
          visit(e);
          queue->push(v);
        }
      }
    }
    make_frontier();
  }
}

GaiYu0's avatar
GaiYu0 committed
149
150
151
/*!
 * \brief Traverse the graph in topological order.
 *
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
 * The queue object must suffice following interface:
 *   Members:
 *   void push(dgl_id_t);  // push one node
 *   dgl_id_t top();       // get the first node
 *   void pop();           // pop one node
 *   bool empty();         // return true if the queue is empty
 *   size_t size();        // return the size of the queue
 * For example, std::queue<dgl_id_t> is a valid queue type.
 *
 * The visit function must be compatible with following interface:
 *   void (*visit)(dgl_id_t );
 *
 * The frontier function must be compatible with following interface:
 *   void (*make_frontier)(void);
 *
 * \param graph The graph.
GaiYu0's avatar
GaiYu0 committed
168
 * \param reversed If true, follows the in-edge direction
169
170
171
 * \param queue The queue used to do bfs.
 * \param visit The function to call when a node is visited.
 * \param make_frontier The function to indicate that a new froniter can be made;
GaiYu0's avatar
GaiYu0 committed
172
 */
173
template<typename Queue, typename VisitFn, typename FrontierFn>
174
void TopologicalNodes(const GraphInterface& graph,
GaiYu0's avatar
GaiYu0 committed
175
                      bool reversed,
176
                      Queue* queue,
GaiYu0's avatar
GaiYu0 committed
177
178
                      VisitFn visit,
                      FrontierFn make_frontier) {
179
180
  const auto get_degree = reversed? &GraphInterface::OutDegree : &GraphInterface::InDegree;
  const auto neighbor_iter = reversed? &GraphInterface::PredVec : &GraphInterface::SuccVec;
GaiYu0's avatar
GaiYu0 committed
181
182
183
184
185
186
  uint64_t num_visited_nodes = 0;
  std::vector<uint64_t> degrees(graph.NumVertices(), 0);
  for (dgl_id_t vid = 0; vid < graph.NumVertices(); ++vid) {
    degrees[vid] = (graph.*get_degree)(vid);
    if (degrees[vid] == 0) {
      visit(vid);
187
      queue->push(vid);
GaiYu0's avatar
GaiYu0 committed
188
189
190
      ++num_visited_nodes;
    }
  }
191
  make_frontier();
GaiYu0's avatar
GaiYu0 committed
192

193
194
195
196
197
  while (!queue->empty()) {
    const size_t size = queue->size();
    for (size_t i = 0; i < size; ++i) {
      const dgl_id_t u = queue->top();
      queue->pop();
GaiYu0's avatar
GaiYu0 committed
198
199
200
      for (auto v : (graph.*neighbor_iter)(u)) {
        if (--(degrees[v]) == 0) {
          visit(v);
201
          queue->push(v);
GaiYu0's avatar
GaiYu0 committed
202
203
204
205
          ++num_visited_nodes;
        }
      }
    }
206
    make_frontier();
GaiYu0's avatar
GaiYu0 committed
207
  }
208

GaiYu0's avatar
GaiYu0 committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
  if (num_visited_nodes != graph.NumVertices()) {
    LOG(FATAL) << "Error in topological traversal: loop detected in the given graph.";
  }
}

/*!\brief Tags for ``DFSEdges``. */
enum DFSEdgeTag {
  kForward = 0,
  kReverse,
  kNonTree,
};
/*!
 * \brief Traverse the graph in a depth-first-search (DFS) order.
 *
 * The traversal visit edges in its DFS order. Edges have three tags:
 * FORWARD(0), REVERSE(1), NONTREE(2)
 *
 * A FORWARD edge is one in which `u` has been visisted but `v` has not.
 * A REVERSE edge is one in which both `u` and `v` have been visisted and the edge
 * is in the DFS tree.
 * A NONTREE edge is one in which both `u` and `v` have been visisted but the edge
 * is NOT in the DFS tree.
 *
 * \param source Source node.
 * \param reversed If true, DFS follows the in-edge direction
 * \param has_reverse_edge If true, REVERSE edges are included
 * \param has_nontree_edge If true, NONTREE edges are included
 * \param visit The function to call when an edge is visited; the edge id and its
 *              tag will be given as the arguments.
 */
template<typename VisitFn>
240
void DFSLabeledEdges(const GraphInterface& graph,
GaiYu0's avatar
GaiYu0 committed
241
242
243
244
245
                     dgl_id_t source,
                     bool reversed,
                     bool has_reverse_edge,
                     bool has_nontree_edge,
                     VisitFn visit) {
246
247
  const auto succ = reversed? &GraphInterface::PredVec : &GraphInterface::SuccVec;
  const auto out_edge = reversed? &GraphInterface::InEdgeVec : &GraphInterface::OutEdgeVec;
GaiYu0's avatar
GaiYu0 committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

  if ((graph.*succ)(source).size() == 0) {
    // no out-going edges from the source node
    return;
  }

  typedef std::tuple<dgl_id_t, size_t, bool> StackEntry;
  std::stack<StackEntry> stack;
  std::vector<bool> visited(graph.NumVertices());
  visited[source] = true;
  stack.push(std::make_tuple(source, 0, false));
  dgl_id_t u = 0;
  size_t i = 0;
  bool on_tree = false;

  while (!stack.empty()) {
    std::tie(u, i, on_tree) = stack.top();
    const dgl_id_t v = (graph.*succ)(u)[i];
    const dgl_id_t uv = (graph.*out_edge)(u)[i];
    if (visited[v]) {
      if (!on_tree && has_nontree_edge) {
        visit(uv, kNonTree);
      } else if (on_tree && has_reverse_edge) {
        visit(uv, kReverse);
      }
      stack.pop();
      // find next one.
      if (i < (graph.*succ)(u).size() - 1) {
        stack.push(std::make_tuple(u, i+1, false));
      }
    } else {
      visited[v] = true;
      std::get<2>(stack.top()) = true;
      visit(uv, kForward);
      // expand
      if ((graph.*succ)(v).size() > 0) {
        stack.push(std::make_tuple(v, 0, false));
      }
    }
  }
}

}  // namespace traverse
}  // namespace dgl

#endif  // DGL_GRAPH_TRAVERSAL_H_
294