The main function of the network layer is routing packets from the source machine to the destination machine. And the routing algorithm is that part of the network layer software responsible for deciding which output line an incoming packet should be transmitted on.
The idea is to build a graph of the network, with each node of the graph representing a router and each edge of the graph representing a communication line, or link. To choose a route between a given pair of routers, the algorithm just finds the shortest path between them on the graph. For this, Dijkstra's algorithm based on the idea of greedy algorithms is given below.