Esempio n. 1
0
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);
}
Esempio n. 2
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;

	
}
Esempio n. 3
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;
}
Esempio n. 4
0
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;
}
Esempio n. 5
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;
}