Algorithm::SixDegrees is a Perl module that can find a path through linked elements in a set.
my $sd1 = Algorithm::SixDegrees->new();
$sd1->data_source( actors => &starred_in );
$sd1->data_source( movies => &stars_of );
@elems = $sd1->make_link('actors', 'Tom Cruise', 'Kevin Bacon');
my $sd2 = Algorithm::SixDegrees->new();
$sd2->forward_data_source( friends => &friends, @args );
$sd2->reverse_data_source( friends => &friend_of, @args );
@elems = $sd2->make_link('friends', 'Bob', 'Mark');
Algorithm::SixDegrees is a Perl implementation of a breadth-first search through a set of linked elements in order to find the shortest possible chain linking two specific elements together.
In simpler terms, this module will take a bunch of related items and attempt to find a relationship between two of them. It looks for the shortest (and generally, simplest) relationship it can find.