Beispiel #1
0
void Dijkstra(node*list, int destiny, int count_nodes, int**node_distance, int**node_hops) {
    adj_node*links;
    int dijkstra_u=0;
    int dijkstra_identifier;
    Heap*heap;
    int i, *heap_place;
    heap_place=malloc(count_nodes*sizeof(int));
    if(heap_place==NULL)exit(-1);
    for(i=0; i<count_nodes; i++) heap_place[i]=-1;
    if(count_nodes>0) {
        heap=NewHeap(count_number_nodes(list, count_nodes));
        Initialize_distance_matrix(count_nodes, &(*node_distance), &(*node_hops), list, destiny, heap, &heap_place);
        while(HeapEmpty(heap)) {
            dijkstra_identifier= RemoveMax(heap, (*node_distance), &heap_place, (*node_hops));
            //printf("removed %d from heap\n", dijkstra_identifier);
            dijkstra_u = dijkstra_identifier - 1;
            if((*node_distance)[dijkstra_u]!=-1) {
                /*for each uv*/
                for(links=(list[dijkstra_u]).link; links!=NULL; links=links->next) {
                    if(heap_place[(links->identifier)-1]!=-1) {
                        if(((*node_distance)[(links->identifier)-1] <= min((*node_distance)[dijkstra_u],links->preference))&&
                                ((links->preference)<=(*node_distance)[dijkstra_u])) {
                            /*if it is a consecutive peer route, the route is not usable*/
                            if(!(((*node_distance)[dijkstra_u]==2)&&((links->preference)==2))) {

                                if(((*node_distance)[(links->identifier)-1])==min((*node_distance)[dijkstra_u],links->preference)) {
                                    /*same route type, chooses the one with less hops*/
                                    if(((*node_hops)[(links->identifier)-1])>((*node_hops)[dijkstra_u]+1))
                                        (*node_hops)[(links->identifier)-1]=(*node_hops)[dijkstra_u]+1;
                                } else {
                                    /*changed route type so just updates the number of hops*/
                                    (*node_hops)[(links->identifier)-1]=(*node_hops)[dijkstra_u]+1;
                                    //these 2 lines were outside the else
                                    (*node_distance)[(links->identifier)-1] = min((*node_distance)[dijkstra_u],links->preference);


                                }
                                FixUp(heap, heap_place[(links->identifier)-1], (*node_distance), &heap_place, (*node_hops));

                            }
                        }
                    }
                }
            }
        }
        invert_weights(&(*node_distance), count_nodes);
    }
    free(heap_place);
    return;
}
Beispiel #2
0
int main() 
{
	ElemType arr[] = {1, 7, 5, 3, 8, 6, 15, 13, 14};
	int i, n = sizeof(arr)/sizeof(arr[0]);

	pHeapHeader pHH = (pHeapHeader)malloc(sizeof(struct HeapHeader));
	initHeap(pHH, n);
    createHeap(pHH, arr, n);	

	InsertHeap(pHH, 10);
	for(i=0; i<pHH->currentSize; i++) {
		printf("%d\t", (pHH->point)[i]);
	}
	printf("\nMaxHeap value: %d\n", RemoveMax(pHH));
	for(i=0; i<pHH->currentSize; i++) {
		printf("%d\t", (pHH->point)[i]);
	}



	return 0;
}