[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()); |
---|
| 87 | |
---|
| 88 | double sum=0; |
---|
| 89 | for(VertexScore<V> v : vertexsScore){ |
---|
| 90 | System.out.println(v.getVertex().toString()+ " " + v.getScore()); |
---|
| 91 | sum+=v.getScore(); |
---|
| 92 | } |
---|
| 93 | System.out.println("SUM SCORES: "+sum); |
---|
| 94 | |
---|
| 95 | int volume_graph = 2 * graph.getEdgeCount(); |
---|
| 96 | System.out.println("GRAPH VOLUME: " + volume_graph); |
---|
| 97 | double max_volume = (volume_graph / 3) * 2; |
---|
| 98 | double min_conductance_subset=100; |
---|
| 99 | int min_conductance_index = -1; |
---|
| 100 | |
---|
| 101 | int subsets_count = vertexsScore.size(); |
---|
| 102 | for(int i=0; i<subsets_count; i++){ |
---|
| 103 | System.out.println("i="+i); |
---|
| 104 | int volume_subset = 0; |
---|
| 105 | ArrayList<V> subset = new ArrayList<V>(); |
---|
| 106 | for(int j=0; j<i; j++){ |
---|
| 107 | subset.add(vertexsScore.get(j).getVertex()); |
---|
| 108 | } |
---|
| 109 | int edge_boundary = 0; |
---|
| 110 | for(int j=0; j<i; j++){ |
---|
| 111 | volume_subset += graph.getIncidentEdges(vertexsScore.get(j).getVertex()).size(); |
---|
| 112 | for(E out_edge : graph.getOutEdges(vertexsScore.get(j).getVertex())){ |
---|
| 113 | V opposite = graph.getOpposite(vertexsScore.get(j).getVertex(), out_edge); |
---|
| 114 | if(!subset.contains(opposite)) |
---|
| 115 | edge_boundary++; |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | if(volume_subset > 0){ |
---|
| 119 | double minvolume = volume_subset; |
---|
| 120 | if (volume_subset > (volume_graph - volume_subset)) |
---|
| 121 | minvolume = volume_graph - volume_subset; |
---|
| 122 | double conductance = edge_boundary / minvolume; |
---|
| 123 | if ((volume_subset > min_volume) && (volume_subset < max_volume)){ |
---|
| 124 | if (conductance < min_conductance_subset){ |
---|
| 125 | min_conductance_subset = conductance; |
---|
| 126 | min_conductance_index = i; |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | System.out.println("CONDUCTANCE: "+conductance + " minvolume: "+minvolume + " edge_boundary: "+edge_boundary); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | } |
---|
| 133 | System.out.println("MIN CONDUCTANCE: "+min_conductance_subset + " INDEX "+min_conductance_index ); |
---|
| 134 | |
---|
| 135 | List<V> cluster = new ArrayList<V>(); |
---|
| 136 | for(int i=0; i< min_conductance_index; i++) |
---|
| 137 | cluster.add(vertexsScore.get(i).getVertex()); |
---|
| 138 | |
---|
| 139 | return cluster; |
---|
| 140 | |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | } |
---|