现在的位置: 首页 > 综合 > 正文

距离矢量路由算法-bellman ford算法

2013年03月12日 ⁄ 综合 ⁄ 共 853字 ⁄ 字号 评论关闭

        距离矢量路由算法(Distance Vector Routing Algorithm)是一种类型的路由算法,其在一个路由中重申跳数的个数来寻找一个最短路径生成树。距离矢量路由算法号召每个路由器在每次更新时发送它的整个路由表,但是仅仅给它的邻居。距离矢量路由算法倾向于路由循环,但是比链路状态路由算法计算更简单。也叫做bellman-ford路由算法。 

Bellman-Ford算法是求解单源点的最短路径问题的一种算法。单源点的最短路径问题是指:给定一个加权有向图G和源点s,对于图G中的任意一点v,求从s到v的最短路径。

Bellman-Ford算法伪代码如下:

BF(G,w,s) // G = Graph, w = weight, s=source
Determine Single Source(G,s);
set Distance(s) = 0; Predecessor(s) = nil;
for each vertex v in G other than s, 
   set Distance(v) = infinity, Predecessor(v) = nil;
for i <- 1 to |V(G)| - 1 do   //|V(G)| Number of vertices in the graph
   for each edge (u,v) in G do
      if Distance(v) > Distance(u) + w(u,v) then
         set Distance(v) = Distance(u) + w(u,v), Predecessor(v) = u;  
for each edge (u,r) in G do
   if Distance(r) > Distance(u) + w(u,r);
      return false; //This means that the graph contains a cycle of negative weight 
                    //and the shortest paths are not well defined
return true; //Lengths of shortest paths are in Distance arrayde:Bellman-Ford-Algorithmus

抱歉!评论已关闭.