Best path in a grid
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 best path in a grid is a classic problem in computer science and algorithms, often encountered in robotics, gaming, and network routing. It involves determining the optimal route from a starting point to an endpoint within a two-dimensional grid, subject to various constraints.
Problem Definition
A grid is typically defined as a matrix of cells or nodes, where each cell can represent information such as terrain type, cost to traverse, or obstacles. The objective is to find the shortest or cost-effective path from a designated start node to an end node.
Pathfinding Algorithms
Several algorithms are commonly used to solve the best path problem in grids. Here's a breakdown of some popular approaches:
1. Breadth-First Search (BFS)
• Description: BFS is a simple algorithm used for unweighted grids, where each move from one node to another has the same cost. • Advantages: Guaranteed to find the shortest path. • Disadvantages: Inefficient for large grids, as it explores all nodes at the current depth before moving deeper. • Performance: , where is the number of vertices and is the number of edges.
2. Depth-First Search (DFS)
• Description: DFS is used for exploring paths deeply before backtracking, more suitable for enumerating all possible paths. • Advantages: Requires less memory than BFS. • Disadvantages: Can get stuck in one branch, not guaranteed to find the shortest path without modification. • Performance: .
3. Dijkstra's Algorithm
• Description: Used for graphs with non-negative weights to determine the shortest path. • Advantages: Finds the shortest path efficiently but can be computationally expensive. • Disadvantages: Not suitable for graphs with negative weights without modification. • Performance: , which can be optimized to using a min-priority queue.
4. A* Search Algorithm
• Description: A* combines features of BFS and Dijkstra by using a heuristic to prioritize movement. • Advantages: More efficient than Dijkstra for less complex graphs when heuristics are well-chosen. • Disadvantages: Optimality depends on the heuristic; can degrade into Dijkstra's if not chosen carefully. • Performance: , with performance heavily dependent on the accuracy of the heuristic.
Algorithm Comparison
| Algorithm | Optimality Guaranteed | Complexity | Memory Usage | Suitable For |
| BFS | Yes | High | Unweighted grids | |
| DFS | No | Low | Path exploration | |
| Dijkstra | Yes | or | Moderate | Weighted graphs |
| A* | Yes (with good heuristic) | High | Shortest path with heuristic |
Example
Consider a grid where `0` represents a traversable cell, and `1` represents an obstacle. The task is to find the lowest cost path from the top-left corner to the bottom-right.
0 0 0 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0 1 Finish
• Manhattan Distance: Suitable for grids where movement is restricted to vertical/horizontal directions: . • Euclidean Distance: Fits scenarios where diagonal moves are allowed: .
Related reading
- best possible implementation of the travelling salesman / vehicle routing use case
- Best sorting algorithms for C / .NET in different scenarios
- Best way to compare two complex objects
- Best way to do powerOfint x, int n?
- Best way of calculating n choose k?
- Best way to determine if a sequence is in another sequence?
- Best way to efficiently find high density regions
- Best way to find a number in an array that's out of order?

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.