VOOZH about

URL: https://www.geeksforgeeks.org/python/shortest-path-problem-between-routing-terminals-implementation-in-python/

⇱ Shortest Path Problem Between Routing Terminals - Implementation in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Shortest Path Problem Between Routing Terminals - Implementation in Python

Last Updated : 12 Jul, 2025
The famous Dijkstra's algorithm can be used in a variety of contexts - including as a means to find the shortest route between two routers, also known as Link state routing. This article explains a simulation of Dijkstra's algorithm in which the nodes (routers) are terminals. Once the shortest path between two nodes (terminals) is calculated, the shortest path itself is sent as a message to each terminal on its path in sequence, until the destination terminal is reached. Whenever the message has traversed a node, its terminal displays the traversal. In this way, it is possible to both see and simulate the passage of a message across a shortest calculated route. The procedure to run the following code is as follows:
  • Execute the driver code
  • Before providing any input to the driver code, run the router codes router1.py, router2.py, etc. in separate terminals/tabs.
  • Now provide input to the driver code in the form of a matrix G, in which any entry G[i, j] is the distance from node i to node j. The matrix must be symmetrical. If i=j, then D[i, j]=0 as the distance between a node and itself is considered to be nothing. If there is no direct connection between two nodes, then D[i, j]=999 (the equivalent of infinity).
  • Specify source and destination nodes, where the nodes vary from 0 to 3, and represent terminals 1 to 4 respectively.
This implementation specifies four nodes but this can easily be extended to N nodes with N terminals and port numbers representing processes running on them respectively. Consider the following example of a four-node network, with distances between them as specified and nodes numbered 0 to 3 from left to right:
👁 Image
Distances between terminals
For this network, the matrix G with entries G[i, j] as specified above would be:
[[0, 1, 999, 999],
 [1, 0, 2, 999],
 [999, 2, 0, 3],
 [999, 999, 3, 0]]
This matrix would have to be input to the driver code. Dijkstra's algorithm is used to find the shortest path between source and destination. A list containing the remaining path is sent to each node en route to the final destination. The implementation in Python is specified below.
👁 Image
Dijkstra Output
Terminal Output -
👁 Image
Terminal Output
Comment