这是 omnet 使用 Dijkstra 计算最短路径(跳)的结构。
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);
}
}
}
}
我想找到并记录下一个最短的一跳。谁能帮我解决这个问题?理论上,我需要创建一个新结构来计算最短路径,而无需先前选择的下一个节点对吗?谢谢