/** * Finds the set of clusters which have the strongest "community structure". The more edges * removed the smaller and more cohesive the clusters. * * @param graph the graph */ public Set<Set<V>> transform(Graph<V, E> graph) { if (mNumEdgesToRemove < 0 || mNumEdgesToRemove > graph.getEdgeCount()) { throw new IllegalArgumentException("Invalid number of edges passed in."); } edges_removed.clear(); for (int k = 0; k < mNumEdgesToRemove; k++) { BetweennessCentrality<V, E> bc = new BetweennessCentrality<V, E>(graph); E to_remove = null; double score = 0; for (E e : graph.getEdges()) { Integer weight = mWeight.get(e); if (bc.getEdgeScore(e) / weight > score) { to_remove = e; score = bc.getEdgeScore(e) / weight; } } edges_removed.put(to_remove, graph.getEndpoints(to_remove)); graph.removeEdge(to_remove); } WeakComponentClusterer<V, E> wcSearch = new WeakComponentClusterer<V, E>(); Set<Set<V>> clusterSet = wcSearch.transform(graph); for (Map.Entry<E, Pair<V>> entry : edges_removed.entrySet()) { Pair<V> endpoints = entry.getValue(); graph.addEdge(entry.getKey(), endpoints.getFirst(), endpoints.getSecond()); } return clusterSet; }
private void addEdges() { // remove old edges if available Iterator<String> e = edgeLabelMap.keySet().iterator(); while (e.hasNext()) { graph.removeEdge(e.next()); } edgeLabelMap.clear(); boolean isDistance = measure.isDistance(); Attribute id = exampleSet.getAttributes().getId(); List<SortableEdge> sortableEdges = new LinkedList<SortableEdge>(); for (int i = 0; i < exampleSet.size(); i++) { Example example = exampleSet.getExample(i); for (int j = i + 1; j < exampleSet.size(); j++) { Example comExample = exampleSet.getExample(j); if (isDistance) sortableEdges.add( new SortableEdge( example.getValueAsString(id), comExample.getValueAsString(id), null, measure.calculateDistance(example, comExample), SortableEdge.DIRECTION_INCREASE)); else sortableEdges.add( new SortableEdge( example.getValueAsString(id), comExample.getValueAsString(id), null, measure.calculateSimilarity(example, comExample), SortableEdge.DIRECTION_DECREASE)); } } Collections.sort(sortableEdges); int numberOfEdges = distanceSlider.getValue(); int counter = 0; double minStrength = Double.POSITIVE_INFINITY; double maxStrength = Double.NEGATIVE_INFINITY; Map<String, Double> strengthMap = new HashMap<String, Double>(); for (SortableEdge sortableEdge : sortableEdges) { if (counter > numberOfEdges) break; String idString = edgeFactory.create(); graph.addEdge( idString, sortableEdge.getFirstVertex(), sortableEdge.getSecondVertex(), EdgeType.UNDIRECTED); edgeLabelMap.put(idString, Tools.formatIntegerIfPossible(sortableEdge.getEdgeValue())); double strength = sortableEdge.getEdgeValue(); minStrength = Math.min(minStrength, strength); maxStrength = Math.max(maxStrength, strength); strengthMap.put(idString, strength); counter++; } for (Entry<String, Double> entry : strengthMap.entrySet()) { edgeStrengthMap.put( entry.getKey(), (entry.getValue() - minStrength) / (maxStrength - minStrength)); } }