Пример #1
0
 /** appends city to the route */    
 void Route::addCity(const City &city)
 {
     cities_.push_back(city);
     citiesIds_.push_back(city.getId());
 }    
Пример #2
0
//it loads the distances and prevs arrays of the Graph object according to the source city
void Graph::dijkstra( int source )
{
    //vector<int> distances(size); dynamicly allocated no need to deallocation

    //priority_queue<int, vector<int>, minHeapComparator> tmpDistances;
    bool firstIteration = true;
    bool isDone = false;
    this->prevs = new int[this->numVertices];
    this->distances = new int[this->numVertices];

    // the completed paths array is parallel with the distances array and it consists of 1's and 0's as flags
    bool *completedPaths = new bool[this->numVertices];
    for(int i = 0; i < this->numVertices; i++)
        completedPaths[i] = false; //initialize all of them with 0's

    completedPaths[source] = true; //the source to source path is considered as completed

    //initialize the distances and prev. values of nodes
    for(int i = 0; i < this->numVertices; i++)
    {
        prevs[i] = -1; // prev. is null

        this->distances[i] = 9999; //distance is infinite
        (i == source) && ( this->distances[i] = 0); //initialize the source's distance with 0

    }

    //debugging
    this->printCurrDistAndPrevs();

    while(!isDone)
    {
        //for each iteration of the loop, determine the initial minIndex value
        //initial minIndex is the first index with an uncompleted path
        int minIndex;

        //in the first iteration, do relaxation with the source node
        if( firstIteration)
        {
            minIndex = source;
            firstIteration = false;
        }
        else
        {
            for(int i = 0; i < numVertices; i++ )
            {
                if( !completedPaths[i])
                {
                    minIndex = i;
                    break;
                }
            }

            //now starting from the initial minIndex, search for real min. index
            for(int i = 0; i < numVertices; i++)
            {
                //if that path is completed, just ignore
                if( completedPaths[i] )
                    continue;

                if( this->distances[i] < this->distances[minIndex] )
                    minIndex = i; // we have a new min. distance
            }
        }

        //according to the Dijkstra's algorithm, the chosen one with min. distance is a completed path from now on.
        completedPaths[minIndex] = true;

        City *tmpCity = this->adjList[minIndex];
        for( int i = 0; i < tmpCity->getDegree(); i++)
        {
            //if the current city's distance + 1 is less than the curr. distance of its neighbor, do relaxation
            if (this->distances[ tmpCity->getId() ] + 1 < this->distances[ tmpCity->getNeighborIds()[i] ] )
            {
                this->prevs[ tmpCity->getNeighborIds()[i] ] = tmpCity->getId();
                this->distances[ tmpCity->getNeighborIds()[i] ] = this->distances[ tmpCity->getId() ] + 1;
            }

            this->printCurrDistAndPrevs();
        }

        // if there is even a single path which is not completed so far, continue to process
        isDone = true;
        cout << "\n------\ncompletedPaths: ";
        for(int i = 0; i < this->numVertices; i++)
        {
            cout << (completedPaths[i] ? "1 " : "0 ");
            if( !completedPaths[i] )
            {
                isDone = false;
                break;
            }
        }
        cout << "\n" << endl;

    }

    delete []completedPaths;
}