void vtkPolyDataSingleSourceShortestPath::ShortestPath(int startv, int endv) { int i, u, v; InitSingleSource(startv); HeapInsert(startv); this->f->SetValue(startv, 1); int stop = 0; while ((u = HeapExtractMin()) >= 0 && !stop) { // u is now in s since the shortest path to u is determined this->s->SetValue(u, 1); // remove u from the front set this->f->SetValue(u, 0); if (u == endv && StopWhenEndReached) stop = 1; // Update all vertices v adjacent to u for (i = 0; i < Adj[u]->GetNumberOfIds(); i++) { v = Adj[u]->GetId(i); // s is the set of vertices with determined shortest path...do not use them again if (!this->s->GetValue(v)) { // Only relax edges where the end is not in s and edge is in the front set double w = EdgeCost(this->GetInput(), u, v); if (this->f->GetValue(v)) { Relax(u, v, w); } // add edge v to front set else { this->f->SetValue(v, 1); this->d->SetValue(v, this->d->GetValue(u) + w); // Set Predecessor of v to be u this->pre->SetValue(v, u); HeapInsert(v); } } } } }
struct QuantizedValue *FindMatch(uint8 const *sample, int ndims, uint8 *weights, struct QuantizedValue *q) { InitHeap(&TheQueue); struct QuantizedValue *bestmatch=0; double besterror=1.0e63; PUSHNODE(q); for(;;) { struct QuantizedValue *test=(struct QuantizedValue *) RemoveHeapItem(&TheQueue); if (! test) break; // heap empty // printf("got pop node =%p minerror=%f\n",test,test->MinError); if (test->MinError>besterror) break; if (test->Children[0]) { // it's a parent node. put the children on the queue struct QuantizedValue *c1=test->Children[0]; struct QuantizedValue *c2=test->Children[1]; c1->MinError=MinimumError(c1,sample,ndims,weights); if (c1->MinError < besterror) HeapInsert(&TheQueue,&(c1->MinError)); c2->MinError=MinimumError(c2,sample,ndims,weights); if (c2->MinError < besterror) HeapInsert(&TheQueue,&(c2->MinError)); } else { // it's a leaf node. This must be a new minimum or the MinError // test would have failed. if (test->MinError < besterror) { bestmatch=test; besterror=test->MinError; } } } if (bestmatch) { SquaredError+=besterror; bestmatch->NQuant++; for(int i=0;i<ndims;i++) bestmatch->Sums[i]+=sample[i]; } return bestmatch; }
void ValueTableOpLogMeta::InsertMergeRowOpLogMeta( int32_t row_id, const RowOpLogMeta& row_oplog_meta) { int32_t index = heap_index_[row_id]; if (index >= 0) { HeapIncrease(index, row_oplog_meta); } else { HeapInsert(row_id, row_oplog_meta); num_new_oplog_metas_++; } }
int main () { Data_t a [10] = {4,2,7,1,9,4,6,2,8,0}; struct BHeap *heap = HeapBuild(a, 10); // struct BHeap *heap = HeapNew(10); HeapPrint(heap); printf("%d\n", HeapExtractMin(heap)); HeapPrint (heap); HeapInsert (heap, 1); HeapPrint(heap); HeapDestroy(heap); return 0; }
void Insert(State* s) { State* existing = TableFind(s); if (existing) { if (s->Distance() < existing->Distance()) { existing->distance_so_far = s->distance_so_far; existing->distance_to_goal = s->distance_to_goal; existing->parent = s->parent; if (existing->heap_index >= 0) { HeapUpdatePriority(existing->heap_index); } else { HeapInsert(existing); } } else if (existing->heap_index == -1) { HeapInsert(existing); } } else { TableInsert(s); HeapInsert(s); } }
void dijkstra(int n,graph *heads[n],int start,int end) { heap *H=calloc(8,sizeof(H)), **IndexAddress=calloc(8,sizeof(heap)*n); H->id = start; H->right = H->left = H; int county[n],i,min; char check[n]; for(i=0;i<n;i++) { county[i] = 1000000; check[i] = 'w'; } graph *node; county[start] = 0; while(H!=NULL) { min = ExtractMin(&H,n); if(check[min]!='b') node = heads[min]; else node = H = NULL; check[min] = 'b'; while(node!=NULL) { if(check[node->dest]=='w') { check[node->dest] = 'g'; county[node->dest] = county[min]+node->time; HeapInsert(&H,county[node->dest],node->dest,IndexAddress); } else if(check[node->dest]=='g' && county[node->dest]>county[min]+node->time) { county[node->dest] = county[min]+node->time; DecreaseKey(&H,IndexAddress[node->dest],county[node->dest]); } node = node->next; } } if(county[end]!=1000000) printf("%d\n",county[end]); else printf("NONE\n"); }
void Mainloop() { long closedset_sz = 0; long openset_sz = 0; //long start_time = clock(); time_t time_start,time_end; time(&time_start); Tree tree= TREE_INITIALIZER(Node_compare); _Node p0 = {0}; p0.uXYZ.lXYZ[0] = 0; p0.uXYZ.lXYZ[1] = 0; ComputeF(&p0); _Node* temp = Node_new(&p0); temp->uXYZ.lXYZ[0] = 0; temp->uXYZ.lXYZ[1] = 0; _Node* p = Node_new(temp); TREE_INSERT(&tree, _Node, linkage, p); HeapInsert(p); openset_sz++; while(true) { _Node* p = HeapRemove(); p->expended = true; closedset_sz++; openset_sz--; if (p->h == 0) { printf("\nThe MLCS is:\n"); int l = CommonSeq(p); printf("\n\n|MLCS|=%d\n", l); break; } for(int i=0; i<ABC_len; i++) { int j=0; if (NUMBYTE == 1) { while (j<MAXSTR) { temp->uXYZ.cXYZ[j] = cur_T[j][p->uXYZ.cXYZ[j]+1][i]; if (temp->uXYZ.cXYZ[j]==str_lngth[j]+1) break; j++; } } else { while (j<MAXSTR) { temp->uXYZ.sXYZ[j] = cur_T[j][p->uXYZ.sXYZ[j]+1][i]; if (temp->uXYZ.sXYZ[j]==str_lngth[j]+1) break; j++; } } if (j < MAXSTR) continue; _Node *q = TREE_FIND(&tree, _Node, linkage, temp); if (q) { //node is already in tree if (!q->expended && (q->fg & 0xffff) < (p->fg & 0xffff)+1) { q->parent = p; q->fg = ((q->h+(p->fg & 0xffff)+1) << 16) | ((p->fg & 0xffff)+1); } } else { q = Node_new(temp); q->parent = p; q->fg = p->fg+1; ComputeF(q); TREE_INSERT(&tree, _Node, linkage, q); HeapInsert(q); openset_sz++; } } } printf("|closedset|=%d\n|openset|=%d\n\n", closedset_sz, openset_sz+1); time(&time_end); double elapsedTime = difftime(time_end,time_start); printf("The time is %6.2f seconds\n",elapsedTime); }