Beispiel #1
0
// Dijkstra's algorithm: Compute the distance from a vertex
// This algorithm uses a priority queue (ordered by min distance)
// Returns 0 on success, negative value on error
int graph_shortest_path_from(graph_t* self, vertex_id_t from) {
    b_heap_t* heap;
    vertex_id_t current_id;
    vertex_t* current_vertex;
    adjacent_t* current_adjacent;
    size_t distance = 0;

    if (from >= self->size)
        return -1;

    heap = b_heap_new();

    if (!heap)
        return -1;

    graph_recompute(self,
                    GRAPH_RECOMPUTE_REACHED_BIT | GRAPH_RECOMPUTE_DISTANCE_BIT);

    self->v[from]->distance = 0;

    // The heap initially contains just the first vertex, with a distance 0
    // NOTE: The &from is because we must store pointers
    printf("Pre-insert\n");
    b_heap_insert(heap, &from, 0);

    while (!b_heap_empty(heap)) {
        printf("Pre-scan\n");
        current_id = *((vertex_id_t*)b_heap_front(heap));
        printf("Scanning: %zu\n", current_id);
        b_heap_pop(heap);

        current_vertex = self->v[current_id];
        current_adjacent = current_vertex->adjacents_head;

        if (current_vertex->reached)
            continue;

        current_vertex->reached = 1;

        // Check all the neighbors, calculate distance to them
        while (current_adjacent) {
            distance = current_vertex->distance + current_adjacent->weight;

            // If found a shorter path, add them to the queue
            // Note that at this point an element can be multiple times in
            // the queue, but since it's inserted with different priority
            // the node will be processed at the correct time
            if (self->v[current_adjacent->id]->distance > distance) {
                self->v[current_adjacent->id]->distance = distance;
                self->v[current_adjacent->id]->reached_from = current_id;
                b_heap_insert(heap, &(current_adjacent->id), distance);
            }
            current_adjacent = current_adjacent->next;
        }
    }

    b_heap_destroy(heap);

    return 0;
}
Beispiel #2
0
b_heap_t* adjacents_binary_heap(graph_t* self) {
    b_heap_t* ret = b_heap_new();
    size_t i;
    adjacent_t* current;

    for (i = 0; i < self->size; ++i) {
        current = self->v[i]->adjacents_head;

        while (current) {
            b_heap_insert(
                ret, adjacent_auxiliar_new(i, current->id, current->weight),
                current->weight);
            current = current->next;
        }
    }

    return ret;
}
Beispiel #3
0
int main()
{
    b_heap *H=NULL,*x,*min;
    int n,i;
    printf("\nenter the no of elements to be inserted in heap..\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    { 
         x=(b_heap *)malloc(sizeof(b_heap));
         printf("\n enter the value of key..  ");
         scanf("%d",&x->key);
         H=b_heap_insert(H,x);
         printf("\n traversal of heap is...   ");
         b_heap_traverse(H); 
         printf("\n");
                     
    }
    //printf("find the minimum key...\n");
    min=b_heap_min(H);
    printf("\n minimum key value is: %d",min->key);
    getch();
    
}