int64_t pagerank (stinger_t * S, int64_t NV, double * pr, double * tmp_pr_in, double epsilon, double dampingfactor, int64_t maxiter) { double * tmp_pr = NULL; if(tmp_pr_in) { tmp_pr = tmp_pr_in; } else { tmp_pr = (double *) xmalloc (sizeof(double) * NV); } int64_t iter = maxiter; double delta = 1; while (delta > epsilon && iter > 0) { OMP("omp parallel for") for (uint64_t v = 0; v < NV; v++) { tmp_pr[v] = 0; STINGER_FORALL_EDGES_OF_VTX_BEGIN(S, v) { tmp_pr[v] += (((double)pr[STINGER_EDGE_DEST]) / ((double) stinger_outdegree_get(S, STINGER_EDGE_DEST))); } STINGER_FORALL_EDGES_OF_VTX_END(); } OMP("omp parallel for") for (uint64_t v = 0; v < NV; v++) { tmp_pr[v] = tmp_pr[v] * dampingfactor + (((double)(1-dampingfactor)) / ((double)NV)); } delta = 0; OMP("omp parallel for reduction(+:delta)") for (uint64_t v = 0; v < NV; v++) { double mydelta = tmp_pr[v] - pr[v]; if(mydelta < 0) mydelta = -mydelta; delta += mydelta; } OMP("omp parallel for") for (uint64_t v = 0; v < NV; v++) { pr[v] = tmp_pr[v]; } } if (!tmp_pr_in) free(tmp_pr); }
/* produces the egoent of the stinger vertex ID given in JSON form */ string_t * egonet_to_json(stinger_t * S, int64_t vtx) { string_t vertices, edges; string_init_from_cstr(&vertices, "\"vertices\" : [ \n"); string_init_from_cstr(&edges, "\"edges\" : [ \n"); char vtx_str[1UL<<20UL]; FILE * vtx_file = fmemopen(vtx_str, sizeof(vtx_str), "w"); char edge_str[1UL<<20UL]; int edge_added = 0; stinger_vertex_to_json_with_type_strings(stinger_vertices_get(S), stinger_vtype_names_get(S), stinger_physmap_get(S), vtx, vtx_file, 2); fflush(vtx_file); string_append_cstr(&vertices, vtx_str); fseek(vtx_file, 0, SEEK_SET); int_hm_seq_t * neighbors = int_hm_seq_new(stinger_outdegree_get(S, vtx) + stinger_indegree_get(S, vtx)); int64_t which = 1; STINGER_FORALL_EDGES_OF_VTX_BEGIN(S, vtx) { int64_t source = int_hm_seq_get(neighbors, STINGER_EDGE_DEST); if(INT_HT_SEQ_EMPTY == source) { source = which++; int_hm_seq_insert(neighbors, STINGER_EDGE_DEST, source); fprintf(vtx_file, ",\n"); stinger_vertex_to_json_with_type_strings(stinger_vertices_get(S), stinger_vtype_names_get(S), stinger_physmap_get(S), STINGER_EDGE_DEST, vtx_file, 2); fputc('\0',vtx_file); fflush(vtx_file); string_append_cstr(&vertices, vtx_str); fseek(vtx_file, 0, SEEK_SET); } char * etype = stinger_etype_names_lookup_name(S, STINGER_EDGE_TYPE); const char prepend_str [] = ",\n"; if (STINGER_IS_OUT_EDGE) { if(!edge_added) { edge_added = 1; if(etype) { sprintf(edge_str, "{ \"type\" : \"%s\", \"src\" : %ld, \"dest\" : %ld, \"weight\" : %ld, \"time1\" : %ld, \"time2\" : %ld, \"source\" : %ld, \"target\": %ld }", etype, STINGER_EDGE_SOURCE, STINGER_EDGE_DEST, STINGER_EDGE_WEIGHT, STINGER_EDGE_TIME_FIRST, STINGER_EDGE_TIME_RECENT, (long int) 0, source); } else { sprintf(edge_str, "{ \"type\" : \"%ld\", \"src\" : %ld, \"dest\" : %ld, \"weight\" : %ld, \"time1\" : %ld, \"time2\" : %ld, \"source\" : %ld, \"target\": %ld }", STINGER_EDGE_TYPE, STINGER_EDGE_SOURCE, STINGER_EDGE_DEST, STINGER_EDGE_WEIGHT, STINGER_EDGE_TIME_FIRST, STINGER_EDGE_TIME_RECENT, (long int) 0, source); } } else { if(etype) { sprintf(edge_str, ",\n{ \"type\" : \"%s\", \"src\" : %ld, \"dest\" : %ld, \"weight\" : %ld, \"time1\" : %ld, \"time2\" : %ld, \"source\" : %ld, \"target\": %ld }", etype, STINGER_EDGE_SOURCE, STINGER_EDGE_DEST, STINGER_EDGE_WEIGHT, STINGER_EDGE_TIME_FIRST, STINGER_EDGE_TIME_RECENT, (long int) 0, source); } else { sprintf(edge_str, ",\n{ \"type\" : \"%ld\", \"src\" : %ld, \"dest\" : %ld, \"weight\" : %ld, \"time1\" : %ld, \"time2\" : %ld, \"source\" : %ld, \"target\": %ld }", STINGER_EDGE_TYPE, STINGER_EDGE_SOURCE, STINGER_EDGE_DEST, STINGER_EDGE_WEIGHT, STINGER_EDGE_TIME_FIRST, STINGER_EDGE_TIME_RECENT, (long int) 0, source); } } string_append_cstr(&edges, edge_str); } uint64_t dest = STINGER_EDGE_DEST; STINGER_FORALL_OUT_EDGES_OF_VTX_BEGIN(S, dest) { int64_t target = int_hm_seq_get(neighbors, STINGER_EDGE_DEST); if(INT_HT_SEQ_EMPTY != target) { char * etype = stinger_etype_names_lookup_name(S, STINGER_EDGE_TYPE); if(etype) { sprintf(edge_str, ",\n{ \"type\" : \"%s\", \"src\" : %ld, \"dest\" : %ld, \"weight\" : %ld, \"time1\" : %ld, \"time2\" : %ld, \"source\" : %ld, \"target\": %ld }", etype, STINGER_EDGE_SOURCE, STINGER_EDGE_DEST, STINGER_EDGE_WEIGHT, STINGER_EDGE_TIME_FIRST, STINGER_EDGE_TIME_RECENT, source, target); } else { sprintf(edge_str, ",\n{ \"type\" : \"%ld\", \"src\" : %ld, \"dest\" : %ld, \"weight\" : %ld, \"time1\" : %ld, \"time2\" : %ld, \"source\" : %ld, \"target\": %ld }", STINGER_EDGE_TYPE, STINGER_EDGE_SOURCE, STINGER_EDGE_DEST, STINGER_EDGE_WEIGHT, STINGER_EDGE_TIME_FIRST, STINGER_EDGE_TIME_RECENT, source, target); } string_append_cstr(&edges, edge_str); } } STINGER_FORALL_OUT_EDGES_OF_VTX_END();