Esempio n. 1
0
void GRAPHmstP2 (Graph G, Vertex parnt[], double cst[]) {
    Vertex v, w, fr[maxV]; 
    link p;

    GRAPHmaxcst(G);

    for (v = 0; v < G->V; v++) {
        cst[v] = INFINITO;
        parnt[v] = -1;
    }
    PQinit(G->V);
    cst[0] = 0;
    fr[0] = 0;
    PQinsert(0);
    while (!PQempty()) {
        v = PQdelmin(cst);
        parnt[v] = fr[v];
        for (p=G->adj[v];p!=NULL;p=p->next){
            w = p->w;
            if (parnt[w] == -1){
                if (cst[w] == INFINITO){
                    cst[w] = p->cst;
                    fr[w] = v;
                    PQinsert(w);
                }
                else if (cst[w] > p->cst){
                    cst[w] = p->cst;
                    fr[w] = v;
                }
            }
        }
    }
}
Esempio n. 2
0
void dijkstra(Digraph G, Vertex s){
  Vertex v, w; link p;
  for (v = 0; v < G->V; v++) {
    cst[v] = INFINITO;
    parnt[v] = -1;
  }
  PQinit(G->V);
  cst[s] = 0;
  parnt[s] = s;
  PQinsert(s);
  while (!PQempty()) {
    v = PQdelmin();
    for(p=G->adj[v];p!=NULL;p=p->next)
      if(cst[w=p->w]==INFINITO){
        cst[w]=cst[v]+p->cst;
        parnt[w]=v;
        PQinsert(w);
      }else if(cst[w]>cst[v]+p->cst){
        cst[w]=cst[v]+p->cst;
        parnt[w] = v;
        PQdec(w);
      }
  }

  PQfree();
}
Esempio n. 3
0
void dijkstra(Graph G, Vertex s, Vertex parnt[], double cst[]) {
	Vertex v, w; 
	link p;

    GRAPHmaxcst(G);

	for (v = 0; v < G->V; v++) {
		cst[v] = INFINITO;
		parnt[v] = -1;
	}
	
	PQinit(G->V);
	cst[s] = 1;
	parnt[s] = s;
	PQinsert(s);
	
	while (!PQempty()) {
		v = PQdelmax(cst);
		for(p=G->adj[v];p!=NULL;p=p->next) {
			if (cst[w=p->w]==INFINITO) {
				cst[w]=cst[v]*p->cst;
				parnt[w]=v;
				PQinsert(w);
			}
			else if(cst[w]<cst[v]*p->cst) {
				cst[w]=cst[v]*p->cst;
				parnt[w] = v;
			}
		}
	}
	PQfree();
}
Esempio n. 4
0
void GRAPHpfs(Graph G, int st[], double wt[])  { 
  link t; 
  int v, w;
  PQinit(); 
  priority = wt;
  for (v = 0; v < G->V; v++){ 
      st[v] = -1; 
      fr[v] = -1; 
  }
  fr[0] = 0;
  PQinsert(0);
  while (!PQempty())
    {
      v = PQdelmin(); st[v] = fr[v];
      for (t = G->adj[v]; t != NULL; t = t->next)
	if (fr[w = t->v] == -1){
	   wt[w] = P;
	   PQinsert(w); 
	   fr[w] = v;
	}
	else if ((st[w] == -1) && (P < wt[w])){
	  wt[w] = P; 
	  PQdec(w); 
	  fr[w] = v; 
	}
    }
}
Esempio n. 5
0
void GRAPHpfs(Tree T, int s, int *st, double *wt){
  int v, w;
  link t;
  struct fibheap *pq;

  pq = fh_makeheap();
  fh_setcmp(pq, wt_cmp);
  /* PQinit(); */
  /* priority = wt; */
  for (v = 0; v < G->V; v++) {
    st[v] = -1; 
    wt[v] = Inf;
    PQinsert(v);
  }
  wt[s] = 0.0;
  PQdec(s);
  while (!PQempty())
    if (wt[v = PQdelmin()] != maxWT)
      for (t = G->adj[v]; t != NULL; t = t->next)
	if (P < wt[w = t->v]){
	  wt[w] = P; 
	  PQdec(w); 
	  st[w] = v;
	}
}
void PQsort(Item a[], int l, int r)
{
  int k;

  PQinit(r-l);
  for (k = l; k <= r; k++)
    PQinsert(a[k]);
  for (k = r; k >=l; k--)
    a[k] = PQdelmax();
}
Esempio n. 7
0
/* triPath:
 * Find the shortest path with lengths in g from
 * v0 to v1. The returned vector (dad) encodes the
 * shorted path from v1 to v0. That path is given by
 * v1, dad[v1], dad[dad[v1]], ..., v0.
 */
static int *
triPath(tgraph * g, int n, int v0, int v1, PQ * pq)
{
    int i, j, adjn;
    double d;
    tnode *np;
    tedge *e;
    int *dad = N_NEW(n, int);

    for (i = 0; i < pq->PQsize; i++)
	N_VAL(pq, i) = UNSEEN;

    PQinit(pq);
    N_DAD(v0) = -1;
    N_VAL(pq, v0) = 0;
    PQinsert(pq, v0);

    while ((i = PQremove(pq)) != -1) {
	N_VAL(pq, i) *= -1;
	if (i == v1)
	    break;
	np = g->nodes + i;
	for (j = 0; j < np->ne; j++) {
	    e = g->edges + np->edges[j];
	    if (e->t == i)
		adjn = e->h;
	    else
		adjn = e->t;
	    if (N_VAL(pq, adjn) < 0) {
		d = -(N_VAL(pq, i) + E_WT(e));
		if (N_VAL(pq, adjn) == UNSEEN) {
		    N_VAL(pq, adjn) = d;
		    N_DAD(adjn) = i;
		    PQinsert(pq, adjn);
		} else if (N_VAL(pq, adjn) < d) {
		    PQupdate(pq, adjn, d);
		    N_DAD(adjn) = i;
		}
	    }
	}
    }
    return dad;
}
bool VoronoiDiagramGenerator::voronoi(int triangulate)
{
	struct Site *newsite, *bot, *top, *temp, *p;
	struct Site *v;
	struct Point newintstar;
	int pm;
	struct Halfedge *lbnd, *rbnd, *llbnd, *rrbnd, *bisector;
	struct Edge *e;
	
	PQinitialize();
	bottomsite = nextone();
	out_site(bottomsite);
	bool retval = ELinitialize();

	if(!retval)
		return false;
	
	newsite = nextone();
	while(1)
	{

		if(!PQempty()) 
			newintstar = PQ_min();
		
		//if the lowest site has a smaller y value than the lowest vector intersection, process the site
		//otherwise process the vector intersection		

		if (newsite != (struct Site *)NULL 	&& (PQempty() || newsite -> coord.y < newintstar.y
			|| (newsite->coord.y == newintstar.y && newsite->coord.x < newintstar.x)))
		{/* new site is smallest - this is a site event*/
			out_site(newsite);						//output the site
			lbnd = ELleftbnd(&(newsite->coord));				//get the first HalfEdge to the LEFT of the new site
			rbnd = ELright(lbnd);						//get the first HalfEdge to the RIGHT of the new site
			bot = rightreg(lbnd);						//if this halfedge has no edge, , bot = bottom site (whatever that is)
			e = bisect(bot, newsite);					//create a new edge that bisects 
			bisector = HEcreate(e, le);					//create a new HalfEdge, setting its ELpm field to 0			
			ELinsert(lbnd, bisector);					//insert this new bisector edge between the left and right vectors in a linked list	

			if ((p = intersect(lbnd, bisector)) != (struct Site *) NULL) 	//if the new bisector intersects with the left edge, remove the left edge's vertex, and put in the new one
			{	
				PQdelete(lbnd);
				PQinsert(lbnd, p, dist(p,newsite));
			};
			lbnd = bisector;						
			bisector = HEcreate(e, re);					//create a new HalfEdge, setting its ELpm field to 1
			ELinsert(lbnd, bisector);					//insert the new HE to the right of the original bisector earlier in the IF stmt

			if ((p = intersect(bisector, rbnd)) != (struct Site *) NULL)	//if this new bisector intersects with the
			{	
				PQinsert(bisector, p, dist(p,newsite));			//push the HE into the ordered linked list of vertices
			};
			newsite = nextone();	
		}
		else if (!PQempty()) /* intersection is smallest - this is a vector event */			
		{	
			lbnd = PQextractmin();						//pop the HalfEdge with the lowest vector off the ordered list of vectors				
			llbnd = ELleft(lbnd);						//get the HalfEdge to the left of the above HE
			rbnd = ELright(lbnd);						//get the HalfEdge to the right of the above HE
			rrbnd = ELright(rbnd);						//get the HalfEdge to the right of the HE to the right of the lowest HE 
			bot = leftreg(lbnd);						//get the Site to the left of the left HE which it bisects
			top = rightreg(rbnd);						//get the Site to the right of the right HE which it bisects

			out_triple(bot, top, rightreg(lbnd));		//output the triple of sites, stating that a circle goes through them

			v = lbnd->vertex;						//get the vertex that caused this event
			makevertex(v);							//set the vertex number - couldn't do this earlier since we didn't know when it would be processed
			endpoint(lbnd->ELedge,lbnd->ELpm,v);	//set the endpoint of the left HalfEdge to be this vector
			endpoint(rbnd->ELedge,rbnd->ELpm,v);	//set the endpoint of the right HalfEdge to be this vector
			ELdelete(lbnd);							//mark the lowest HE for deletion - can't delete yet because there might be pointers to it in Hash Map	
			PQdelete(rbnd);							//remove all vertex events to do with the  right HE
			ELdelete(rbnd);							//mark the right HE for deletion - can't delete yet because there might be pointers to it in Hash Map	
			pm = le;								//set the pm variable to zero
			
			if (bot->coord.y > top->coord.y)		//if the site to the left of the event is higher than the Site
			{										//to the right of it, then swap them and set the 'pm' variable to 1
				temp = bot; 
				bot = top; 
				top = temp; 
				pm = re;
			}
			e = bisect(bot, top);					//create an Edge (or line) that is between the two Sites. This creates
													//the formula of the line, and assigns a line number to it
			bisector = HEcreate(e, pm);				//create a HE from the Edge 'e', and make it point to that edge with its ELedge field
			ELinsert(llbnd, bisector);				//insert the new bisector to the right of the left HE
			endpoint(e, re-pm, v);					//set one endpoint to the new edge to be the vector point 'v'.
													//If the site to the left of this bisector is higher than the right
													//Site, then this endpoint is put in position 0; otherwise in pos 1
			deref(v);								//delete the vector 'v'

			//if left HE and the new bisector don't intersect, then delete the left HE, and reinsert it 
			if((p = intersect(llbnd, bisector)) != (struct Site *) NULL)
			{	
				PQdelete(llbnd);
				PQinsert(llbnd, p, dist(p,bot));
			};

			//if right HE and the new bisector don't intersect, then reinsert it 
			if ((p = intersect(bisector, rrbnd)) != (struct Site *) NULL)
			{	
				PQinsert(bisector, p, dist(p,bot));
			};
		}
		else break;
	};

	


	for(lbnd=ELright(ELleftend); lbnd != ELrightend; lbnd=ELright(lbnd))
	{	
		e = lbnd -> ELedge;

		clip_line(e);
	};

	cleanup();

	return true;
	
}
Esempio n. 9
0
void voronoi(int triangulate, Site * (*nextsite) (void))
{
    Site *newsite, *bot, *top, *temp, *p;
    Site *v;
    Point newintstar = {0};
    char pm;
    Halfedge *lbnd, *rbnd, *llbnd, *rrbnd, *bisector;
    Edge *e;

    edgeinit();
    siteinit();
    PQinitialize();
    bottomsite = (*nextsite) ();
#ifdef STANDALONE
    out_site(bottomsite);
#endif
    ELinitialize();

    newsite = (*nextsite) ();
    while (1) {
	if (!PQempty())
	    newintstar = PQ_min();

	if (newsite != (struct Site *) NULL && (PQempty()
						|| newsite->coord.y <
						newintstar.y
						|| (newsite->coord.y ==
						    newintstar.y
						    && newsite->coord.x <
						    newintstar.x))) {
	    /* new site is smallest */
#ifdef STANDALONE
	    out_site(newsite);
#endif
	    lbnd = ELleftbnd(&(newsite->coord));
	    rbnd = ELright(lbnd);
	    bot = rightreg(lbnd);
	    e = gvbisect(bot, newsite);
	    bisector = HEcreate(e, le);
	    ELinsert(lbnd, bisector);
	    if ((p = hintersect(lbnd, bisector)) != (struct Site *) NULL) {
		PQdelete(lbnd);
		PQinsert(lbnd, p, dist(p, newsite));
	    }
	    lbnd = bisector;
	    bisector = HEcreate(e, re);
	    ELinsert(lbnd, bisector);
	    if ((p = hintersect(bisector, rbnd)) != (struct Site *) NULL)
		PQinsert(bisector, p, dist(p, newsite));
	    newsite = (*nextsite) ();
	} else if (!PQempty()) {
	    /* intersection is smallest */
	    lbnd = PQextractmin();
	    llbnd = ELleft(lbnd);
	    rbnd = ELright(lbnd);
	    rrbnd = ELright(rbnd);
	    bot = leftreg(lbnd);
	    top = rightreg(rbnd);
#ifdef STANDALONE
	    out_triple(bot, top, rightreg(lbnd));
#endif
	    v = lbnd->vertex;
	    makevertex(v);
	    endpoint(lbnd->ELedge, lbnd->ELpm, v);
	    endpoint(rbnd->ELedge, rbnd->ELpm, v);
	    ELdelete(lbnd);
	    PQdelete(rbnd);
	    ELdelete(rbnd);
	    pm = le;
	    if (bot->coord.y > top->coord.y) {
		temp = bot;
		bot = top;
		top = temp;
		pm = re;
	    }
	    e = gvbisect(bot, top);
	    bisector = HEcreate(e, pm);
	    ELinsert(llbnd, bisector);
	    endpoint(e, re - pm, v);
	    deref(v);
	    if ((p = hintersect(llbnd, bisector)) != (struct Site *) NULL) {
		PQdelete(llbnd);
		PQinsert(llbnd, p, dist(p, bot));
	    }
	    if ((p = hintersect(bisector, rrbnd)) != (struct Site *) NULL) {
		PQinsert(bisector, p, dist(p, bot));
	    }
	} else
	    break;
    }

    for (lbnd = ELright(ELleftend); lbnd != ELrightend;
	 lbnd = ELright(lbnd)) {
	e = lbnd->ELedge;
	clip_line(e);
#ifdef STANDALONE
	out_ep(e);
#endif
    }
}
Esempio n. 10
0
void
voronoi(Site *(*nextsite)(void))
{
    Site * newsite, * bot, * top, * temp, * p, * v ;
    Point newintstar ;
    int pm , c;
    Halfedge * lbnd, * rbnd, * llbnd, * rrbnd, * bisector ;
    Edge * e ;

    newintstar.x = newintstar.y = c = 0;
    
    PQinitialize() ;
    rubyvorState.bottomsite = (*nextsite)() ;
    out_site(rubyvorState.bottomsite) ;
    ELinitialize() ;
    newsite = (*nextsite)() ;
    
    while (1)
    {
        if(!PQempty())
            newintstar = PQ_min() ;
        
        if (newsite != (Site *)NULL && (PQempty()
            || newsite -> coord.y < newintstar.y
            || (newsite->coord.y == newintstar.y
            && newsite->coord.x < newintstar.x)))
        {
            /* new site is smallest */
            {
                out_site(newsite) ;
            }
            lbnd = ELleftbnd(&(newsite->coord)) ;
            rbnd = ELright(lbnd) ;
            bot = rightreg(lbnd) ;
            e = bisect(bot, newsite) ;
            bisector = HEcreate(e, le) ;
            ELinsert(lbnd, bisector) ;
            p = intersect(lbnd, bisector) ;
            if (p != (Site *)NULL)
            {
                PQdelete(lbnd) ;
                PQinsert(lbnd, p, dist(p,newsite)) ;
            }
            lbnd = bisector ;
            bisector = HEcreate(e, re) ;
            ELinsert(lbnd, bisector) ;
            p = intersect(bisector, rbnd) ;
            if (p != (Site *)NULL)
            {
                PQinsert(bisector, p, dist(p,newsite)) ;
            }
            newsite = (*nextsite)() ;
        }
        else if (!PQempty())   /* intersection is smallest */
        {
            lbnd = PQextractmin() ;
            llbnd = ELleft(lbnd) ;
            rbnd = ELright(lbnd) ;
            rrbnd = ELright(rbnd) ;
            bot = leftreg(lbnd) ;
            top = rightreg(rbnd) ;
            out_triple(bot, top, rightreg(lbnd)) ;
            v = lbnd->vertex ;
            makevertex(v) ;
            endpoint(lbnd->ELedge, lbnd->ELpm, v);
            endpoint(rbnd->ELedge, rbnd->ELpm, v) ;
            ELdelete(lbnd) ;
            PQdelete(rbnd) ;
            ELdelete(rbnd) ;
            pm = le ;
            if (bot->coord.y > top->coord.y)
            {
                temp = bot ;
                bot = top ;
                top = temp ;
                pm = re ;
            }
            e = bisect(bot, top) ;
            bisector = HEcreate(e, pm) ;
            ELinsert(llbnd, bisector) ;
            endpoint(e, re-pm, v) ;
            deref(v) ;
            p = intersect(llbnd, bisector) ;
            if (p  != (Site *) NULL)
            {
                PQdelete(llbnd) ;
                PQinsert(llbnd, p, dist(p,bot)) ;
            }
            p = intersect(bisector, rrbnd) ;
            if (p != (Site *) NULL)
            {
                PQinsert(bisector, p, dist(p,bot)) ;
            }
        }
        else
        {
            break ;
        }
    }
    
    for( lbnd = ELright(getELleftend()) ;
         lbnd != getELrightend() ;
         lbnd = ELright(lbnd))
    {
        e = lbnd->ELedge ;
        out_ep(e) ;
    }
}
Esempio n. 11
0
int voronoi(struct Site *(*nextsite) (void))
{
    struct Site *newsite, *bot, *top, *temp, *p;
    struct Site *v;
    struct Point newintstar;
    int pm;
    struct Halfedge *lbnd, *rbnd, *llbnd, *rrbnd, *bisector;
    struct Edge *e;
    int counter = 0;

    PQinitialize();
    bottomsite = (*nextsite) ();
    ELinitialize();

    newsite = (*nextsite) ();
    while (1) {
        if (!PQempty())
            newintstar = PQ_min();

        if (newsite != (struct Site *)NULL &&
                (PQempty() || newsite->coord.y < newintstar.y ||
                 (newsite->coord.y == newintstar.y &&
                  newsite->coord.x < newintstar.x))) {	/* new site is smallest */

            G_percent(counter++, nsites, 2);

            lbnd = ELleftbnd(&(newsite->coord));
            rbnd = ELright(lbnd);
            bot = rightreg(lbnd);
            e = bisect(bot, newsite);
            bisector = HEcreate(e, le);
            ELinsert(lbnd, bisector);
            if ((p = intersect(lbnd, bisector)) != (struct Site *)NULL) {
                PQdelete(lbnd);
                PQinsert(lbnd, p, dist(p, newsite));
            }
            lbnd = bisector;
            bisector = HEcreate(e, re);
            ELinsert(lbnd, bisector);
            if ((p = intersect(bisector, rbnd)) != (struct Site *)NULL) {
                PQinsert(bisector, p, dist(p, newsite));
            }
            /* get next site, but ensure that it doesn't have the same
               coordinates as the previous. If so, step over to the following
               site. Andrea Aime 4/7/2001 */
            do
                temp = (*nextsite) ();
            while (temp != (struct Site *)NULL &&
                    temp->coord.x == newsite->coord.x &&
                    temp->coord.y == newsite->coord.y);
            newsite = temp;
        }
        else if (!PQempty()) {
            /* intersection is smallest */
            lbnd = PQextractmin();
            llbnd = ELleft(lbnd);
            rbnd = ELright(lbnd);
            rrbnd = ELright(rbnd);
            bot = leftreg(lbnd);
            top = rightreg(rbnd);

            v = lbnd->vertex;
            makevertex(v);
            endpoint(lbnd->ELedge, lbnd->ELpm, v);
            endpoint(rbnd->ELedge, rbnd->ELpm, v);
            ELdelete(lbnd);
            PQdelete(rbnd);
            ELdelete(rbnd);
            pm = le;
            if (bot->coord.y > top->coord.y) {
                temp = bot;
                bot = top;
                top = temp;
                pm = re;
            }
            e = bisect(bot, top);
            bisector = HEcreate(e, pm);
            ELinsert(llbnd, bisector);
            endpoint(e, re - pm, v);
            deref(v);
            if ((p = intersect(llbnd, bisector)) != (struct Site *)NULL) {
                PQdelete(llbnd);
                PQinsert(llbnd, p, dist(p, bot));
            }
            if ((p = intersect(bisector, rrbnd)) != (struct Site *)NULL) {
                PQinsert(bisector, p, dist(p, bot));
            }
        }
        else
            break;
    }

    G_percent(1, 1, 1);

    return 0;
}