计算下一个最短路径(到目的地的最小跳数)

calculate next shortest path(minimum hop to destination)

本文关键字:目的地 下一个 最短路径 计算      更新时间:2023-10-16

这里是omnet使用Dijkstra计算最短路径(hop)的结构。

void cTopology::calculateUnweightedSingleShortestPathsTo(Node * _target) {
    // multiple paths not supported :-(
    if (!_target) throw cRuntimeError(this, "..ShortestPathTo(): target node is NULL");
    target = _target;
    for (int i = 0; i < num_nodes; i++) {
        nodev[i].known = false; // not really needed for unweighted
        nodev[i].dist = INFINITY;
        nodev[i].out_path = NULL;
    }
    target - > dist = 0;
    std::deque < Node * > q;
    q.push_back(target);
    while (!q.empty()) {
        Node * v = q.front();
        q.pop_front();
        // for each w adjacent to v...
        for (int i = 0; i < v - > num_in_links; i++) {
            if (!(v - > in_links[i] - > enabl)) continue;
            Node * w = v - > in_links[i] - > src_node;
            if (!w - > enabl) continue;
            if (w - > dist == INFINITY) {
                w - > dist = v - > dist + 1;
                w - > out_path = v - > in_links[i];
                q.push_back(w);
            }
        }
    }
}

我想找到并记录下下一个最短的跳跃。有人能在这件事上帮我吗?理论上,我需要创建一个新的结构,在没有之前选择的下一个节点的情况下计算最短路径,对吧?。感谢

让它变得更容易
你可以使用

parent[]
存储有关上一个节点的数据/id'。。

那么要获得路径,你只需要反转它。

parent[parent[goal]]->parent[target]->parent[目标]

关于代码,你可以在这里看到。。。

http://pastebin.com/YUBicFjy