0

I need to calculate modularity on a small graph and I have stumbled across this project which provides a class to compute Newman modularity score. However, the method needs a module membership:

public static <V,E,M> double computeModularity (Graph<V,E> g,Transformer<V,M> moduleMembership)

so my question is: is there a better way than this to assign membership to my nodes?

// assuming I have a Graph<Person, String> thisNet
EdgeBetweennessClusterer<Person, String> edgeBetweennessClusterer = new EdgeBetweennessClusterer<>(1);
Set<Set<Person>> clusters = edgeBetweennessClusterer.transform(thisNet);
int membership = 0;
for (Set s : clusters) {
    for (Iterator it = s.iterator(); it.hasNext();) {
        Person inset = (Person) it.next();
        inset.membership = membership;
    } 
    membership++;
}

then I will apply a Transformer like:

Transformer<Person, Integer> componentMembership = new Transformer<Person, Integer>() {
    @Override
    public Integer transform(Person s) {
       return s.membership;
    }
};
SlowLoris
  • 995
  • 6
  • 28
user299791
  • 2,021
  • 3
  • 31
  • 57
  • 1
    You could alternatively use a `Map` that maps the `Person` to the membership value. This map could be filled with the loop that you already posted, and the `Transformer` could simply look up the person in the map. Does this answer your question? – Marco13 Sep 25 '14 at 08:39
  • so it looks like my way is reasonable... thanks for the alternative! – user299791 Sep 25 '14 at 08:41
  • "Reasonable" - that's a bit fuzzy. You should NOT have public fields in the `Person` class. And if this information is only used temporarily for a very specific algorithm, you should not pollute your business model class with such an (otherwise possibly useless) field. Imagine you need to run 10 other algorithms, and each of them requires one or more data items to be associated with a `Person` - would you place them all as public fields into the `Person` class? – Marco13 Sep 25 '14 at 08:43
  • got your point, thanks – user299791 Sep 25 '14 at 08:44

0 Answers0