|
1 | | -function s = tricount (A, check) |
| 1 | +function s = tricount (A, arg2, arg3) |
2 | 2 | %GRB.TRICOUNT count triangles in a matrix. |
3 | 3 | % s = GrB.tricount (A) counts the number of triangles in the matrix A. |
4 | 4 | % spones (A) must be symmetric; results are undefined if spones (A) is |
|
7 | 7 | % To check the input matrix A, use GrB.tricount (A, 'check'). This check |
8 | 8 | % takes additional time so by default the input is not checked. |
9 | 9 | % |
10 | | -% See also GrB.ktruss. |
| 10 | +% If d is a vector of length n with d(i) equal to the degree of node i, |
| 11 | +% then s = tricount (A, d) can be used. Otherwise, tricount must compute |
| 12 | +% the degrees first. |
| 13 | +% |
| 14 | +% See also GrB.ktruss, GrB.entries. |
| 15 | +% |
| 16 | +% ADDED: sort if warranted. See LAGraph_tricount. |
11 | 17 |
|
12 | 18 | [m, n] = size (A) ; |
13 | 19 | if (m ~= n) |
14 | 20 | gb_error ('A must be square') ; |
15 | 21 | end |
16 | | -if (nargin < 2) |
17 | | - check = false ; |
18 | | -else |
19 | | - check = isequal (check, 'check') ; |
| 22 | + |
| 23 | +d = [ ] ; |
| 24 | +check = false ; |
| 25 | + |
| 26 | +if (nargin == 2) |
| 27 | + if (ischar (arg2)) |
| 28 | + % s = tricount (A, 'check') |
| 29 | + check = isequal (arg2, 'check') ; |
| 30 | + else |
| 31 | + % s = tricount (A, d) |
| 32 | + d = arg2 ; |
| 33 | + end |
| 34 | +elseif (nargin == 3) |
| 35 | + if (ischar (arg2)) |
| 36 | + % s = tricount (A, 'check', d) |
| 37 | + check = isequal (arg2, 'check') ; |
| 38 | + d = arg3 ; |
| 39 | + else |
| 40 | + % s = tricount (A, d, 'check') |
| 41 | + d = arg2 ; |
| 42 | + check = isequal (arg3, 'check') ; |
| 43 | + end |
20 | 44 | end |
21 | 45 |
|
22 | 46 | if (check && ~issymmetric (spones (A))) |
23 | 47 | gb_error ('pattern of A must be symmetric') ; |
24 | 48 | end |
25 | 49 |
|
| 50 | +if (isequal (class (d), 'GrB')) |
| 51 | + d = double (d) ; |
| 52 | +end |
| 53 | + |
| 54 | +% determine if A should be sorted first |
| 55 | +if (n > 1000 && GrB.entries (A) >= 10*n) |
| 56 | + if (isempty (d)) |
| 57 | + % compute the degree of each node, if not provided on input |
| 58 | + if (GrB.isbyrow (A)) |
| 59 | + d = double (GrB.entries (A, 'row', 'degree')) ; |
| 60 | + else |
| 61 | + d = double (GrB.entries (A, 'col', 'degree')) ; |
| 62 | + end |
| 63 | + end |
| 64 | + % sample the degree |
| 65 | + p = randperm (n, 1000) ; |
| 66 | + sample = d (randperm (n, 1000)) ; |
| 67 | + dmean = full (mean (sample)) ; |
| 68 | + dmed = full (median (sample)) ; |
| 69 | + % fprintf ('mean degree: %g median: %g\n', dmean, dmed) ; |
| 70 | + if (dmean > 4 * dmed) |
| 71 | + % sort if the average degree is very high compared to the median |
| 72 | + % fprintf ('sorting A first\n') ; |
| 73 | + [~, p] = sort (d, 'descend') ; |
| 74 | + A = A (p,p) ; |
| 75 | + clear p |
| 76 | + end |
| 77 | +end |
| 78 | + |
26 | 79 | % C, L, and U will have the same format as A |
27 | 80 | C = GrB (n, n, 'int64', GrB.format (A)) ; |
28 | 81 | L = tril (A, -1) ; |
|
0 commit comments