Shortest Path Algorithm in Computer Network
Last Updated :
14 Feb, 2024
In between sending and receiving data packets from the sender to the receiver, it will go through many routers and subnets. So as a part of increasing the efficiency in routing the data packets and decreasing the traffic, we must find the shortest path. In this article, we are discussing the shortest path algorithms.
What is Shortest Path Routing?
It refers to the algorithms that help to find the shortest path between a sender and receiver for routing the data packets through the network in terms of shortest distance, minimum cost, and minimum time.
- It is mainly for building a graph or subnet containing routers as nodes and edges as communication lines connecting the nodes.
- Hop count is one of the parameters that is used to measure the distance.
- Hop count: It is the number that indicates how many routers are covered. If the hop count is 6, there are 6 routers/nodes and the edges connecting them.
- Another metric is a geographic distance like kilometers.
- We can find the label on the arc as the function of bandwidth, average traffic, distance, communication cost, measured delay, mean queue length, etc.
Common Shortest Path Algorithms
- Dijkstra’s Algorithm
- Bellman Ford’s Algorithm
- Floyd Warshall’s Algorithm
Dijkstra’s Algorithm
The Dijkstra’s Algorithm is a greedy algorithm that is used to find the minimum distance between a node and all other nodes in a given graph. Here we can consider node as a router and graph as a network. It uses weight of edge .ie, distance between the nodes to find a minimum distance route.
Algorithm:
1: Mark the source node current distance as 0 and all others as infinity.
2: Set the node with the smallest current distance among the non-visited nodes as the current node.
3: For each neighbor, N, of the current node:
- Calculate the potential new distance by adding the current distance of the current node with the weight of the edge connecting the current node to N.
- If the potential new distance is smaller than the current distance of node N, update N's current distance with the new distance.
4: Make the current node as visited node.
5: If we find any unvisited node, go to step 2 to find the next node which has the smallest current distance and continue this process.
Example:
Consider the graph G:
Graph GNow,we will start normalising graph one by one starting from node 0.
step 1Nearest neighbour of 0 are 2 and 1 so we will normalize them first .
step 3
Similarly we will normalize other node considering it should not form a cycle and will keep track in visited nodes.
step 5Bellman Ford’s Algorithm
The Bell man Ford’s algorithm is a single source graph search algorithm which help us to find the shortest path between a source vertex and any other vertex in a give graph. We can use it in both weighted and unweighted graphs. This algorithm is slower than Dijkstra's algorithm and it can also use negative edge weight.
Algorithm
1: First we Initialize all vertices v in a distance array dist[] as INFINITY.
2: Then we pick a random vertex as vertex 0 and assign dist[0] =0.
3: Then iteratively update the minimum distance to each node (dist[v]) by comparing it with the sum of the distance from the source node (dist[u]) and the edge weight (weight) N-1 times.
4: To identify the presence of negative edge cycles, with the help of following cases do one more round of edge relaxation.
- We can say that a negative cycle exists if for any edge uv the sum of distance from the source node (dist[u]) and the edge weight (weight) is less than the current distance to the largest node(dist[v])
- It indicates the absence of negative edge cycle if none of the edges satisfies case1.
Example: Bellman ford detecting negative edge cycle in a graph.
Consider the Graph G:
Graph GStep 1:

Step 2:

Step 3:

Step 4:

Step 5:

Outcome: The graph contains a negative cycle in the path from node D to node F and then to node E.
Floyd Warshall’s Algorithm
The Floyd Warshall’s Algorithm is used to find the shortest path between any two nodes in a given graph. It keeps a matrix of distances between each pair of vertices.it will continue iterating the matrix until it reaches at a shortest path.
Algorithm:
1: Using the data about the graph, make a matrix.
2: By taking all vertices as an intermediate vertex, we have to update the final matrix.
3: It is to be noted that it includes at a time we pick one vertex, and we update the shortest path which includes this chosen vertex as an in-between point along the path.
4: When we select a vertex say k almost like the middle of the path, in previous calculations we have already taken all vertices P{0,1,2..,k-1} as potential middle points.
5: We have to consider the following subpoints while dealing with the source and destination vertices I,j respectively
- If vertex k is not the part of shortest path from I to j, we don’t have to change dist[i][j] value .ie, it will remain unchanged.
- If vertex k is indeed part of shortest path from I to j, update dist[i][j] to the sum of dist[i][k] and dist[k][j] but note that only if dist[i][j] is greater than this value we newly calculated.
Example: Consider the given graph G,
Graph GStep 1:

Step 2:

Step 3:

Step 4:

Step 5:

Step 6:

Step 7:

Similar Reads
What is OSI Model? - Layers of OSI Model
The OSI (Open Systems Interconnection) Model is a set of rules that explains how different computer systems communicate over a network. OSI Model was developed by the International Organization for Standardization (ISO). The OSI Model consists of 7 layers and each layer has specific functions and re
13 min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
TCP/IP Model
The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie
7 min read
Types of Network Topology
Network topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. Common types of network topology include bus, star, ring, mesh, and tree topologies, each with its advantages and disadvantages. In this article, we will discuss different types of n
12 min read
Computer Network Tutorial
A Computer Network is a system where two or more devices are linked together to share data, resources and information. These networks can range from simple setups, like connecting two devices in your home, to massive global systems, like the Internet. Below are the main components of a computer netw
7 min read
Basics of Computer Networking
A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read