Skip to content

Commit 02f2ecb

Browse files
Add Chebyshev distance algorithm (#496)
1 parent cb4760c commit 02f2ecb

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using NUnit.Framework;
2+
using Algorithms.LinearAlgebra.Distances;
3+
using FluentAssertions;
4+
using System;
5+
6+
namespace Algorithms.Tests.LinearAlgebra.Distances;
7+
8+
public class ChebyshevTests
9+
{
10+
[TestCase(new[] { 1.0, 1.0 }, new[] { 2.0, 2.0 }, 1.0)]
11+
[TestCase(new[] { 1.0, 1.0, 9.0 }, new[] { 2.0, 2.0, -5.2 }, 14.2)]
12+
[TestCase(new[] { 1.0, 2.0, 3.0 }, new[] { 1.0, 2.0, 3.0 }, 0.0)]
13+
[TestCase(new[] { 1.0, 2.0, 3.0, 4.0 }, new[] { 1.75, 2.25, -3.0, 0.5 }, 6.0)]
14+
public void DistanceTest(double[] point1, double[] point2, double expectedDistance)
15+
{
16+
Chebyshev.Distance(point1, point2).Should().BeApproximately(expectedDistance, 0.01);
17+
}
18+
19+
[TestCase(new[] { 2.0, 3.0 }, new[] { -1.0 })]
20+
[TestCase(new[] { 1.0 }, new[] { 1.0, 2.0, 3.0 })]
21+
public void DistanceThrowsArgumentExceptionOnDifferentPointDimensions(double[] point1, double[] point2)
22+
{
23+
Action action = () => Chebyshev.Distance(point1, point2);
24+
action.Should().Throw<ArgumentException>();
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace Algorithms.LinearAlgebra.Distances;
5+
6+
/// <summary>
7+
/// Implementation of Chebyshev distance.
8+
/// It is the maximum absolute difference between the measures in all dimensions of two points.
9+
/// In other words, it is the maximum distance one has to travel along any coordinate axis to get from one point to another.
10+
///
11+
/// It is commonly used in various fields such as chess, warehouse logistics, and more.
12+
/// </summary>
13+
public static class Chebyshev
14+
{
15+
/// <summary>
16+
/// Calculate Chebyshev distance for two N-Dimensional points.
17+
/// </summary>
18+
/// <param name="point1">First N-Dimensional point.</param>
19+
/// <param name="point2">Second N-Dimensional point.</param>
20+
/// <returns>Calculated Chebyshev distance.</returns>
21+
public static double Distance(double[] point1, double[] point2)
22+
{
23+
if (point1.Length != point2.Length)
24+
{
25+
throw new ArgumentException("Both points should have the same dimensionality");
26+
}
27+
28+
// distance = max(|x1-y1|, |x2-y2|, ..., |xn-yn|)
29+
return point1.Zip(point2, (x1, x2) => Math.Abs(x1 - x2)).Max();
30+
}
31+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ find more than one implementation for the same objective but using different alg
5858
* [IHeuristicKnapsackSolver](./Algorithms/Knapsack/IHeuristicKnapsackSolver.cs)
5959
* [Linear Algebra](./Algorithms/LinearAlgebra)
6060
* [Distances](./Algorithms/LinearAlgebra/Distances)
61+
* [Chebyshev](./Algorithms/LinearAlgebra/Distances/Chebyshev.cs)
6162
* [Euclidean](./Algorithms/LinearAlgebra/Distances/Euclidean.cs)
6263
* [Manhattan](./Algorithms/LinearAlgebra/Distances/Manhattan.cs)
6364
* [Eigenvalue](./Algorithms/LinearAlgebra/Eigenvalue)

0 commit comments

Comments
 (0)