[27] | 1 | |
---|
| 2 | package clustering; |
---|
| 3 | |
---|
| 4 | import edu.uci.ics.jung.algorithms.scoring.PageRank; |
---|
| 5 | import edu.uci.ics.jung.algorithms.scoring.PageRankWithPriors; |
---|
| 6 | import edu.uci.ics.jung.graph.Graph; |
---|
| 7 | import java.util.ArrayList; |
---|
| 8 | import java.util.Collection; |
---|
| 9 | import java.util.Collections; |
---|
| 10 | import java.util.Iterator; |
---|
| 11 | import java.util.List; |
---|
| 12 | import org.apache.commons.collections15.Transformer; |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | public class LocalSpectral<V,E> { |
---|
| 16 | |
---|
| 17 | Graph<V,E> graph; |
---|
| 18 | |
---|
| 19 | double alpha = 0.15; |
---|
| 20 | |
---|
| 21 | public Graph<V, E> getGraph() { |
---|
| 22 | return graph; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | public void setGraph(Graph<V, E> graph) { |
---|
| 26 | this.graph = graph; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | public LocalSpectral(Graph<V,E> input){ |
---|
| 30 | graph = input; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | public void removeCutEdges(List<V> cut){ |
---|
| 34 | for(V vertex : cut){ |
---|
| 35 | Collection<E> out_edges = graph.getOutEdges(vertex); |
---|
| 36 | for(E edge : out_edges){ |
---|
| 37 | V opposite_vertex = graph.getOpposite(vertex, edge); |
---|
| 38 | if (!cut.contains(opposite_vertex)){ |
---|
| 39 | graph.removeEdge(edge); |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | public List<VertexScore<V>> getGlobalRank(){ |
---|
| 47 | PageRankWithPriors rank; |
---|
| 48 | rank = new PageRank(graph, alpha); |
---|
| 49 | rank.evaluate(); |
---|
| 50 | Collection<V> vertexs = graph.getVertices(); |
---|
| 51 | Iterator<?> vertexsIterator = vertexs.iterator(); |
---|
| 52 | ArrayList<VertexScore<V>> vertexsScore = new ArrayList<VertexScore<V>>(); |
---|
| 53 | while(vertexsIterator.hasNext()){ |
---|
| 54 | V vertex = (V) vertexsIterator.next(); |
---|
| 55 | Double score = (Double) rank.getVertexScore(vertex); |
---|
| 56 | VertexScore<V> vertexscore = new VertexScore<V>(vertex,score); |
---|
| 57 | vertexsScore.add(vertexscore); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | Collections.sort(vertexsScore,new VertexScoreComparator()); |
---|
| 61 | return vertexsScore; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | public List<V> clusterPageRankPriors(V seed, double min_volume){ |
---|
| 65 | PageRankWithPriors rank; |
---|
| 66 | if(seed != null){ |
---|
| 67 | Transformer transf = new SeedTransformer(seed); |
---|
| 68 | rank = new PageRankWithPriors(graph, transf, alpha); |
---|
| 69 | } |
---|
| 70 | else{ |
---|
| 71 | rank = new PageRank(graph, alpha); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | rank.evaluate(); |
---|
| 75 | Collection<V> vertexs = graph.getVertices(); |
---|
| 76 | Iterator<?> vertexsIterator = vertexs.iterator(); |
---|
| 77 | ArrayList<VertexScore<V>> vertexsScore = new ArrayList<VertexScore<V>>(); |
---|
| 78 | while(vertexsIterator.hasNext()){ |
---|
| 79 | V vertex = (V) vertexsIterator.next(); |
---|
| 80 | Double score = (Double) rank.getVertexScore(vertex); |
---|
| 81 | int degree = graph.getIncidentEdges(vertex).size(); |
---|
| 82 | VertexScore<V> vertexscore = new VertexScore<V>(vertex,score/degree); |
---|
| 83 | vertexsScore.add(vertexscore); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | Collections.sort(vertexsScore,new VertexScoreComparator()); |
---|
[32] | 87 | |
---|
| 88 | System.out.println("\n\nClustering with prior seed: "+seed.toString()); |
---|
[27] | 89 | |
---|
| 90 | int volume_graph = 2 * graph.getEdgeCount(); |
---|
| 91 | double max_volume = (volume_graph / 3) * 2; |
---|
| 92 | double min_conductance_subset=100; |
---|
| 93 | int min_conductance_index = -1; |
---|
| 94 | |
---|
| 95 | int subsets_count = vertexsScore.size(); |
---|
| 96 | for(int i=0; i<subsets_count; i++){ |
---|
| 97 | int volume_subset = 0; |
---|
| 98 | ArrayList<V> subset = new ArrayList<V>(); |
---|
| 99 | for(int j=0; j<i; j++){ |
---|
| 100 | subset.add(vertexsScore.get(j).getVertex()); |
---|
| 101 | } |
---|
| 102 | int edge_boundary = 0; |
---|
| 103 | for(int j=0; j<i; j++){ |
---|
| 104 | volume_subset += graph.getIncidentEdges(vertexsScore.get(j).getVertex()).size(); |
---|
| 105 | for(E out_edge : graph.getOutEdges(vertexsScore.get(j).getVertex())){ |
---|
| 106 | V opposite = graph.getOpposite(vertexsScore.get(j).getVertex(), out_edge); |
---|
| 107 | if(!subset.contains(opposite)) |
---|
| 108 | edge_boundary++; |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | if(volume_subset > 0){ |
---|
| 112 | double minvolume = volume_subset; |
---|
| 113 | if (volume_subset > (volume_graph - volume_subset)) |
---|
| 114 | minvolume = volume_graph - volume_subset; |
---|
| 115 | double conductance = edge_boundary / minvolume; |
---|
| 116 | if ((volume_subset > min_volume) && (volume_subset < max_volume)){ |
---|
| 117 | if (conductance < min_conductance_subset){ |
---|
| 118 | min_conductance_subset = conductance; |
---|
| 119 | min_conductance_index = i; |
---|
| 120 | } |
---|
| 121 | } |
---|
[32] | 122 | //System.out.println("CONDUCTANCE: "+conductance + " minvolume: "+minvolume + " edge_boundary: "+edge_boundary); |
---|
[27] | 123 | } |
---|
| 124 | |
---|
| 125 | } |
---|
[32] | 126 | System.out.println("MIN CONDUCTANCE: "+min_conductance_subset); |
---|
[27] | 127 | |
---|
[32] | 128 | System.out.println("CLUSTER: "); |
---|
[27] | 129 | List<V> cluster = new ArrayList<V>(); |
---|
| 130 | for(int i=0; i< min_conductance_index; i++) |
---|
| 131 | cluster.add(vertexsScore.get(i).getVertex()); |
---|
[32] | 132 | |
---|
| 133 | for(V node : cluster) |
---|
| 134 | System.out.println(node.toString()); |
---|
[27] | 135 | |
---|
| 136 | return cluster; |
---|
| 137 | |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | } |
---|