0

我生成了这段代码来测试一个随机无向图 100 次,并随机生成图的节点和权重。我的问题是,当我尝试通过调用最小距离来存储最短路径时出现问题,当我返回列表的大小时,它始终为 1。出了什么问题?

// 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;

    }
4

1 回答 1

0

您的列表中始终只有 1 个元素的原因是您list<double> pathCount;在外部 for 循环的主体内。

这意味着在每次迭代中,您都在销毁旧列表并创建一个新列表,并且只向其附加 1 个值。

相反,将定义移到pathCountfor 循环之外。这样,它将比 for 循环具有更大的范围。

当然,我不能保证修复后程序的正确性,因为neighbourer() vertex_v,weight_wDisjkstraComputePaths的定义丢失了。

于 2015-12-20T05:13:25.113 回答