Shortest distance between points algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Finding the shortest distance between points is a fundamental problem in computer science, mathematics, and many applied fields such as robotics, transportation, and geographical information systems. Various algorithms have been developed to solve this problem efficiently, each with its unique applications and limitations. This article will delve into the technical aspects of these algorithms, exploring their methodologies, complexities, and use cases.
Problem Statement
The objective is to determine the shortest path between two points, often denoted as source and destination, within a given space or graph. The space could be a 2D plane, 3D space, or a graph structure where nodes represent points and edges signify possible paths with associated distances or weights.
Key Algorithms
1. Euclidean Distance
The Euclidean distance is the straight-line distance between two points in Euclidean space. For two points and in a 2D plane, the distance is calculated using the formula:
Example: Consider points A(2, 3) and B(5, 7). The Euclidean distance is:
2. Dijkstra's Algorithm
Dijkstra's algorithm is a graph-based method used to find the shortest path from a single source node to all other nodes in a weighted graph. It guarantees finding the shortest path to each node if the edge weights are non-negative.
Steps Involved:
- Initialize the distance to the source node as 0 and all other nodes as infinity.
- Mark all nodes unvisited. Set the source node as current.
- For the current node, consider all its unvisited neighbors and calculate their tentative distances. Compare the newly calculated tentative distance with the current assigned value and update if smaller.
- Once considered, mark the current node visited.
- Select the unvisited node with the smallest tentative distance as the new "current node" and repeat steps 3-5 until all nodes are visited.
Complexity: The time complexity of Dijkstra's algorithm is , but it can be reduced to using a priority queue, where V is the vertex count.
3. A* Search Algorithm
A* (A-star) is an extension of Dijkstra's algorithm that uses heuristics to optimize the search by prioritizing nodes closer to the goal. It combines the actual cost to reach a node and the estimated cost from that node to the goal.
Formula: Where: • is the total cost of the node. • is the cost from the start node to the current node. • is the heuristic estimate of the cost from the current node to the goal.
Example: For finding a path on a grid, a common heuristic is the Manhattan distance if moves are allowed only along grid lines.
Complexity: The worst-case time complexity is exponential, but it's significantly reduced in practice due to the heuristic's guidance.
Summary Table
| Algorithm | Method | Complexity | Use Case |
| Euclidean Distance | Straight-line between points | Simple 2D or 3D distance calculation | |
| Dijkstra's Algorithm | Graph-based, shortest path with non-negative weights | or | Road networks, maps, IP routing |
| A* Search Algorithm | Graph-based, heuristic-driven | Exponential in worst-case but efficient with good heuristics | Pathfinding in games, robot navigation |
Additional Considerations
Multi-Dimensional Spaces
For high-dimensional data, distance computation can become complex. Techniques such as Principal Component Analysis (PCA) can reduce dimensionality to facilitate easier distance calculations.
Approximations and Performance
Depending on the application's requirements, exact precision might not be necessary. In such cases, approximations like Chebyshev or Manhattan distances can offer faster computations with acceptable accuracy.
Applications Beyond Geometry
Shortest path algorithms have practical applications beyond geometry, including network optimization, social networks analytics, and machine learning, specifically in clustering or classification tasks where distance metrics guide the model's logic.
In conclusion, the choice of algorithm significantly impacts the efficiency and applicability of solutions to shortest distance problems. By understanding each method's characteristics and strengths, one can better fit the approach to specific needs, optimizing both performance and accuracy.
Related reading
- Shortest path and sorting points in a 2-dimensional space
- Shortest path DFS, BFS or both?
- Shortest path fewest nodes for unweighted graph
- Shortest path in absence of the given edge
- Shortest path with even number of edges
- Show label probability/confidence in NLTK
- Shortest path in graph where cost depends on the history of traversing
- Shortest path on a graph where distances change dynamically? maximum energy path

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.