I am faced with the following dataset:
const main = 'test1';
const data = [
{
from: 'test1',
to: 'test2'
},
{
from: 'test2',
to: 'test3'
},
{
from: 'test3',
to: 'test4'
},
{
from: 'test4',
to: 'test2'
},
{
from: 'test1',
to: 'test4'
}
];
My goal is to determine the number of connections to the main node (in this case test1). For instance, if we consider node test3, it requires 2 links to reach test1:
test3 → test2 → test1
The same applies to node test2, where only 1 link is needed to arrive at test1.
What would be the most efficient approach to calculate this? Ultimately, I aim to find the longest chain of connections leading to test1. In this particular example, it amounts to 3:
test3 → test2 → test4 → test1