- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Shortest Path algorithm in Computer Network
In computer networks, the shortest path algorithms aim to find the optimal paths between the network nodes so that routing cost is minimized. They are direct applications of the shortest path algorithms proposed in graph theory.
Explanation
Consider that a network comprises of N vertices (nodes or network devices) that are connected by M edges (transmission lines). Each edge is associated with a weight, representing the physical distance or the transmission delay of the transmission line. The target of shortest path algorithms is to find a route between any pair of vertices along the edges, so the sum of weights of edges is minimum. If the edges are of equal weights, the shortest path algorithm aims to find a route having minimum number of hops.
Common Shortest Path Algorithms
Some common shortest path algorithms are −
Bellman Ford’s Algorithm
Dijkstra’s Algorithm
Floyd Warshall’s Algorithm
The following sections describes each of these algorithms.
Bellman Ford Algorithm
Input − A graph representing the network; and a source node, s
Output − Shortest path from s to all other nodes.
Initialize distances from s to all nodes as infinite (∞); distance to itself as 0; an array dist[] of size |V| (number of nodes) with all values as ∞ except dist[s].
Calculate the shortest distances iteratively. Repeat |V|- 1 times for each node except s −
Repeat for each edge connecting vertices u and v −
If dist[v] > (dist[u] + weight of edge u-v), Then
Update dist[v] = dist[u] + weight of edge u-v
The array dist[] contains the shortest path from s to every other node.
Dijkstra’s Algorithm
Input − A graph representing the network; and a source node, s
Output − A shortest path tree, spt[], with s as the root node.
Initializations −
An array of distances dist[] of size |V| (number of nodes), where dist[s] = 0 and dist[u] = ∞ (infinite), where u represents a node in the graph except s.
An array, Q, containing all nodes in the graph. When the algorithm runs into completion, Q will become empty.
An empty set, S, to which the visited nodes will be added. When the algorithm runs into completion, S will contain all the nodes in the graph.
Repeat while Q is not empty −
Remove from Q, the node, u having the smallest dist[u] and which is not in S. In the first run, dist[s] is removed.
Add u to S, marking u as visited.
For each node v which is adjacent to u, update dist[v] as −
If (dist[u] + weight of edge u-v) < dist[v], Then
Update dist[v] = dist[u] + weight of edge u-v
The array dist[] contains the shortest path from s to every other node.
Floyd Warshall Algorithm
Input − A cost adjacency matrix, adj[][], representing the paths between the nodes in the network.
Output − A shortest path cost matrix, cost[][], showing the shortest paths in terms of cost between each pair of nodes in the graph.
Populate cost[][] as follows:
If adj[][] is empty Then cost[][] = ∞ (infinite)
Else cost[][] = adj[][]
N = |V|, where V represents the set of nodes in the network.
Repeat for k = 1 to N −
Repeat for i = 1 to N −
Repeat for j = 1 to N −
If cost[i][k] + cost[k][j] < cost[i][j], Then
Update cost[i][j] := cost[i][k] + cost[k][j]
The matrix cost[][] contains the shortest cost from each node, i , to every other node, j.
- Related Articles
- What is the Shortest Path Routing in Computer Network?
- Dijkstra’s Shortest Path Algorithm
- C++ Program for Dijkstra’s shortest path algorithm?
- Yen's k-Shortest Path Algorithm in Data Structure
- What is a Routing Algorithm in Computer Network?
- C / C++ Program for Dijkstra's shortest path algorithm
- Dijkstra’s algorithm to compute the shortest path through a graph
- What is an Adaptive Routing Algorithm in Computer Network?
- Shortest path algorithms in Javascript
- Shortest Path in Binary Matrix in C++
- Bellman–Ford Algorithm for Shortest Paths
- Shortest Path in a Directed Acyclic Graph
- Shortest Path with Alternating Colors in Python
- Shortest Path Visiting All Nodes in C++
- Repeaters in Computer Network.?
