| 62 | //------------------------------------------------------------------------------ |
| 63 | |
| 64 | GrB_Info Pagerank // GrB_SUCCESS or error condition |
| 65 | ( |
| 66 | LAGraph_PageRank **Phandle, // output: array of LAGraph_PageRank structs |
| 67 | GrB_Matrix A, // binary input graph, not modified |
| 68 | int itermax, // max number of iterations |
| 69 | double tol, // stop when norm (r-rnew,2) < tol |
| 70 | int *iters // number of iterations taken |
| 71 | ) { |
| 72 | |
| 73 | //-------------------------------------------------------------------------- |
| 74 | // initializations |
| 75 | //-------------------------------------------------------------------------- |
| 76 | |
| 77 | GrB_Info info ; |
| 78 | float rsum ; |
| 79 | float *X = NULL ; |
| 80 | LAGraph_PageRank *P = NULL ; |
| 81 | GrB_BinaryOp op_diff = NULL ; |
| 82 | GrB_Index n, nvals, *I = NULL ; |
| 83 | GrB_Vector r = NULL, t = NULL, d = NULL ; |
| 84 | GrB_Matrix C = NULL, D = NULL, T = NULL ; |
| 85 | GrB_Info rc; |
| 86 | |
| 87 | assert(Phandle); |
| 88 | (*Phandle) = NULL ; |
| 89 | |
| 90 | // n = size (A,1) ; // number of nodes |
| 91 | rc = GrB_Matrix_nrows(&n, A) ; |
| 92 | assert(rc == GrB_SUCCESS) ; |
| 93 | if(n == 0) return (GrB_SUCCESS) ; |
| 94 | |
| 95 | // teleport = (1 - 0.85) / n |
| 96 | float one = 1.0 ; |
| 97 | float teleport = (one - DAMPING) / ((float) n) ; |
| 98 | |
| 99 | // r (i) = 1/n for all nodes i |
| 100 | float x = 1.0 / ((float) n) ; |
| 101 | rc = GrB_Vector_new(&r, GrB_FP32, n) ; |
| 102 | assert(rc == GrB_SUCCESS) ; |
| 103 | rc = GrB_assign(r, NULL, NULL, x, GrB_ALL, n, NULL) ; |
| 104 | assert(rc == GrB_SUCCESS) ; |
| 105 | |
| 106 | // d (i) = out deg of node i |
| 107 | rc = GrB_Vector_new(&d, GrB_FP32, n) ; |
| 108 | assert(rc == GrB_SUCCESS) ; |
| 109 | rc = GrB_reduce(d, NULL, NULL, GrB_PLUS_FP32, A, NULL) ; |
| 110 | assert(rc == GrB_SUCCESS) ; |
| 111 | // GxB_print (d, 3) ; |
| 112 | |
| 113 | // D = (1/diag (d)) * DAMPING |
| 114 | bool iso ; |
| 115 | bool jumbled ; |
| 116 | GrB_Type type ; |
| 117 | GrB_Index vi_size; |
| 118 | GrB_Index vx_size; |
| 119 | |
| 120 | rc = GxB_Vector_export_CSC(&d, &type, &n, &I, (void **)(&X), &vi_size, |
| 121 | &vx_size, &iso, &nvals, &jumbled, NULL) ; |