-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLAGraph.h
More file actions
444 lines (355 loc) · 12.9 KB
/
LAGraph.h
File metadata and controls
444 lines (355 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
240
241
242
243
244
245
246
247
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// LAGraph.h
#define LAGRAPH_ERROR(error_message,info) \
{ \
if (message != NULL) strcpy (message, error_message) ; \
LAGRAPH_FREE_ALL ; \
return (info) ; \
}
// int info ; must be declared to use this macro
#define LAGraph_TRY(LAGraph_or_GrB_method) \
{ \
info = (LAGraph_or_GrB_method) ; \
if (info < 0) \
{ \
LAGRAPH_ERROR ("" __LINE__ __FILE__ , info) ; \
} \
}
// LAGraph_internal.h
#include "LAGraph.h"
#define TRY(method) LAGraph_TRY(method)
typedef enum
{
// enum:
// adjacency
// undirected
// directed
// bipartite
// one kind so far
// later: incidence
// A(i,j) is the edge (i,j)
// undirected: A is square, symmetric (both tril and triu present)
// directed: A is square, unsymmetric or might happen to symmetric
// bipartite: A is rectangular (later) or might happen to be square
LAGRAPH_UNKNOWN = 0,
LAGRAPH_ADJACENCY_UNDIRECTED = 1,
LAGRAPH_ADJACENCY_DIRECTED = 2,
// LAGRAPH_ADJACENCY_UNDIRECTED_TRIL = ...,
// LAGRAPH_ADJACENCY_UNDIRECTED_TRIU = ...,
// LAGRAPH_BIPARTITE = ...,
// LAGRAPH_BIPARTITE_DIRECTED = ...,
// LAGRAPH_BIPARTITE_UNDIRECTED = ...,
// LAGRAPH_INCIDENCE_* = ...,
// LAGRAPH_MULTIGRAPH_* = ...,
// LAGRAPH_HYPERGRAPH = ...,
// LAGRAPH_HYPERGRAPH_DIRECTED = ...,
// for example:
//
// 5 node7 8 11 ...
// ith row: [ -1 -1 1 -1 -1 1 ] ith edge
// ...
}
LAGraph_Kind ;
// LAGraph graph
typedef struct
{
GrB_Matrix A ; // the graph itself:
LAGraph_Kind kind ; // the kind of graph:
// possible future:
// multigraph ..
// GrB_Matrix *Amult ; // array of size nmatrices
// int nmatrices ;
//-----------------------------------------------------------
// cached:
// if present, they are correct.
// to invalidate: delete them
// TODO: write a utility to clear all cache
// when does an algorithm choose to (a) *update* these? (b) delete?
// default: algo decides
// others modes?
// can be recomputed at any time via utility functions,
// or freed at will (if Graph is input/output to a method)
GrB_Matrix AT ;
GrB_Vector rowdegree ;
GrB_Vector coldegree ;
consider: enum A_is_symmetric : yes, no, i don't know
// AT = A' regardless of kind
// rowdegree (i) = nvals (A (i,:)), regardless of kind
// coldegree (j) = nvals (A (:,j)), regardless of kind
// possible future cached properties:
// GrB_Vector rowsum, colsum ;
// rowsum (i) = sum (A (i,:)), regardless of kind
// colsum (j) = sum (A (:,j)), regardless of kind
}
LAGraph_Graph_struct ;
typedef struct LAGraph_Graph_struct *LAGraph_Graph ;
// TODO: discuss sanity checking.
// what if A is unsymmetric, but the graph is labeled "undirected"?
// error? use A+A' ?
LAGraph_Algorithm_Variant ?
GrB_Info LAGraph_BreadthFirstSearch_variant
or
// rule: NULL vs GrB_NULL: use NULL (discuss this later)
// rule: if any output (*arg) is NULL, do not compute
// rule: if any input arg is NULL: use default, don't use it, etc
// rule: output vectors and matrices are "new'd" by the LAGraph function:
// usage:
GrB_Vector level ;
LAGraph_BreadthFirstSearch (&level, NULL, ...)
//------------------------------------------------------------------------------
// ALGO: breadth first search
//------------------------------------------------------------------------------
// Policy: in general, if an input *x is NULL, do not compute
// TODO: hops for all methods?
GrB_Vector level ;
GrB_Vector parent ;
int LAGraph_BreadthFirstSearch // no _Variant suffix
(
// outputs:
GrB_Vector *level, // if NULL do not compute
GrB_Vector *parent, // if NULL do not compute
// inputs:
LAGraph_Graph G,
GrB_Index source,
char *message
) ;
GrB_Vector parent ; // undefined
result = LAGraph_BreadthFirstSearch (NULL, &parent, G, 0, NULL) ;
if result is bad, parent = NULL
int LAGraph_BreadthFirstSearch_MultiSource
(
// outputs:
GrB_Matrix *level, // if NULL do not compute
GrB_Matrix *parent, // if NULL do not compute
// inputs:
LAGraph_Graph G,
GrB_Index *sources, size_t nsources // or LAGraph_array? GrB_array?
char *message
) ;
info = LAGraph_whatever (&level, NULL, G, Src, 5) ;
if info is 'error' then level is returned as level == NULL.
// expert function:
GrB_Info LAGraph_BreadthFirstSearch_Frontier
(
// input/output:
GrB_Vector level,
GrB_Vector parent,
GrB_Vector frontier,
// inputs:
LAGraph_Graph G,
GrB_Vector vertex_mask, // filter the vertices
bool vertex_mask_complement,
bool vertex_mask_structural,
// or:
// GrB_Descriptor desc, // mask complement, mask structural:
// // NULL, GrB_DESC_C, GrB_DESC_S, or GrB_DESC_SC
uint64_t hops // if hops=0, no limit (or INT64_MAX), or "NONE"
) ;
//------------------------------------------------------------------------------
// ALGO: connected components:
//------------------------------------------------------------------------------
// TODO: utility to do SCC = Comm * G * Comm'
// TODO: utility to do Gnew = Perm * G * Perm' ; or Gnew = G (P,P)
// TODO: utility to convert Perm matrix <==> perm "vector" (array?) GrB_Index*
// SCC graph = Community * G * Community', a contracted graph
GrB_Info LAGraph_Community_Map
(
// output:
uint64_t *ncomponents,
GrB_Vector *component, // vertex i is in component(i), a dense vector.
// input:
GrB_Matrix Community // Community(c,i)=1 becomes c=component(i)
) ;
// Gnew = Perm * G *Perm', then Gnew is block diagonal
GrB_Info LAGraph_Community_Permutation
(
// output:
GrB_Matrix *Perm, // Perm(...), n-by-n
// input:
GrB_Matrix Community // Community(c,i)=1 becomes c=component(i)
) ;
GrB_Info LAGraph_ConnectedComponents
(
// output:
GrB_Matrix *Community, // k-by-n if there are k components. GrB_BOOL.
// Community(c,i)=1 if c=component(i)
// TODO: or n-by-k?
// input:
LAGraph_Graph G // if directed: strongly cc
) ;
GrB_Info LAGraph_ConnectedComponents_Weakly
(
// output:
GrB_Matrix *Community, // k-by-n if there are k components. GrB_BOOL.
// Community(c,i)=1 if c=component(i)
// input:
LAGraph_Graph G
) ;
//------------------------------------------------------------------------------
// ALGO: centrality:
//------------------------------------------------------------------------------
// TODO: utility to remove self loops, or add all self-loops (G=G+I), ...
// TODO: utility like trace(G), trace-like methods
// TODO: G =G+G', G = spones(G)
// random number generator? vertex sampler?
// exact vs approx
// TODO make 2 different typedef enums, for VertexC. and EdgeC.
typedef enum
{
LAGR_BETWEENNESS_FP32 = 4,
LAGR_BETWEENNESS_FP64 = 5,
...
}
LAGraph_VertexCentrality_Type ;
// this: easy mode
GrB_Vector centrality ; // uninit
LAGraph_something (¢rality, ...)
// then for 'expert' mode
GrB_Vector x ; //
LAGraph_something_init (&x, ...) or GrB_Vector_new (&x, n, 1, INT32)
for a billion times:
LAGraph_something_tiny (x, ...) // reuse it, might be faster
GrB_Info LAGraph_VertexCentrality
(
// output:
GrB_Vector *centrality,
// input:
LAGraph_Graph G,
LAGraph_VertexCentrality_Type kind
// betweenness, eigenvector, degree, pagerank, ...
// which pagerank? parameters to pagerank?
// LAGR_BETWEENNESS_FP32,
// LAGR_BETWEENNESS_FP64
) ;
LAGraph_VertexCentrality (°ree, G, LAGR_OUTDEGREE) ;
// utility function:
LAGraph_Degree (°ree, G, kind)
// or:
LAGraph_Degree_in (°ree, G)
LAGraph_Degree_out (°ree, G) // ... etc
LAGraph_Degree_etc (°ree, G) // ... etc
GrB_Info LAGraph_EdgeCentrality
(
// output:
GrB_Matrix *Centrality,
// input:
LAGraph_Graph G,
LAGraph_EdgeCentrality_Type kind
// betweeness, eigenvector, degree, pagerank, ...
// LAGR_BETWEENNESS_FP32,
// LAGR_BETWEENNESS_FP64
) ;
//------------------------------------------------------------------------------
// ALGO: shortest paths:
//------------------------------------------------------------------------------
GrB_Info LAGraph_ShortestPath_[...]
GrB_Info LAGraph_ShortestPath_SingleSource
(
// output: if NULL do not compute
GrB_Vector *distance, // type: INT64, FP32, or FP64
// INT64: if G is int*
// UINT64: if G is bool, uint*
// FP32: if G is FP32
// FP64: if G is FP64
GrB_Vector *parent, // tree
GrB_Vector *hops, // # of hops, level from source (call it "level")?
// input:
GrB_Index source,
LAGraph_Graph G,
// error handling (every LAGraph function has this last):
char *message
) ;
// negative-weight-cycle: result is not defined, must stop!
// report info < 0 (an error)
GrB_Info LAGraph_ShortestPath_AllPairs
(
// output:
GrB_Matrix *Distance,
GrB_Matrix *Parent,
GrB_Matrix *Hops,
// input:
LAGraph_Graph G,
// input/output
char *message
) ;
// Dist, Parent undefined on input, created on output, just like GrB_Matrix_new
info = LAGraph_something (&Dist, &Parent, NULL, G) ;
//------------------------------------------------------------------------------
// final design for error handling:
//------------------------------------------------------------------------------
// but is LAGRAPH_MESSAGE_LENGTH too wordy?
#define LAGRAPH_MESSAGE_LENGTH 256
char message [LAGRAPH_MESSAGE_LENGTH] ;
int info = LAGraph_ShortestPath_AllPairs (Dis, Pa, Hop, G, &message) ;
// convention: 0:ok, < error, > warning.
// if no message, we set message [0] = '\0' (if not NULL)
// no message:
int info = LAGraph_ShortestPath_AllPairs (Dis, Pa, Hop, G, NULL) ;
LAGraph info: not an enum, just an int
//------------------------------------------------------------------------------
// what's next?
//------------------------------------------------------------------------------
GAP: BFS, SSSP, TriangleCount, Conn.Components, PageRank, BetweennessCentrality
LDBC: Local Clustering Coef, CDLP, different PageRank
Luby's MIS
minimum spanning forest
k-truss
max flow
Louvain community detection
Notation
Doxygen
//------------------------------------------------------------------------------
// ALGO: triangle counting
//------------------------------------------------------------------------------
// LAGraph_Graph G: is a read-only object if "input"
int LAGraph_TriangleCount
(
uint64_t *ntriangles, // # of triangles
// input:
LAGraph_Graph G,
// input/output:
char *message
) ;
int LAGraph_TriangleCount_expert
(
uint64_t *ntriangles, // # of triangles
// input:
int method,
LAGraph_Graph G,
// input/output:
char *message
) ;
// TODO: # triangles incident on each node/edge
//------------------------------------------------------------------------------
// ALGO: k-truss
//------------------------------------------------------------------------------
// this should OK: G = func(G); we know G is aliased in/out
LAGraph_anything (/* out: */ &G, k, /* in: */ G, &message) ;
G = NULL ; // make a new G
LAGraph_anything (/* out: */ &G, k, /* in: */ H, &message) ;
LAGraph_Graph G ;
LAGraph_anything (/* out: */ &G, k, /* in: */ G, &message) ;
LAGraph_Graph Gnew = LAGraph_create ( ) ;
LAGraph_anything (/* out: */ Gnew, k, /* in: */ G, &message) ;
int LAGraph_Ktruss_next
(
// input/output:
LAGraph_Graph Gnext,
// input:
uint64_t k,
LAGraph_Graph Gprev, // (k-1)-truss on input, with support A(i,j)
// input/output:
char *message
) ;
LAGraph_Graph K = NULL ;
int LAGraph_Ktruss_all
(
// output:
LAGraph_Graph *K, // an array of LAGraph_Graph, each with support
// output:
uint64_t *kmax,
LAGraph_Graph G, // just a graph
// input/output:
char *message
) ;
// k = 12 on output:
// K[3], ... K[11].