Adjacent values

3

I need to create an algorithm that calculates the minimum distance value between the indexes 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 value pairs have adjacent indexes:

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

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

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

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

Indices 4 and 5 have adjacent values because there is no value in array A

which is strictly between A [4] = 5 and A [5] = 3; the only such value could be number 4,

and that is not present in the array.

    
asked by anonymous 11.02.2016 / 20:57

1 answer

3

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

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


Example 1


Excerpted from the SO question 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 integer N's.   A pair of indexes (P, Q) where 0 ≤ P < Q < N, it is said adjacent, if no value "is strictly" between A [P] and A [Q].   ...


Here, the rule for creating tuples (P, Q) indicates that there can be no values intermediaries 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 indexes (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, you 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 O (N * log (N)) complexity

I'm assuming the source looks like the following challenge in the Codility website:

Shortest Adjacency Sequency

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


The smallest path from the first element ( 1 ) to the last element ( 2 ) must be found, following the rules outlined in the link above.

Sample 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 until 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 until 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 up 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 the problem, the solution (smallest path) is [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 (according to the comments).


Below is an example 2 implementation (in Python):

Coding Iota 2011 Coding Challenge - ShortestAdjSeq

    
12.02.2016 / 02:16