Skip to content

Instantly share code, notes, and snippets.

@dsp
Created January 26, 2010 06:29
Show Gist options
  • Save dsp/286604 to your computer and use it in GitHub Desktop.
Save dsp/286604 to your computer and use it in GitHub Desktop.
public class OutdegreeCentrality extends CentralityImpl {
/**
* Return the Type of centrality.
*
* @return The type.
*/
public Type getType() {
return Type.NodeCentrality;
}
/**
* @see CentralityImpl#getWeight(Graph g)
* @param g The graph
* @return The mapping
*/
public HashMap<Node, Double> getWeight(final Graph g) {
if (g == null) {
throw new IllegalArgumentException(
"Graph passed to Outdegree is null.");
}
HashMap<Node, Double> values = new HashMap<Node, Double>();
//add all nodes to the list and set their degree to 0.0:
for (Node node : g.getNodeList().values()) {
values.put(node, 0.0);
}
//increase the degree of each node by one for each outgoing edge:
for (Edge edge : g.getEdgeList()) {
double degree = values.get(edge.getSourceNode());
values.put(edge.getSourceNode(),
(degree + 1));
}
return values;
}
/**
* @see Centrality#getRequiredAPIVersion()
* @return The required api version
*/
public int getRequiredAPIVersion() {
return 0;
}
/**
* @see Centrality#getVersion()
* @return The version
*/
public int getVersion() {
return 1;
}
/**
* @see Centrality#getName()
* @return The name
*/
public String getName() {
return "Outdegree";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment