Adjacent values

I need to create an algorithm that calculates the minimum distance value between the indices of an array containing adjacent values . But what are adjacent values could anyone give me examples?

For example, in Matrix a such that:

  

  A [0] = 0
  A [1] = 3
  A [2] = 3
  A [3] = 7
  A [4] = 5
  A [5] = 3
  A [6] = 11
  A [7] = 1

The following pairs of values have adjacent indices:

  (0, 7), (1, 2), (1, 4),

  (1, 5), (1, 7), (2, 4),

  (2, 5), (2, 7), (3, 4),

  (3, 6), (4, 5), (5, 7).

Indexes 4 and 5 have adjacent values because there is no value in the array a

Which lies strictly between [4] = 5 and [5] = 3; the only such value could be the number 4,

And that is not present in the Matrix.

Author: Gomiero, 2016-02-11

1 answers

Adjacent values can have many different meanings, and depend primarily on the statement of the problem and the area to which it applies.

Two Step-by-step examples, to show different rules for the concept adjacent values .


Example 1


Extracted from the question in the OS in English:

Trying to sort and find the nearest pair of adjacent values in an array in C #

In this case, the rule is:

...
A non-empty array with zero index contains n integers.
A pair of indices (P, Q) where 0 ≤ P ...


Here, the rule for creating the tuples (p, Q) indicates that there can be no values intermediates between 2 elements, therefore:


Valor   |  0 |  3 |  3 |  7 |  5 |  3 | 11 |  1 |  
--------+----+----+----+----+----+----+----+----+  
Índice  |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  


Tuples (P, Q) of indices (explanation follows the notation valor[índice] or valor):

(0, 1)  => não, porque tem o valor 1[7] entre 0 e 3
(0, 2)  => não, porque tem o valor 1[7] entre 0 e 3
(0, 3)  => não, porque tem o valores 1[7] e 5[4] entre 0 e 7
(0, 4)  => não, porque tem o valor 1[7] e 3[1] entre 0 e 5
(0, 5)  => não, porque tem o valor 1[7] entre 0 e 3
(0, 6)  => não, porque tem o valores 1[7], 3[1], 5[4] e 7[3] entre 0 e 11
(0, 7)  => SIM, porque não há valor entre 0 e 1 (adjacentes)

(1, 2)  => SIM, porque não há valor entre 3 e 3 (adjacentes)
(1, 3)  => não, porque tem o valor 5[4] entre 3 e 7
(1, 4)  => SIM, porque não há valor entre 3 e 5 (adjacentes)
(1, 5)  => SIM, porque não há valor entre 3 e 3 (adjacentes)
(1, 6)  => não, porque tem o valores 5[4] e 7[3] entre 3 e 11
(1, 7)  => SIM, porque não há valor entre 3 e 1 (adjacentes)

(2, 3)  => não, porque tem o valor 5[4] entre 3 e 7
(2, 4)  => SIM, porque não há valor entre 3 e 5 (adjacentes)
.....
.....
.....

And following these steps, we get the set of tuples:

(0, 7), (1, 2), (1, 4),
(1, 5), (1, 7), (2, 4),
(2, 5), (2, 7), (3, 4),
(3, 6), (4, 5), (5, 7)

Example 2

Similar to the question you asked in:

Algorithm of adjacent numbers with complexity O (N * log (N))

I'm assuming the origin looks like the following challenge on the Codility site:


Shortest Adjacency Sequence

(I did not copy the text here due to copyright, but just click on the link and the button View on the site)


Must find the smallest path from the first element (1) to the last element (2), following the rules described in the link above.

Example Vector:

Valor   |  1 | 10 |  6 |  5 | 10 |  7 |  5 |  2 |
--------+----+----+----+----+----+----+----+----+
Índice  |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |



Some possible paths (explanation follows the notation valor[índice] or value):


[1, 10, 6, 5, 10, 7, 5, 2]

Explanation: is the original vector itself.


[1, 10, 6, 5, 10, 7, => 5, 10, 7, 5, 2]

Explanation: traverses the vector to the value 5[6], returns to the value 5[3] (adjacent) and continues to the end.


[1, 10, 6, 5, => 10, 6, 5, 10, 7, 5, 2]

Explanation: traverses the vector to the value 10[4], returns to the value 10[1] (adjacent) and continues to the end.


[1, => 10, 7, 5, 2];

Explanation: traverses the vector to the value 10[1], jumps to the value 10[4] (adjacent) and continues to the end.


[1, => 10, => 5, 2]

Explanation: traverses the vector to the value 10[1], jumps to the value 10[4] (adjacent), returns to the value 5[3] (previous), jumps to the value 5[6] (adjacent) and continues to the end.


In this example, according to problem, the solution (smallest path) is the [1, 10, 5, 2].



Therefore, the term valores adjacentes depends on the rule specified in the problem and also the area where the term is used (as per the comments).


Below follows an implementation (in Python) of Example 2:

Codility Iota 2011 Coding Challenge-ShortestAdjSeq

 3
Author: Gomiero, 2017-05-23 12:37:27