Line data Source code
1 : //----------------------------------------------------------------------------
2 : // LAGraph/experimental/test/test_lcc.c: tests for Local Clustering Coefficient
3 : // ----------------------------------------------------------------------------
4 :
5 : // LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
6 : // SPDX-License-Identifier: BSD-2-Clause
7 : //
8 : // For additional details (including references to third party source code and
9 : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See
10 : // Contributors.txt for a full list of contributors. Created, in part, with
11 : // funding and support from the U.S. Government (see Acknowledgments.txt file).
12 : // DM22-0790
13 :
14 : // Contributed by Timothy A. Davis, Texas A&M University
15 :
16 : //-----------------------------------------------------------------------------
17 :
18 : #include <stdio.h>
19 : #include <acutest.h>
20 :
21 : #include <LAGraphX.h>
22 : #include <LAGraph_test.h>
23 : #include "LG_Xtest.h"
24 :
25 : char msg [LAGRAPH_MSG_LEN] ;
26 : LAGraph_Graph G = NULL ;
27 : GrB_Matrix A = NULL ;
28 : #define LEN 512
29 : char filename [LEN+1] ;
30 :
31 : typedef struct
32 : {
33 : const char *name ;
34 : }
35 : matrix_info ;
36 :
37 : const matrix_info files [ ] =
38 : {
39 : { "A.mtx" },
40 : { "jagmesh7.mtx" },
41 : { "west0067.mtx" }, // unsymmetric
42 : { "bcsstk13.mtx" },
43 : { "karate.mtx" },
44 : { "ldbc-cdlp-undirected-example.mtx" },
45 : { "ldbc-undirected-example-bool.mtx" },
46 : { "ldbc-undirected-example-unweighted.mtx" },
47 : { "ldbc-undirected-example.mtx" },
48 : { "ldbc-wcc-example.mtx" },
49 : { "" },
50 : } ;
51 :
52 : //****************************************************************************
53 1 : void test_lcc (void)
54 : {
55 : #if LAGRAPH_SUITESPARSE
56 1 : LAGraph_Init (msg) ;
57 :
58 1 : for (int k = 0 ; ; k++)
59 10 : {
60 :
61 : // load the matrix as A
62 11 : const char *aname = files [k].name ;
63 11 : if (strlen (aname) == 0) break;
64 10 : printf ("\n================================== %s:\n", aname) ;
65 10 : TEST_CASE (aname) ;
66 10 : snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
67 10 : FILE *f = fopen (filename, "r") ;
68 10 : TEST_CHECK (f != NULL) ;
69 10 : OK (LAGraph_MMRead (&A, f, msg)) ;
70 10 : fclose (f) ;
71 :
72 : // construct a directed graph G with adjacency matrix A
73 10 : OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_DIRECTED, msg)) ;
74 10 : TEST_CHECK (A == NULL) ;
75 :
76 : // check for self-edges
77 10 : OK (LAGraph_Cached_IsSymmetricStructure (G, msg));
78 10 : OK (LAGraph_Cached_NSelfEdges (G, msg)) ;
79 :
80 30 : for (int jit = 0 ; jit <= 1 ; jit++)
81 : {
82 20 : printf ("jit: %d\n", jit) ;
83 20 : OK (LG_SET_JIT (jit ? GxB_JIT_ON : GxB_JIT_OFF)) ;
84 :
85 20 : GrB_Vector c = NULL ;
86 :
87 : // compute the local clustering coefficient
88 20 : OK (LAGraph_lcc (&c, G, msg)) ;
89 :
90 : GrB_Index n ;
91 20 : OK (GrB_Vector_size (&n, c)) ;
92 20 : LAGraph_PrintLevel pr = (n <= 100) ? LAGraph_COMPLETE : LAGraph_SHORT ;
93 :
94 20 : GrB_Vector cgood = NULL ;
95 20 : OK (LG_check_lcc(&cgood, G, msg)) ;
96 20 : OK (GrB_wait (cgood, GrB_MATERIALIZE)) ;
97 : // cgood = abs (cgood - c)
98 20 : OK (GrB_eWiseAdd (cgood, NULL, NULL, GrB_MINUS_FP64, cgood, c,
99 : NULL)) ;
100 20 : OK (GrB_apply (cgood, NULL, NULL, GrB_ABS_FP64, cgood, NULL)) ;
101 20 : double err = 0 ;
102 : // err = max (cgood)
103 20 : OK (GrB_reduce (&err, NULL, GrB_MAX_MONOID_FP64, cgood, NULL)) ;
104 20 : printf ("err: %g\n", err) ;
105 20 : TEST_CHECK (err < 1e-6) ;
106 20 : OK (GrB_free (&cgood)) ;
107 :
108 20 : printf ("\nlcc:\n") ;
109 20 : OK (LAGraph_Vector_Print (c, pr, stdout, msg)) ;
110 20 : OK (GrB_free (&c)) ;
111 :
112 : }
113 :
114 10 : OK (LAGraph_Delete (&G, msg)) ;
115 : }
116 :
117 1 : LAGraph_Finalize (msg) ;
118 : #endif
119 1 : }
120 :
121 : //------------------------------------------------------------------------------
122 : // test_errors
123 : //------------------------------------------------------------------------------
124 :
125 1 : void test_errors (void)
126 : {
127 : #if LAGRAPH_SUITESPARSE
128 1 : LAGraph_Init (msg) ;
129 :
130 1 : snprintf (filename, LEN, LG_DATA_DIR "%s", "karate.mtx") ;
131 1 : FILE *f = fopen (filename, "r") ;
132 1 : TEST_CHECK (f != NULL) ;
133 1 : OK (LAGraph_MMRead (&A, f, msg)) ;
134 1 : TEST_MSG ("Loading of adjacency matrix failed") ;
135 1 : fclose (f) ;
136 :
137 : // construct an undirected graph G with adjacency matrix A
138 1 : OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_UNDIRECTED, msg)) ;
139 1 : TEST_CHECK (A == NULL) ;
140 :
141 1 : OK (LAGraph_Cached_IsSymmetricStructure (G, msg));
142 1 : OK (LAGraph_Cached_NSelfEdges (G, msg)) ;
143 :
144 1 : GrB_Vector c = NULL ;
145 :
146 : // c is NULL
147 1 : int result = LAGraph_lcc (NULL, G, msg) ;
148 1 : printf ("\nresult: %d\n", result) ;
149 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
150 :
151 1 : OK (LAGraph_Delete (&G, msg)) ;
152 1 : LAGraph_Finalize (msg) ;
153 : #endif
154 1 : }
155 :
156 : //****************************************************************************
157 :
158 : TEST_LIST = {
159 : {"lcc", test_lcc},
160 : {"lcc_errors", test_errors},
161 : {NULL, NULL}
162 : };
|