All ▲lgorithms

All ▲lgorithms

  • Algorithms
  • Categories
  • Libraries
  • Blog

›Computational geometry

Artificial Intelligence

  • Dbscan
  • Isodata
  • Linear regression
  • Logistic regression
  • Neutral style transfer
  • Sat
  • Tsp
  • A star
  • Artificial neutral network
  • Convolutional neutral network
  • Decision tree
  • Factorization machines
  • Gaussian mixtrue model
  • Gradient boostring trees
  • Hierachical clustering
  • Image processing
  • K nearest neighbors
  • K means
  • Minimax
  • Native bayes
  • Nearest sequence memory
  • Neutral network
  • Perceptron
  • Principal component analysis
  • Q learning
  • Random forest
  • Restricted boltzman machine

Backtracking

  • Algorithm x
  • Crossword Puzzle
  • Knight tour
  • M coloring problem
  • N queen
  • Number of ways in maze
  • Partitions of set
  • Permutation of strings
  • Powerset
  • Rat in maze
  • Subset sum
  • Sudoku solve

Bit manipulation

  • Adding using bits
  • Bit divisor
  • Byte swapper
  • Convert numbers to binary
  • Count set bits
  • Flip bits
  • Hamming distace
  • Invert bit
  • Lonely integer
  • Magic number
  • Maximun xor value
  • Power of 2
  • Subset generation
  • Sum binary numbers
  • Sum equals xor
  • Thrice unique number
  • Twice unique number
  • Xor swap

Cellular automaton

  • Brians brain
  • Conways game of life
  • Elementary cellular automata
  • Generic algorithm
  • Langtons ant
  • Nobili cellular automata
  • Von neoumann cellular automata

Computational geometry

  • 2d line intersection
  • 2d separating axis test
  • Area of polygon
  • Area of triangle
  • Axis aligned bounding box collision
  • Bresenham line
  • Chans algorithm
  • Cohen sutherland lineclip
  • Distance between points
  • Graham scan
  • Halfplane intersection
  • Jarvis march
  • Quickull
  • Sphere tetrahedron intersection
  • Sutherland hodgeman clipping

Cryptography

  • Affine cipher
  • Atbash cipher
  • Autokey cipher
  • Baconian cipher
  • Caesar cipher
  • Colummnar cipher
  • Vigenere cipher

Data structures

  • Bag
  • Hashes
  • Linked list
  • List
  • Queue
  • Stack
  • Tree

Divide and conquer

  • Strassen matrix manipulation
  • Closest pair of point
  • Inversion count
  • Karatsuba multiplication
  • Maximum contiguous subsequence sum
  • Merge sort using divide and conquer
  • Quick sort using divide and conquer
  • Tournament method to find min max
  • Warnock algorithm
  • X power y

Dynamic programming

  • Array median
  • Optima binary search tree

Gaming theory

  • Nim next best move game
  • Nim win loss game
  • Grundy numbers kayle game

Graphs

  • Bipartite check
  • Adjacency lists graphs representation

Greedy algorithms

  • Activity selection
  • Dijkstra shortest path
  • Egyptian fraction

Math

  • 2 sum
  • Add polynomials
  • Amicable numbers
  • Armstrong numbers
  • Automorphic numbers
  • Average stream numbers
  • Babylonian method
  • Binomial coefficient
  • Catalan number
  • Check is square
  • Convolution
  • Coprime numbers
  • Count digits
  • Count trailing zeroes
  • Decoding of string
  • Delannoy number
  • Derangements
  • Dfa division
  • Diophantine
  • Divided differences
  • Euler totient
  • Exponentiation power
  • Factorial
  • Fast fourier transform
  • Fast inverse square root

Networking

  • Packet sniffer
  • Determine endianess
  • Validate ip

Numerical analysis

  • Integral
  • Monte carlo
  • Runge kutt

Randomized algorithms

  • Birthday paradox
  • Karger minimum cut algorithm
  • Kth smallest element algorithm
  • Random from stream
  • Random node linked list
  • Randomized quicksort
  • Reservoir sampling
  • Shuffle an array

Searches

  • Binary search
  • Exponential search
  • Fibonacci search
  • Fuzzy search
  • Interpolation search
  • Jump search
  • Linear search
  • Ternay search

Selections algorithms

  • Median of medians
  • Quick select

Sorting

  • Bead sort
  • Bogo sort
  • Bubble sort
  • Bucket sort
  • Circle sort
  • Comb sort
  • Counting sort
  • Cycle sort
  • Flash sort
  • Gnome sort
  • Heap sort
  • Insertion sort
  • Intro sort
  • Merge sort
  • Pipeonhole sort
  • Quick sort
  • Radix sort
  • Selection sort
  • Shaker sort
  • Shell sort
  • Sleep sort
  • Stooge sort
  • Topological sort
  • Tree sort

Strings

  • Aho corasick algorithm
  • Anagram search
  • Arithmetic on large numbers
  • Boyer moore algorithm
  • Finite automata
  • Kasai algorithm
  • Kmp algorithm
  • Levenshteing distance
  • Lipogram checker

Online challenges

  • Coderbyte
  • Code chef
  • Code eval
  • Hackerearth
  • Hackerrank
  • Leetcode
  • Project euler
  • Rosalind
  • Spoj
  • Top coder

No category

  • Average
  • Biggest of n numbers
  • Biggest suffix
  • Fifteen puzzle
  • Jaccard similarity
  • Jose phus problem
  • Lapindrom checker
  • Leap year
  • Magic square
  • Majority element
  • Minimum subarray size with degree
  • No operator addition
  • Paint fill
  • Split list
  • Tokenizer
  • Unique number

Distance between points

The distance between two points can have several different definitions depending on the specific situation, although without any further specification it usually refers to the length of the segment connecting the two points (also called Euclidean distance).

Euclidean Distance

The Euclidean distance is generally defined as the lenght of the segment connecting two points.

Euclidean distance

Formula

The Euclidean distance can be easily calculated when the coordinates of the two points are known. For example, in the XY plane, the formulas would be the following:

P = (P_x, P_y)

Q = (Q_x, Q_y)

d(P, Q) = \sqrt{(P_x - Q_x)^2 + (P_y - Q_y)^2}

The formula above can be generalized for vector spaces of any dimension, by taking the square root of the sum of the squares of differences in each dimension:

P = (P_1, P_2, ..., P_n)

Q = (Q_1, Q_2, ..., Q_n)

d(P, Q) = \sqrt{(P_1 - Q_1)^2 + (P_2 - Q_2)^2 + ... + (P_n - Q_n)^2}

d(P, Q) = \sqrt{\sum_{i=1}^n (P_i - Q_i)^2}

Algorithm

The algorithm for the Euclidean distance can be derived directly from the defining formula above:

/* Assume that P and Q are N-dimensional arrays */
distance(P, Q) {
   N = dim(P)
   square_sum = 0.0                              /* initialize running sum */
   for dimension i from 1 to N {                 /* iterate over all dimensions */
      square_sum = square_sum + (P[i] - Q[i])^2  /* sum squares of differences */
   }
   distance = sqrt(square_sum);                  /* distance is the square root of the sum of squared differences */
   return distance;
}

Manhattan Distance

Among the many possible definitions of distance, another one commonly found in computer science is the Manhattan distance. This is typically a distance used when movements are constrained to happen over a grid:

Manhattan distance

Formula

The formula below applies when calculating the Manhattan distance over an N-dimensional grid where adjacent nodes are assumed to have distance 1:

P = (P_1, P_2, ..., P_n)

Q = (Q_1, Q_2, ..., Q_n)

d(P, Q) = \sum_{i=1}^n |P_i - Q_i|

Algorithm

The algorithm can be implemented as a direct derivation of the formula:

/* Assume that P and Q are N-dimensional arrays */
manhattan_distance(P, Q) {
   N = dim(P)
   distance = 0.0                             /* initialize running sum */
   for dimension i from 1 to N {              /* iterate over all dimensions */
      distance = distance + abs(P[i] - Q[i])  /* sum squares of differences */
   }
   return distance;
}

Minkowski Distance

Minkowski Distance is a metric in a normed vector space which can be considered as a generalization of both the Euclidean distance and the Manhattan distance. It is named after the German mathematician Hermann Minkowski.

Formula

The formula below applies when calculating the Minkowski distance over an N-dimensional grid:

$P = (p_1, p_2, ..., p_n)\$ $Q = (q_1, q_2, ..., q_n)\$ $d(P, Q) = \left( \sum_{i=1}^{n} |p_i - q_i|^m \right) ^ {\frac{1}{m}}$

Algorithm

/* Assume that P and Q are N-dimensional arrays */
manhattan_distance(P, Q) {
   N = dim(P)
   distance = 0.0                             /* initialize running sum */
   for dimension i from 1 to N {              /* iterate over all dimensions */
      distance = distance + abs(P[i] - Q[i])^m  /* sum the processed differences */
   }
   return distance^(1/m);
}

Performance

Both Euclidean and Manhattan distance algorithms for each respective distance definition are linear (O(n)) with respect to the number of dimensions with which each point is represented. The Minkowski distance algorithm's complexity is around O(nm) for n dimensional vector and the factor power m.

Implementations

LanguageLink

Helpful Links

  • Euclidean distance
  • Manhattan distance
  • Minkowski distance
  • Norm a generalization of the concept of distance
  • Lp space a generalization of a class of norms
Last updated on 2020-10-2 by ddhira123
← Cohen sutherland lineclipGraham scan →
  • Euclidean Distance
    • Formula
    • Algorithm
  • Manhattan Distance
    • Formula
    • Algorithm
  • Minkowski Distance
    • Formula
    • Algorithm
  • Performance
  • Implementations
  • Helpful Links
All ▲lgorithms
Implementations
C++JavaJavascriptRubyGoMore ...
Libraries Docs
PythonJavaJavascriptMore ...
Community
GithubGitterTwitterInstagramFacebook
More
BlogStickers & T-ShirtsContributingCategories
Powered by Tryhtml
Copyright © 2021 The All ▲lgorithms Project.