[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(); |
---|
[36] | 51 | Iterator<V> vertexsIterator = vertexs.iterator(); |
---|
[27] | 52 | ArrayList<VertexScore<V>> vertexsScore = new ArrayList<VertexScore<V>>(); |
---|
| 53 | while(vertexsIterator.hasNext()){ |
---|
[36] | 54 | V vertex = vertexsIterator.next(); |
---|
[27] | 55 | Double score = (Double) rank.getVertexScore(vertex); |
---|
[36] | 56 | VertexScore<V> vertexscore = new VertexScore<V>(vertex,score.floatValue()); |
---|
[27] | 57 | vertexsScore.add(vertexscore); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | Collections.sort(vertexsScore,new VertexScoreComparator()); |
---|
| 61 | return vertexsScore; |
---|
| 62 | } |
---|
| 63 | |
---|
[36] | 64 | public List<V> clusterPageRankPriors(List<V> seed, double min_volume){ |
---|
[27] | 65 | PageRankWithPriors rank; |
---|
| 66 | if(seed != null){ |
---|
[36] | 67 | if(seed.size() == 1){ |
---|
| 68 | Transformer transf = new SeedTransformer(seed.get(0)); |
---|
| 69 | rank = new PageRankWithPriors(graph, transf, alpha); |
---|
| 70 | } |
---|
| 71 | else{ |
---|
| 72 | Transformer transf = new ListSeedTransformer(seed); |
---|
| 73 | rank = new PageRankWithPriors(graph, transf, alpha); |
---|
| 74 | } |
---|
| 75 | |
---|
[27] | 76 | } |
---|
| 77 | else{ |
---|
| 78 | rank = new PageRank(graph, alpha); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | rank.evaluate(); |
---|
| 82 | Collection<V> vertexs = graph.getVertices(); |
---|
| 83 | Iterator<?> vertexsIterator = vertexs.iterator(); |
---|
| 84 | ArrayList<VertexScore<V>> vertexsScore = new ArrayList<VertexScore<V>>(); |
---|
| 85 | while(vertexsIterator.hasNext()){ |
---|
| 86 | V vertex = (V) vertexsIterator.next(); |
---|
| 87 | Double score = (Double) rank.getVertexScore(vertex); |
---|
[36] | 88 | int degree = graph.inDegree(vertex); |
---|
| 89 | VertexScore<V> vertexscore = new VertexScore<V>(vertex,score.floatValue()/degree); |
---|
[27] | 90 | vertexsScore.add(vertexscore); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | Collections.sort(vertexsScore,new VertexScoreComparator()); |
---|
[32] | 94 | |
---|
| 95 | System.out.println("\n\nClustering with prior seed: "+seed.toString()); |
---|
[27] | 96 | |
---|
| 97 | int volume_graph = 2 * graph.getEdgeCount(); |
---|
| 98 | double max_volume = (volume_graph / 3) * 2; |
---|
| 99 | double min_conductance_subset=100; |
---|
| 100 | int min_conductance_index = -1; |
---|
[34] | 101 | int volume_cluster=0; |
---|
[27] | 102 | |
---|
| 103 | int subsets_count = vertexsScore.size(); |
---|
| 104 | for(int i=0; i<subsets_count; i++){ |
---|
| 105 | int volume_subset = 0; |
---|
| 106 | ArrayList<V> subset = new ArrayList<V>(); |
---|
| 107 | for(int j=0; j<i; j++){ |
---|
| 108 | subset.add(vertexsScore.get(j).getVertex()); |
---|
| 109 | } |
---|
| 110 | int edge_boundary = 0; |
---|
| 111 | for(int j=0; j<i; j++){ |
---|
[36] | 112 | volume_subset += graph.inDegree(vertexsScore.get(j).getVertex()); |
---|
[27] | 113 | for(E out_edge : graph.getOutEdges(vertexsScore.get(j).getVertex())){ |
---|
| 114 | V opposite = graph.getOpposite(vertexsScore.get(j).getVertex(), out_edge); |
---|
| 115 | if(!subset.contains(opposite)) |
---|
| 116 | edge_boundary++; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | if(volume_subset > 0){ |
---|
| 120 | double minvolume = volume_subset; |
---|
| 121 | if (volume_subset > (volume_graph - volume_subset)) |
---|
| 122 | minvolume = volume_graph - volume_subset; |
---|
| 123 | double conductance = edge_boundary / minvolume; |
---|
[34] | 124 | |
---|
[27] | 125 | if ((volume_subset > min_volume) && (volume_subset < max_volume)){ |
---|
| 126 | if (conductance < min_conductance_subset){ |
---|
| 127 | min_conductance_subset = conductance; |
---|
| 128 | min_conductance_index = i; |
---|
[34] | 129 | volume_cluster = volume_subset; |
---|
[27] | 130 | } |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | } |
---|
[32] | 135 | System.out.println("MIN CONDUCTANCE: "+min_conductance_subset); |
---|
[27] | 136 | |
---|
[34] | 137 | System.out.println("CLUSTER: ("+min_conductance_index+" elements, volume "+ volume_cluster +", volume graph "+ 2 * this.graph.getEdgeCount()+")"); |
---|
[27] | 138 | List<V> cluster = new ArrayList<V>(); |
---|
| 139 | for(int i=0; i< min_conductance_index; i++) |
---|
| 140 | cluster.add(vertexsScore.get(i).getVertex()); |
---|
[32] | 141 | |
---|
[34] | 142 | String node_list = ""; |
---|
[32] | 143 | for(V node : cluster) |
---|
[34] | 144 | node_list += node.toString() + ","; |
---|
| 145 | System.out.println(node_list); |
---|
[27] | 146 | |
---|
| 147 | return cluster; |
---|
| 148 | |
---|
| 149 | } |
---|
| 150 | |
---|
[36] | 151 | |
---|
[27] | 152 | } |
---|