Home > Articles > Programming > C/C++

  • Print
  • + Share This
Like this article? We recommend

Dijkstra and Link-State Routing

By the early 1980s, concerns began to arise about the scalability of distance-vector routing. Two particular aspects caused problems:

  • In environments in which the topology of the network changes frequently, distance-vector routing converges too slowly to maintain accurate distance information.

  • Update messages contain distances to all nodes, so the message size grows with the size of the entire network.

As a result of these problems, link-state routing was developed. With link-state routing, each router stores a graph representing the topology of the entire network and computes its routing table based on the graph using Dijkstra's single-source shortest-paths algorithm. To keep the graph up-to-date, routers share information about which links are up and which are down (the link state). When connectivity changes are detected, the information is flooded throughout the network in what is called a link-state advertisement.

Because only local information (neighbor connectivity) has to be shared, link-state routing does not have the message size problems of distance vector routing. Also, because each router computes its own shortest paths, it takes much less time to react to changes in the network and recalculate accurate routing tables. One disadvantage of link-state routing is that it places more of a burden on each router in terms of computation and memory use. Even so, it has proved to be an effective protocol and is now formalized in the Open Shortest Path First (OSPF) protocol (see RFC 1583), which is currently one of the preferred interior gateway routing protocols.

So how does the OSPF protocol work? As mentioned previously, it uses Dijkstra's algorithm. This algorithm finds all the shortest paths from the source vertex to every other vertex by iteratively growing the set of vertices (S) to which it already knows the shortest path. At each step of the algorithm, the vertex in the set V – S with the smallest distance label is added to S (V is the set of all vertices in the graph). Then the out edges of the vertex (call it vertex u) are "relaxed"—that is, for each edge (u,v), the distance of vertex v is updated according to this statement: d[v] = min(w(u,v) + d[u], d[v]) (where w(u,v) is the weight of the edge (u,v) and d[u] gives the shortest known distance to vertex u). The algorithm then loops back, processing the next vertex in V – S with the lowest distance label. The algorithm finishes when S contains all vertices reachable from the source vertex.

The rest of this article shows how to use the BGL dijkstra_shortest_paths function to solve the single-source shortest-path problem for a network of routers and illustrates how this information is used to compute a routing table. Figure 2 shows the example network described in RFC 1583. In the figure, RT stands for router, N stands for network (which is a group of addresses treated as a single destination entity), and H stands for host.

Figure 2 A directed graph representing a group of Internet routers using the same routing protocol. The edges are weighted with the cost of transmission.

To demonstrate Dijkstra's algorithm, you will compute the shortest-path tree for Router 6. The main steps of the program are as follows:

#include <fstream> // for file I/O
#include <boost/graph/graphviz.hpp> // for read/write_graphviz()
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/lexical_cast.hpp>
int main()
{
  using namespace boost;
  · Read directed graph in from Graphviz dot fileÒ
  · Copy the graph, converting string labels to integer weightsÒ
  · Find router sixÒ
  · Setup parent property map to record the shortest-paths treeÒ
  · Run the Dijkstra algorithmÒ
  · Set the color of the edges in the shortest-paths tree to blackÒ
  · Write the new graph to a Graphviz dot fileÒ
  · Write the routing table for router sixÒ
  return EXIT_SUCCESS;
}

The first step is to create the graph. I have stored the graph from Figure 2 in a Graphviz dot file. The Graphviz package provides tools that automatically lay out and draw graphs; it is available at http://www.graphviz.org. The Graphviz tools use a special file format for graphs, called dot files. BGL includes a parser for reading this file format into a BGL graph. The parser can be accessed through the read_graphviz function defined in boost/graph/graphviz.hpp. Because the graph is directed, you use the GraphvizDigraph type. For an undirected graph, you would use the GraphvizGraph type.

·Read directed graph in from Graphviz dot file Ò∫ 
 GraphvizDigraph g_dot;
 read_graphviz("figs/ospf-graph.dot", g_dot);

The GraphvizDigraph type stores the properties of vertices and edges as strings. Although strings might be convenient for file I/O and display purposes, edge weights must be represented as integers so that they can be easily manipulated inside Dijkstra's algorithm. Therefore, g_dot is copied to a new graph. Each edge in the GraphvizDigraph type has a set of attributes stored in a std::map<std::string, std::string>. The edge weights from Figure 2 are stored in the label attribute of each edge. The label is converted to an int using boost::lexical_cast; then the edge is inserted into the new graph. Because the Graph type and GraphvizDigraph are both based on adjacency_list with VertexList=vecS, the vertex descriptor types for both graphs are integers. The result of source(*ei, g_dot) can thus be used directly in the call to add_edge on graph g.

· Copy the graph, converting string labels to integer weights Ò∫ 
 typedef adjacency_list<vecS, vecS, directedS, no_property,
   property<edge_weight_t, int> > Graph;
 typedef graph_traits<Graph>::vertex_descriptor vertex_descriptor;
 Graph g(num_vertices(g_dot));
 property_map<GraphvizDigraph, edge_attribute_t>::type
   edge_attr_map = get(edge_attribute, g_dot);
 graph_traits<GraphvizDigraph>::edge_iterator ei, ei_end;
 for (tie(ei, ei_end) = edges(g_dot); ei != ei_end; ++ei) {
   int weight = lexical_cast<int>( edge_attr_map[*ei]["label"] );
   property<edge_weight_t, int> edge_property(weight);
   add_edge(source(*ei, g_dot), target(*ei, g_dot), edge_property, g);
 }

To use Router 6 as the source of the shortest-path search, the vertex descriptor for the router must be located. The program searches for the vertex with an attribute label of RT6.

· Find router six Ò∫ 
 vertex_descriptor router_six;
 property_map<GraphvizDigraph, vertex_attribute_t>::type
   vertex_attr_map = get(vertex_attribute, g_dot);
 graph_traits<GraphvizDigraph>::vertex_iterator vi, vi_end;
 for (tie(vi, vi_end) = vertices(g_dot); vi != vi_end; ++vi)
   if ("RT6" == vertex_attr_map[*vi]["label"]) {
     router_six = *vi; break;
   }

Together, the shortest paths from Router 6 to all other routers form a shortest-path tree. An efficient way to represent such a tree is to record the parent of each node in the tree. Here you simply use a std::vector to record the parents.

· Setup parent property map to record the shortest-paths tree Ò∫ 
 std::vector<vertex_descriptor> parent(num_vertices(g));
 // All vertices start out as their own parent
 typedef graph_traits<Graph>::vertices_size_type size_type;
 for (size_type p = 0; p < num_vertices(g); ++p)
  parent[p] = p;

You are now ready to invoke Dijkstra's algorithm. Pass in the parent array as the argument to the predecessor_map named parameter.

· Run the Dijkstra algorithm Ò∫ 
 dijkstra_shortest_paths(g, router_six, predecessor_map(&parent[0]));

Next, set the color attribute of the tree edges to black, in preparation for printing the graph to a Graphviz dot file. The tree edges have been recorded in the parent array. For every vertex i in the graph, edge (parent[i],i) is a tree edge, unless parent[i] = i, in which case i is the root vertex or an unreachable vertex.

· Set the color of the edges in the shortest-paths tree to black Ò∫ 
 graph_traits<GraphvizDigraph>::edge_descriptor e;
 for (size_type i = 0; i < num_vertices(g); ++i)
   if (parent[i] != i) {
     e = edge(parent[i], i, g_dot).first;
     edge_attr_map[e]["color"] = "black";
   }

Now write the graph to a dot file. The default color for the edges is set to gray (for nontree edges). Figure 3 shows the computed shortest-path tree for Router 6.

· Write the new graph to a Graphviz dot file Ò∫ 
 graph_property<GraphvizDigraph, graph_edge_attribute_t>::type&
   graph_edge_attr_map = get_property(g_dot, graph_edge_attribute);
 graph_edge_attr_map["color"]="gray";
 write_graphviz("figs/ospf-sptree.dot", g_dot);

Figure 3 The shortest-path tree for Router 6.

The last step is to compute the routing table for Router 6. The routing table has three columns: the destination, the next hop that should be taken to get to the destination, and the total cost to reach the destination. To populate the routing table, entries are created for each destination in the network. Fill in the information for each entry by following the shortest path backward from the destination to Router 6 using the parent map. A vertex that is its own parent is skipped because either the vertex is Router 6 (the source vertex) or the vertex is not reachable from Router 6.

· Write the routing table for router six Ò∫ 
 std::ofstream rtable("routing-table.dat");
 rtable << "Dest  Next Hop  Total Cost" << std::endl;
 for (tie(vi, vi_end) = vertices(g_dot); vi != vi_end; ++vi)
   if (parent[*vi] != *vi) {
     rtable << vertex_attr_map[*vi]["label"] << "  ";
     · Follow path backward to router six using parentsÒ
   }

While following the path from each destination to Router 6, the edge weights are accumulated into the total path_cost. You also record the child of the current vertex because, at loop termination, this is the vertex to use as the next hop.

·Follow path backward to router six using parents Ò∫ 
 vertex_descriptor v = *vi, child;
 int path_cost = 0;
 property_map<Graph, edge_weight_t>::type
 weight_map = get(edge_weight, g);
 do {
   path_cost += get(weight_map, edge(parent[v], v, g).first);
   child = v;
   v = parent[v];
 } while (v != parent[v]);
 rtable << vertex_attr_map[child]["label"] << "   ";
 rtable << path_cost << std::endl;

Table 1 shows the resulting routing table.

Table 1 Routing Table for Router 6, Computed from the Shortest Paths in Figure 3

Dest

Next Hop

Total Cost

RT1

RT3

7

RT2

RT3

7

RT3

RT3

6

RT4

RT3

7

RT5

RT5

6

RT7

RT10

8

RT8

RT10

8

RT9

RT10

11

RT10

RT10

7

RT11

RT10

10

RT12

RT10

11

N1

RT3

10

N2

RT3

10

N3

RT3

7

N4

RT3

8

N6

RT10

8

N7

RT10

12

N8

RT10

10

N9

RT10

11

N10

RT10

13

N12

RT10

10

N13

RT5

14

N14

RT5

14

N15

RT10

17

H1

RT10

21


  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020