top

Search

Software Key Tutorial

.

UpGrad

Software Key Tutorial

Link State Routing Algorithm

Introduction

In the dynamic changing world of computer networks, influential and trustworthy communication is essential. The routing process is crucial because it selects the best path for data packets traveling across a network from source to destination. The link state routing algorithm is a complex and robust solution for routing algorithms crucial in making these judgments. This tutorial will provide in-depth insights and explain the link state routing algorithm.

Overview

In computer networks, link state routing is a technique where each router exchanges information about its neighboring routers with every other router in the network.  With the help of this technology, routers can completely understand the network architecture, resulting in dependable and effective data transport.

Link state routing algorithms are crucial for maintaining effective and dependable communication inside computer networks because they provide routers with a thorough awareness of the network architecture and enable the optimum path selection for data delivery.

What is a Link State Routing Algorithm?


Link state routing algorithm is a strategy in which each router communicates information about its neighborhood with every other router in the internetwork. It is an inner protocol every router uses to communicate information or knowledge about the rest of the routers on the network. The link state routing algorithm is also known as the Shortest Path First Algorithm.

Here is the purpose of the link state routing algorithm with an example:

Suppose we have three routers in a network: Router-1, Router-2, and Router-3.

  • Router-1 wishes to deliver a data packet to Router-2.

  • Router-1 has two ways to reach Router-2: one through Router-3 and the other directly. Router-1 will pick the path with the least distance.

So, the data packet will be transferred from the second way, i.e., Router-1 --> Router-3 --> Router-2.

Link State Routing Protocols

In packet-switching networks for computer communications, link state routing protocols are a subset of routing protocols. The shortest-path-first protocols are another name for them. Instead of distance-vector routing protocols, link-state routing techniques employ a graph-based map of the network's links to find the optimal route to a destination.

Each network node builds a unique map of the network's connection that shows which nodes are connected to which others. Each node determines the optimum next hop from it to every potential destination in the network based on this map.

Some key features of link-state routing protocols are:

  • Link State Packet: Short packet that carries routing information.

  • Link-State Database: Collection of information acquired from the link-state packet.

  • Dijkstra Algorithm or Shortest Path First Algorithm): Computations on the database results in the shortest path.

  • Routing Table: Collections of known pathways and interfaces.

Phases of Link State Routing

The phases of Link State Routing are as follows:

  • Neighbor Discovery: Each router in the network detects its surrounding routers and generates a neighbor table. This table offers information on the immediately linked neighbors.

  • Link Cost Measurement: Each router measures the cost or metric associated with each neighbor. The cost might depend on characteristics like latency, bandwidth, or other criteria.

  • Building and Distributing Link State Packets: Each router creates and delivers link state packets to all other routers in the network. These packets carry information on the router's state, including its neighbors and the link charges. The link state packets are flooded throughout the network to ensure every router gets a comprehensive image of the network architecture.

  • Route Calculation: Once all routers have received the link state packets and have a comprehensive view of the network topology, each router separately calculates the best next hop for every conceivable destination in the network. This is often done using a shortest-path algorithm like Dijkstra's algorithm. The collection of best next hops constitutes the routing table for each router.

Link State Routing Algorithm Steps

Here are the steps of the link state routing algorithm:

  1. Initialization

Assign a cost of 0 to the source node and infinity to all other nodes. Then, create a list of visited nodes initially containing only the source node.

  1. Determine Neighbors' Costs

For each neighboring node of the source node, determine the cost of the link between the source node and its neighbors.

  1. Update Costs

For each neighbor, calculate a tentative cost from the source node to that neighbor via the current node. If the calculated tentative cost is lower than the current assigned cost, update the cost.

  1. Select Next Node

From the set of nodes not yet visited, select the node with the smallest tentative cost as the next node to visit.

  1. Mark Node as Visited

Mark the selected node as visited and add it to the list of visited nodes.

  1. Update Neighbors

For each neighboring node of the selected node that has not been visited, calculate the total cost from the source node via the selected node. If this total cost is less than the current assigned cost, update the cost.

  1. Repeat Steps 4-6

Repeat the process of selecting the next node with the smallest tentative cost, marking it as visited, and updating its neighbors' costs until all nodes have been visited.

  1. Path Selection

Once all nodes have been visited, the shortest path from the source node to each other node is determined based on the assigned costs and the path taken.

Features of Link State Routing Algorithm

Some of the features of link state routing algorithms are:

  • Finding neighbors: Each router creates a neighbor table after finding its neighbors.

  • Measuring link cost: Each router calculates the cost (delay, bandwidth, etc.) to every one of its neighbors. 

  • Information Exchange: Each router in the network shares the routing table it has produced with the other routers. This makes data transmission quicker and more dependable.

  • Link State Routing method: The link state routing method only transfers information when the connection changes. This reduces pointless updates and saves network resources.

  • Large Memory Requirement: The link state routing technique needs a lot of memory since it keeps a routing database in memory. This database contains network topology data and is used for route computations.

  • CPU overhead: In the link state routing technique, computing the shortest route may result in CPU overhead. This is because finding the best paths requires complicated computations.

  • Flooding: The link state routing method distributes routing information by flooding. Heavy traffic and associated problems like indefinite looping may result from this.

Calculation of Shortest Path

Let's walk through an example of the link state routing algorithm to calculate the shortest path in a network. In this example, we'll use a small network with nodes and their link costs. We'll find the shortest path from a source node to all other nodes in the network.

Let us consider the following network topology:


In the above example, 

  • Node A is directly connected to nodes B and C with link costs of 3 and 1, respectively.

  • Node B is directly connected to nodes A and C with link costs of 3 and 2, respectively.

  • Node C is directly connected to nodes A and B with link costs of 1 and 2, respectively.

Here is the step-by-step calculation:

Initialization:

  • Source node: A

  • Assign cost 0 to A, and infinity to B and C.

  • Visited nodes: {A}

Determine Neighbors' Costs:

  • Neighbors of A: B (cost 3), C (cost 1)

  • Neighbors of B: A (cost 3), C (cost 2)

  • Neighbors of C: A (cost 1), B (cost 2)

Update Costs:

  • Node A's neighbors:

    • B: Current cost (3), tentative cost (0 + 3 = 3)

    • C: Current cost (1), tentative cost (0 + 1 = 1)

  • Update tentative costs for B and C.

Select Next Node:

  • The next node to visit is C (lowest tentative cost).

Mark Node as Visited:

  • Mark C as visited.

  • Visited nodes: {A, C}

Update Neighbors:

  • Node C's neighbors:

    • A: Current cost (1), tentative cost (1 + 1 = 2)

    • B: Current cost (2), tentative cost (1 + 2 = 3)

    • Update tentative costs for A and B.

Repeat Selecting and Marking Nodes, Then Updating Their Neighbors:

The next node to visit is B (lowest tentative cost).

Mark Node as Visited:

  • Mark B as visited.

  • Visited nodes: {A, C, B}

Update Neighbors:

  • No further updates needed as all nodes have been visited.

Path Selection:

  • The shortest paths from node A to each node:

    • A to B: A -> C -> B (Total cost: 3)

    • A to C: A -> C (Total cost: 1)

In this example, the link state routing algorithm calculates the shortest paths from node A to nodes B and C in the network. The algorithm considers link costs and iteratively updates tentative costs to determine the optimal paths.

Link State Routing Algorithm Example

Implementing the complete link state routing algorithm in Java requires a bit more code, as it involves maintaining a network graph, calculating shortest paths, and updating routing tables. Here is a simplified example that demonstrates the core logic of the algorithm.

import java.util.*;

class Node {
    String name;
    int cost;

    public Node(String name, int cost) {
        this.name = name;
        this.cost = cost;
    }
}

public class LinkStateRoutingExample {
    public static void main(String[] args) {
        Map<String, List<Node>> network = new HashMap<>();
        network.put("A", Arrays.asList(new Node("B", 3), new Node("C", 1)));
        network.put("B", Arrays.asList(new Node("A", 3), new Node("C", 2)));
        network.put("C", Arrays.asList(new Node("A", 1), new Node("B", 2)));

        String sourceNode = "A";
        Map<String, Integer> distance = new HashMap<>();
        Map<String, String> previous = new HashMap<>();
        Set<String> visited = new HashSet<>();

        // Initialization
        for (String node : network.keySet()) {
            distance.put(node, Integer.MAX_VALUE);
            previous.put(node, null);
        }
        distance.put(sourceNode, 0);

        while (!visited.containsAll(network.keySet())) {
            String current = getMinDistanceNode(distance, visited);
            visited.add(current);

            for (Node neighbor : network.get(current)) {
                int alt = distance.get(current) + neighbor.cost;
                if (alt < distance.get(neighbor.name)) {
                    distance.put(neighbor.name, alt);
                    previous.put(neighbor.name, current);
                }
            }
        }

        // Display shortest paths
        for (String node : network.keySet()) {
            if (!node.equals(sourceNode)) {
                System.out.println("Shortest path from " + sourceNode + " to " + node + ": " + getPath(node, previous));
            }
        }
    }

    private static String getMinDistanceNode(Map<String, Integer> distance, Set<String> visited) {
        int minDistance = Integer.MAX_VALUE;
        String minNode = null;
        for (Map.Entry<String, Integer> entry : distance.entrySet()) {
            if (!visited.contains(entry.getKey()) && entry.getValue() < minDistance) {
                minDistance = entry.getValue();
                minNode = entry.getKey();
            }
        }
        return minNode;
    }

    private static String getPath(String target, Map<String, String> previous) {
        Stack<String> path = new Stack<>();
        while (target != null) {
            path.push(target);
            target = previous.get(target);
        }
        StringBuilder sb = new StringBuilder();
        while (!path.isEmpty()) {
            sb.append(path.pop());
            if (!path.isEmpty()) {
                sb.append(" -> ");
            }
        }
        return sb.toString();
    }
}

In the above example, we define a Node class to represent each node in the network. The LinkStateRoutingExample class demonstrates the steps of the link state routing algorithm, including initialization, shortest path calculation, and displaying results. Keep in mind that this example focuses on the algorithm's logic and simplifies certain aspects like data structures and input handling.

Conclusion

Network administrators and engineers need to understand the principles of the link state routing algorithm and its importance in the development of link state routing algorithms like OSPF and IS-IS. By mastering this technique, they may construct robust, scalable networks that offer dependable connectivity and straightforward data transfer over intricate infrastructures.

FAQs

  1. What are the advantages of the Link State Routing protocol?

The main advantages of the Link State Routing protocol are scalability, efficient use of network resources, accurate network topology information, and fast network convergence.

  1. What is the Shortest Path Tree of link state routing in computer networks?

In Link State Routing, the Shortest Path Tree is a tree representation of the network's topology that shows the shortest paths from a source router to all other routers in the network.

  1. Can you provide an example of the link state routing algorithm steps?

Consider a network with five routers (A, B, C, D, E) with the following link costs:

A to B: 2

A to C: 4

B to C: 1

B to D: 7

C to D: 3

C to E: 5

D to E: 2

The routers employ the Link State Routing Algorithm to generate their routing tables. Each router determines the shortest way to all other routers in the network, resulting in optimum routes from each router to every destination.

Leave a Reply

Your email address will not be published. Required fields are marked *