理解网络层:距离-向量路由算法与IP地址解析

需积分: 39 0 下载量 197 浏览量 更新于2024-08-22 收藏 3.7MB PPT 举报
"距离-向量路由算法举例-计算机网络ppt" 在计算机网络中,路由算法是网络层的核心组成部分,它们负责确定数据包在网络中的最佳路径。距离-向量路由算法是一种广泛使用的路由策略,该算法基于贝尔曼-福特(Bellman-Ford)原理,每个路由器维护一张到其所有邻居的距离向量表,并周期性地交换这些信息以更新路由表。这个过程旨在寻找到各个目的地的最短路径。 标题中的"距离-向量路由算法举例"可能涉及一个具体的教学案例,如图所示,可能包含一个子网的拓扑结构以及路由器A、I、H、K和J之间的距离和路由信息。描述中的数字8、10、12、6可能代表了不同路由器间的距离或者跳数,用于计算最优路径。 在第二版的吴功宜编著《计算机网络》中,第六章详细介绍了网络层的相关知识。网络层是负责数据包路由选择的关键层次,它的主要任务包括通过路由选择算法找到最佳路径,利用数据链路层的服务,执行拥塞控制和网络互联功能,并为传输层提供服务。 网络互联是通过诸如网桥、路由器和网关等设备实现的,这些设备连接两个或更多网络,形成一个更大的互联网络。在这样的系统中,每个设备必须有唯一的IP地址,这是TCP/IP协议在网络层使用的地址标识符。IP地址是一个32位二进制数,在IPv4中通常以点分十进制形式表示(如202.113.29.119),分为网络号和主机号两部分,用于区分网络和网络内的设备。 IP地址的分类包括A、B、C、D、E五类,其中A、B、C类用于主机,D类用于多播,E类保留给将来使用。分类依据是网络号和主机号的位数,例如,A类地址的网络号占用了前8位,主机号则占据了剩下的24位。随着互联网的发展,IP地址的需求激增,导致IPv4地址的枯竭,因此后来引入了IPv6,提供了更大的地址空间,用128位的二进制地址来满足未来的需求。 距离-向量路由算法的工作原理是,每个路由器定期向邻居发送自己的整个路由表,收到信息的路由器会比较并更新自己的表,以确保拥有最新的路径信息。例如,路由器J可能会收到A、I、H、K的输入,根据这些输入更新其路由表,以确定到各个网络的最短路径。这个过程不断迭代,直到网络中所有路由器的路由表稳定下来。 了解并掌握距离-向量路由算法,以及IP地址的分类和结构,对于理解网络层的工作原理、设计和维护复杂网络至关重要。同时,还需要熟悉其他路由算法,如链路状态路由算法(如OSPF)和混合型算法(如BGP),以及路由协议如ICMP(Internet控制报文协议)和IGMP(Internet组管理协议),这些都构成了网络层的重要组成部分。
2016-10-26 上传
Inputs: [AorV] Either A or V where A is a NxN adjacency matrix, where A(I,J) is nonzero if and only if an edge connects point I to point J NOTE: Works for both symmetric and asymmetric A V is a Nx2 (or Nx3) matrix of x,y,(z) coordinates [xyCorE] Either xy or C or E (or E3) where xy is a Nx2 (or Nx3) matrix of x,y,(z) coordinates (equivalent to V) NOTE: only valid with A as the first input C is a NxN cost (perhaps distance) matrix, where C(I,J) contains the value of the cost to move from point I to point J NOTE: only valid with A as the first input E is a Px2 matrix containing a list of edge connections NOTE: only valid with V as the first input E3 is a Px3 matrix containing a list of edge connections in the first two columns and edge weights in the third column NOTE: only valid with V as the first input [SID] (optional) 1xL vector of starting points. If unspecified, the algorithm will calculate the minimal path from all N points to the finish point(s) (automatically sets SID = 1:N) [FID] (optional) 1xM vector of finish points. If unspecified, the algorithm will calculate the minimal path from the starting point(s) to all N points (automatically sets FID = 1:N) Outputs: [costs] is an LxM matrix of minimum cost values for the minimal paths [paths] is an LxM cell containing the shortest path arrays [showWaitbar] (optional) a scalar logical that initializes a waitbar if nonzero Note: If the inputs are [A,xy] or [V,E], the cost is assumed to be (and is calculated as) the point to point Euclidean distance If the inputs are [A,C] or [V,E3], the cost is obtained from either the C matrix or from the edge weights in the 3rd column of E3 Example: % Calculate the (all pairs) shortest distances and paths using [A,C] inputs n = 7; A = zeros(n); xy = 10*rand(n,2) tri = delaunay(xy(:,1),xy(:,2)); I = tri(:); J = tri(:,[2 3 1]); J = J(:); IJ = I + n*(J-1); A(IJ) = 1 a = (1:n); b = a(ones(n,1),:); C = round(reshape(sqrt(sum((xy(b,:) - xy(b',:)).^2,2)),n,n)) [costs,paths] = dijkstra(A,C) Example: % Calculate the shortest distance and path from point 3 to 5 n = 15; A = zeros(n); xy = 10*rand(n,2) tri = delaunay(xy(:,1),xy(:,2)); I = tri(:); J = tri(:,[2 3 1]); J = J(:); IJ = I + n*(J-1); A(IJ) = 1 [cost,path] = dijkstra(A,xy,3,5) gplot(A,xy,'b.:'); hold on; plot(xy(path,1),xy(path,2),'ro-','LineWidth',2) for k = 1:n, text(xy(k,1),xy(k,2),[' ' num2str(k)],'Color','k'); end Example: % Calculate the shortest distances and paths from the 3rd point to all the rest n = 7; V = 10*rand(n,2) I = delaunay(V(:,1),V(:,2)); J = I(:,[2 3 1]); E = [I(:) J(:)] [costs,paths] = dijkstra(V,E,3) Example: % Calculate the shortest distance and path from points [1 3 4] to [2 3 5 7] n = 7; V = 10*rand(n,2) I = delaunay(V(:,1),V(:,2)); J = I(:,[2 3 1]); E = [I(:) J(:)] [costs,paths] = dijkstra(V,E,[1 3 4],[2 3 5 7]) Revision Notes: (3/13/15) Previously, this code ignored edges that have a cost of zero, potentially producing an incorrect result when such a condition exists. I have solved this issue by using NaNs in the table rather than a sparse matrix of zeros.