double pareto(double a, double b) { double y1; double y2; double y3; y1 = 1 - randfrac(); y2 = -(1/b)*(log(y1)); y3 = pow(a, exp(y2)); return y3; }
Graph * geo(long seed, geo_parms *pp) { double p,d,L,radius,r; u_char *occ; register int scale; unsigned nbytes, index, x, y; u_long units,pertrange; int mallocd; register i,j; Graph *G; Vertex *up,*vp; char namestr[128]; gb_trouble_code = 0; if (seed) /* zero seed means "already init'd */ gb_init_rand(seed); scale = pp->scale; L = sqrt(2.0) * scale; gb_trouble_code = 0; if ((pp->alpha <= 0.0) || (pp->alpha > 1.0)) gb_trouble_code=bad_specs+1; if (pp->gamma < 0.0) gb_trouble_code=bad_specs+GEO_TRBL; if (pp->beta < 0.0) gb_trouble_code=bad_specs+GEO_TRBL; if (pp->n > (scale * scale)) gb_trouble_code=bad_specs+GEO_TRBL; if (gb_trouble_code) return NULL; radius = pp->gamma * L; /* comes in as a fraction of diagonal */ #define posn(x,y) ((x)*scale)+(y) #define bitset(v,i) (v[(i)/NBBY]&(1<<((i)%NBBY))) #define setbit(v,i) v[(i)/NBBY] |= (1<<((i)%NBBY)) /* create a new graph structure */ if ((G=gb_new_graph(pp->n)) == NULL) return NULL; nbytes = ((scale*scale)%NBBY ? (scale*scale/NBBY)+1 : (scale*scale)/NBBY); if (nbytes < STACKMAX) { /* small amount - just do it in the stack */ occ = (u_char *) alloca(nbytes); mallocd = 0; } else { occ = (u_char *) malloc(nbytes); mallocd = 1; } for (i=0; i<nbytes; i++) occ[i] = 0; /* place each of n points in the plane */ for (i=0,vp = G->vertices; i<pp->n; i++,vp++) { sprintf(namestr,"%d",i); /* name is just node number */ vp->name = gb_save_string(namestr); do { vp->xpos = gb_next_rand()%scale; /* position: 0..scale-1 */ vp->ypos = gb_next_rand()%scale; /* position: 0..scale-1 */ index = posn(vp->xpos,vp->ypos); } while (bitset(occ,index)); setbit(occ,index); } /* Now go back and add the edges */ for (i=0,up = G->vertices; i<(pp->n)-1; i++,up++) for (j=i+1, vp = &G->vertices[i+1]; j<pp->n; j++,vp++) { d = distance(up,vp); switch (pp->method) { case RG2: /* Waxman's */ d = randfrac()*L; /* fall through */ case RG1: /* Waxman's */ p = pp->alpha * exp(-d/(L*pp->beta)); break; case RANDOM: /* the classic */ p = pp->alpha; break; case EXP: p = pp->alpha * exp(-d/(L-d)); break; case DL: /* Doar-Leslie */ p = pp->alpha * (pp->gamma/pp->n) * exp(-d/(L*pp->beta)); break; case LOC: /* Locality model, two probabilities */ if (d <= radius) p = pp->alpha; else p = pp->beta; break; default: die("Bad edge method in geo()!"); } if (randfrac() < p) gb_new_edge(up,vp,(int)rint(d)); } /* Fill in the "id" string to say how the graph was created */ G->Gscale = scale; sprintf(G->id,"geo(%ld,", seed); i = strlen(G->id); i += printparms(G->id+i,pp); G->id[i] = ')'; G->id[i+1] = (char) 0; strcpy(G->util_types,GEO_UTIL); /* clean up */ if (mallocd) free(occ); return G; }