OSPF 路由协议原型系统设计与实现

include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include<cmath>
#include <limits>
using namespace std;

const int MAX_NODES = 1024;

//const int INFINITY = INT_MAX;
const int infinity = std::numeric_limits<int>::max();

struct Node {
    int id;
    int distance;
    Node* previous;
    vector<pair<int, int>>links;
};

Node nodes[MAX_NODES];
int dist[MAX_NODES]; // 到每个节点的最短距离
bool visited[MAX_NODES]; // 标记节点是否被访问过

 

struct Compare {
    bool operator()(const pair<int, Node*>& a, const pair<int, Node*>& b) {
        return a.first > b.first; // 优先选择距离更短的节点
    }
};

void dijkstra(int source) {
    priority_queue<pair<int, Node*>, vector<pair<int, Node*>>, Compare> pq; // 使用自定义的比较函数
    pq.push(make_pair(0, &nodes[source]));
    while (!pq.empty()) {
        Node* currentNode = pq.top().second;
        pq.pop();
        if (visited[currentNode->id]) {
            continue;
        }
        visited[currentNode->id] = true;
        if (currentNode->distance > dist[currentNode->id]) {
            continue;
        }
        for (auto& neighbor : nodes[currentNode->id].links) { // 获取当前节点的邻居节点和距离信息
            int neighborId = neighbor.first; // 邻居节点ID
            int cost = neighbor.second; // 到邻居节点的距离
            if (!visited[neighborId]) { // 如果邻居节点未被访问过,则进行下一步处理
                int newDistance = currentNode->distance + cost; // 计算从源节点到邻居节点的距离
                if (newDistance < dist[neighborId]) { // 如果新距离更短,则更新最短距离和前驱节点信息
                    dist[neighborId] = newDistance;
                    Node* newNode = &nodes[neighborId];
                    newNode->distance = newDistance;
                    newNode->previous = currentNode;
                    pq.push(make_pair(newDistance, newNode)); // 将邻居节点加入优先队列中等待进一步处理
                }
            }
        }
    }
}

 

void printPath(int source, int destination) { // 输出最短路径的函数
    Node* current = &nodes[destination];
    while (current != nullptr) { // 反向追踪路径直到源节点
        cout << "Path: " << current->id << " -> "; // 输出当前节点的ID和箭头指向下一个节点ID的字符串
        current = current->previous; // 更新当前节点为上一个节点指针所指向的节点指针
    }
    cout << "Source node: " << nodes[source].id << endl; // 输出源节点ID

int main() {
    // 初始化节点和链路信息,构建网络拓扑。
    int n = 5; // 节点数量。
    for (int i = 0; i < n; i++) { // 初始化节点信息,包括节点ID和邻居节点信息。每个节点最多有n-1个邻居节点。
        nodes[i].id = i;
        nodes[i].links.resize(n - 1); // 初始化邻居节点信息向量大小为n-1,
        for (int j = 0; j < n - 1; j++) { // 为每个节点随机添加n-1个邻居节点和距离信息。
            nodes[i].links[j] = make_pair(rand() % n, rand() % 100); // 随机选择邻居节点ID和距离。
        }
    }
    // 初始化最短距离数组和访问标记数组。
    for (int i = 0; i < n; i++) { // 初始化最短距离数组和访问标记数组,默认初始值为无穷大和false。
        dist[i] = infinity; // 最短距离初始化为无穷大。
        visited[i] = false; // 访问标记初始化为false。
    }
    // 调用Dijkstra算法计算最短路径。
    dijkstra(0); // 从节点0开始计算最短路径。
    // 打印从节点0到其他节点的最短路径
    for (int i = 1; i < n; i++) {
        printPath(0, i); // 从节点0打印到节点i的最短路径
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值