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
//------------------------------------------------------------------------------
// LAGraph_HITS: Hyperlink-Induced Topic Search algorithm using GraphBLAS API
//------------------------------------------------------------------------------
// 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 Aurko Routh, Texas A&M University
// TODO: almost ready for src. this is advanced; add a basic method and add to src
// TODO: rename to LAGr_HubsAndAuthorities?
//------------------------------------------------------------------------------
#define LG_FREE_WORK \
{ \
GrB_free (&h_old) ; \
GrB_free (&a_old) ; \
}
#define LG_FREE_ALL \
{ \
LG_FREE_WORK ; \
GrB_free(&h); \
GrB_free(&a); \
}
#include "LAGraphX.h"
#include "LG_internal.h"
// Check graph with error messages if it's empty
int LAGr_HITS
(
GrB_Vector *hubs,
GrB_Vector *authorities,
int *iters,
const LAGraph_Graph G,
float tol,
int itermax,
char *msg
)
{
LG_CLEAR_MSG ;
GrB_Vector h = NULL, a = NULL, h_old = NULL, a_old=NULL;
LG_ASSERT (hubs != NULL, GrB_NULL_POINTER) ;
LG_ASSERT (authorities != NULL, GrB_NULL_POINTER) ;
LG_TRY (LAGraph_CheckGraph (G, msg)) ;
GrB_Matrix AT ;
GrB_Vector out_degree = G->out_degree, in_degree = NULL ;
if (G->kind == LAGraph_ADJACENCY_UNDIRECTED ||
G->is_symmetric_structure == LAGraph_TRUE)
{
// A and A' have the same structure
AT = G->A ;
in_degree = out_degree ;
}
else
{
// A and A' differ
AT = G->AT ;
LG_ASSERT_MSG (AT != NULL,
LAGRAPH_NOT_CACHED, "G->AT is required") ;
in_degree = G->in_degree ;
}
// Initializations
GrB_Index n;
(*hubs) = NULL;
(*authorities) = NULL;
GRB_TRY(GrB_Matrix_nrows(&n, AT))
float rdiff = 100;
GRB_TRY (GrB_Vector_new (&h_old, GrB_FP32, n)) ;
GRB_TRY (GrB_Vector_new (&a_old, GrB_FP32, n)) ;
GRB_TRY (GrB_Vector_new (&h, GrB_FP32, n));
GRB_TRY (GrB_Vector_new (&a, GrB_FP32, n)) ;
float defaultValue = 1.0;
GRB_TRY(GrB_assign(a, NULL, NULL, defaultValue, GrB_ALL, n, NULL));
GRB_TRY(GrB_assign(h, NULL, NULL, defaultValue, GrB_ALL, n, NULL));
bool flag = true ;
if (in_degree != NULL && out_degree != NULL)
{
GrB_Index in_nonempty = 0, out_nonempty = 0 ;
// Count the number of non-zero entries in the in_degree vector
GRB_TRY (GrB_Vector_nvals(&in_nonempty, in_degree)) ;
// Count the number of non-zero entries in the out_degree vector
GRB_TRY (GrB_Vector_nvals(&out_nonempty, out_degree)) ;
flag = (in_nonempty + out_nonempty) > n/16.0;
}
for((*iters) = 0; (*iters) < itermax && rdiff > tol; (*iters)++) {
// Save old values of h and a
GrB_Vector temp = h_old ; h_old = h ; h = temp ;
temp = a_old ; a_old = a ; a = temp ;
if(flag) {
//a = 0
GRB_TRY(GrB_assign(a, NULL, GrB_PLUS_FP32, 0.0, GrB_ALL, n, NULL));
//h = 0
GRB_TRY(GrB_assign(h, NULL, GrB_PLUS_FP32, 0.0, GrB_ALL, n, NULL));
// a += AT . h
GRB_TRY(GrB_mxv(a, NULL,NULL, LAGraph_plus_second_fp32, AT, h_old, NULL));
// h += A . a
GRB_TRY(GrB_mxv(h, NULL,NULL, LAGraph_plus_second_fp32, G->A, a_old, NULL));
} else {
// a = AT . h
GRB_TRY(GrB_mxv(a, NULL,NULL, LAGraph_plus_second_fp32, AT, h_old, NULL));
// h = A . a
GRB_TRY(GrB_mxv(h, NULL,NULL, LAGraph_plus_second_fp32, G->A, a_old, NULL));
}
// float maxA;
float sumA;
GRB_TRY(GrB_reduce(&sumA, NULL, GrB_PLUS_MONOID_FP32, a, NULL)); // Calculate the sum of all elements in the vector
GRB_TRY(GrB_assign(a, NULL, GrB_DIV_FP32, sumA, GrB_ALL, n, NULL)); // Divide all elements by the sum
float sumH;
GRB_TRY(GrB_reduce(&sumH, NULL, GrB_PLUS_MONOID_FP32, h, NULL)); // Calculate the sum of all elements in the vector
GRB_TRY(GrB_assign(h, NULL, GrB_DIV_FP32, sumH, GrB_ALL, n, NULL)); // Divide all elements by the sum
// Deal with tolerance
// a_old -= a
GRB_TRY (GrB_assign(a_old, NULL, GrB_MINUS_FP32, a, GrB_ALL, n, NULL));
// a_old = abs(a_old)
GRB_TRY(GrB_apply (a_old, NULL, NULL, GrB_ABS_FP32, a_old, NULL));
// rdiff = sum(a_old)
GRB_TRY(GrB_reduce (&rdiff, NULL, GrB_PLUS_MONOID_FP32, a_old, NULL));
// h_old -= h
GRB_TRY (GrB_assign (h_old, NULL, GrB_MINUS_FP32, h, GrB_ALL, n, NULL));
// h_old = abs(h_old)
GRB_TRY (GrB_apply (h_old, NULL, NULL, GrB_ABS_FP32, h_old, NULL));
// rdiff += sum(h_old)
GRB_TRY (GrB_reduce (&rdiff, GrB_PLUS_FP32, GrB_PLUS_MONOID_FP32, h_old, NULL)) ;
// rdiff = rdiff/2
rdiff /= 2;
}
// Normalize
float sumA;
GRB_TRY(GrB_reduce(&sumA, NULL, GrB_PLUS_MONOID_FP32, a, NULL)); // Calculate the sum of all elements in the vector
GRB_TRY(GrB_assign(a, NULL, GrB_DIV_FP32, sumA, GrB_ALL, n, NULL)); // Divide all elements by the sum
float sumH;
GRB_TRY(GrB_reduce(&sumH, NULL, GrB_PLUS_MONOID_FP32, h, NULL)); // Calculate the sum of all elements in the vector
GRB_TRY(GrB_assign(h, NULL, GrB_DIV_FP32, sumH, GrB_ALL, n, NULL)); // Divide all elements by the sum
(*hubs) = h;
(*authorities) = a;
LG_FREE_WORK;
return (GrB_SUCCESS);
}