Skip to content

Commit 3d1d197

Browse files
committed
Added documentation for Linear Search and added Minkowski Distance for Distance between 2 points
1 parent ec8c009 commit 3d1d197

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

docs/distance-between-points.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,38 @@ manhattan_distance(P, Q) {
119119
}
120120
```
121121

122+
## Minkowski Distance
123+
124+
**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.
125+
126+
### Formula
127+
128+
The formula below applies when calculating the Minkowski distance over an N-dimensional grid:
129+
130+
$P = (p_1, p_2, ..., p_n)\\$
131+
$Q = (q_1, q_2, ..., q_n)\\$
132+
$d(P, Q) = \left( \sum_{i=1}^{n} |p_i - q_i|^m \right) ^ {\frac{1}{m}}$
133+
134+
### Algorithm
135+
136+
```
137+
/* Assume that P and Q are N-dimensional arrays */
138+
manhattan_distance(P, Q) {
139+
N = dim(P)
140+
distance = 0.0 /* initialize running sum */
141+
for dimension i from 1 to N { /* iterate over all dimensions */
142+
distance = distance + abs(P[i] - Q[i])^m /* sum the processed differences */
143+
}
144+
return distance^(1/m);
145+
}
146+
```
147+
148+
122149
## Performance
123150

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

128155

129156
## Implementations
@@ -136,11 +163,13 @@ represented.
136163

137164
- [Euclidean distance][euclidean]
138165
- [Manhattan distance][manhattan]
166+
- [Minkowski distance][minkowski]
139167
- [Norm][norm] a generalization of the concept of distance
140168
- [L<sup>p</sup> space][lp-space] a generalization of a class of norms
141169

142170

143171
[euclidean]: https://en.wikipedia.org/wiki/Euclidean_distance
144172
[manhattan]: https://en.wikipedia.org/wiki/Taxicab_geometry
173+
[minkowski]: https://en.wikipedia.org/wiki/Minkowski_distance
145174
[norm]: https://en.wikipedia.org/wiki/Norm_(mathematics)
146175
[lp-space]: https://en.wikipedia.org/wiki/Lp_space

docs/linear-search.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,36 @@ title: Linear search
44
sidebar_label: Linear search
55
---
66

7-
[Open a pull request](https://github.com/AllAlgorithms/algorithms/tree/master/docs/linear-search.md) to add the content for this algorithm.
7+
**Linear Search** or commonly called as sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.
8+
9+
## Algorithm
10+
11+
Linear Search requires two parameters: the array/list and the key. It traverses the entire array/list starting from the first element to the last element of the array. During the traversal, the algorithm checks if the element is equal to the key. Once it finds an element that equals to the key, the function immediately returns the index. The result will be considered as `NOT FOUND` if there is no any element in the array equals the key.
12+
13+
The algorithm can be expressed in pseudocode as follows:
14+
15+
```
16+
LinearSearch(array, key){
17+
idx = 0
18+
for idx = 0...n
19+
if array[idx] = key then
20+
return idx
21+
22+
return -1
23+
}
24+
```
25+
26+
Here is the simulation of Linear Search Algorithm:
27+
28+
<img src="https://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif"/>
29+
Source: Tutorialspoint
30+
31+
## Complexity
32+
33+
As the algorithm traverses entire list during runtime, then it has the complexity O(N).
34+
35+
## Implementation
36+
37+
| | Language | Link |
38+
|:-: | :-: | :-: |
39+
| <img src="https://cdn.abranhe.com/projects/algorithms/logos/python.svg" width="30px"> | Python | [dbscan.py](https://github.com/AllAlgorithms/python/blob/master/algorithms/searches/linear_search.py) |

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy