int main(){ // BellmanFord BF(readFile("10000Test.txt")); // cout << "Done Reading\n"; // while (1){ // int start, end; // cin >> start >> end; // cout << BF.getShortestPathYen(start, end) << " "; // cout << BF.getShortestPath(start, end) << endl; // } FibonacciHeap<int> Fib; while (1){ printf("1-Insert\n2-removeMin\n"); int choice; cin >> choice; switch (choice){ case 1: int y; cin >> y; Fib.insert(y); break; case 2: Fib.extractMin(); break; default: break; } cout << "Minimum Element: " << Fib.getMin() << "\tSize : " << Fib.size() << endl; } while (1); }
int main() { FibonacciHeap<int> mQ = FibonacciHeap<int>(); int arr[RUN]; for (int i = 0; i < RUN; i++) { arr[i] = i; } for (int i = 0; i < RUN; i++) { mQ.insertNode(arr + i, arr[i]); } #ifdef DEBUG cout<<"Printing Heap"<<endl; mQ.printHeap(); #endif for (int i = 0; i < RUN; i++) { assert(*(mQ.extractMin()) == i); } #ifdef DEBUG cout<<"Printing Heap"<<endl; mQ.printHeap(); #endif for (int i = 0; i < RUN/2; i++) { mQ.insertNode(arr + i, arr[i]); } #ifdef DEBUG cout<<"Printing Heap"<<endl; mQ.printHeap(); #endif #ifdef DEBUG cout<<"Decreasing Key 50 to 5"<<endl; #endif mQ.decreaseKey(arr +4, -1); #ifdef DEBUG mQ.printHeap(); #endif assert(*mQ.extractMin() == 4); return 0; }
void Graph<T>::mst(T inf) { std::vector<EdgeNode<T>*> minSpanTree; typename std::map<T,VertexNode<T>*>::iterator iter = graph->begin(); std::map<T,bool> visitedMap; std::map<T,FibonacciNode<EdgeNode<T>*>*> pointerList; FibonacciHeap<EdgeNode<T>*> fiboHeap; for(;iter!=graph->end();++iter) { VertexNode<T>* vertex = iter->second; //set visited false for each vertex visitedMap[vertex->getVertex()]=false; EdgeNode<T>* edge = new EdgeNode<T>(NULL,vertex,inf); //assign key to infinity. FibonacciNode<EdgeNode<T>*>* fNode = new FibonacciNode<EdgeNode<T>*>(inf,edge); pointerList[vertex->getVertex()] = fNode; fiboHeap.insert(fNode); } int i=0; while(!fiboHeap.isEmpty()) { i++; FibonacciNode<EdgeNode<T>*>* node = fiboHeap.removeMin(); fiboHeap.printFibo(); if(node != NULL) { EdgeNode<T>* tempEdge = node->getData(); //just a tweak to store VertexNode<T>* source = tempEdge->getDestination(); minSpanTree.push_back(tempEdge); visitedMap[source->getVertex()]=true; EdgeNode<T>* temp = source->edgeListHead; while(temp != NULL) { VertexNode<T>* vertex = temp->getDestination(); int fibKey = pointerList[vertex->getVertex()]->getKey(); int edgeKey = temp->getCost(); if(!visitedMap[vertex->getVertex()] && fibKey> edgeKey) { temp->setSource(source); temp->setCost(edgeKey); FibonacciNode<EdgeNode<T>*>* fibNode = pointerList.find(vertex->getVertex())->second; fiboHeap.decreaseKey(fibNode,edgeKey); } temp = temp->next; } } } //cout<<"Total cost::"<<i<<endl; // typename std::vector<EdgeNode<T>*>::iterator printIter = minSpanTree.begin(); // for(;printIter!=minSpanTree.end();++printIter) // cout<<(*printIter)->getSource()->getVertex()<<" "<<(*printIter)->getDestination()->getVertex()<<endl; }
int main() { FibonacciHeap H; char op[10]; int x; while (true) { scanf("%s", op); if (strcmp(op, "push") == 0) { scanf("%d", &x); H.insert(x); } else if (strcmp(op, "pop") == 0) { printf("%d\n", H.extract_min()); } else { printf("Command not found.\n"); } } return 0; }
int main() { file.open("fibonacciHeap.txt"); FibonacciHeap<int> heap; for(int i = 0; i < 1000; i++) heap.insert(i); cout<<"minimo "<<heap.getMinimumKey()<<endl; heap.extractMin(); cout<<"minimo "<<heap.getMinimumKey()<<endl; heap.decreaseKey(999,-2); cout<<"minimo "<<heap.getMinimumKey()<<endl; printHeap(heap); file.close(); return 0; }
DenseSymmetricMatrix compute_shortest_distances_matrix(RandomAccessIterator begin, RandomAccessIterator end, const Neighbors& neighbors, DistanceCallback callback) { timed_context context("Distances shortest path relaxing"); const IndexType n_neighbors = neighbors[0].size(); const IndexType N = (end-begin); FibonacciHeap* heap = new FibonacciHeap(N); bool* s = new bool[N]; bool* f = new bool[N]; DenseSymmetricMatrix shortest_distances(N,N); for (IndexType k=0; k<N; k++) { // fill s and f with false, fill shortest_D with infinity for (IndexType j=0; j<N; j++) { shortest_distances(k,j) = numeric_limits<DenseMatrix::Scalar>::max(); s[j] = false; f[j] = false; } // set distance from k to k as zero shortest_distances(k,k) = 0.0; // insert kth object to heap with zero distance and set f[k] true heap->insert(k,0.0); f[k] = true; // while heap is not empty while (heap->get_num_nodes()>0) { // extract min and set (s)olution state as true and (f)rontier as false ScalarType tmp; int min_item = heap->extract_min(tmp); s[min_item] = true; f[min_item] = false; // for-each edge (min_item->w) for (IndexType i=0; i<n_neighbors; i++) { // get w idx int w = neighbors[min_item][i]; // if w is not in solution yet if (s[w] == false) { // get distance from k to i through min_item ScalarType dist = shortest_distances(k,min_item) + callback(begin[min_item],begin[w]); // if distance can be relaxed if (dist < shortest_distances(k,w)) { // relax distance shortest_distances(k,w) = dist; // if w is in (f)rontier if (f[w]) { // decrease distance in heap heap->decrease_key(w, dist); } else { // insert w to heap and set (f)rontier as true heap->insert(w, dist); f[w] = true; } } } } } // clear heap to re-use heap->clear(); } delete heap; delete[] s; delete[] f; return shortest_distances; }
int shortest_path(int strt,int* hops) //int main() { //printf("\n\nK-Shortest Path Algorithm\n\n"); /* STEP 0: Initialization */ long n; int dat; //int strt = 3; /*if(argv[1]== NULL) { printf("1 Argument required: shortestpath.exe [graph file name]\n"); printf("Example: ./Dijkstra.exe scotland_big.dat\n"); return 0; }*/ // Define test vertices std::vector<Node*> vertices; std::vector<Edge*> edges; // Read source file //printf("Loading %s\n", argv[1]); std::ifstream indat("network.txt"); char inp[100]; if(indat) { indat.getline(inp, 160); n = atol(inp); for(int j=0; j<n-1 ; j++) { Node* v = new Node(j, -1); vertices.push_back(v); } //printf("Vertices loaded...\n"); vertices.push_back(new Node(n-1, 0)); vertices[n-1]->state = LABELED; // Read edges and initialize while(!indat.eof()) { memset(inp, '\0', sizeof(inp)); indat.getline(inp, 160); int k=1; while(inp[k] != ' ' && inp[k]!='\0') k++; std::string inpstr = inp; int tail = atoi(const_cast<char*>(inpstr.substr(0, k).c_str())); int l=k+1; while(inp[l] != ' ' && inp[l]!='\0') l++; int head = atoi(const_cast<char*>(inpstr.substr(k+1, l).c_str())); k=l+1; while(inp[k] != ' ' && inp[k]!='\0') k++; double length = atof(const_cast<char*>(inpstr.substr(l+1, k).c_str())); Edge* edge = new Edge(vertices[tail], vertices[head], length); edge->head->addIncomingEdge(edge); edge->tail->addOutgoingEdge(edge); edges.push_back(edge); Edge* edge1 = new Edge(vertices[head], vertices[tail], length); edge1->head->addIncomingEdge(edge1); edge1->tail->addOutgoingEdge(edge1); edges.push_back(edge1); } } else { //printf("Could not open input data...\n"); return 0; } /*printf("Edges loaded...\n"); printf("Vertices: %d\nEdges: %d\n\n", vertices.size(), edges.size()); printf("Building shortest path tree...\n");*/ /* STEP 1: Shortest Path Tree */ FibonacciHeap* heap = new FibonacciHeap(); heap->insertVertex(vertices[n-1]); bool abort = false; long j = 0; // Scan do { // Delete minimum path Node* v = heap->deleteMin(); v->state = SCANNED; for(int i = 0; i < v->incomingEdges.size(); i++) { Edge* currentEdge = v->incomingEdges[i]; Node* headOfCurrentEdge = currentEdge->tail; if(headOfCurrentEdge->state != SCANNED) { if(headOfCurrentEdge->state == UNLABELED) { // Insert a vertex with infinite key headOfCurrentEdge->state = LABELED; headOfCurrentEdge->pred = v; headOfCurrentEdge->key = v->key + currentEdge->length; heap->insertVertex(headOfCurrentEdge); } else if(headOfCurrentEdge->key > v->key + currentEdge->length ) { // decrease the key of a vertex with finite key headOfCurrentEdge->pred = v; heap->decreaseKey(v->key + currentEdge->length, headOfCurrentEdge); } } } } while(!heap->isEmpty()); // Print out path Node* temp = vertices[strt]; if(!temp->pred) { //printf("There exist no s-t paths\n"); return 0; } int vertexCount = 0; //printf("Shortest Path found:\n"); //printf("Distance: %f\n", vertices[strt]->key); while(temp) { dat = temp->data; if(hops!=NULL) hops[dat]++; //printf("%d", dat); //link<<dat; temp = temp->pred; if(temp) { // printf(" - "); //link<<" - "; } vertexCount++; } //link<<"\n"; //printf("\nVertices passed: %d\n", vertexCount); //char chh = getchar(); return (vertexCount-1); }