#include <limits.h>
#include <sparse/SparseMatrix.h>
#include <sparse/BinaryHeap.h>
-#if PQ
-#include <sparse/LinkedList.h>
-#include <sparse/PriorityQueue.h>
-#endif
#include <stddef.h>
static size_t size_of_matrix_type(int type){
}
-#if PQ
-void SparseMatrix_kcore_decomposition(SparseMatrix A, int *coreness_max0, int **coreness_ptr0, int **coreness_list0){
- /* give an undirected graph A, find the k-coreness of each vertex
- A: a graph. Will be made undirected and self loop removed
- coreness_max: max core number.
- coreness_ptr: array of size (coreness_max + 2), element [corness_ptr[i], corness_ptr[i+1])
- . of array coreness_list gives the vertices with core i, i <= coreness_max
- coreness_list: array of size n = A->m
- */
- SparseMatrix B;
- int i, j, *ia, *ja, n = A->m, nz, istatus, neighb;
- PriorityQueue pq = NULL;
- int gain, deg, k, deg_max = 0, deg_old;
- int *coreness_ptr, *coreness_list, coreness_now;
- int *mask;
-
- assert(A->m == A->n);
- B = SparseMatrix_symmetrize(A, FALSE);
- B = SparseMatrix_remove_diagonal(B);
- ia = B->ia;
- ja = B->ja;
-
- mask = MALLOC(sizeof(int)*n);
- for (i = 0; i < n; i++) mask[i] = -1;
-
- pq = PriorityQueue_new(n, n-1);
- for (i = 0; i < n; i++){
- deg = ia[i+1] - ia[i];
- deg_max = MAX(deg_max, deg);
- gain = n - 1 - deg;
- pq = PriorityQueue_push(pq, i, gain);
- //fprintf(stderr,"insert %d with gain %d\n",i, gain);
- }
-
-
- coreness_ptr = MALLOC(sizeof(int)*(deg_max+2));
- coreness_list = MALLOC(sizeof(int)*n);
- deg_old = 0;
- coreness_ptr[deg_old] = 0;
- coreness_now = 0;
-
- nz = 0;
- while (PriorityQueue_pop(pq, &k, &gain)){
- deg = (n-1) - gain;
- if (deg > deg_old) {
- //fprintf(stderr,"deg = %d, cptr[%d--%d]=%d\n",deg, deg_old + 1, deg, nz);
- for (j = deg_old + 1; j <= deg; j++) coreness_ptr[j] = nz;
- coreness_now = deg;
- deg_old = deg;
- }
- coreness_list[nz++] = k;
- mask[k] = coreness_now;
- //fprintf(stderr,"=== \nremove node %d with gain %d, mask with %d, nelement=%d\n",k, gain, coreness_now, pq->count);
- for (j = ia[k]; j < ia[k+1]; j++){
- neighb = ja[j];
- if (mask[neighb] < 0){
- gain = PriorityQueue_get_gain(pq, neighb);
- //fprintf(stderr,"update node %d with gain %d, nelement=%d\n",neighb, gain+1, pq->count);
- istatus = PriorityQueue_remove(pq, neighb);
- assert(istatus != 0);
- pq = PriorityQueue_push(pq, neighb, gain + 1);
- }
- }
- }
- coreness_ptr[coreness_now + 1] = nz;
-
- *coreness_max0 = coreness_now;
- *coreness_ptr0 = coreness_ptr;
- *coreness_list0 = coreness_list;
-
- if (Verbose){
- for (i = 0; i <= coreness_now; i++){
- if (coreness_ptr[i+1] - coreness_ptr[i] > 0){
- fprintf(stderr,"num_in_core[%d] = %d: ",i, coreness_ptr[i+1] - coreness_ptr[i]);
-#if 0
- for (j = coreness_ptr[i]; j < coreness_ptr[i+1]; j++){
- fprintf(stderr,"%d,",coreness_list[j]);
- }
-#endif
- fprintf(stderr,"\n");
- }
- }
- }
- if (Verbose)
-
-
- if (B != A) SparseMatrix_delete(B);
- FREE(mask);
-}
-
-void SparseMatrix_kcoreness(SparseMatrix A, int **coreness){
-
- int coreness_max, *coreness_ptr = NULL, *coreness_list = NULL, i, j;
-
- if (!(*coreness)) coreness = MALLOC(sizeof(int)*A->m);
-
- SparseMatrix_kcore_decomposition(A, &coreness_max, &coreness_ptr, &coreness_list);
-
- for (i = 0; i <= coreness_max; i++){
- for (j = coreness_ptr[i]; j < coreness_ptr[i+1]; j++){
- (*coreness)[coreness_list[j]] = i;
- }
- }
-
- assert(coreness_ptr[coreness_ptr[coreness_max+1]] = A->m);
-
-}
-
-
-
-
-void SparseMatrix_khair_decomposition(SparseMatrix A, int *hairness_max0, int **hairness_ptr0, int **hairness_list0){
- /* define k-hair as the largest subgraph of the graph such that the degree of each node is <=k.
- Give an undirected graph A, find the k-hairness of each vertex
- A: a graph. Will be made undirected and self loop removed
- hairness_max: max hair number.
- hairness_ptr: array of size (hairness_max + 2), element [corness_ptr[i], corness_ptr[i+1])
- . of array hairness_list gives the vertices with hair i, i <= hairness_max
- hairness_list: array of size n = A->m
- */
- SparseMatrix B;
- int i, j, jj, *ia, *ja, n = A->m, nz, istatus, neighb;
- PriorityQueue pq = NULL;
- int gain, deg = 0, k, deg_max = 0, deg_old;
- int *hairness_ptr, *hairness_list, l;
- int *mask;
-
- assert(A->m == A->n);
- B = SparseMatrix_symmetrize(A, FALSE);
- B = SparseMatrix_remove_diagonal(B);
- ia = B->ia;
- ja = B->ja;
-
- mask = MALLOC(sizeof(int)*n);
- for (i = 0; i < n; i++) mask[i] = -1;
-
- pq = PriorityQueue_new(n, n-1);
- for (i = 0; i < n; i++){
- deg = ia[i+1] - ia[i];
- deg_max = MAX(deg_max, deg);
- gain = deg;
- pq = PriorityQueue_push(pq, i, gain);
- }
-
-
- hairness_ptr = MALLOC(sizeof(int)*(deg_max+2));
- hairness_list = MALLOC(sizeof(int)*n);
- deg_old = deg_max;
- hairness_ptr[deg_old + 1] = n;
-
- nz = n - 1;
- while (PriorityQueue_pop(pq, &k, &gain)){
- deg = gain;
- mask[k] = deg;
-
- if (deg < deg_old) {
- //fprintf(stderr,"cptr[%d--%d]=%d\n",deg, deg_old + 1, nz);
- for (j = deg_old; j >= deg; j--) hairness_ptr[j] = nz + 1;
-
- for (jj = hairness_ptr[deg_old]; jj < hairness_ptr [deg_old+1]; jj++){
- l = hairness_list[jj];
- //fprintf(stderr,"=== \nremove node hairness_list[%d]= %d, mask with %d, nelement=%d\n",jj, l, deg_old, pq->count);
- for (j = ia[l]; j < ia[l+1]; j++){
- neighb = ja[j];
- if (neighb == k) deg--;/* k was masked. But we do need to update ts degree */
- if (mask[neighb] < 0){
- gain = PriorityQueue_get_gain(pq, neighb);
- //fprintf(stderr,"update node %d with deg %d, nelement=%d\n",neighb, gain-1, pq->count);
- istatus = PriorityQueue_remove(pq, neighb);
- assert(istatus != 0);
- pq = PriorityQueue_push(pq, neighb, gain - 1);
- }
- }
- }
- mask[k] = 0;/* because a bunch of nodes are removed, k may not be the best node! Unmask */
- pq = PriorityQueue_push(pq, k, deg);
- PriorityQueue_pop(pq, &k, &gain);
- deg = gain;
- mask[k] = deg;
- deg_old = deg;
- }
- //fprintf(stderr,"-------- node with highes deg is %d, deg = %d\n",k,deg);
- //fprintf(stderr,"hairness_lisrt[%d]=%d, mask[%d] = %d\n",nz,k, k, deg);
- assert(deg == deg_old);
- hairness_list[nz--] = k;
- }
- hairness_ptr[deg] = nz + 1;
- assert(nz + 1 == 0);
- for (i = 0; i < deg; i++) hairness_ptr[i] = 0;
-
- *hairness_max0 = deg_max;
- *hairness_ptr0 = hairness_ptr;
- *hairness_list0 = hairness_list;
-
- if (Verbose){
- for (i = 0; i <= deg_max; i++){
- if (hairness_ptr[i+1] - hairness_ptr[i] > 0){
- fprintf(stderr,"num_in_hair[%d] = %d: ",i, hairness_ptr[i+1] - hairness_ptr[i]);
-#if 0
- for (j = hairness_ptr[i]; j < hairness_ptr[i+1]; j++){
- fprintf(stderr,"%d,",hairness_list[j]);
- }
-#endif
- fprintf(stderr,"\n");
- }
- }
- }
- if (Verbose)
-
-
- if (B != A) SparseMatrix_delete(B);
- FREE(mask);
-}
-
-
-void SparseMatrix_khairness(SparseMatrix A, int **hairness){
-
- int hairness_max, *hairness_ptr = NULL, *hairness_list = NULL, i, j;
-
- if (!(*hairness)) hairness = MALLOC(sizeof(int)*A->m);
-
- SparseMatrix_khair_decomposition(A, &hairness_max, &hairness_ptr, &hairness_list);
-
- for (i = 0; i <= hairness_max; i++){
- for (j = hairness_ptr[i]; j < hairness_ptr[i+1]; j++){
- (*hairness)[hairness_list[j]] = i;
- }
- }
-
- assert(hairness_ptr[hairness_ptr[hairness_max+1]] = A->m);
-
-}
-#endif
-
void SparseMatrix_page_rank(SparseMatrix A, real teleport_probablity, int weighted, real epsilon, real **page_rank){
/* A(i,j)/Sum_k A(i,k) gives the probablity of the random surfer walking from i to j
A: n x n square matrix