The MJRTY algorithm sets out to solve the problem of finding the majority element in a stream (an element comprising more than 50% of the stream). Moore proposed to solve this by using only 2 pieces of information and a single scan of the data. Imagine you have a stream of names (“matt”, “timon”, “matt”, “matt”, “rob”, “ben”, …) and you wanted to know if any name appeared in more than half the stream. Boyer and Moore proposed the following:
count = 0
majority = ""
for val in stream:
if count == 0:
majority = val
count = 1
elif val == majority:
count += 1
else:
count -= 1
print majority if count > 0 else "no majority!"
if stream = A A A A B B B B C => result = C, but it isn't truth.
About this algotithm: http://ift.tt/rChbeM
Aucun commentaire:
Enregistrer un commentaire