Routing Algorithms
来源:互联网 发布:你可能需要与该网络isp 编辑:程序博客网 时间:2024/06/04 19:01
The main function of the network layer is routing packets from the source machine to the destination machine. And the routing algorithm is that part of the network layer software responsible for deciding which output line an incoming packet should be transmitted on.
The idea is to build a graph of the network, with each node of the graph representing a router and each edge of the graph representing a communication line, or link. To choose a route between a given pair of routers, the algorithm just finds the shortest path between them on the graph. For this, Dijkstra's algorithm based on the idea of greedy algorithms is given below.
const int MAX_NODES = 1024; // maximum number of nodesconst int INFINITY = 1000000000; // a number larger than every maximum pathint n, dist[MAX_NODES][MAX_NODES]; // dist[i][j] is the distance from i to jvoid shortest_path(int s, int t, int path[])// the shortest path from t to s{ struct state { // the path being worked onint predecessor; // previous nodeint length; // length from source to this nodeenum {permanent, tentative} label; // label state of a priority queue} state[MAX_NODES];int i, k, min;struct state *p;for (p = &state[0]; p < &state[n]; p++) { // initialize statep->predecessor = -1;p->length = INFINITY;p->label = tentative;}state[t].length = 0; state[t].label = permanent;k = t; // k is the initial working nodedo { // Is there a better path from k?for (i = 0; i < n; i++) // this graph has n nodesif (dist[k][i] != 0 && state[i].label == tentative) {if (state[k].length + dist[k][i] < state[i].length) {state[i].predecessor = k;state[i].length = state[k].length + dist[k][i];}}// Find the tentatively labeled node with the smallest label.k = 0; min = INFINITY;for (i = 0; i < n; i++)if (state[i].label == tentative && state[i].length < min) {min = state[i].length;k = i;}state[k].label = permanent;// update the priority queue} while (k != s);// Copy the path into the output array.i = 0; k = s;do {path[i++] = k; k = state[k].predecessor; } while (k >= 0);}And an example is presented below to illustrate the algorithm.
0 0
- Routing Algorithms
- Routing
- Routing
- Routing
- Algorithms
- algorithms
- algorithms
- Algorithms
- ALGORITHMS
- Algorithms
- Algorithms
- unicast-routing
- 测试Routing
- Routing简介
- Routing参考文献
- PCB routing
- IP Routing
- Source Routing
- QT中窗口-与视口的转换
- mybatis 开发环境搭建
- wcf abc开发4
- eclipse.exe无法运行
- javascript实现 1,2,3,4,5,6,7,8,9倒序(用递归),并放入一个数组中
- Routing Algorithms
- NSRunLoop
- 扁平化设计的前世今生
- wcf abc开发5
- iOS-在后台仍然处理定位服务CLLocationManagerDelegate
- MySql表空间的概念
- 【Java基础】
- Java 8简明教程
- javascript 排序 冒泡排序法 + 插入排序法