Created
January 4, 2013 14:33
-
-
Save dapurv5/4452969 to your computer and use it in GitHub Desktop.
Bipartite Matching with Apache Hama
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void compute(Iterator<TextPair> messages) | |
throws IOException { | |
if(isMatched()){ | |
voteToHalt(); | |
return; | |
} | |
switch((int)getSuperstepCount() % 4){ | |
case 0: | |
if( Objects.equal(getComponent(), LEFT)){ | |
sendMessageToNeighbors(getNewMessage()); | |
} | |
break; | |
case 1: | |
if( Objects.equal(getComponent(), RIGHT)){ | |
List<TextPair> buffer = new ArrayList<TextPair>(); | |
while(messages.hasNext()){ | |
buffer.add(messages.next()); | |
} | |
if(buffer.size()>0){ | |
TextPair luckyMsg = buffer.get( | |
RandomUtils.nextInt(random,buffer.size())); | |
Text sourceVertex = getSourceVertex(luckyMsg); | |
sendMessage(sourceVertex, getNewMessage()); | |
} | |
} | |
break; | |
case 2: | |
if( Objects.equal(getComponent(), LEFT)){ | |
List<TextPair> buffer = new ArrayList<TextPair>(); | |
while(messages.hasNext()){ | |
buffer.add(messages.next()); | |
} | |
if(buffer.size() > 0){ | |
TextPair luckyMsg = buffer.get( | |
RandomUtils.nextInt(random,buffer.size())); | |
Text sourceVertex = getSourceVertex(luckyMsg); | |
setMatchVertex(sourceVertex); | |
sendMessage(sourceVertex, getNewMessage()); | |
} | |
} | |
break; | |
case 3: | |
if(Objects.equal(getComponent(), RIGHT)){ | |
if(messages.hasNext()){ | |
Text sourceVertex = getSourceVertex(messages.next()); | |
setMatchVertex(sourceVertex); | |
} | |
} | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment