コード例 #1
0
ファイル: rr-D.c プロジェクト: Felipe31/Ramalingam-Reps
void dijkstra(vertex *vertices, heap *root){
    int min_vertex, old_cost, new_cost, adjacent;
    //printGraph(vertices, n_ver);
    node *min_node;
    while(root->root_node){
        min_node = heapExtractMin(root);
        min_vertex = min_node->vertex;
        vertices[min_vertex].heap_node.key = -1;
        //printf("\nmin_vertex: %d\n", min_vertex);
        edge *aux = vertices[min_vertex].adjacent;
        while(aux){
            adjacent = aux->head_vertex;
            new_cost = vertices[min_vertex].cost + aux->cost;
                //printf("new_cost: %d\tadjacent: %d\n", new_cost, adjacent);
            if(new_cost < vertices[adjacent].cost){
                if(vertices[adjacent].heap_node.key == -1){
                    vertices[adjacent].heap_node.key = new_cost;
                    heapInsert(root, &(vertices[adjacent].heap_node));
                }
                else{
                    heapDecreaseKey(root, &(vertices[adjacent].heap_node), new_cost);
                }
                vertices[adjacent].predecessor = min_vertex;
                vertices[adjacent].cost = new_cost;
                //printGraph(vertices, n_ver);
            }
            aux = aux->next;
        }
    }
}
コード例 #2
0
ファイル: dijkstra.c プロジェクト: perico63/dijkstra
void dijkstra(vertex *vertices, heap *root){
	int min_vertex, old_cost, new_cost, adjacent;
	node *min_node;
	while(root->root_node){
		min_node = heapExtractMin(root);
		min_vertex = min_node->vertex;
		free(min_node);
		vertices[min_vertex].heap_node = NULL;
		edge *aux = vertices[min_vertex].adjacent;
		while(aux){
			adjacent = aux->head_vertex;
			new_cost = vertices[min_vertex].cost + aux->cost;
			if(new_cost < vertices[adjacent].cost){
				if(!vertices[adjacent].heap_node){
					vertices[adjacent].heap_node = heapInsert(root, new_cost, adjacent);
				}
				else{
					heapDecreaseKey(root, vertices[adjacent].heap_node, new_cost);
				}
				vertices[adjacent].predecessor = min_vertex;
				vertices[adjacent].cost = new_cost;
			}
			aux = aux->next;
		}
	}
}