static void DeleteElement(PQueue *h, PQueueElement *x) { Replace(h, x, INT_MIN); if (ExtractMin(h) != x) { iError.RaiseError("iPQueue.DeleteElement",CONTAINER_INTERNAL_ERROR); } }
void Dijkstra() { long uNode,vNode,weight,ind; InitializeSingleSource(); scanf("%ld",&source); dist[source]=0; heap_size=0; memset(flag,0,sizeof(0)); Insert(source,0); while(heap_size>0 && flag[0]<=vertex) { uNode=ExtractMin(); if(flag[uNode]) continue; flag[0]+=1; flag[uNode]=1; for(ind=1; ind<=adj[uNode]; ind++) { vNode=adjMat[uNode][ind]; if(flag[vNode]) continue; weight=costMat[uNode][vNode]; Relax(uNode,vNode,weight); } } }
Node AVL_tree::Remove(Node& node, data d){ if(node == NULL) return NULL; if(d < node->d) node->left = Remove(node->left,d); else if(d > node->d) node->right = Remove(node->right,d); else{ // d == node->d Node l = node->left; Node r = node->right; delete(node); --mSize; if(r == NULL) return l; Node min = FindMin(r); min->right = ExtractMin(r); min->left = l; Balance(min); return min; } Balance(node); return node; }
static void DeleteElement(PQueue *h, PQueueElement *x) { Replace(h, x, INT_MIN); if (ExtractMin(h) != x) { abort(); } }
Node AVL_tree::ExtractMin(Node& node){ if(node->left == NULL){ return node->right; } node->left = ExtractMin(node->left); Balance(node); return node; }
static intptr_t Pop(PQueue *p,void *result) { PQueueElement *x = ExtractMin(p); if (x == NULL) return INT_MAX; if (result) memcpy(result,x->Data,p->ElementSize); return x->Key; }
FibonacciHeapNode<Key, Data> * FibonacciHeap<Key, Data>::Delete(FibonacciHeapNode<Key, Data> * node) { FibonacciHeapNode<Key, Data> * parent = node->m_pParent; if (parent != NULL) { Cut(node, parent); CascadingCut(parent); } m_pMin = node; return ExtractMin(); }
void AStar::Execute(const Graph &Graph, const string &VetexId) { const auto& Vertexes = Graph.GetVertexes(); Vertex* pVertexStart = Vertexes.find(VetexId)->second; vector< Vertex* > Q; // 初始化顶点 for (auto& it : Vertexes) { Vertex* pV = it.second; pV->PathfindingData.Cost = 0; pV->PathfindingData.pParent = nullptr; pV->PathfindingData.Heuristic = 0x0FFFFFFF; pV->PathfindingData.Flag = false; } // 初始化起始顶点 pVertexStart->PathfindingData.pParent = 0; pVertexStart->PathfindingData.Cost = 0; pVertexStart->PathfindingData.Heuristic = Estimate(pVertexStart, m_pVTarget); // 把起始顶点放入列表中 Q.push_back(pVertexStart); pVertexStart->PathfindingData.Flag = true; for (; Q.size() > 0;) { // 选出最小路径估计的顶点 auto v = ExtractMin(Q); v->PathfindingData.Flag = false; if (v == m_pVTarget) { return; } // 对所有的出边进行“松弛” const auto& EO = v->GetEdgesOut(); for (auto& it : EO) { Edge* pEdge = it.second; Vertex* pVEnd = pEdge->GetEndVertex(); bool bRet = Relax(v, pVEnd, pEdge->GetWeight()); // 如果松弛成功,加入列表中 if (bRet && pVEnd->PathfindingData.Flag == false) { Q.push_back(pVEnd); pVEnd->PathfindingData.Flag = true; } } } }
FibHeap::~FibHeap() { FibHeapNode *Temp; if (GetHeapOwnership()) { while (MinRoot != NULL) { Temp = ExtractMin(); delete Temp; } } }
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"); }
int* GetSortedArray(int arr[][n],int k){ int size=n*k; int* output=(int*)malloc(sizeof(int)*size); HNode* heapRoot=CreateHeap(k); //printf("\nCapacity : %d",heapRoot->capacity); int i; for(i=0;i<k;i++){ InsertHeap(heapRoot,arr[i][0],i,0); } /* struct HeapNode* tmpNode=ExtractMin(heapRoot); printf("\nData : %d arrayIndex : %d elm Index : %d",tmpNode->data,tmpNode->arrayIndex,tmpNode->elmIndex); tmpNode=ExtractMin(heapRoot); printf("\nData : %d arrayIndex : %d elm Index : %d",tmpNode->data,tmpNode->arrayIndex,tmpNode->elmIndex); InsertHeap(heapRoot,arr[0][1],0,1); tmpNode=ExtractMin(heapRoot); printf("\nData : %d arrayIndex : %d elm Index : %d",tmpNode->data,tmpNode->arrayIndex,tmpNode->elmIndex); */ int index=0; struct HeapNode* tmpNode; int arrayIndex; int elmIndex; while(heapRoot->count>0){ tmpNode=ExtractMin(heapRoot); //printf("\n%d ",tmpNode->data); output[index++]=tmpNode->data; arrayIndex=tmpNode->arrayIndex; elmIndex=tmpNode->elmIndex; if(elmIndex<n-1){ InsertHeap(heapRoot,arr[arrayIndex][elmIndex+1],arrayIndex,elmIndex+1); } } /* while(heapRoot->count){ output[index++]=ExtractMin(heapRoot)->data; }*/ return output; }
void DJ(long start,long vert) { long i,u; Init(start,vert); while(heapsize>0) { u=ExtractMin(); for(i=0;i<a[u].Num_of_adj;i++) { if(cost[u]+a[u].weight[i]<cost[a[u].adj[i]]) { par[a[u].adj[i]]=u; cost[a[u].adj[i]]=cost[u]+a[u].weight[i]; Insert_heap(heapsize++,a[u].adj[i]); } } } }
int FibHeap::Delete(FibHeapNode *theNode) { FibHeapNode Temp; int Result; if (theNode == NULL) return NOTOK; Temp.NegInfinityFlag = 1; Result = DecreaseKey(theNode, Temp); if (Result == OK) if (ExtractMin() == NULL) Result = NOTOK; if (Result == OK) { if (GetHeapOwnership()) delete theNode; else theNode->NegInfinityFlag = 0; } return Result; }
void Dijkstra::Execute(const Graph& graph, const string& vetexId) { m_Ret.PathTree.clear(); const auto &Vertexes = graph.GetVertexes(); Vertex* pVertexStart = Vertexes.find(vetexId)->second; vector< Vertex* > Q; //// 初始化 //for (auto& it : Vertexes) //{ // it.second->PathfindingData.Cost = 0x0FFFFFFF; // Q.push_back(it.second); // pVertexStart->PathfindingData.pParent = nullptr; //} ////m_Ret.PathTree[pVertexStart] = 0; // 起始顶点的前驱顶点为空 //pVertexStart->PathfindingData.Cost = 0; //for (; Q.size() > 0;) //{ // // 选出最小路径估计的顶点 // auto v = ExtractMin(Q); // // 对所有的出边进行“松弛” // const auto& EO = v->GetEdgesOut(); // for (auto& it : EO) // { // Edge* pEdge = it.second; // Relax(v, pEdge->GetEndVertex(), pEdge->GetWeight()); // } //} // 初始化 for (auto& it : Vertexes) { it.second->PathfindingData.Cost = 0x0FFFFFFF; pVertexStart->PathfindingData.pParent = nullptr; } // 初始化起始顶点 //m_Ret.PathTree[pVertexStart] = 0; // 起始顶点的前驱顶点为空 pVertexStart->PathfindingData.Cost = 0; pVertexStart->PathfindingData.pParent = nullptr; // 把起始顶点放入列表中 Q.push_back(pVertexStart); pVertexStart->PathfindingData.Flag = true; for (; Q.size() > 0;) { // 选出最小路径估计的顶点 auto v = ExtractMin(Q); v->PathfindingData.Flag = false; // 对所有的出边进行“松弛” const auto& EO = v->GetEdgesOut(); for (auto& it : EO) { Edge* pEdge = it.second; Vertex* pVEnd = pEdge->GetEndVertex(); bool bRel = Relax(v, pVEnd, pEdge->GetWeight()); if (bRel && pVEnd->PathfindingData.Flag == false) { Q.push_back(pVEnd); pVEnd->PathfindingData.Flag = true; } } } }
void AVL_tree::RemoveMin(){ Node min = ExtractMin(root); delete(min); min = NULL; --mSize; }