Skip to main content

Why Recomputing Shortest Paths Is Inefficient: A Step Towards Smarter Graph Algorithms

A Step Towards Smarter Graph Algorithms

Updated 5 min readMarkdown
Part of seriesRethinking Graph Algorithms
Why Recomputing Shortest Paths Is Inefficient: A Step Towards Smarter Graph Algorithms

Imagine that you are using Google Maps while you are driving somewhere, and suddenly there's a road blockage. In this case Google Maps quickly reroutes and gives a new route where there is no blockage or a better route. So we just assume that, Google Maps has adjusted and taken a different way.

However, older navigation systems that do not work the same as Google Maps, they will actually recalculate the entire route from scratch to reach our destination. The recalculation of this route results in wasting of computer resources and mathematically does not make sense. This problem is not only in GPS systems, it is one of the basic issues that in computer science we face when finding the shortest routes between two points on a graph. So it is an interesting area to explore, what areas of calculation can be eliminated altogether and what is the point of unnecessary recalculation in computer systems.

What Is a Shortest Path?

Graph is a pictorial representation of how the points are connected. These destination points are called nodes or cities or intersection and the path between them is termed an edge. Imagine edge as a road, with the edges representing the time required or the distance separating the cities.

When I first thought of the shortest path problem, I thought it is very straightforward, and there are many algorithms that can be used to solve such kind of problem. The most efficient algorithm would be Dijkstra's algorithm.

In Dijkstra's algorithm, we are given any weighted undirected graph, It calculates the shortest distance from each node to the source and updates this value if it finds a shorter path than the one previously saved. Its process mainly involves progressing to the closest unvisited node, and the distances are adjusted along the way, and this is done until all the nodes are visited. It has a flawless function, and it is used in computer science education on a global level.

The Problem

If you've calculated the most efficient route through a city of one hundred thousand intersections, and a repair was made to an existing road that provides faster access, then the traditional method would require going through every single one of those intersections again, recalculating the distance for any existing road that would need to be considered in connection with the one that was recently repaired.
This method therefore requires all previous calculations to be discarded and the data to be replaced with new data, resulting in significant inefficiency due to having to redo calculations based on a very small (one) change, causing all intersection nodes and roads to be recalculated.

A Simple Example

Take a small graph with four nodes

S (start), A, B, and D.

S --4-- A
S --2-- B
A --3-- D
B --9-- D

Running Dijkstra from S gives:

Node Shortest Distance Route
S 0 —
A 4 S → A
B 2 S → B
D 7 S → A → D

Now the road B → D gets an upgrade. Its weight drops from 9 to 2. What changes?

Node Before After
S 0 0 no change
A 4 4 no change
B 2 2 no change
D 7 4 updated

Only D changed. But Dijkstra re-evaluates all four nodes anyway. In a graph with 100,000 nodes, this means visiting 99,999 nodes for no reason.

The Smarter Question

Now after performing the first run, we have some data regarding how the roads are utilised, the paths between the nodes, and the weight required to reach each city or each point or each node. So when one road gets blocked, we do not need to check all the roads again. We only need to fix the route that used that blocked road. So that means that most of our old calculation is still correct. Only a small part is affected. So we fix that only. This is called output-sensitive algorithm design. Its main working is that it depends on how much has changed and not on how big the whole map is.

"We do not need to redo everything, We just need to fix what has changed."

Why It Matters

Every real world network like roads being repaired or internet connections becoming slow have to be able to adjust with changes. Isolated or new networks will have their shortest paths, if they have them, will have to keep them accurate and updated as quickly as possible.

For example, by getting hang of a smart algorithm that just updates only what has changed, we can increase the efficiency of our network systems with no new infrastructure required, only changes in the way we approach the problem.

This has made me curious

Can algorithms be designed in a such a way that it adjusts to the changes instead of restarting every time?

No comments yet. Be the first to comment.

Rethinking Graph Algorithms

Part 1 of 1

This series explores the limitations of classical graph algorithms and investigates more efficient approaches for dynamic and real-world systems. Starting from fundamental concepts like shortest path algorithms, it gradually builds towards smarter, output-sensitive techniques that reduce unnecessary computation.