-1

I am using a quad tree data structure to find nearby objects on a 2D plane. I have already implemented a working quad tree which returns an array for each object including all of it's neighbours. The problem is, I need each unique pair of objects from this group of lists.

Let's say I have the following points [a, b, c, d] in an array. And that:

a is near b,

b is near c,

c is near d,

Therefore,

a is NOT near c or d,

b is NOT near d,

c is NOT near a,

d is NOT near a or b,

Therefore by iterating over each object and asking for it's neighbours, I will get the following array, where each element is an array of its own where the first element is the object in question and each other element are it's neighbours:

[
  [a, b],       // a is only near b
  [b, a, c],    // So b is near c and also a
  [c, b, d],    // So c is near b and also d
  [d, c]        // d is only near c
]

I want an array containing only the unique pairs of neighbouring objects (E.g. having both [a, b] and [b, a] is unacceptable) and so in this case the solution would be:

[
  [a, b],
  [b, c],
  [c, d]
]

How can I achieve this result?

Also it would be greatly appreciated if the code was as optimised as possible. Thank you so much!

Jacob
  • 48
  • 6
  • Show a minimal working example in which you need help. – Dominique Fortin Sep 09 '20 at 03:38
  • @DominiqueFortin I am not entirely sure what you mean. I cannot really show a codepen as the quadtree itself is too complicated to implement. I am just asking for a function to filter an array from [[a,b],[b,a,c],[c,b,d],[d,c]] to [[a,b],[b,c],[c,d]]. – Jacob Sep 09 '20 at 03:53
  • You should come to Stackoverflow for help on something concrete you've made, not ask for somebody to code something for you. – Dominique Fortin Sep 09 '20 at 03:58
  • @DominiqueFortin I'm sure the solution to my problem is actually a pretty simple Array.filter function or something but I can't work out how to do it. I have coded this quad tree and the objects, but its all part of a much larger program that won't fit onto a codepen example, even if you don't want to give me any code, you could at least point me in a direction. So it is concrete, I'm just posting a question like everyone else does. I'll see if I can make a small code example... – Jacob Sep 09 '20 at 04:11
  • No it is not simple. Maybe using reduce but I'm not sure. – Dominique Fortin Sep 09 '20 at 04:44
  • @DominiqueFortin I solved the problem, it was in fact not overly complicated – Jacob Sep 10 '20 at 00:00

2 Answers2

1

I was thinking more around those lines

let unsortedArray = [
  ['a', 'b'],
  ['b', 'a', 'c'],
  ['c', 'b', 'd'],
  ['d', 'c']
];

function solver(arr) {
  let marked = {};
  let result = [];
  
  arr.forEach((arcs) => {
    let first;
    arcs.forEach((vertex, i) =>{
      if (i === 0 ) {
        first = vertex;
      } else {
        let forward = first+vertex, backward = vertex+first;

        if (!marked.hasOwnProperty(forward) && !marked.hasOwnProperty(backward)) {
          result.push([first, vertex]);
        }
        marked[forward] = 0;
        marked[backward] = 0;
      }
    });
  });
  
  return result;
}

console.log(solver(unsortedArray));
Dominique Fortin
  • 2,212
  • 15
  • 20
0

I solved the sorting but it is quite computationally expensive, if anyone knows how to shorten this code it would be greatly appreciated!

let unsortedArray = [
  ['a', 'b'],
  ['b', 'a', 'c'],
  ['c', 'b', 'd'],
  ['d', 'c']
];

function solver(arr) {
  // Detach first elements
  let groups = [];
  for (let i = 0; i < arr.length; i++) {
    let body = arr[i].shift();
    groups[i] = {body, others: arr[i]};
  }
  // Get all pairs
  let pairs = [];
  for (let i = 0; i < groups.length; i++) {
    for (let j = 0; j < groups[i].others.length; j++) {
      let pair = [groups[i].body, groups[i].others[j]];
      pairs.push(pair);
    }
  }
  // Filter non unique pairs
  for (let i = 0; i < pairs.length; i++) {
    for (let j = i + 1; j < pairs.length; j++) {
      if (pairs[i] == pairs[j] || (pairs[i][0] == pairs[j][1] && pairs[i][1] == pairs[j][0])) {
        pairs.splice(j, 1);
      }
    }
  }
  return pairs;
}

let sortedArray = solver(unsortedArray);
console.log(sortedArray);
Jacob
  • 48
  • 6