Manhattan distance
Manhattan distance is the sum of the absolute differences between corresponding coordinates of two points or vectors.
Classic Computer Science Problems in Swift: Essential techniques for practicing programmers
Manhattan distance is the sum of the absolute differences between corresponding coordinates of two points.
- How it is used
- Guides A* search toward a goal in a four-direction maze.
- How deep it goes
- Defines the row-and-column calculation and implements it in Swift, then explains admissibility without a formal proof.
- Examples
- Uses Manhattan street blocks, maze locations, a grid figure, and a Swift distance function.
- What you can do after
- Build it from scratch as a Swift heuristic for four-direction A* maze search.
Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI
Manhattan distance is the sum of the absolute coordinate differences between two points.
- How it is used
- Creates geographical machine-learning features that represent proximity and urban travel patterns.
- How deep it goes
- Defines the metric, contrasts it with Euclidean distance, and applies it to coordinates and nearest-neighbor queries.
- Examples
- Uses Tokyo Airbnb coordinates, the Imperial Palace, and nearby transportation and facility categories.
- What you can do after
- Apply it via scikit-learn KDTree and coordinate transformations to create proximity features.
Data Without Labels
Manhattan distance is the sum of absolute coordinate differences between two points, corresponding to movement along perpendicular axes.
- How it is used
- Uses absolute coordinate differences to measure similarity or distance between observations during clustering.
- How deep it goes
- Defines the grid-path formula, contrasts L1 with L2, and notes high-dimensional preference without detailing implementation.
- Examples
- Uses coordinate points, grid-like movement, and centroid or hierarchical clustering as application contexts.
- What you can do after
- Apply it as a distance metric when comparing clustering choices for a dataset.
Outlier Detection in Python
Manhattan distance is the sum of the absolute coordinate-wise differences between two points.
- How it is used
- Uses coordinate-wise differences to compare records, including mixed datasets and some outlier-detection workflows.
- How deep it goes
- Defines the metric, compares its weighting with Euclidean distance, and demonstrates implementation; it stops short of a full detector.
- Examples
- Uses one-hot encoded color and weight rows, pairwise distance tables, and Python calls to manhattan_distances.
- What you can do after
- Apply it via scikit-learn to calculate distances between numeric or one-hot encoded records.
Read how Outlier Detection in Python explains Manhattan distance →
Also covered in
- Natural Language Processing in Action, Second Edition
- Knowledge Graphs and LLMs in Action
- Advanced Algorithms and Data Structures
What to know first
- Euclidean distanceEuclidean distance is the straight-line distance between two points, calculated as the square root of the sum of squared differences between corresponding coordinates.
- l2 normThe L2 norm of a vector is its Euclidean length, calculated as the square root of the sum of the squares of its components.
- embeddingAn embedding is a numerical vector representation of an item in a feature space, encoding selected properties or relationships for computational tasks such as comparison or retrieval.
Related concepts
- Euclidean distancecontrasts with · Euclidean distance is the straight-line distance between two points, calculated as the square root of the sum of squared differences between corresponding coordinates.
- minkowski distanceequivalent to · Minkowski distance is a parameterized metric between points based on a positive-order norm, including Manhattan and Euclidean distance as special cases.
- distancecontrasts with · A distance function assigns a numerical measure of separation or dissimilarity to pairs of points or objects in a defined space.
- heuristickind of · A heuristic is a practical rule or approximate method for guiding judgment, search, or problem solving without guaranteeing an exact or optimal result.
Questions about Manhattan distance
What is Manhattan distance?
Manhattan distance is the sum of the absolute differences between corresponding coordinates of two points or vectors.
Which Manning books explain Manhattan distance?
Classic Computer Science Problems in Swift: Essential techniques for practicing programmers, Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI, Data Without Labels and Outlier Detection in Python.
What should I know before learning Manhattan distance?
Manning books that teach Manhattan distance usually introduce Euclidean distance, l1 norm, l2 norm and embedding first.
What is the difference between Manhattan distance and Euclidean distance?
Manhattan distance is the sum of the absolute differences between corresponding coordinates of two points or vectors. Euclidean distance is the straight-line distance between two points, calculated as the square root of the sum of squared differences between corresponding coordinates.
What is Manhattan distance a kind of?
Manhattan distance is a kind of heuristic. A heuristic is a practical rule or approximate method for guiding judgment, search, or problem solving without guaranteeing an exact or optimal result.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.



