Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

c++ - Implementing the k-shortest paths problem

I have implemented a directed weighted graph using map.

struct Edge
{
    int destination;
    int weight;
    Edge(int, int);
};

private:
    int number;
    std::map<std::string, int> vertices;
    std::map<int, std::string> reverseV;
    std::map<int, std::vector<Edge>> vEdges;
public:
public:
    Graph();
    Graph(std::string);
    void addVertex(std::string);
    void addEdge(std::string, std::string, int);
    void ShortestPaths(std::string, std::string);
};

I have to implement function, which solves the k-shortest path routing. Here is the algorithm: https://en.wikipedia.org/wiki/K_shortest_path_routing

And here is my function:

void Graph::ShortestPaths(std::string from, std::string to);
int numberOfwantedPaths = 3; 
    //int sumW = 0;
    std::vector<std::string> path; 
    std::pair<std::vector<std::string>, int> pathANDsumW; 
    std::set<std::pair<std::vector<std::string>, int> > Allpaths; 


    path.push_back(from);
    pathANDsumW = make_pair(path, 0);
    
    std::map<int, int> numberOfpaths; 

    for (auto& it : this->reverseV) 
    {
        numberOfpaths[it.first] = 0;
    }

    std::priority_queue<std::pair<std::vector<std::string>, int>, 
        std::vector<std::pair<std::vector<std::string>, int>>, 
        std::greater<std::pair<std::vector<std::string>, int> > > MinHeap;

    MinHeap.push(pathANDsumW);

    while (!MinHeap.empty() && numberOfpaths[this->vertices[to]] < numberOfwantedPaths)
    {
        std::vector<std::string> tempPath = MinHeap.top().first;
        int sumW = MinHeap.top().second;
        std::string currKey = tempPath[tempPath.size() - 1]; 
        int currKeyVal = this->vertices[currKey];
        numberOfpaths[currKeyVal]++;
        MinHeap.pop();

        if (currKey == to)
        {
            Allpaths.insert(make_pair(tempPath, sumW));
            break;
        }
        if (numberOfpaths[currKeyVal] <= numberOfwantedPaths)
        {
            for (auto& it : this->vEdges[currKeyVal])
            {
                std::vector<std::string> newPath = tempPath;
                newPath.push_back(this->reverseV[it.destination]);
                if (newPath[newPath.size()-1] == to)
                {
                    Allpaths.insert(make_pair(newPath, sumW));
                }
                MinHeap.push(make_pair(newPath, sumW + it.weight));

            }
        }
    }
//printing part
    for (auto& it : Allpaths)
    {
        for (int i = 0; i < it.first.size(); i++)
        {
            std::cout << it.first[i] << " ";
            if (i < it.first.size() - 1)
            {
                std::cout << " -> ";
            }
        }
        std::cout << std::endl;
    }

I can't find the error. What am I doing wrong? It prints only one path 3 times.

question from:https://stackoverflow.com/questions/65848417/implementing-the-k-shortest-paths-problem

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...