Line data Source code
1 : //----------------------------------------------------------------------------
2 : // LAGraph/expirimental/test/test_KCoreDecompose.c: test cases for k-core
3 : // decomposition
4 : // ----------------------------------------------------------------------------
5 :
6 : // LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
7 : // SPDX-License-Identifier: BSD-2-Clause
8 : //
9 : // For additional details (including references to third party source code and
10 : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See
11 : // Contributors.txt for a full list of contributors. Created, in part, with
12 : // funding and support from the U.S. Government (see Acknowledgments.txt file).
13 : // DM22-0790
14 :
15 : // Contributed by Pranav Konduri, Texas A&M University
16 :
17 : //-----------------------------------------------------------------------------
18 :
19 : #include <stdio.h>
20 : #include <acutest.h>
21 :
22 : #include <LAGraphX.h>
23 : #include <LAGraph_test.h>
24 : #include "LG_Xtest.h"
25 :
26 : char msg [LAGRAPH_MSG_LEN] ;
27 : LAGraph_Graph G = NULL ;
28 : GrB_Matrix A = NULL, D1 = NULL, D2 = NULL;
29 : GrB_Vector c = NULL;
30 : #define LEN 512
31 : char filename [LEN+1] ;
32 :
33 : typedef struct
34 : {
35 : uint64_t kval ;
36 : const char *name ;
37 : }
38 : matrix_info ;
39 :
40 : const matrix_info files [ ] =
41 : {
42 : { 4, "karate.mtx" },
43 : { 6, "west0067.mtx" },
44 : // { 10, "amazon0601.mtx" },
45 : // { 64, "cit-Patents.mtx"},
46 : // { 2208, "hollywood-2009.mtx"},
47 : // { 111, "as-Skitter.mtx"},
48 : { 0, "" },
49 : } ;
50 :
51 :
52 1 : void test_KCoreDecompose (void)
53 : {
54 1 : LAGraph_Init (msg) ;
55 : #if LAGRAPH_SUITESPARSE
56 :
57 1 : for (int k = 0 ; ; k++)
58 2 : {
59 : // load the matrix as A
60 3 : const char *aname = files [k].name ;
61 3 : uint64_t kval = files [k].kval ;
62 3 : if (strlen (aname) == 0) break;
63 2 : TEST_CASE (aname) ;
64 2 : snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
65 2 : FILE *f = fopen (filename, "r") ;
66 2 : TEST_CHECK (f != NULL) ;
67 2 : OK (LAGraph_MMRead (&A, f, msg)) ;
68 2 : TEST_MSG ("Loading of adjacency matrix failed") ;
69 :
70 : // construct an undirected graph G with adjacency matrix A
71 2 : OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_DIRECTED, msg)) ;
72 2 : TEST_CHECK (A == NULL) ;
73 :
74 : // check if the pattern is symmetric - if it isn't make it.
75 2 : OK (LAGraph_Cached_IsSymmetricStructure (G, msg)) ;
76 :
77 2 : if (G->is_symmetric_structure == LAGraph_FALSE)
78 : {
79 1 : printf("This matrix is not symmetric. \n");
80 : // make the adjacency matrix symmetric
81 1 : OK (LAGraph_Cached_AT (G, msg)) ;
82 1 : OK (GrB_eWiseAdd (G->A, NULL, NULL, GrB_LOR, G->A, G->AT, NULL)) ;
83 1 : G->is_symmetric_structure = true ;
84 : // consider the graph as directed
85 1 : G->kind = LAGraph_ADJACENCY_DIRECTED ;
86 : }
87 : else
88 : {
89 1 : G->kind = LAGraph_ADJACENCY_UNDIRECTED ;
90 : }
91 :
92 : // check for self-edges, and remove them.
93 2 : OK (LAGraph_Cached_NSelfEdges (G, msg)) ;
94 2 : if (G->nself_edges != 0)
95 : {
96 : // remove self-edges
97 1 : printf ("graph has %g self edges\n", (double) G->nself_edges) ;
98 1 : OK (LAGraph_DeleteSelfEdges (G, msg)) ;
99 1 : printf ("now has %g self edges\n", (double) G->nself_edges) ;
100 1 : TEST_CHECK (G->nself_edges == 0) ;
101 : }
102 :
103 : bool ok;
104 : //get the kcore at the designated k-value
105 2 : LAGraph_KCore(&c, G, kval, msg) ;
106 :
107 : //decompose the k-core from vector c into D1
108 2 : LAGraph_KCore_Decompose(&D1, G, c, kval, msg);
109 :
110 : //decompose the k-core from vector c into D2
111 2 : LG_check_kcore_decompose(&D2, G, c, kval, msg);
112 :
113 : //check equality
114 2 : OK (LAGraph_Matrix_IsEqual (&ok, D1, D2, msg)) ;
115 2 : TEST_CHECK(ok);
116 :
117 2 : OK (LAGraph_Delete (&G, msg)) ;
118 : }
119 :
120 : #endif
121 1 : LAGraph_Finalize (msg) ;
122 1 : }
123 :
124 : //------------------------------------------------------------------------------
125 : // test_errors
126 : //------------------------------------------------------------------------------
127 :
128 1 : void test_errors (void)
129 : {
130 1 : LAGraph_Init (msg) ;
131 : #if LAGRAPH_SUITESPARSE
132 :
133 1 : snprintf (filename, LEN, LG_DATA_DIR "%s", "karate.mtx") ;
134 1 : FILE *f = fopen (filename, "r") ;
135 1 : TEST_CHECK (f != NULL) ;
136 1 : OK (LAGraph_MMRead (&A, f, msg)) ;
137 1 : TEST_MSG ("Loading of adjacency matrix failed") ;
138 :
139 : // construct an undirected graph G with adjacency matrix A
140 1 : OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_UNDIRECTED, msg)) ;
141 1 : TEST_CHECK (A == NULL) ;
142 :
143 1 : OK (LAGraph_Cached_NSelfEdges (G, msg)) ;
144 :
145 1 : uint64_t kval = 2 ;
146 1 : GrB_Vector c = NULL ;
147 :
148 : // c is NULL
149 1 : int result = LAGraph_KCore_Decompose (&D1, G, NULL, kval, msg) ;
150 1 : printf ("\nresult: %d %s\n", result, msg) ;
151 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
152 :
153 : // G is invalid
154 1 : result = LAGraph_KCore_Decompose (&D1, NULL, c, kval, msg) ;
155 1 : printf ("\nresult: %d %s\n", result, msg) ;
156 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
157 1 : TEST_CHECK (c == NULL) ;
158 :
159 : // G may have self edges
160 1 : G->nself_edges = LAGRAPH_UNKNOWN ;
161 1 : result = LAGraph_KCore_Decompose(&D1, G, c, kval, msg);
162 1 : printf ("\nresult: %d %s\n", result, msg) ;
163 1 : TEST_CHECK (result == -1004) ;
164 1 : TEST_CHECK (c == NULL) ;
165 :
166 : // G is directed
167 1 : G->nself_edges = 0 ;
168 1 : G->kind = LAGraph_ADJACENCY_DIRECTED ;
169 1 : G->is_symmetric_structure = LAGraph_FALSE ;
170 1 : result = LAGraph_KCore_Decompose(&D1, G, c, kval, msg);
171 1 : printf ("\nresult: %d %s\n", result, msg) ;
172 1 : TEST_CHECK (result == -1005) ;
173 1 : TEST_CHECK (c == NULL) ;
174 :
175 1 : result = LG_check_kcore_decompose(&D1, G, c, kval, msg);
176 1 : printf ("\nresult: %d %s\n", result, msg) ;
177 1 : TEST_CHECK (result == -1005) ;
178 1 : TEST_CHECK (c == NULL) ;
179 :
180 1 : OK (LAGraph_Delete (&G, msg)) ;
181 : #endif
182 1 : LAGraph_Finalize (msg) ;
183 1 : }
184 :
185 : TEST_LIST = {
186 : #if LAGRAPH_SUITESPARSE
187 : {"KCoreDecompose", test_KCoreDecompose},
188 : {"KCoreDecompose_errors", test_errors},
189 : #endif
190 : {NULL, NULL}
191 : };
|