Line data Source code
1 : //----------------------------------------------------------------------------
2 : // LAGraph/expirimental/test/test_AllKCore.c: test cases for full 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 ;
29 : GrB_Vector c1 = NULL, c2 = NULL;
30 : #define LEN 512
31 : char filename [LEN+1] ;
32 :
33 : typedef struct
34 : {
35 : uint64_t kmax ;
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_AllKCore (void)
53 : {
54 1 : LAGraph_Init (msg) ;
55 :
56 1 : for (int k = 0 ; ; k++)
57 2 : {
58 : // load the matrix as A
59 3 : const char *aname = files [k].name ;
60 3 : uint64_t kmax = files [k].kmax ;
61 3 : if (strlen (aname) == 0) break;
62 2 : printf ("\n================================== %s: ==================================\n", aname) ;
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 2 : int64_t check_kmax = -1; //flag to check kmax in LG_check_kcore
104 : uint64_t km1;
105 : uint64_t km2;
106 : bool ok;
107 : //test the k-core
108 2 : OK(LAGraph_KCore_All(&c1, &km1, G, msg)) ;
109 : // printf ("kmax: %lu km1 %lu\n", kmax, km1) ;
110 2 : TEST_CHECK(kmax == km1) ;
111 :
112 2 : OK(LG_check_kcore(&c2, &km2, G, check_kmax, msg)) ;
113 : // printf ("kmax: %lu km1 %lu\n", kmax, km2) ;
114 :
115 2 : TEST_CHECK(kmax == km2) ;
116 2 : TEST_CHECK(km1 == km2) ;
117 2 : OK (LAGraph_Vector_IsEqual (&ok, c1, c2, msg)) ;
118 2 : TEST_CHECK (ok) ;
119 2 : GrB_free (&c1) ;
120 2 : GrB_free (&c2) ;
121 :
122 2 : OK (LAGraph_Delete (&G, msg)) ;
123 : }
124 :
125 1 : LAGraph_Finalize (msg) ;
126 1 : }
127 :
128 : //------------------------------------------------------------------------------
129 : // test_errors
130 : //------------------------------------------------------------------------------
131 :
132 1 : void test_errors (void)
133 : {
134 1 : LAGraph_Init (msg) ;
135 :
136 1 : snprintf (filename, LEN, LG_DATA_DIR "%s", "karate.mtx") ;
137 1 : FILE *f = fopen (filename, "r") ;
138 1 : TEST_CHECK (f != NULL) ;
139 1 : OK (LAGraph_MMRead (&A, f, msg)) ;
140 1 : TEST_MSG ("Loading of adjacency matrix failed") ;
141 :
142 : // construct an undirected graph G with adjacency matrix A
143 1 : OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_UNDIRECTED, msg)) ;
144 1 : TEST_CHECK (A == NULL) ;
145 :
146 1 : OK (LAGraph_Cached_NSelfEdges (G, msg)) ;
147 :
148 : uint64_t kmax ;
149 1 : GrB_Vector c = NULL ;
150 :
151 : // c is NULL
152 1 : int result = LAGraph_KCore_All (NULL, &kmax, G, msg) ;
153 1 : printf ("\nresult: %d %s\n", result, msg) ;
154 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
155 :
156 : // G is invalid
157 1 : result = LAGraph_KCore_All (&c, &kmax, NULL, msg) ;
158 1 : printf ("\nresult: %d %s\n", result, msg) ;
159 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
160 1 : TEST_CHECK (c == NULL) ;
161 :
162 : // G may have self edges
163 1 : G->nself_edges = LAGRAPH_UNKNOWN ;
164 1 : result = LAGraph_KCore_All (&c, &kmax, G, msg) ;
165 1 : printf ("\nresult: %d %s\n", result, msg) ;
166 1 : TEST_CHECK (result == -1004) ;
167 1 : TEST_CHECK (c == NULL) ;
168 :
169 : // G is undirected
170 1 : G->nself_edges = 0 ;
171 1 : G->kind = LAGraph_ADJACENCY_DIRECTED ;
172 1 : G->is_symmetric_structure = LAGraph_FALSE ;
173 1 : result = LAGraph_KCore_All (&c, &kmax, G, msg) ;
174 1 : printf ("\nresult: %d %s\n", result, msg) ;
175 1 : TEST_CHECK (result == -1005) ;
176 1 : TEST_CHECK (c == NULL) ;
177 :
178 1 : OK (LAGraph_Delete (&G, msg)) ;
179 1 : LAGraph_Finalize (msg) ;
180 1 : }
181 :
182 : TEST_LIST = {
183 : {"AllKCore", test_AllKCore},
184 : {"AllKCore_errors", test_errors},
185 : {NULL, NULL}
186 : };
|