graph_op.h 6 KB
Newer Older
1
2
3
4
5
/*!
 *  Copyright (c) 2018 by Contributors
 * \file dgl/graph_op.h
 * \brief Operations on graph index.
 */
Minjie Wang's avatar
Minjie Wang committed
6
7
8
#ifndef DGL_GRAPH_OP_H_
#define DGL_GRAPH_OP_H_

9
#include <vector>
Minjie Wang's avatar
Minjie Wang committed
10
#include "graph.h"
11
#include "immutable_graph.h"
Minjie Wang's avatar
Minjie Wang committed
12
13
14
15
16

namespace dgl {

class GraphOp {
 public:
17
18
  /*!
   * \brief Return the line graph.
Minjie Wang's avatar
Minjie Wang committed
19
20
21
22
23
   *
   * If i~j and j~i are two edges in original graph G, then
   * (i,j)~(j,i) and (j,i)~(i,j) are the "backtracking" edges on
   * the line graph.
   *
24
   * \param graph The input graph.
Minjie Wang's avatar
Minjie Wang committed
25
   * \param backtracking Whether the backtracking edges are included or not
26
27
   * \return the line graph
   */
Minjie Wang's avatar
Minjie Wang committed
28
  static Graph LineGraph(const Graph* graph, bool backtracking);
GaiYu0's avatar
GaiYu0 committed
29

Minjie Wang's avatar
Minjie Wang committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  /*!
   * \brief Return a disjoint union of the input graphs.
   *
   * The new graph will include all the nodes/edges in the given graphs.
   * Nodes/Edges will be relabled by adding the cumsum of the previous graph sizes
   * in the given sequence order. For example, giving input [g1, g2, g3], where
   * they have 5, 6, 7 nodes respectively. Then node#2 of g2 will become node#7
   * in the result graph. Edge ids are re-assigned similarly.
   *
   * \param graphs A list of input graphs to be unioned.
   * \return the disjoint union of the graphs
   */
  static Graph DisjointUnion(std::vector<const Graph*> graphs);

  /*!
   * \brief Partition the graph into several subgraphs.
   *
Minjie Wang's avatar
Minjie Wang committed
47
48
   * This is a reverse operation of DisjointUnion. The graph will be partitioned
   * into num graphs. This requires the given number of partitions to evenly
Minjie Wang's avatar
Minjie Wang committed
49
50
   * divides the number of nodes in the graph.
   * 
Minjie Wang's avatar
Minjie Wang committed
51
   * \param graph The graph to be partitioned.
Minjie Wang's avatar
Minjie Wang committed
52
53
54
   * \param num The number of partitions.
   * \return a list of partitioned graphs
   */
Minjie Wang's avatar
Minjie Wang committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  static std::vector<Graph> DisjointPartitionByNum(const Graph* graph, int64_t num);

  /*!
   * \brief Partition the graph into several subgraphs.
   *
   * This is a reverse operation of DisjointUnion. The graph will be partitioned
   * based on the given sizes. This requires the sum of the given sizes is equal
   * to the number of nodes in the graph.
   * 
   * \param graph The graph to be partitioned.
   * \param sizes The number of partitions.
   * \return a list of partitioned graphs
   */
  static std::vector<Graph> DisjointPartitionBySizes(const Graph* graph, IdArray sizes);
69

70
71
72
73
74
75
76
77
78
79
80
81
82
83
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 Return a readonly disjoint union of the input graphs.
  *
  * The new readonly graph will include all the nodes/edges in the given graphs.
  * Nodes/Edges will be relabled in the given sequence order by batching over CSR Graphs.
  * For example, giving input [g1, g2, g3], where
  * they have 5, 6, 7 nodes respectively. Then node#2 of g2 will become node#7
  * in the result graph. Edge ids are re-assigned similarly.
  *
  * \param ImmutableGraph A list of input graphs to be unioned.
  * \return the disjoint union of the ImmutableGraph
  */
  static ImmutableGraph DisjointUnion(std::vector<const ImmutableGraph*> graphs);

   /*!
   * \brief Partition the ImmutableGraph into several immutable subgraphs.
   *
   * This is a reverse operation of DisjointUnion. The graph will be partitioned
   * into num graphs. This requires the given number of partitions to evenly
   * divides the number of nodes in the graph.
   *
   * \param graph The ImmutableGraph to be partitioned.
   * \param num The number of partitions.
   * \return a list of partitioned ImmutableGraph
   */
  static std::vector<ImmutableGraph> DisjointPartitionByNum(const ImmutableGraph *graph,
          int64_t num);

  /*!
  * \brief Partition the ImmutableGraph into several immutable subgraphs.
  *
  * This is a reverse operation of DisjointUnion. The graph will be partitioned
  * based on the given sizes. This requires the sum of the given sizes is equal
  * to the number of nodes in the graph.
  *
  * \param graph The ImmutableGraph to be partitioned.
  * \param sizes The number of partitions.
  * \return a list of partitioned ImmutableGraph
  */
  static std::vector<ImmutableGraph> DisjointPartitionBySizes(const ImmutableGraph *batched_graph,
          IdArray sizes);

112
113
114
  /*!
   * \brief Map vids in the parent graph to the vids in the subgraph.
   *
Da Zheng's avatar
Da Zheng committed
115
116
   * If the Id doesn't exist in the subgraph, -1 will be used.
   *
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
   * \param parent_vid_map An array that maps the vids in the parent graph to the
   * subgraph. The elements store the vertex Ids in the parent graph, and the
   * indices indicate the vertex Ids in the subgraph.
   * \param query The vertex Ids in the parent graph.
   * \return an Id array that contains the subgraph node Ids.
   */
  static IdArray MapParentIdToSubgraphId(IdArray parent_vid_map, IdArray query);

  /*!
   * \brief Expand an Id array based on the offset array.
   *
   * For example,
   * ids:     [0, 1, 2, 3, 4],
   * offset:  [0, 2, 2, 5, 6, 7],
   * result:  [0, 0, 2, 2, 2, 3, 4].
   * The offset array has one more element than the ids array.
   * (offset[i], offset[i+1]) shows the location of ids[i] in the result array.
   *
   * \param ids An array that contains the node or edge Ids.
   * \param offset An array that contains the offset after expansion.
   * \return a expanded Id array.
   */
  static IdArray ExpandIds(IdArray ids, IdArray offset);
140
141
142
143
144
145
146

  /*!
   * \brief Convert the graph to a simple graph.
   * \param graph The input graph.
   * \return a new immutable simple graph with no multi-edge.
   */
  static ImmutableGraph ToSimpleGraph(const GraphInterface* graph);
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

  /*!
   * \brief Convert the graph to a mutable bidirected graph.
   *
   * If the original graph has m edges for i -> j and n edges for
   * j -> i, the new graph will have max(m, n) edges for both
   * i -> j and j -> i.
   *
   * \param graph The input graph.
   * \return a new mutable bidirected graph.
   */
  static Graph ToBidirectedMutableGraph(const GraphInterface* graph);

  /*!
   * \brief Same as BidirectedMutableGraph except that the returned graph is immutable.
   * \param graph The input graph.
   * \return a new immutable bidirected graph.
   */
  static ImmutableGraph ToBidirectedImmutableGraph(const GraphInterface* graph);
Minjie Wang's avatar
Minjie Wang committed
166
167
168
169
170
};

}  // namespace dgl

#endif  // DGL_GRAPH_OP_H_