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
|
//----------------------------------------------------------------------------
// LAGraph/experimental/test/test_lcc.c: tests for Local Clustering Coefficient
// ----------------------------------------------------------------------------
// LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
// SPDX-License-Identifier: BSD-2-Clause
//
// For additional details (including references to third party source code and
// other files) see the LICENSE file or contact permission@sei.cmu.edu. See
// Contributors.txt for a full list of contributors. Created, in part, with
// funding and support from the U.S. Government (see Acknowledgments.txt file).
// DM22-0790
// Contributed by Timothy A. Davis, Texas A&M University
//-----------------------------------------------------------------------------
// todo: write a simple lcc method, as LG_check_lcc, and compare its results
// with LAGraph_lcc
#include <stdio.h>
#include <acutest.h>
#include <LAGraphX.h>
#include <LAGraph_test.h>
char msg [LAGRAPH_MSG_LEN] ;
LAGraph_Graph G = NULL ;
GrB_Matrix A = NULL ;
#define LEN 512
char filename [LEN+1] ;
typedef struct
{
bool symmetric ;
const char *name ;
}
matrix_info ;
// this known solution is listed here in lower precision than double allows
double lcc_west0067 [67] = {
0.0909091,
0.0238095,
0.0714286,
0.0238095,
0.125,
0.119048,
0.178571,
0.133333,
0.0952381,
0.0833333,
0.0666667,
0.1,
0.0892857,
0.0714286,
0.138889,
0.0555556,
0.0694444,
0.0555556,
0.0714286,
0.0769231,
0.0333333,
0.0666667,
0.0666667,
0.0333333,
0.0694444,
0.0444444,
0.0555556,
0.0777778,
0.0333333,
0,
0.0904762,
0.0535714,
0.0714286,
0.107143,
0.0892857,
0,
0.0681818,
0.0357143,
0.0178571,
0.0666667,
0.0555556,
0.0694444,
0.0666667,
0.111111,
0.0444444,
0.142857,
0.166667,
0.2,
0.0448718,
0.166667,
0.166667,
0.119048,
0.166667,
0.190476,
0.104167,
0.0333333,
0.0444444,
0.0444444,
0.0777778,
0.0416667,
0.0222222,
0.0972222,
0.142857,
0.0416667,
0.0555556,
0.196429,
0.155556
} ;
const matrix_info files [ ] =
{
{ 1, "A.mtx" },
{ 1, "jagmesh7.mtx" },
{ 0, "west0067.mtx" }, // unsymmetric
{ 1, "bcsstk13.mtx" },
{ 1, "karate.mtx" },
{ 1, "ldbc-cdlp-undirected-example.mtx" },
{ 1, "ldbc-undirected-example-bool.mtx" },
{ 1, "ldbc-undirected-example-unweighted.mtx" },
{ 1, "ldbc-undirected-example.mtx" },
{ 1, "ldbc-wcc-example.mtx" },
{ 0, "" },
} ;
//****************************************************************************
void test_lcc (void)
{
LAGraph_Init (msg) ;
#if LAGRAPH_SUITESPARSE
for (int k = 0 ; ; k++)
{
// load the matrix as A
const char *aname = files [k].name ;
bool symmetric = files [k].symmetric ;
if (strlen (aname) == 0) break;
printf ("\n================================== %s:\n", aname) ;
TEST_CASE (aname) ;
snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
FILE *f = fopen (filename, "r") ;
TEST_CHECK (f != NULL) ;
OK (LAGraph_MMRead (&A, f, msg)) ;
// construct a directed graph G with adjacency matrix A
OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_DIRECTED, msg)) ;
TEST_CHECK (A == NULL) ;
// check for self-edges
OK (LAGraph_Cached_NSelfEdges (G, msg)) ;
bool sanitize = (G->nself_edges != 0) ;
GrB_Vector c = NULL ;
double t [2] ;
// compute the local clustering coefficient
OK (LAGraph_lcc (&c, G->A, symmetric, sanitize, t, msg)) ;
GrB_Index n ;
OK (GrB_Vector_size (&n, c)) ;
LAGraph_PrintLevel pr = (n <= 100) ? LAGraph_COMPLETE : LAGraph_SHORT ;
// check result c for west0067
if (strcmp (aname, "west0067.mtx") == 0)
{
GrB_Vector cgood = NULL ;
OK (GrB_Vector_new (&cgood, GrB_FP64, n)) ;
for (int k = 0 ; k < n ; k++)
{
OK (GrB_Vector_setElement (cgood, lcc_west0067 [k], k)) ;
}
OK (GrB_wait (cgood, GrB_MATERIALIZE)) ;
printf ("\nlcc (known result, but with float precision):\n") ;
OK (LAGraph_Vector_Print (cgood, pr, stdout, msg)) ;
// cgood = abs (cgood - c)
OK (GrB_eWiseAdd (cgood, NULL, NULL, GrB_MINUS_FP64, cgood, c,
NULL)) ;
OK (GrB_apply (cgood, NULL, NULL, GrB_ABS_FP64, cgood, NULL)) ;
double err = 0 ;
// err = max (cgood)
OK (GrB_reduce (&err, NULL, GrB_MAX_MONOID_FP64, cgood, NULL)) ;
printf ("err: %g\n", err) ;
TEST_CHECK (err < 1e-6) ;
OK (GrB_free (&cgood)) ;
}
printf ("\nlcc:\n") ;
OK (LAGraph_Vector_Print (c, pr, stdout, msg)) ;
OK (GrB_free (&c)) ;
OK (LAGraph_Delete (&G, msg)) ;
}
#else
printf ("test skipped\n") ;
#endif
LAGraph_Finalize (msg) ;
}
//------------------------------------------------------------------------------
// test_errors
//------------------------------------------------------------------------------
void test_errors (void)
{
LAGraph_Init (msg) ;
#if LAGRAPH_SUITESPARSE
snprintf (filename, LEN, LG_DATA_DIR "%s", "karate.mtx") ;
FILE *f = fopen (filename, "r") ;
TEST_CHECK (f != NULL) ;
OK (LAGraph_MMRead (&A, f, msg)) ;
TEST_MSG ("Loading of adjacency matrix failed") ;
// construct an undirected graph G with adjacency matrix A
OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_UNDIRECTED, msg)) ;
TEST_CHECK (A == NULL) ;
OK (LAGraph_Cached_NSelfEdges (G, msg)) ;
GrB_Vector c = NULL ;
double t [2] ;
// c is NULL
int result = LAGraph_lcc (NULL, G->A, true, false, t, msg) ;
printf ("\nresult: %d\n", result) ;
TEST_CHECK (result == GrB_NULL_POINTER) ;
// G->A is held by column
OK (GxB_set (G->A, GxB_FORMAT, GxB_BY_COL)) ;
result = LAGraph_lcc (&c, G->A, true, true, t, msg) ;
printf ("\nresult: %d\n", result) ;
TEST_CHECK (result == GrB_INVALID_VALUE) ;
TEST_CHECK (c == NULL) ;
OK (LAGraph_Delete (&G, msg)) ;
#else
printf ("test skipped\n") ;
#endif
LAGraph_Finalize (msg) ;
}
//****************************************************************************
TEST_LIST = {
{"lcc", test_lcc},
{"lcc_errors", test_errors},
{NULL, NULL}
};
|