Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConnectedComponentsVertex |
|
| 11.0;11 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, software | |
13 | * distributed under the License is distributed on an "AS IS" BASIS, | |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 | * See the License for the specific language governing permissions and | |
16 | * limitations under the License. | |
17 | */ | |
18 | ||
19 | package org.apache.giraph.examples; | |
20 | ||
21 | import org.apache.giraph.graph.IntIntNullIntVertex; | |
22 | import org.apache.hadoop.io.IntWritable; | |
23 | ||
24 | import java.io.IOException; | |
25 | ||
26 | /** | |
27 | * Implementation of the HCC algorithm that identifies connected components and | |
28 | * assigns each vertex its "component identifier" (the smallest vertex id | |
29 | * in the component) | |
30 | * | |
31 | * The idea behind the algorithm is very simple: propagate the smallest | |
32 | * vertex id along the edges to all vertices of a connected component. The | |
33 | * number of supersteps necessary is equal to the length of the maximum | |
34 | * diameter of all components + 1 | |
35 | * | |
36 | * The original Hadoop-based variant of this algorithm was proposed by Kang, | |
37 | * Charalampos, Tsourakakis and Faloutsos in | |
38 | * "PEGASUS: Mining Peta-Scale Graphs", 2010 | |
39 | * | |
40 | * http://www.cs.cmu.edu/~ukang/papers/PegasusKAIS.pdf | |
41 | */ | |
42 | @Algorithm( | |
43 | name = "Connected components", | |
44 | description = "Finds connected components of the graph" | |
45 | ) | |
46 | 26 | public class ConnectedComponentsVertex extends IntIntNullIntVertex { |
47 | /** | |
48 | * Propagates the smallest vertex id to all neighbors. Will always choose to | |
49 | * halt and only reactivate if a smaller id has been sent to it. | |
50 | * | |
51 | * @param messages Iterator of messages from the previous superstep. | |
52 | * @throws IOException | |
53 | */ | |
54 | @Override | |
55 | public void compute(Iterable<IntWritable> messages) throws IOException { | |
56 | 70 | int currentComponent = getValue().get(); |
57 | ||
58 | // First superstep is special, because we can simply look at the neighbors | |
59 | 70 | if (getSuperstep() == 0) { |
60 | 26 | for (IntWritable neighbor : getNeighbors()) { |
61 | 64 | if (neighbor.get() < currentComponent) { |
62 | 26 | currentComponent = neighbor.get(); |
63 | } | |
64 | } | |
65 | // Only need to send value if it is not the own id | |
66 | 26 | if (currentComponent != getValue().get()) { |
67 | 20 | setValue(new IntWritable(currentComponent)); |
68 | 20 | for (IntWritable neighbor : getNeighbors()) { |
69 | 56 | if (neighbor.get() > currentComponent) { |
70 | 36 | sendMessage(new IntWritable(neighbor.get()), getValue()); |
71 | } | |
72 | } | |
73 | } | |
74 | ||
75 | 26 | voteToHalt(); |
76 | 26 | return; |
77 | } | |
78 | ||
79 | 44 | boolean changed = false; |
80 | // did we get a smaller id ? | |
81 | 44 | for (IntWritable message : messages) { |
82 | 44 | int candidateComponent = message.get(); |
83 | 44 | if (candidateComponent < currentComponent) { |
84 | 16 | currentComponent = candidateComponent; |
85 | 16 | changed = true; |
86 | } | |
87 | 44 | } |
88 | ||
89 | // propagate new component id to the neighbors | |
90 | 44 | if (changed) { |
91 | 16 | setValue(new IntWritable(currentComponent)); |
92 | 16 | sendMessageToAllEdges(getValue()); |
93 | } | |
94 | 44 | voteToHalt(); |
95 | 44 | } |
96 | } |