0

I generated this code to test a random undirected graph 100 times and randomly generating the nodes and weights of the graph. My problem is that when I try to store the shortest path by calling minimum distance something is going wrong and when I return the size of my list it is always 1. What is wrong?

// Random Graph Generator
    for (int n = 1; n <= 101; ++n)
    {
        int r = 0;
        nodeCount = 10;           //rand() % 8128 + 64;

        while (r <= nodeCount)
        {

            ++r;
            int nodeNumb = (rand() % 6); // Generates a possible node from 0 to 6 (seven possiblities) 
            int nodeDest = (rand() % 6); // Generates a possible node destination the same as above

            int node_weight = rand() % 100 + 1; // Generate random weight of node from 1 to 101

                                                // Create adjacency list
            adjacency_list[nodeNumb].push_back(neighbourer(nodeDest, node_weight));
            // For undirected graph create opposite connection back 
            adjacency_list[nodeDest].push_back(neighbourer(nodeNumb, node_weight));
        }

        vector<weight_w> min_distance; // declare vector for minimum distance
        vector<vertex_v> previous; // declare vector to hold previos 

        int origin = 3; // origin to be inputted
        int destination = 5; // destination to be inputted

        list<double> pathCount;
        DijkstraComputePaths(origin, adjacency_list, min_distance, previous);
        pathCount.push_back(min_distance[destination]);

        for (int deleteIterator = 0; deleteIterator <= 6; ++deleteIterator)
        {
            adjacency_list[deleteIterator].clear(); 
        }

        cout << "The List Size is: " << pathCount.size() << endl;

    }
ForeverStudent
  • 2,487
  • 1
  • 14
  • 33
Daniel R
  • 103
  • 1
  • 5

1 Answers1

0

The reason you always only have only 1 element in your list is because you have list<double> pathCount; inside the body of your outer for loop.

This means on every iteration you are destroying your old list and creating a fresh one and only appending 1 value to it.

Instead, move the definition of pathCount outside the for loop. This way it will have larger scope than the for loop.

of course I cannot guarantee correctness of your program after that fix because definitions for neighbourer() vertex_v, weight_w and DisjkstraComputePaths are missing.

ForeverStudent
  • 2,487
  • 1
  • 14
  • 33