Line data Source code
1 : //------------------------------------------------------------------------------
2 : // LAGraph/experimental/test/test_msf.c: test cases for Min Spanning Forest
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 : // todo: write a simple msf method, as LG_check_msf, and compare its results
19 : // with LAGraph_msf
20 :
21 : #include <stdio.h>
22 : #include <acutest.h>
23 :
24 : #include <LAGraphX.h>
25 : #include <LAGraph_test.h>
26 :
27 : char msg [LAGRAPH_MSG_LEN] ;
28 : LAGraph_Graph G = NULL ;
29 : GrB_Matrix A = NULL ;
30 : GrB_Matrix S = NULL ;
31 : GrB_Matrix C = NULL ;
32 : #define LEN 512
33 : char filename [LEN+1] ;
34 :
35 : typedef struct
36 : {
37 : bool symmetric ;
38 : const char *name ;
39 : }
40 : matrix_info ;
41 :
42 : const matrix_info files [ ] =
43 : {
44 : { 1, "A.mtx" },
45 : { 1, "jagmesh7.mtx" },
46 : { 0, "west0067.mtx" }, // unsymmetric
47 : { 1, "bcsstk13.mtx" },
48 : { 1, "karate.mtx" },
49 : { 1, "ldbc-cdlp-undirected-example.mtx" },
50 : { 1, "ldbc-undirected-example-bool.mtx" },
51 : { 1, "ldbc-undirected-example-unweighted.mtx" },
52 : { 1, "ldbc-undirected-example.mtx" },
53 : { 1, "ldbc-wcc-example.mtx" },
54 : { 0, "" },
55 : } ;
56 :
57 : //****************************************************************************
58 1 : void test_msf (void)
59 : {
60 : #if LAGRAPH_SUITESPARSE
61 1 : LAGraph_Init (msg) ;
62 :
63 1 : for (int k = 0 ; ; k++)
64 10 : {
65 :
66 : // load the matrix as A
67 11 : const char *aname = files [k].name ;
68 11 : bool symmetric = files [k].symmetric ;
69 11 : if (strlen (aname) == 0) break;
70 10 : printf ("\n================================== %s:\n", aname) ;
71 10 : TEST_CASE (aname) ;
72 10 : snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
73 10 : FILE *f = fopen (filename, "r") ;
74 10 : TEST_CHECK (f != NULL) ;
75 10 : OK (LAGraph_MMRead (&A, f, msg)) ;
76 10 : fclose (f) ;
77 :
78 : // ensure A is uint64
79 : GrB_Index nrows, ncols ;
80 10 : OK (GrB_Matrix_nrows (&nrows, A)) ;
81 10 : OK (GrB_Matrix_ncols (&ncols, A)) ;
82 10 : OK (GrB_Matrix_new (&S, GrB_UINT64, nrows, ncols)) ;
83 10 : OK (GrB_assign (S, NULL, NULL, A, GrB_ALL, nrows, GrB_ALL, ncols,
84 : NULL)) ;
85 10 : GrB_Index n = nrows ;
86 10 : OK (GrB_free (&A)) ;
87 :
88 : // construct a directed graph G with adjacency matrix S
89 10 : OK (LAGraph_New (&G, &S, LAGraph_ADJACENCY_DIRECTED, msg)) ;
90 10 : TEST_CHECK (S == NULL) ;
91 :
92 10 : bool sanitize = (!symmetric) ;
93 :
94 : // compute the min spanning forest
95 10 : C = NULL ;
96 10 : int result = LAGraph_msf (&C, G->A, sanitize, msg) ;
97 10 : printf ("result: %d\n", result) ;
98 10 : LAGraph_PrintLevel pr = (n <= 100) ? LAGraph_COMPLETE : LAGraph_SHORT ;
99 :
100 : // check result C for A.mtx
101 10 : if (strcmp (aname, "A.mtx") == 0)
102 : {
103 1 : GrB_Matrix Cgood = NULL ;
104 1 : OK (GrB_Matrix_new (&Cgood, GrB_UINT64, n, n)) ;
105 1 : OK (GrB_Matrix_setElement (Cgood, 1, 1, 0)) ;
106 1 : OK (GrB_Matrix_setElement (Cgood, 1, 2, 0)) ;
107 1 : OK (GrB_Matrix_setElement (Cgood, 1, 3, 1)) ;
108 1 : OK (GrB_Matrix_setElement (Cgood, 1, 4, 1)) ;
109 1 : OK (GrB_Matrix_setElement (Cgood, 1, 5, 1)) ;
110 1 : OK (GrB_Matrix_setElement (Cgood, 1, 6, 0)) ;
111 1 : OK (GrB_wait (Cgood, GrB_MATERIALIZE)) ;
112 1 : printf ("\nmsf (known result):\n") ;
113 1 : OK (LAGraph_Matrix_Print (Cgood, pr, stdout, msg)) ;
114 1 : bool ok = false ;
115 1 : OK (LAGraph_Matrix_IsEqual (&ok, C, Cgood, msg)) ;
116 1 : TEST_CHECK (ok) ;
117 1 : OK (GrB_free (&Cgood)) ;
118 : }
119 :
120 10 : printf ("\nmsf:\n") ;
121 10 : OK (LAGraph_Matrix_Print (C, pr, stdout, msg)) ;
122 10 : OK (GrB_free (&C)) ;
123 10 : OK (LAGraph_Delete (&G, msg)) ;
124 : }
125 :
126 1 : LAGraph_Finalize (msg) ;
127 : #endif
128 1 : }
129 :
130 : //------------------------------------------------------------------------------
131 : // test_errors
132 : //------------------------------------------------------------------------------
133 :
134 1 : void test_errors (void)
135 : {
136 : #if LAGRAPH_SUITESPARSE
137 1 : LAGraph_Init (msg) ;
138 :
139 : // C and A are NULL
140 1 : int result = LAGraph_msf (NULL, NULL, true, msg) ;
141 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
142 :
143 : // A must be square
144 1 : OK (GrB_Matrix_new (&A, GrB_UINT64, 3, 4)) ;
145 1 : result = LAGraph_msf (&C, A, true, msg) ;
146 1 : TEST_CHECK (result == GrB_DIMENSION_MISMATCH) ;
147 :
148 1 : OK (GrB_free (&A)) ;
149 1 : LAGraph_Finalize (msg) ;
150 : #endif
151 1 : }
152 :
153 : //****************************************************************************
154 :
155 : TEST_LIST = {
156 : {"msf", test_msf},
157 : {"msf_errors", test_errors},
158 : {NULL, NULL}
159 : };
|