Identifying direct relations between rows using Spark

Input :

>>> a
[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (3, 4), (4, 5)]

Output :

>>> tsf
[(1, [2]), (2, [3]), (3, [4]), (4, [5])]

I'm identifying only direct relations from a given starting point to end and separating it. For ex:

Above input can be logically represented as :

1->2->3->4->5

I'm trying to get direct relations from a key like 1 and so on.

I've a normal python code something like this,

>>> for k,g in itertools.groupby(a, operator.itemgetter(0)):
...     c.append((k,[x[1] for x in g]))

>>> c
[(1, [2, 3, 4, 5]), (2, [3, 4]), (3, [4]), (4, [5])]
>>> tsf = []
>>> for each_pair in c:
...     start = each_pair[0]
...     endings = each_pair[1]
...     direct_dependencies = []
...     for i in endings:
...         not_direct_dep = False
...         for j in endings:
...             deps = d.get(j)
...             if deps is None:
...                 continue
...             if i in deps:
...                 not_direct_dep = True
...                 break;
...         if not not_direct_dep:
...             direct_dependencies.append(i)
...     tsf.append((start, direct_dependencies))

This works to get what I need, I'm transporting this logic to spark to process over a million records and require data preprocessing before this step.

In place of dictionary object like d I was planning to broadcast a d_rdd with similar content and use it within a map function called for each pair of the c_rdd.

Now, I'm not able to do that because spark doesn't allow such operation. And I'm not able to approach in another way because I'd need to access all the relations of given value from any worker. Any help is appreciated.