Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
    binheap_t h;
    int32_t a[1024];
    int32_t i, j, r;
    int32_t parent, child;
    binheap_node_t *nodes[1024];
    
    for (i = 0; i < 1024; i++) {
        a[i] = 1024 - i;
    }
    
    binheap_init(&h, compare_int, NULL);
    for (i = 0; i < 1024; i++) {
        binheap_insert(&h, a + i);
    }
    
    for (i = 0; i < 1024; i++) {
        parent = (i - 1) / 2;
        child = (2 * i) + 1;
        if (!i) {
            if (*(int *) h.array[i]->datum > *(int *) h.array[child]->datum ||
                *(int *) h.array[i]->datum > *(int *) h.array[child + 1]->datum) {
                printf("Error follows:\n");
            }
            printf("%4d:      %4d %4d %4d\n",
                   h.array[i]->index,
                   *(int *) h.array[i]->datum,
                   *(int *) h.array[child]->datum,
                   *(int *) h.array[child + 1]->datum);
        } else if (i < (h.size - 1) / 2) {
            if (*(int *) h.array[i]->datum < *(int *) h.array[parent]->datum ||
                *(int *) h.array[i]->datum > *(int *) h.array[child]->datum ||
                *(int *) h.array[i]->datum > *(int *) h.array[child + 1]->datum) {
                printf("Error follows:\n");
            }
            printf("%4d: %4d %4d %4d %4d\n",
                   h.array[i]->index,
                   *(int *) h.array[parent]->datum,
                   *(int *) h.array[i]->datum,
                   *(int *) h.array[child]->datum,
                   *(int *) h.array[child + 1]->datum);
        } else {
            if (*(int *) h.array[i]->datum < *(int *) h.array[parent]->datum) {
                printf("Error follows:\n");
            }
            printf("%4d: %4d %4d\n",
                   h.array[i]->index,
                   *(int *) h.array[parent]->datum,
                   *(int *) h.array[i]->datum);
        }
    }
    
    binheap_delete(&h);
    
    binheap_init_from_array(&h, a, sizeof (*a), 1024, compare_int, NULL);
    
    for (i = 0; i < 1024; i++) {
        parent = (i - 1) / 2;
        child = (2 * i) + 1;
        if (!i) {
            if (*(int *) h.array[i]->datum > *(int *) h.array[child]->datum ||
                *(int *) h.array[i]->datum > *(int *) h.array[child + 1]->datum) {
                printf("Error follows:\n");
            }
            printf("%4d:      %4d %4d %4d\n",
                   h.array[i]->index,
                   *(int *) h.array[i]->datum,
                   *(int *) h.array[child]->datum,
                   *(int *) h.array[child + 1]->datum);
        } else if (i < (h.size - 1) / 2) {
            if (*(int *) h.array[i]->datum < *(int *) h.array[parent]->datum ||
                *(int *) h.array[i]->datum > *(int *) h.array[child]->datum ||
                *(int *) h.array[i]->datum > *(int *) h.array[child + 1]->datum) {
                printf("Error follows:\n");
            }
            printf("%4d: %4d %4d %4d %4d\n",
                   h.array[i]->index,
                   *(int *) h.array[parent]->datum,
                   *(int *) h.array[i]->datum,
                   *(int *) h.array[child]->datum,
                   *(int *) h.array[child + 1]->datum);
        } else {
            if (*(int *) h.array[i]->datum < *(int *) h.array[parent]->datum) {
                printf("Error follows:\n");
            }
            printf("%4d: %4d %4d\n",
                   h.array[i]->index,
                   *(int *) h.array[parent]->datum,
                   *(int *) h.array[i]->datum);
        }
    }
    
    while (!binheap_is_empty(&h)) {
        binheap_remove_min(&h);
        
        for (i = 0; i < h.size; i++) {
            parent = (i - 1) / 2;
            child = (2 * i) + 1;
            if (h.size == 1) {
                printf("%4d:      %4d\n",
                       h.array[i]->index,
                       *(int *) h.array[i]->datum);
            } else if (!i) {
                if (*(int *) h.array[i]->datum > *(int *) h.array[child]->datum ||
                    *(int *) h.array[i]->datum > *(int *) h.array[child + 1]->datum) {
                    printf("Error follows:\n");
                }
                printf("%4d:      %4d %4d %4d\n",
                       h.array[i]->index,
                       *(int *) h.array[i]->datum,
                       *(int *) h.array[child]->datum,
                       *(int *) h.array[child + 1]->datum);
            } else if (i < (h.size - 1) / 2) {
                if (*(int *) h.array[i]->datum < *(int *) h.array[parent]->datum ||
                    *(int *) h.array[i]->datum > *(int *) h.array[child]->datum ||
                    *(int *) h.array[i]->datum > *(int *) h.array[child + 1]->datum) {
                    printf("Error follows:\n");
                }
                printf("%4d: %4d %4d %4d %4d\n",
                       h.array[i]->index,
                       *(int *) h.array[parent]->datum,
                       *(int *) h.array[i]->datum,
                       *(int *) h.array[child]->datum,
                       *(int *) h.array[child + 1]->datum);
            } else {
                if (*(int *) h.array[i]->datum < *(int *) h.array[parent]->datum) {
                    printf("Error follows:\n");
                }
                printf("%4d: %4d %4d\n",
                       h.array[i]->index,
                       *(int *) h.array[parent]->datum,
                       *(int *) h.array[i]->datum);
            }
        }
    }
    
    binheap_delete(&h);
    
    binheap_init(&h, compare_int, NULL);
    for (i = 0; i < 1024; i++) {
        nodes[i] = binheap_insert(&h, a + i);
    }
    
    for (i = 0; i < 1024; i++) {
        printf("%d\n", *(int *) nodes[i]->datum);
    }
    
    for (j = 0; j < 1; j++) {
        r = 1020;
        a[r] = 0;
        binheap_decrease_key(&h, nodes[r]);
        for (i = 0; i < h.size; i++) {
            parent = (i - 1) / 2;
            child = (2 * i) + 1;
            if (h.size == 1) {
                printf("%4d:      %4d\n",
                       h.array[i]->index,
                       *(int *) h.array[i]->datum);
            } else if (!i) {
                if (*(int *) h.array[i]->datum > *(int *) h.array[child]->datum ||
                    *(int *) h.array[i]->datum > *(int *) h.array[child + 1]->datum) {
                    printf("Error follows:\n");
                }
                printf("%4d:      %4d %4d %4d\n",
                       h.array[i]->index,
                       *(int *) h.array[i]->datum,
                       *(int *) h.array[child]->datum,
                       *(int *) h.array[child + 1]->datum);
            } else if (i < (h.size - 1) / 2) {
                if (*(int *) h.array[i]->datum < *(int *) h.array[parent]->datum ||
                    *(int *) h.array[i]->datum > *(int *) h.array[child]->datum ||
                    *(int *) h.array[i]->datum > *(int *) h.array[child + 1]->datum) {
                    printf("Error follows:\n");
                }
                printf("%4d: %4d %4d %4d %4d\n",
                       h.array[i]->index,
                       *(int *) h.array[parent]->datum,
                       *(int *) h.array[i]->datum,
                       *(int *) h.array[child]->datum,
                       *(int *) h.array[child + 1]->datum);
            } else {
                if (*(int *) h.array[i]->datum < *(int *) h.array[parent]->datum) {
                    printf("Error follows:\n");
                }
                printf("%4d: %4d %4d\n",
                       h.array[i]->index,
                       *(int *) h.array[parent]->datum,
                       *(int *) h.array[i]->datum);
            }
        }
    }
    
    binheap_delete(&h);
    
    return 0;
}
Ejemplo n.º 2
0
/*--------------------------------------------------------------*/
int shortest(Graph *graph, Heap *H, double *A_weights, const PathVertex *start, List *paths, int (*match)
   (const void *key1, const void *key2), int gw, int gh) {

AdjList            *adjlist;

PathVertex         *pth_vertex,
                   *adj_vertex;

ListElmt           *element,
                   *member;

int                found,
                   i;


/*PairHeap			H;
Position			*P;	*/

/*Heap				*H;				*/	/* A_weights binary heap, used for priority queue */
/*double				*A_weights;	*/		/* array of weights to be min-heapified */

CoordData			Index2Coord[(gw*gh*2)+1];
int					Coord2Index[gw][gh][2];

int					index;
AdjList				*Coord2Vertex[gw][gh][2];


/*****************************************************************************
*                                                                            *
*  Initialize all of the vertices in the graph.                              *
*                                                                            *
*****************************************************************************/

found = 0;
index = 1;

for (element = list_head(&graph_adjlists(graph)); element != NULL; element =
   list_next(element)) {

   pth_vertex = ((AdjList *)list_data(element))->vertex;
   

   if (match(pth_vertex, start)) {
	   short   int x,y,z;

      /***********************************************************************
      *                                                                      *
      *  Initialize the start vertex.                                        *
      *                                                                      *
      ***********************************************************************/

	   x = ((CoordData*)((PathVertex*)pth_vertex)->data)->x;
	   y = ((CoordData*)((PathVertex*)pth_vertex)->data)->y;
	   z = ((CoordData*)((PathVertex*)pth_vertex)->data)->z;
	   Coord2Vertex[x][y][z] = list_data(element);
	   
	   
	  pth_vertex->color = white;
      pth_vertex->d = 0;
      pth_vertex->parent = NULL;
      found = 1;
	  

	  A_weights[index] = pth_vertex->d;

	  Coord2Index[x][y][z] = index;
	  Index2Coord[index].x = x;
	  Index2Coord[index].y = y;
	  Index2Coord[index].z = z;
	  index++;

      }

   else {
	   short   int x,y,z;

      /***********************************************************************
      *                                                                      *
      *  Initialize vertices other than the start vertex.                    *
      *                                                                      *
      ***********************************************************************/

	   x = ((CoordData*)((PathVertex*)pth_vertex)->data)->x;
	   y = ((CoordData*)((PathVertex*)pth_vertex)->data)->y;
	   z = ((CoordData*)((PathVertex*)pth_vertex)->data)->z;
	   Coord2Vertex[x][y][z] = list_data(element);

	   
	   
      pth_vertex->color = white;
      pth_vertex->d = DBL_MAX;
      pth_vertex->parent = NULL;

	  A_weights[index] = pth_vertex->d;
	  
	  Coord2Index[x][y][z] = index;
	  Index2Coord[index].x = x;
	  Index2Coord[index].y = y;
	  Index2Coord[index].z = z;
	  index++;


   }
}

/*****************************************************************************
*                                                                            *
*  Return if the start vertex was not found.                                 *
*                                                                            *
*****************************************************************************/

if (!found)
   return -1;


binheap_build(H,A_weights,gw*gh*2);		/* build the heap */

/*****************************************************************************
*                                                                            *
*  Use Dijkstra's algorithm to compute shortest paths from the start vertex. *
*                                                                            *
*****************************************************************************/

i = 0;

while (i < graph_vcount(graph)) {
	short   int x,y,z;

   /**************************************************************************
   *                                                                         *
   *  Select the white vertex with the smallest shortest-path estimate.      *
   *                                                                         *
   **************************************************************************/


	index = binheap_indexofmin(H);			/* get index of minimum-weight edge */
	binheap_extract(H);						/* remove it from the heap */
	
	x = Index2Coord[index].x;
	y = Index2Coord[index].y;
	z = Index2Coord[index].z;
	adjlist = Coord2Vertex[x][y][z];
	
	/**************************************************************************
   *                                                                         *
   *  Color the selected vertex black.                                       *
   *                                                                         *
   **************************************************************************/

   ((PathVertex *)adjlist->vertex)->color = black;
	

   /**************************************************************************
   *                                                                         *
   *  Traverse each vertex adjacent to the selected vertex.                  *
   *                                                                         *
   **************************************************************************/

   for (member = list_head(&adjlist->adjacent); member != NULL; member =
      list_next(member)) {
	   short   int		px,py,pz;

      adj_vertex = list_data(member);
	  
	  px = ((CoordData*)((PathVertex*)adj_vertex)->data)->x;
	  py = ((CoordData*)((PathVertex*)adj_vertex)->data)->y;
	  pz = ((CoordData*)((PathVertex*)adj_vertex)->data)->z;

      /***********************************************************************
      *                                                                      *
      *  Find the adjacent vertex in the list of adjacency-list structures.  *
      *                                                                      *
      ***********************************************************************/
	   
	  pth_vertex = ((AdjList*)Coord2Vertex[px][py][pz])->vertex;
					
	  /*****************************************************************
	  *                                                                *
	  *  Relax the adjacent vertex in the list of adjacency-list       *
	  *  structures.                                                   *
	  *                                                                *
	  *****************************************************************/

	  if (relax(adjlist->vertex, pth_vertex, adj_vertex->weight)) {
		  
		  /* update pth_vertex->d in FH */
		  binheap_decrease_key(H, Coord2Index[px][py][pz] , pth_vertex->d);
		}
   }

   /**************************************************************************
   *                                                                         *
   *  Prepare to select the next vertex.                                     *
   *                                                                         *
   **************************************************************************/

   i++;

}

/* destroy binary heap */
/*binheap_destroy(H);*/
/*free(A_weights);*/

/*****************************************************************************
*                                                                            *
*  Load the vertices with their path information into a list.                *
*                                                                            *
*****************************************************************************/


list_init(paths, NULL);

for (element = list_head(&graph_adjlists(graph)); element != NULL; element =
   list_next(element)) {

   /**************************************************************************
   *                                                                         *
   *  Load each black vertex from the list of adjacency-list structures.     *
   *                                                                         *
   **************************************************************************/

   pth_vertex = ((AdjList *)list_data(element))->vertex;

   if (pth_vertex->color == black) {

      if (list_ins_next(paths, list_tail(paths), pth_vertex) != 0) {
         printf("Problem inserting!\n");
         list_destroy(paths);
         return -1;
      }
   }
}

return 0;

}