J
J
jonasas2015-11-14 23:05:41
Algorithms
jonasas, 2015-11-14 23:05:41

How can you find a loop in a singly linked list using MapReduce?

I have a set of elements that can have a parent of the same type. Elements that have parents form graphs (singly linked lists).
My task is to for each element that has a non-top parent, specify as the parent the element that is the top of the entire chain. For example: from 1 -> 2 -> 3 -> 4you need to get: 1 -> 4, 2 -> 4, 3 -> 4.
Here's my algorithm:
Map is given pairs of elements as input C -> P. On the Map output, I send pairs of C -> P, P -> C.
Thus, the Reduce input receives an element as a key K, its parent P and child C: K -> {P, C}.
At the output of the Reduce step, I tell the child of the key element that its parent will now be the parent of the key element.
Since the lists are short, all the work is done in a small number of MapReduce iterations. But if there is a loop, then the chains are no longer cancelled.
So the question is how to localize and eliminate the loop. Or maybe someone will come up with a more elegant algorithm for the task at hand.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SeptiM, 2015-11-16
@SeptiM

The task is unclear. I realized that there is a set of elements. For each element, the following is defined. Globally, elements can form several connected components, each of which is either a path or a cycle, or form the letter p (tail + loop).
But what it means to throw out intermediaries, I do not understand. Can you put it in a more general way?
And also explain where your task ended and the algorithm began.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question