# The shortest path algorithm in OSPF

Source: Internet
Author: User
1. Dijkstra Algorithm Introduction

In mathematics, the algorithm that calculates the shortest path to the other nodes as the starting point of a node is called the "single Source Shortest path" algorithm. The problem of finding "single source shortest path" can be mathematically accurately described as follows:

"single Source Shortest path" problem: known one by n nodes (V0. N) A forward connected graph g= (v,e), and a weighted function C (e) of the edges in the graph, where V represents a collection of nodes, E represents a collection of all edges, and assumes a non-negative ownership, the shortest path from the specified node in G to each of the other nodes V0.

Dijkstra algorithm is a very classical algorithm to solve the above problems, the basic idea is to design a shortest path tree construction method, according to the non-descending order from V0 to each node of the shortest path, the first step to find and V0 the shortest distance between the node and the path to this node, The second step is to find and V0 the node and the path to this node, so repeatedly, finally find the shortest path V0 to all nodes, construct the whole shortest path tree.

For the above construction method, intuitively it is easy to think: and V0 the shortest distance between the node should be in and V0 directly adjacent to the node, and V0 short-distance node is either in and V0 directly adjacent to the node, or adjacent to these adjacent nodes, so gradually spread considering, should be able to find and V0 the shortest distance , the second short 、....... the nth short node and the corresponding path, and because it is connected graph, finally sure all nodes can be fully considered, but also to complete the whole shortest path tree construction.

In fact, the above intuition is very effective, the Dijkstra algorithm is a refinement and optimization of the above process: and V0 is the shortest node is directly connected with the V0 node is right; the node range shorter than the next can be reduced to, and V0 directly adjacent to the node, plus the shortest node directly adjacent to the first selected nodes , the range of the third-shortest node can be obtained by analogy, that is, on the basis of the nodes examined in the previous step, plus the nodes directly adjacent to the second short node. In this way, the shortest path to all nodes can be given in non-descending order.

In order to accurately describe the above construction process mathematically, it is necessary to introduce the concept of set to classify nodes and paths.

We divide the nodes into two sets:

A: the collection of nodes that have been selected into the shortest path tree.

B: The collection of remaining nodes.

For a path, we are divided into three sets:

I: A collection of paths that have been selected into the shortest path tree

II: candidate Path Collection: The next path to join the shortest path tree will be selected from this collection.

III: The collection of other paths remaining (abandoned paths or paths not yet considered).

For better understanding, it is necessary to define and emphasize the definition of "path": A path is an ordered set consisting of one or more edges, starting with V0 and other nodes as the end point. Edge, which can be understood as a segment of the path, only the path to the node directly adjacent to and V0 directly corresponds to an edge. From V0 to all nodes, there may be one or more paths, and non-shortest paths will be discarded during the calculation and put into set Iii.

As can be seen from the previous description, the Dijkstra algorithm is a recursive construction process, because any recursion must have a definite initial state, so it is necessary to get the initial value of the set defined in the Dijkstra algorithm above: To calculate the shortest path with V0 as the starting point, The initial state is obviously there and only V0 is in set a, so the initial value of set A is V0. The initial value of collection B is the remaining node. As mentioned earlier, the next join set a node, must be and V0 directly with the edge of the node, so, the first path to join the shortest path tree must also be in these and V0 directly connected to the path represented by the edge, so the initial value of the Set II is the V0 directly connected with the edge of the path. In addition, the initial state shortest path tree is empty, so the initial value of set I is empty. Set I, ii clear the words, Set III nature clear.

Let's begin the process of recursively constructing the shortest path tree:

The first step: Select a shortest path from the Set II and put it into the shortest path tree, corresponding to the node of the end of the path (which is recorded as x) should be moved from set B to collection A. The second step: the end of all the edges from X, consider the node which does not belong to the set A, which is recorded as Y, calculates the path value from V0 x to Y, calculated as: The path value of V0 to node x in the shortest path tree plus the value of this edge (x, y). For ease of description, we take the path from V0 x to Y as (v0x) Y. Then we investigate the candidate path in Set II, if there is no path to node Y, then directly add the path (v0x) y as the candidate path to the Set II; If the path value of the path is less than or equal to the path value of path (v0x) y, then the path ( v0x) y is placed in Set III as an abandoned path, otherwise the path of the original set II to Y is discarded into set II, (v0x) y as the candidate path into set Ii.  For the Y node there are multiple cases, the second step of the method is calculated and compared by one. Repeat the first and second steps until Set II and set B are empty.

The second step is actually a recalculation of the candidate path, because after the introduction of a new node in the collection A, the scope of the investigation becomes larger, it is likely that the shortest path to a node is no longer the shortest path in the original node scope, and it needs to be adjusted according to the method of the second step.

To give you a better understanding of this construction process, here is a more typical example, as follows:

The process for calculating the shortest path tree for this forward connected graph is as follows:

 Candidate Path Collection Path length Nodes in the shortest Path tree (collection A) Shortest Path Tree Description V0v1V0v2V0v4 501045 V0 Null In the initial state, only the node V0 in the shortest path tree, and the candidate path is the path represented by the V0 directly connected edge. V0v1V0v4V0v2v3 504535 V0, V2 V0v0V0v2 The v0v2 is the shortest in all candidate paths, so put in the shortest path tree and V2 into set a. Examine all the end points of the edge that are starting from the node V2 of the set a, and the node in which it is not in set a, where only the node is V3, the value of the path V3 is calculated from V0 through the node V2, to V0v2v3, because there is no path to V3 in the candidate path, So the v0v2v3 path is placed directly into the candidate path collection. V0v4V0v2v3v1 50454555 V0, V2, V3 V0v0V0v2V0v2v3 The v0v2v3 is the shortest in all candidate paths, so put in the shortest path tree and V3 into set a. Examine all the end points of the edge that are starting from the node V3 of the set a, to the node in which it is not in set a, which is the node v1,v4, computes the path value from V0 through the node V3, to the path V0v2v3v1 and v0v2v3v4 of the two nodes, and to reach V1 and the candidate path, The path value of the V4 is compared: V0v2v3v1 is less than v0v1, so v0v1 is removed from the candidate path, V0v2v3v1 is placed in the candidate path collection, v0v2v3v4 is larger than v0v4, so v0v4 is retained in the candidate path, and the v0v2v3v4 path is discarded. V0v2v3v1 45 V0, V2, V3, V4 V0v0V0v2V0v2v3V0v4 V0V4 and V0v2v3v1 path values are equal, arbitrary selection v0v4 placed in the shortest path tree, V4 into set a. Examine all the endpoints of the edges that are starting with the node V4 that you just put in set a, where the nodes that are not in collection A are not (although there is an edge v4v3, but V3 is already in collection a), so no selection or calculation is made. V0, V2, V3, V4, V1 V0v0V0v2V0v2v3V0v4V0v2v3v1 The candidate path v0v2v3v1 into the shortest path tree, when the selected path collection is empty, and all nodes have been put into set a. End of calculation.

2. Proof of the Dijkstra algorithm

The Dijkstra algorithm proves that the tree must be the shortest path tree in order to prove its construction process. In order to give proof, we first analyze the construction process.

The second step in the analysis of the construction process can be concluded one .

Conclusion One : for a node y that has not yet been selected into set a, the candidate path is the least of all the paths from V0, halfway through the path of the node in set a to reach Y .

This conclusion is based on the construction process of the second step candidate path, it is easy to conclude that: since the first construction to the candidate path of Y, the path from V0 to the y of the node that has just been added to the set a, is directly into the candidate path set, indicating that the first constructed to Y candidate path satisfies the condition "from V0, Only pass through the nodes in set a in the middle. In addition, each new node in set A is considered to be taken from V0 through the new node to the path of Y, and compared to the original candidate path, select the smaller of the two, the process continues until Y is selected into set a. This process clearly guarantees that the candidate path to Y has been maintained "from V0, halfway through the node in set a", and because it iterates through all the nodes in a, it is the shortest of all of this characteristic path.

With the conclusion of one , it is proved that the Dijkstra algorithm can be weakened to prove the conclusion two .

Conclusion two : assuming that the next step in the construction process will be node Y, then the shortest path to Y must be from V0, halfway through the path of the node in set a to reach Y .

If "Conclusion two" is established, combined with "conclusion One", the shortest path and candidate path of y have the same attribute "starting from V0, only through the node in set a", and "Conclusion one" is clearly stated, The candidate path is the smallest in the path of this attribute, so the shortest path value is the candidate path for the next step to select the node y in set a, which proves the correctness of the process of constructing the shortest path tree in the algorithm.

Here we prove "conclusion two". It is very simple to use contradiction: assuming that the shortest path to Y is not only passing through the set a, it must go through a node W not in set A, so this path must contain a path from V0 to W, which is obviously shorter than the path through W to Y from V0. And Y is the next step to put into the set a node, according to our non-descending order to construct the shortest path tree process (the first shortest, the second short ...), the path from V0 to W should already be in the shortest path tree, that is, node w should already be in set a, contradiction, proof.

3. Use of the Dijkstra algorithm in the OSPF protocol

Theoretically, as long as the link between the nodes and the properties of the edges are described clearly, the graph is described clearly, and the shortest path tree can be computed using the Dijkstra algorithm.

For routing protocols using the Dijkstra algorithm, the shortest path tree can be computed independently for each router if it can describe the entire network topology (to which it should be), and let each router in the area know the network topology of the entire area clearly. In this sense, for a routing protocol that uses the Dijkstra algorithm, the following issues need to be addressed:

Description of network topology; The network topology describes the dissemination of information; efficiency: In the case of minimizing resources (bandwidth, memory, CPU), the optimal path of dynamic computing to other network segments is calculated in the shortest time.

3.1 OSPF Description of the network topology

The link state advertisement (LSA) is used in OSPF to describe the network topology (that is, the map).

In OSPF, the Router LSA is used to describe the connection and link (edge) properties between routers (nodes), with the possibility of theoretically calculating the shortest path. But only theory, from the point of view of practice, for special network situation and application scenario, need to do some optimization work.

Consider a broadcast network with n nodes first, if you use Routerlsa to describe, you need to describe N node 22 (n-1)/2 links connected, and N nodes need to establish a n (n-1)/2 adjacency, describing information needs to be propagated between these nodes that establish the adjacency relationship , it consumes a lot of resources. If we change a way of thinking, we put this broadcast network into a pseudo-node, the other N nodes are connected to the pseudo-node, so as long as the n link is described, n nodes only need to establish a total n adjacency relationship with the pseudo-node, which can greatly reduce the amount of information description and information transmission. According to this idea, the description of the broadcast network is optimized in OSPF, using the designated router Dr to assume the role of the pseudo-node, and using the network LSA to describe the connection of the DR and the individual routers in the broadcast network.

For the NBMA network description, the processing mode and broadcast network are basically the same.

Second, OSPF is designed to provide dynamic routing capabilities for a larger internal network. If the internal network is large, there are many links that need to be described, and storing, propagating, and computing These link information will consume a lot of bandwidth, memory, and CPU resources. OSPF solves this problem by dividing the larger network into multiple area, each area using the ROUTERLSA and network LSA to describe the interior network topology clearly, and using the Dijkstra algorithm to calculate the shortest path tree and route within the region. As for the network topology outside the area, the regional boundary router ABR does not pass the router LSA and Networklsa of the adjacent areas into the region, but summarizes the routes of each network segment computed within the adjacent area, and regards these segments as directly connected to themselves. The network Summary LSA is generated to describe these segments and pass in the area. In this way, for an adjacent AREA,ABR with n segments and multiple routers, it is only necessary to generate N network description information and pass in this area, which greatly reduces the link description information entering the area, and reduces the storage, propagation and computation consumption. It is important to note that the area-partitioning approach results in a link-state database in the area's internal router that is not the actual network topology, and thus may occur when the loop is computed, in order to avoid loops, Requires that all areas be connected to each other by the backbone area, or region 0.

In addition, OSPF is designed as an IGP that, in addition to the routing within this autonomous system, also needs to deal with routes outside the autonomous system, which are introduced at ASBR and can be seen as directly connected to ASBR, and OSPF introduces as External LSA to describe such autonomous system out-of-routing. In addition, since the as External LSA does not regenerate on the ABR when it passes into other area, the other area must also know from the point of view which node the autonomous system routes are introduced from, so the ASBR Summary LSA needs to be introduced to describe which node the external route was introduced from , ASBR Summarylsa is generated on the ABR, and from other area, it can be thought that the ASBR is directly connected to the ABR, and the network segment corresponding to the external route of autonomous system is directly connected to ASBR.

calculation of Shortest path in 3.2 OSPF

The following is a brief introduction to the shortest path calculation process for OSPF, please refer to RFC2328 for detailed details:

First, the area internal route is computed. Because the ROUTERLSA and network LSA accurately describes the topology of the networks within the entire area, the shortest path to each node (router) can be computed according to the Dijkstra algorithm. Then, according to Routerlsa description of the network segment connected to the router, the shortest path to each network segment is obtained. Note that the calculation process, if there are multiple shortest paths with the same cost, are retained.

Next, calculate the route across the area. Because from the inside of an area, the adjacent area of the route corresponding to the network segment is directly connected to the ABR, and the shortest path to the ABR has been calculated in the previous step, so directly check the Nerwork Summary LSA, you can easily get these network segment shortest path value. In addition, ASBR is also considered to be directly connected to the ABR, so the shortest path to ASBR can also be calculated in this step. Note: In this step, if the router that originated the calculation is an ABR, then it is natural that the network Summary LSA for the backbone area should be considered only, but for a topology with virtual link enabled, routing across areas is only logically through the backbone area, This is actually achieved through the transit area, because the logical link may not be physically optimal, so on the ABR connected to the transit zone, the path to the network Summary LSA in the backbone region may be suboptimal, You also need to examine the network Summary LSA of the transit area to get the shortest path.

Finally, the as external route is computed, because as external routes can be considered to be directly connected to ASBR, and the shortest path to ASBR is already computed in the previous step, so checking the as External LSA one by one can get the shortest path to each external network.

3.3 Next hop and out interface

There are two important elements in the routing table: the next hop and the Out interface. Because it is certain that the two elements are related to the computational process, here is a mention.

In fact, a simple fact is that for any path, its next hop and out interface must be the same as the next hop and out interface of his parent path. Therefore, the next hop and out interface for all paths is determined, and finally the next hop and out interfaces are determined to "nodes that are directly connected to the V0". For OSPF, the "node connected directly to V0" is either a router or a pseudo node (a Dr simulation pseudo-node is used in a broadcast network, NBMA network).

The previous Dijkstra algorithm has mentioned that the process of determining the shortest path to each node or network segment is a candidate path construction and adjustment process, in which the parent path of the candidate path is adjusted many times, but its root cannot jump out and the "V0 directly connected node" is the range, Therefore, the next hop and out interface is also in the range of the next hop and out interface of the node directly connected to the V0, inheriting in the process of computation.

Determine "and V0 directly connected to the node" the next hop and out of the interface is relatively simple, here no longer repeat, want to understand the words can refer to the RFC2328 16.1.1.

There is a special kind of network segment that is the direct connection segment of the node that initiates the OSPF computation, and the next hop and out interface is more straightforward and will not be inherited by other paths.

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.