0

My goal is to find the shortest paths from each node in a graph to each other node via Dijkstra's algorithm, including equal length paths that are different from each other. To store these paths, each node has a vector for each other node in the graph, which stores a vector of lists containing each minimum path (usually with just one list, but occasionally more if multiple shortest paths exist). There is one of these data structures for each node, and one vector contains all of these data structures - so a vector of vectors of a vector of lists.

The second goal is to solve the TSP problem using shortest path lists exhaustively instead of edge by edge.

So, as part of the algorithm I'm trying to collect all of the lists for one node into a single vector of lists. In other words, I'm trying to convert a two-dimensional vector of lists to a one-dimensional vector of lists.

Every list I find in the two dimensional vector I attempt to push into the one dimensional vector. What happens is the first list copies correctly with all its elements in place, but the following attempts to push a list adds an empty list. The code compiles and executes correctly otherwise.

vector<vector<vector<list<road_data>>>> all_shortest_paths;
...
...
...

int ex_path_search(int current_city, int current_distance, int best_distance) {


    vector<list<road_data>> priority_paths;


    for(int i = 0; i < all_shortest_paths[current_city].size(); i++) {
        for(int j=0; j<all_shortest_paths[current_city][i].size(); j++) {

            //an attempt to fix the dysfunctional copy by making a separate list first
            list<road_data> shortpath = all_shortest_paths[current_city][i][j];

            //first iteration copies correctly, each following iteration copies an empty list
            priority_paths.push_back(shortpath);

        }
    }

The all_shortest_paths structure is nonempty with the exception of the list of edges corresponding to the node it belongs to. I've verified that the all shortest paths algorithm works, but even if it didn't, there are lists everywhere they should be.

I'm stuck trying to figure this out. I tried using a list of lists in place of the vector with the same results. The shortpath above when copied is nonempty, but when I try to push it into priority_paths only an empty list appears. Any suggestions?

Edit: priority_paths replaces the previous list with shortlist at the end of the vector and a new empty list when I call push_back. The final dimensions of priority_paths is correct but the only nonempty element is the last shortpath added.

forley
  • 1
  • 1
  • Are these `std::list`s and `std::vector`s? Can you include a test that fails that you expect to pass? Can you include simple test data that produces a failure case? – Yakk - Adam Nevraumont Nov 11 '13 at 23:48
  • Yes they are. I'm just using structures that have worked for me in the past. The graphs I feed it first have only four nodes and randomly generate edges, so there isn't much to them, and each node has only three shortest paths. For example, with a graph A-B-C-D containing edges A-D weight 9, D-C weight 4, and C-B weight 2 will generate all the correct shortest paths but fails as described. – forley Nov 11 '13 at 23:51

0 Answers0