Exemplo n.º 1
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;

	
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
0
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;
}
Exemplo n.º 4
0
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);
}