简体   繁体   中英

(C++) Dijkstra's Algorithm Backtracking issues

I'm programming Djikstra's algorithm in C++ and I'm getting the correct distances from the source node to the end node but i'm having trouble backtracking the previous nodes visited. It's giving me sort of the correct answer but not the correct answer. Also I noticed with different input data of 1 as the start node and 16 as the finish node that my algorithm is using path's that aren't allowed (it goes from 1 -> 10 -> 8 when 8 isn't allowed) but that could just be me getting path backtracking wrong.

http://pastebin.ca/3188762 - Input data (1st = max nodes and then nodes(node num, x,y) then max edges then all edges with the last line being the start and finish node)

http://textuploader.com/awp89 - Output in console

Code:

#include<iostream>
#include<fstream>

using namespace std;

struct Node
{
    int nodeNum;
    double x, y;
};

void dji(double map[50][50],int startNode,int endNode,int maxNodes);

int main()
{
    int tempA, tempB, maxNodes, maxEdges, startNode, endNode;
    double tempD;
    double map[50][50];

    ifstream fin;
    fin.open("ass03.txt");
    if(fin.good())
    {
        fin >> maxNodes;

        Node allNodes[maxNodes];
        for(int i = 0; i < maxNodes; i++)
        {
            for(int k = 0; k < maxNodes; k++)
            {
                map[i][k] = -1;
            }
            map[i][i] = 0;
        }

        for(int i = 0; i < maxNodes; i++)
        {
            fin >> allNodes[i].nodeNum >> allNodes[i].x >> allNodes[i].y;
        }
        fin >> maxEdges;

        for(int i = 0; i < maxEdges; i++)
        {
            fin >> tempA >> tempB >> tempD;
            map[tempA-1][tempB-1] = tempD;
            map[tempB-1][tempA-1] = tempD;
        }

        fin >> startNode >> endNode;


        cout << "\t";

        for(int i = 0; i < maxNodes; i++)
        {
            cout << i+1 << "\t";
        }
        cout << endl;
        for(int i = 0; i < maxNodes; i++)
        {
            cout << i+1 << "\t";
            for(int k = 0; k < maxNodes; k++)
            {
                cout << map[i][k] << "\t";
            }
            cout << endl;
        }


        dji(map, startNode-1, endNode-1, maxNodes);

    }
    else
    {
        cout << "Incorrect filename" << endl;
    }


    return 0;
}




 void dji(double map[50][50], int startNode,int endNode,int maxNodes)
{
    int Intersections[maxNodes], path[maxNodes], temp; // equate for actual endNode
    double Distances[maxNodes];

    for(int a = 0; a < maxNodes; a++)
    {
        Intersections[a] = a;
        Distances[a] = map[startNode][a];

        if(map[startNode][a] != -1)
        {
            path[a] = startNode;
        }
        else
        {
            path[a] = -1;
        }
    }
    Intersections[startNode] = -1;
    Distances[startNode] = 0;

    double minValue = 99999;
    int minNode = 0;

    for(int l = 0; l < maxNodes; l++)//loop max amount of times to avoid having to function loop (disconsider l = startNode)?
    {
        for (int i = 0; i < maxNodes; i++)
        {
                if(Intersections[i] == -1)
                {
                    continue;
                }

                if(Distances[i] > 0 && Distances[i] < minValue)
                {
                minValue = Distances[i];
                minNode = i;
                }
        }


        if(Intersections[minNode] == endNode)
        {
            temp = l;
        }

        Intersections[minNode] = -1;

        cout << " --- Used Node - " << minNode+1 << endl;

        for(int i = 0; i < maxNodes; i++)
        {
            cout << Intersections[i] << " ";

        }
        cout << endl;

        for(int i = 0; i < maxNodes; i++) 
        {
            if(map[minNode][i] < 0)
            {
                continue;
            }

            if(Distances[i] < 0) 
            {
                Distances[i] = minValue + map[minNode][i];
                path[i] = minNode;
                continue;
            }

            if((Distances[minNode] + map[minNode][i]) < Distances[i]) 
            {
                Distances[i] = minValue + map[minNode][i];
                path[i] = minNode;
            }
        }

        minValue = 99999;
    }

    for(int i = 0; i < maxNodes; i++)
    {
        cout << "Node:"  << i+1 << " - PATH= " << path[i] << "     distance = " << Distances[i]  << endl;
    }

    cout << "Additional nodes used: " << temp << endl;

    temp = path[endNode];
    for(int i = 0; i < 4; i++)
    {
        cout << temp << " ";
        temp = path[temp];
    }

    /*temp = path[endNode];

    int temp2 = path[endNode];

    for(int i = 0; i < maxNodes; i++)
    {
        if(i == 0)
        {
            cout << endNode << " ";
            cout << temp << " ";
        }
        if(i%2 == 0)
        {
            if(temp != endNode)
            {
                temp = path[temp2];
                cout << temp << " ";
            }
            else
            {
                cout << temp << " ";
                i = maxNodes;
            }
        }
        else
        {
            if(temp2 != endNode)
            {
                temp2 = path[temp]-1;
                cout << temp2 << " ";
            }
            else
            {
                cout << temp2 << " ";
                i = maxNodes;
            }
        }
    }*/

    //cout << "PATH = " << endNode << " < - " << path[endNode] << " < - " << path[path[endNode]-1] << " < - " << path[path[path[endNode]-1]-1] <<  endl;

    //cout << "TEST" << path[4] << " " << path[8] << " " << path[16] << " " << endl;
}

Thank you for any help

The problem is you're mixing zero-based and one-based indexing. Your vertices are numbered 1-20, and those are the numbers that end up in your path array with valid indices 0-19. You then use the vertex number as the index into the array.

Change your code to either consistently use the vertex number, or consistently use the array index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM