int extract_min(int y[][N],int m,int n,int row,int col) { if(m == row-1 && n == col - 1) { y[m][n] = INF; return 0; } if(m == row - 1) { y[m][n] = y[m][n+1]; return extract_min(y,m,n+1,row,col); } if(n == col - 1) { y[m][n] = y[m+1][n]; return extract_min(y,m+1,n,row,col); } if(y[m+1][n] < y[m][n+1]) { y[m][n] = y[m+1][n]; return extract_min(y,m+1,n,row,col); } y[m][n] = y[m][n+1]; return extract_min(y,m,n+1,row,col); }
int main(int argc, char *argv[]) { FILE *fp; char *line = NULL; size_t len = 0; int val, min; Heap* Hlow = new_heap(); Heap* Hhigh = new_heap(); int sum = 0; if(argv[1] == NULL) { printf("Please specify a source file\n"); exit(1); } if((fp = fopen(argv[1], "rb")) == NULL) { printf("Couldn't open file. Whomp whomp.\n"); exit(1); } while(getline(&line, &len, fp) != EOF) { sscanf(line, "%d", &val); if(Hlow->size > 0) { peek_min(Hlow, &min, &min); if(0-val < min) heap_insert(Hhigh, val, val); else heap_insert(Hlow, 0-val, 0-val); } else { heap_insert(Hlow, 0-val, 0-val); } /* Equalize size of heaps */ if(Hhigh->size > Hlow->size) { extract_min(Hhigh, &val, &val); heap_insert(Hlow, 0-val, 0-val); } else if(Hlow->size > Hhigh->size+1) { extract_min(Hlow, &val, &val); heap_insert(Hhigh, 0-val, 0-val); } peek_min(Hlow, &min, &min); /* printf("Min: %d\n", min); */ sum += (0-min); } printf("Res: %d\n", sum%10000); return 0; }
long long huffman_tree_way(long long *heap, int length) { int i; long long fst, snd, sum, rt=0; build_min_heap(heap, length); for(i=0; i<length-1; i++) { fst = extract_min(heap); snd = extract_min(heap); sum = fst + snd; rt += sum; insert(heap, sum); } return rt; }
void dijkstra_heap(int** W, int s, int n){ P = (int *)malloc(n*sizeof(int)); D = (int *)malloc(n*sizeof(int)); int i = 0; for(; i < n; i++)D[i] = INFTY; D[s] = 0; P[s] = s; int v = 0; for(; v < n; v++){ if(W[s][v] < INFTY){ D[v] = W[s][v]; P[v] = s; } } build_heap(n,s); int u; while(hsize != 0){ // the heap is not empty u = extract_min(); for(v = 0; v < n; v++){ if(D[v] > D[u] + W[u][v]){ D[v] = D[u] + W[u][v]; P[v] = u; decrease_key(v,D[v]); } } } }
void Dijkstra(int** adj_matrix, int num, int start, int dist[]) { int i, u, v; int id, key; bool* access = malloc(sizeof(bool) * num); struct PQueue* pq = create_pqueue(num); for (i = 0; i < num; i++) { if (i == start) { dist[i] = 0; } else { dist[i] = infinite; } access[i] = false; insert(pq, i, dist[i]); } for(i = 0; i < num; i++) { extract_min(pq, &u, &key); access[u] = true; for (v = 0; v < num; v++) { if (!access[v] && adj_matrix[u][v] && dist[v] > dist[u] + adj_matrix[u][v]) { dist[v] = dist[u] + adj_matrix[u][v]; decrease_key(pq, v, dist[v]); } } } destroy_pqueue(pq); free(access); }
void heap_sort(int s[], int n) { int i, *ip; priority_queue q; /* heap for heapsort */ pq_data d[n]; /* this pq has same values for data and pos*/ for (i = 0; i < n; i++) { d[i].item = malloc(sizeof (int)); memcpy(d[i].item, &(s[i]), sizeof (int)); d[i].pos = s[i]; } make_heap(&q, d, n, sizeof (int)); for (i = 0; i < n; i++) { ip = (int*) extract_min(&q); if (ip != NULL) { s[i] = *ip; free(ip); } free(d[i].item); } }
void dijkstra(MGraph *G, int s) { int i, *set, u, v; MinQueue queue; initialize_single_source(G, s); set = malloc(sizeof(int)*G->numVertexes); queue.size = G->numVertexes; queue.heap = malloc(sizeof(int)*(G->numVertexes+1)); //初始化集合S为空 for(i=0;i<G->numVertexes;i++) { set[i] = 0; } set[s] = 1; //初始化队列为空 for(i=0;i<G->numVertexes;i++) { queue.heap[i+1] = i; } //逐步添加最小权值边 while(queue.size>0) { build_min_heap(&queue); u = extract_min(&queue); set[u] = 1; for(i=0;i<G->numVertexes;i++) { if(G->edges[u][i] != INFINITY) { relax(u,i, G->edges[u][i]); } } } }
void dijkstra(struct Graph *graph, int src) { int v = graph->v, i; int pr; for(i = 0; i < v; i++) { pr = 50000; insert(i, pr); } decreaseKey(src, 0); while(!isEmptyminHeap()) { extract_min(heap); int x = min_vertex, y = min_dist; struct adjListNode *vertex = graph->array[x].head; while(vertex != NULL) { int v = vertex->node; if(isInMinHeap(v) && y != 50000 && vertex->weight + y < get_priority(v)) { pr = y + vertex->weight; decreaseKey(v, pr); } vertex = vertex->next; } } // printArray(graph); }
void Prim(struct vertex *G,int r,int n) { struct vertex *Q[n],*u; struct edge *e; int i; for(i=0;i<n;i++) { G[i].key=99999; G[i].p=0; } G[r-1].key=0; for(i=0;i<n;i++) Insert_Key(Q,&G[i],i+1); i=n; while(i!=0) { u=extract_min(Q,i); i--; for(e=u->edgep;e;e=e->next) if(Find_Vertex(Q,&G[e->destin],i)&&(e->weight < G[e->destin].key)) { G[e->destin].key=e->weight; } } vertex_detail(G,n); }
/* * it returns the weight of huffman code, does not build huffman tree * */ static int huffman(pqueue *pq) { int weight = 0; int t1, t2; int p; build_min_heap(pq); while(pq->size > 1){ t1 = extract_min(pq); t2 = extract_min(pq); p = t1 + t2; insert_pqueue(pq, p); weight += p; } return weight; }
struct Node * build_huffman_tree() { int i; struct Node *arg1, *arg2, *node; for(i=1; i<num; i++) { arg1 = extract_min(); arg2 = extract_min(); node = (struct Node *)malloc(sizeof(struct Node)); node->c = 0; /* non-leaf */ node->weight = arg1->weight + arg2->weight; node->left = arg1; node->right = arg2; heap_insert(node); } struct Node *ret = extract_min(); return ret; }
void heapsort(item_type s[], int n) { int i; priority_queue q; /* heap for heapsort */ make_heap(&q, s, n); for(i = 0; i < n; i++) s[i] = extract_min(&q); }
void Prim_problema1(Graph *G, int ini, int fim) { int pai[G->V]; /*Árvore geradora mínima.*/ int key[G->V]; /*Valores utilizados para retirar o vértice com menor peso no corte.*/ int Q[G->V]; /*O vetor Q simula os vértices que ainda estão na fila de prioridade.*/ int peso = 0; /* Inicializando todas as chaves como INFINITO: */ int u; for (u = 0; u < G->V; u++) { key[u] = INFTY; pai[u] = INFTY; Q[u] = TRUE; /*Todos os vértices são adicionados na fila de prioridade {TRUE}. */ } /* Incluindo o primeiro vértice na MST: */ key[0] = fim; pai[0] = -1; /* Primeiro vértice é sempre a raiz da MST. */ printf("\n\nÁrvore geradora mínima por Prim: \n"); int i = ini; int soma = 0; while (i < fim) { int u = extract_min (key, Q, G->V); Node *v; /*Variável para percorrer a lista de adjacência do vértice {u}*/ for (v = G->listadj[u]; v != NULL; v = v->proximo) { if ( (Q[v->id] == TRUE) && (G->matrixadj[u][v->id] < key[v->id]) ) { pai[v->id] = u; key[v->id] = G->matrixadj[u][v->id]; } } if (i != 0) { /* printf("Aresta: %d - %d com peso %d. \n", pai[u], u, G->matrixadj[u][pai[u]]); soma += G->matrixadj[u][pai[u]];*/ if(G->matrixadj[u][pai[u]] > peso) { peso = G->matrixadj[u][pai[u]]; } } i++; } int l, m; int soma2 = 0; for (l = 0; l < G->V; l++) { for (m = l + 1; m < G->V; m++) { if (G->matrixadj[l][m] != INFTY) { soma2 += G->matrixadj[l][m]; } } } printf("De %d a %d => %d Decibeis.\n", ini, fim, peso); //printf("Soma total: %d, Soma tudo: %d\n", soma, soma2); }
int extract_min(heap **h) { heap *hp = *h; int result = hp->item; if (hp->left == NULL && hp->right == NULL) { free (hp); *h = NULL; } else if (hp->left == NULL && hp->right != NULL) hp->item = extract_min (&hp->right); else if (hp->left != NULL && hp->right == NULL) hp->item = extract_min (&hp->left); else if (hp->left->item < hp->right->item) hp->item = extract_min(&hp->left); else hp->item = extract_min(&hp->right); return result; }
void dijkstra(Dict_t * Q, Agraph_t * G, Agnode_t * n) { Agnode_t *u; Agedge_t *e; pre(G); setdist(n, 1); dtinsert(Q, n); while ((u = extract_min(Q))) { for (e = agfstedge(u); e; e = agnxtedge(e, u)) { update(Q, e->node, u, getlength(e)); } } post(G); }
void do_dijkstra(int source) { struct heap_t cur_min; struct adj_t *cur_ver; int index, new_weight; /* All the initializations are already done */ while (len_heap > 0) { /* * Get the least and add it's values to * weights array. */ cur_min = extract_min(heap, &len_heap); index = cur_min.index; weights[index].dist = cur_min.dist; /* * We expect the parents to already be set * when we update the distances of each vertex */ /* Traverse through the elements adj list and * update the weights in heap,and parents in the * final array. */ for (cur_ver = list[index]; cur_ver != NULL; cur_ver= cur_ver->next) { new_weight = weights[index].dist + cur_ver->edge; /* If the new route is shorter, update */ if (new_weight < weights[cur_ver->index].dist) { /* Update parent in final array */ weights[cur_ver->index].parent = index; /* Update dist in final array */ weights[cur_ver->index].dist = new_weight; /* Update heap */ decrease_key(cur_ver->index, heap, len_heap, new_weight); } } } }
/*Caminhos mínimos por Dijkstra: */ void Dijkstra (Graph *G, int source) { int pai[G->V]; /*Árvore de caminhos mínimos.*/ int dist[G->V]; /*Distâncias mínimas.*/ Queue *Q = criar_queue (G->V); initialize_single_source (source, pai, dist, Q, G->V); while (!vazio_queue(Q)) { int u = extract_min (Q, dist, G->V); Node *v; /*Variável para percorrer a lista de adjacência do vértice {u}*/ for (v = G->listadj[u]; v != NULL; v = v->proximo) { relax (u, v->id, G, pai, dist); } printf("Nó = %d (predecessor = %d), caminho mínimo do nó %d até o nó %d = %d\n", u, pai[u], source, u, dist[u]); } }
int mst_prim(int n) { int i; //int m; //m=n+1; for(i=0;i<n;i++) { queue[0][i]=i+1; queue[1][i]=100; queue[2][i]=1; parent[i]=NULL; } queue[1][0]=0; // make weight of root node is 0 and first node is always root....:) //printf("preprocessing is done\n"); int m; int j=0; while(empty_queue(n)!=1) { extract_min(n); //printf(" node extracted is %d\n",u); queue[2][u-1]=0; //printf("parent of %d=%d\n\n",u,parent[u]); if(parent[u]!=NULL) { edge[j++]=parent[u]*10+u; } p=g[u]; while(p!=NULL) { //printf("Enter in while p=%d\n",p->data); m=(p->data)-1; if(queue[2][m]==1 && p->w <queue[1][m]) { parent[p->data]=u; queue[1][m]=p->w; } //printf("weight of %d=%d\n",p->data,queue[1][m]); p=p->next; } } //printf("mst end\n"); }
void Dijkstra(struct graph *G,char s) { struct vertex *Q[G->count],*S[G->count],*u,*v; struct edge *e; int i=0; n=G->count; Initialize_Single_source(G,s); for(v=G->head;v;v=v->vertexp,i++) Q[i]=v; i=0; while(n!=0) { u=extract_min(Q); S[i++]=u; for(e=u->edgep;e;e=e->next) Relax(u,e->destin,e->weight); } }
/* Run Dijkstra's algorithm from vertex s. Fills in arrays d and pi. */ void dijkstra(int first[], int node[], int next[], double w[], double d[], int pi[], int s, int n, int handle[], int heap_index[]) { int size = n; int u, v, i; initialize_single_source(d, handle, heap_index, pi, s, n); while (size > 0) { u = handle[1]; extract_min(d, handle, heap_index, size); --size; i = first[u]; while (i > 0) { v = node[i]; relax(u, v, w[i], d, handle, heap_index, size, pi); i = next[i]; } } }
void run(priority_queue *pq,int dist[],int pred[],int** costs, int source_index, preference_t preferences, place_t places[], int num_places){ int i; /* counter */ q_element temp; /*storage for what get's dequeued */ /*keep going while queue isn't empty */ while(pq->n>0) { /*take the root off the priority queue */ temp=extract_min(pq); dist[temp.ID]=temp.cost; if(DEBUG){ printf("Dist from source with identity %d is: %d \n",temp.ID , temp.cost); } for (i=0;i<num_places;i++) { /*safeguard from integer overflow */ if(costs[temp.ID][i+1]!=INT_MAX){ /*only updates if it will decrease the distance */ if(dist[temp.ID]+costs[temp.ID][i+1] <dist[i+1]) { /*calls update to ensure the priority queue has the correct values and maintains the heap property */ update(i+1,temp.ID,pred,dist,pq,costs); } } } } /*okay dist/pred arrays populated */ /*let's check out what we got, distance wise */ if (DEBUG) { for(i=1;i<=num_places;i++) { printf("The min cost from %s to %s is: %d\n", places[source_index].city, places[i].city, dist[i]); } } }
void heap_sort (int s[], int n) { heap *h = malloc(sizeof(heap)); h->left = NULL; h->right = NULL; h->item = s[0]; int i; for (i = 1; i < n; i++) { insert(h, s[i]); } for (i = 0; i < n; i++) { s[i] = extract_min (&h); } }
/* resolve o puzzle leaf = open list queue = closed list 0 = deu certo -1 = esgotou todas as possibilidades ou entrada invalida */ int solve_puzzle(int **initial_state, int **final_state, heap_node **leaf, node_queue **queue, int dimension) { if(initial_state == NULL || final_state == NULL || dimension < 2) return -1; if(*initial_state == NULL || *final_state == NULL) return -1; if(leaf == NULL || queue == NULL) return -1; int *distances = calculate_manhattan_distances(initial_state, final_state, dimension); if(distances == NULL) return -1; int sum = manhattan_distances_sum(distances, dimension*dimension); sum += hamming_heuristic_sum(distances, dimension*dimension); free(distances); int *array = matrix_to_array(initial_state, dimension); if(array == NULL) return -1; add_priority_queue(leaf, sum, array, dimension*dimension); int *array_aux = (int*) extract_min(leaf); if(array_aux == NULL) return -1; int **x = array_to_matrix(array_aux, dimension); int i=0; do { add_clildren(x, final_state, leaf, queue, dimension); add_closed_set(x, queue, dimension); // rever necessidade da funcao if(i > 20000) { printf("ERROR\n"); return -1; } if(check(x, final_state, dimension) == 0) { free_matrix(x, dimension); printf("necessary steps: %d\n", i); return 0; } free_matrix(x, dimension); x = extract_min_checked(leaf, queue, dimension); i++; } while(*leaf != NULL); free_matrix(x, dimension); return -1; }
int dijkstra(int si,int sj){ int i, j, mdist = 0; for(i = 0;i < N; i++){ for(j = 0;j < N; j++){ D[i][j] = infinity; visited[i][j] = 0; } } D[si][sj] = 0; heap_size = 0; for(i = 0;i < N; i++){ for(j = 0;j < N; j++){ Q[heap_size].i = i; Q[heap_size].j = j; lookup[i][j] = heap_size; heap_size++; } } build_heap(); node min; while(heap_size > 0){ min = extract_min(); relax(min.i,min.j); } for(i = 0;i < N; i++){ for(j = 0;j < N; j++){ if(D[i][j] > mdist){ mdist = D[i][j]; } } } return mdist; }
int main(void){ int i,j; scanf("%d%d",&M,&N); for(i = 0;i < M; i++){ for(j = 0;j < N; j++){ scanf("\n%c %c %c %c",&Park[i][j].top,&Park[i][j].right,&Park[i][j].bottom,&Park[i][j].left); } } for(i = 0;i < M;i ++){ for(j = 0;j < N;j++){ D[i][j] = infinity; visited[i][j] = 0; } } D[0][0] = 0; heap_size = 0; for(i = 0;i < M ;i ++){ for(j = 0;j < N;j ++){ A[heap_size].i = i; A[heap_size].j = j; pointers[i][j] = heap_size; heap_size++; } } build_heap(); vertex min; while(heap_size > 0){ min = extract_min(); dijsktra(min.i,min.j); } printf("%d",D[M-1][N-1]); return 0; }
void Djkistra(Grafo* grafo, int s){ // Inicializamos a distancias de todos os veritces com um numero muito grande inicializa_fonte_unica(grafo, s); int i, u; // Colocamos todos os vertices como não visitados for(i=0;i<grafo->tamanho;i++) grafo->lista[i]->S = 0; // Adiciona todos os vértices do grafo no heap Heap *heap = inicializa_heap(grafo->tamanho); for(i=0;i<grafo->tamanho;i++){ adiciona_heap(heap, i+1, grafo->lista[i]->d, i); } // Construimos o heap build_min_heap(heap); Vertice *aux; // int numero_adj; while(heap->tamanho != 0){ // Extrai o vértice com a menor distancia da origem u = extract_min(heap); // Coloca esse vértice no conjunto dos visitados grafo->lista[u]->S = 1; // Aux caminhará pelos adjacentes a u aux = grafo->lista[u]->prox; while(aux != NULL){ if(grafo->lista[aux->valor]->S == 0) relax(heap, grafo, u, aux->valor); aux = aux->prox; } } libera_heap(heap); }
void dijkstra(int w[VMAX][VMAX], int n, int *dist, int *p, int *Q, int s){ int u, v; for(v = 1; v <= n; v++){/* Percorre todos vértices */ dist[v] = INF;/* Distancia[v] <- Infinito */ p[v] = 0;/* Predecessor[v] <- Nulo */ Q[v] = 1;/* Q <- V[G] */ } dist[s] = 0; /* Distancia[inicio] <- 0 */ while(!Qvazio(Q, n)){ u = extract_min(Q, dist, n); Q[u] = 0;/* Q <- Q - {u} */ for(v = 1; v <= n; v++){ /* Percorre todos vértices */ if(w[u][v] > 0){ /* Se vértice {v} é adj a {u} */ if(dist[v] > dist[u] + w[u][v]){ dist[v] = dist[u] + w[u][v]; p[v] = u; /* Predecessor[v] = {u} */ Q[v] = 1; /* Q <- Q + {v} */ } } } } }
/* Prim's algorithm */ struct edge *mst_prim ( int size, int ugraph[size][size], int r, \ struct edge ugraph_mst[size*size] ) { /* array of key/parent structs for this matrix, where: key = 999999999, arbitrarily high value to simulate infinity parent = -1, meaning NIL, i.e. no parent */ struct vertex_key_parent vertex_info[size]; for ( int i = 0; i < size; i++ ) { vertex_info[i].key = 999999999; vertex_info[i].parent = -1; } /* initialize starting vertex */ vertex_info[r].key = 0; /* lame implementation of a queue */ bool ugraph_queue[size]; for ( int i = 0; i < size; i++ ) { ugraph_queue[i] = true; } /* loop until queue is empty */ while ( queue_populated ( size, ugraph_queue ) ) { /* find a vertex in the queue whose key value is not less than the key value * of any other vertex in the queue */ int u = extract_min ( size, ugraph_queue, vertex_info ); /* simulate a queue pop by marking the element just popped as no longer * present */ ugraph_queue[u] = false; /* loop through all vertices adjacent to vertex u, the one just found */ for ( int v = 0; v < size; v++ ) { if ( v != u && /* if we are not self-referential */ ugraph[u][v] != 0 && /* if this is an adjacency */ ugraph_queue[v] && /* if this is in the queue */ ugraph[u][v] < vertex_info[v].key /* the weight of this edge is less the weight of the current key */ ) { vertex_info[v].parent = u; vertex_info[v].key = ugraph[u][v]; } } } /* populate the set of edges in the minimum spanning tree */ int mst_edge = 0; for ( int i = 0; i < size; i++ ) { if ( vertex_info[i].parent != -1 ) { ugraph_mst[mst_edge].u = vertex_info[i].parent; ugraph_mst[mst_edge].v = i; mst_edge++; } } /* return the mst */ return ugraph_mst; }
int take(PQ *pq, datatype *data) { return extract_min(pq->heap, data); }
Int_Arraylist* Stack_Sp_Grow(const Stack *stack, const size_t *seeds, int nseed, const size_t *targets, int ntarget, Sp_Grow_Workspace *sgw) { size_t nvoxel = Stack_Voxel_Number(stack); sgw->width = stack->width; sgw->height = stack->height; sgw->depth = stack->depth; /* allocate workspace */ if (sgw->size < nvoxel) { sgw->dist = (double*) Guarded_Realloc(sgw->dist, sizeof(double) * nvoxel, "Stack_Sp_Grow"); sgw->path = (int*) Guarded_Realloc(sgw->path, sizeof(int) * nvoxel, "Stack_Sp_Grow"); sgw->checked = (int*) Guarded_Realloc(sgw->checked, sizeof(int) * nvoxel, "Stack_Sp_Grow"); sgw->flag = (uint8_t*) Guarded_Realloc(sgw->flag, sizeof(uint8_t) * nvoxel, "Stack_Sp_Grow"); if (sgw->lengthBufferEnabled) { sgw->length = (double*) Guarded_Realloc( sgw->length, sizeof(double) * nvoxel, "Stack_Sp_Grow"); } } else { if (sgw->dist == NULL) { sgw->dist = (double*) Guarded_Malloc(sizeof(double) * nvoxel, "Stack_Sp_Grow"); } if (sgw->path == NULL) { sgw->path = (int*) Guarded_Malloc(sizeof(int) * nvoxel, "Stack_Sp_Grow"); } if (sgw->checked == NULL) { sgw->checked = (int*) Guarded_Malloc(sizeof(int) * nvoxel, "Stack_Sp_Grow"); } if (sgw->flag == NULL) { sgw->flag = (uint8_t*) Guarded_Malloc(sizeof(uint8_t) * nvoxel, "Stack_Sp_Grow"); } if (sgw->lengthBufferEnabled) { if (sgw->length == NULL) { sgw->length = (double*) Guarded_Malloc( sizeof(double) * nvoxel, "Stack_Sp_Grow"); } } } sgw->size = nvoxel; /* initialize */ size_t s; for (s = 0; s < nvoxel; s++) { sgw->dist[s] = Infinity; if (sgw->length != NULL) { sgw->length[s] = 0.0; } sgw->path[s] = -1; sgw->checked[s] = 0; sgw->flag[s] = 0; } if (sgw->mask != NULL) { memcpy(sgw->flag, sgw->mask, nvoxel); } /* Recruit seeds (source) */ int i; for (i = 0; i < nseed; i++) { if (sgw->sp_option == 1) { sgw->dist[seeds[i]] = -Stack_Array_Value(stack, seeds[i]); } else { sgw->dist[seeds[i]] = 0.0; } sgw->checked[seeds[i]] = 1; sgw->flag[seeds[i]] = SP_GROW_SOURCE; } if (sgw->mask != NULL) { for (s = 0; s < nvoxel; s++) { if (sgw->flag[s] == SP_GROW_SOURCE) { if (sgw->sp_option == 1) { sgw->dist[s] = -Stack_Array_Value(stack, s); } else { sgw->dist[s] = 0.0; } sgw->checked[s] = 1; } } } Int_Arraylist *result = New_Int_Arraylist(); for (i = 0; i < ntarget; i++) { if (sgw->flag[targets[i]] == 2) { /* overlap of source and target */ Int_Arraylist_Add(result, targets[i]); sgw->value = 0.0; return result; } sgw->flag[targets[i]] = SP_GROW_TARGET; } int width = Stack_Width(stack); int height = Stack_Height(stack); int depth = Stack_Depth(stack); double dist[26]; Stack_Neighbor_Dist_R(sgw->conn, sgw->resolution, dist); int is_in_bound[26]; int neighbors[26]; Stack_Neighbor_Offset(sgw->conn, Stack_Width(stack), Stack_Height(stack), neighbors); BOOL stop = FALSE; /* Check neighbors of seeds */ int j; // for (i = 0; i < nseed; i++) { // size_t r = seeds[i]; /* alloc <heap> */ Int_Arraylist *heap = New_Int_Arraylist(); size_t r; for (r = 0; r < nvoxel; r++) { if (sgw->flag[r] == SP_GROW_SOURCE) { /* seeds */ int nbound = Stack_Neighbor_Bound_Test_I(sgw->conn, width, height, depth, r, is_in_bound); if (nbound == sgw->conn) { /* all neighbors are in bound */ for (j = 0; j < sgw->conn; j++) { size_t nbr_index = r + neighbors[j]; if (sgw->checked[nbr_index] != 1) { update_waiting(stack, r, nbr_index, dist[j], heap, result, sgw); } } } else if (nbound > 0) { for (j = 0; j < sgw->conn; j++) { if (is_in_bound[j]) { size_t nbr_index = r + neighbors[j]; if (sgw->checked[nbr_index] != 1) { update_waiting(stack, r, nbr_index, dist[j], heap, result, sgw); } } } } } } //Verify_Int_Heap_I(heap, sgw->dist); ssize_t last_r = -1; while (stop == FALSE) { ssize_t r = extract_min(sgw->dist, sgw->checked, sgw->size, heap); if (r >= 0) { last_r = r; if (sgw->flag[r] == 0) { /* normal voxel */ int nbound = Stack_Neighbor_Bound_Test_I(sgw->conn, width, height, depth, r, is_in_bound); if (nbound == sgw->conn) { /* all neighbors are in bound */ for (j = 0; j < sgw->conn; j++) { size_t nbr_index = r + neighbors[j]; if (sgw->checked[nbr_index] != 1) { update_waiting(stack, r, nbr_index, dist[j], heap, result, sgw); } } } else if (nbound > 0) { for (j = 0; j < sgw->conn; j++) { if (is_in_bound[j]) { size_t nbr_index = r + neighbors[j]; if (sgw->checked[nbr_index] != 1) { update_waiting(stack, r, nbr_index, dist[j], heap, result, sgw); } } } } //Print_Int_Heap(heap, "%d"); //Verify_Int_Heap_I(heap, sgw->dist); } else if (sgw->flag[r] == SP_GROW_TARGET) { /* target reached */ //Int_Arraylist_Add(result, r); stop = TRUE; } else if (sgw->flag[r] == SP_GROW_CONDUCTOR) { /* 0-distance region (super conductor) */ int nbound = Stack_Neighbor_Bound_Test_I(sgw->conn, width, height, depth, r, is_in_bound); size_t tail_index = r; size_t old_tail_index = tail_index; size_t cur_index = r; #ifdef _DEBUG_2 printf("r: %lu\n", r); #endif #define UPDATE_SUPER_CONDUCTOR \ size_t nbr_index = cur_index + neighbors[j]; \ if (sgw->flag[nbr_index] == 4) { \ if (sgw->checked[nbr_index] > 1) { \ Int_Heap_Remove_I_At(heap, nbr_index, sgw->dist, \ sgw->checked); \ } \ tail_index = update_neighbor(cur_index, tail_index, nbr_index,sgw); \ } else { \ update_waiting(stack, cur_index, nbr_index, dist[j], heap, result,sgw); \ } if (nbound == sgw->conn) { /* all neighbors are in bound */ for (j = 0; j < sgw->conn; j++) { UPDATE_SUPER_CONDUCTOR } } else if (nbound > 0) { for (j = 0; j < sgw->conn; j++) { if (is_in_bound[j]) { UPDATE_SUPER_CONDUCTOR } } } int count = 0; while (tail_index != old_tail_index) { old_tail_index = tail_index; size_t cur_index = tail_index; while (sgw->checked[cur_index] != 1) { int nbound = Stack_Neighbor_Bound_Test_I(sgw->conn, width, height, depth, cur_index, is_in_bound); if (nbound == sgw->conn) { /* all neighbors are in bound */ for (j = 0; j < sgw->conn; j++) { UPDATE_SUPER_CONDUCTOR } } else if (nbound > 0) { for (j = 0; j < sgw->conn; j++) { if (is_in_bound[j]) { UPDATE_SUPER_CONDUCTOR } } } sgw->checked[cur_index] = 1; size_t tmp_index = cur_index; cur_index = sgw->path[cur_index]; sgw->path[tmp_index] = r; count++; } }