// Calculate the contribution of current vertex's shortest-path graph to final edge betweenness // (Newman algorithm) // M. E. J. Newman and M. Girvan, Finding and evaluating community structure in networks, Physical // Review E69, 026113 (2004) private SimpleWeightedGraph<String, DefaultWeightedEdge> NewmanAlgorithm( Multigraph<String, DefaultEdge> shortestPathGraph, String thisVertex) { if (shortestPathGraph.edgeSet().size() == 0) // No outer degree return null; // 1. Initialize the edge betweenness digraph SimpleWeightedGraph<String, DefaultWeightedEdge> edgeBetweennessDigraph = new SimpleWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class); // Add vertices for (String curVertex : shortestPathGraph.vertexSet()) edgeBetweennessDigraph.addVertex(curVertex); // 2. Deal with corresponding edges // 2.1. Calculate vertex distance and weight // 2.1.1. Initialize related variables HashMap<String, Integer> mapVertexDistance = new HashMap<String, Integer>(); // Vertex distance for (String curVertex : shortestPathGraph.vertexSet()) mapVertexDistance.put(curVertex, 0); HashMap<String, Double> mapVertexWeight = new HashMap<String, Double>(); // Vertex weight for (String curVertex : shortestPathGraph.vertexSet()) mapVertexWeight.put(curVertex, 1.0); Queue<String> verticesQueue = new LinkedList<String>(); // Store the queue of operated vertices // 2.1.2. Traverse the shortest-path digraph, get corresponding vertex distance and weight verticesQueue.add(thisVertex); String currentVertex = thisVertex; while (!verticesQueue.isEmpty()) { // 2.1.2.1. Read the bottom vertex in the queue, deal with all adjacent edges for (DefaultEdge curEdge : shortestPathGraph.edgesOf(currentVertex)) { String targetVertex = shortestPathGraph.getEdgeTarget(curEdge); if (mapVertexDistance.get(targetVertex) == 0) { // Target vertex value hasn't been evaluated yet mapVertexDistance.put(targetVertex, mapVertexDistance.get(currentVertex) + 1); mapVertexWeight.put(targetVertex, mapVertexWeight.get(currentVertex)); verticesQueue.add(targetVertex); } // Target vertex value has already been set to source vertex distance plus 1 else if (mapVertexDistance.get(targetVertex) == (mapVertexDistance.get(currentVertex) + 1)) mapVertexWeight.put( targetVertex, mapVertexWeight.get(targetVertex) + mapVertexWeight.get(currentVertex)); } // 2.1.2.2. All adjacent edges have been treated, remove the bottom element verticesQueue.remove(); currentVertex = verticesQueue.peek(); } // 2.2. Calculate the contribution of every edges to the edge betweenness // 2.2.1. Sort the vertices descendingly by the distance to the first vertex List<Map.Entry<String, Integer>> listVertexDistance = new ArrayList<Map.Entry<String, Integer>>(mapVertexDistance.entrySet()); Collections.sort( listVertexDistance, new Comparator<Map.Entry<String, Integer>>() { // Sorting the list descendingly @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { return (o2.getValue().compareTo(o1.getValue())); } }); // 2.2.2. Traverse the shortest-path digraph with the sorted vertices list for (Map.Entry<String, Integer> thisVertexDistance : listVertexDistance) { currentVertex = thisVertexDistance.getKey(); for (DefaultEdge curEdge : shortestPathGraph.edgesOf(currentVertex)) { // 2.2.2.1. Get the source vertex String sourceVertex = shortestPathGraph.getEdgeSource(curEdge); String targetVertex = shortestPathGraph.getEdgeTarget(curEdge); String anotherVertex = ""; if (currentVertex.equals(sourceVertex)) anotherVertex = targetVertex; else if (currentVertex.equals(targetVertex)) anotherVertex = sourceVertex; // 2.2.2.2. Calculate the value of the sum of all adjacent edges plus 1 double totalSubWeights = 1.0; for (DefaultWeightedEdge curSubEdge : edgeBetweennessDigraph.edgesOf(currentVertex)) totalSubWeights += edgeBetweennessDigraph.getEdgeWeight(curSubEdge); // 2.2.2.3. The new edge weight is calculated as totalSubWeights * (the quotient of the // weights of the two terminals) double wi = mapVertexWeight.get(anotherVertex); double wj = mapVertexWeight.get(currentVertex); totalSubWeights /= wi / wj; // 2.2.2.4. Add new edge with correct weight into the edge betweenness digraph DefaultWeightedEdge newEdge = new DefaultWeightedEdge(); edgeBetweennessDigraph.addEdge(anotherVertex, currentVertex, newEdge); edgeBetweennessDigraph.setEdgeWeight(newEdge, totalSubWeights); } } return edgeBetweennessDigraph; }
public Set<ApplicationVertex> vertexSet() { return graph.vertexSet(); }