Changeset 36 for branches/localSpectral/src/clustering
- Timestamp:
- Jan 6, 2011, 9:41:42 AM (14 years ago)
- Location:
- branches/localSpectral/src/clustering
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/localSpectral/src/clustering/GraphClusterer.java
r35 r36 6 6 import edu.uci.ics.jung.graph.Graph; 7 7 import edu.uci.ics.jung.graph.SparseGraph; 8 import edu.uci.ics.jung.graph.util.Pair; 8 9 import java.awt.event.ActionEvent; 9 10 import java.awt.event.ActionListener; 11 import java.util.ArrayList; 10 12 import java.util.Collection; 11 13 import java.util.List; 14 import java.util.Scanner; 12 15 import view.Viewer; 13 16 … … 17 20 private int currentElement; 18 21 private Viewer viewGraph; 19 private Graph<String, Edge<String>> graph; 20 private boolean graph_output; 22 private Graph<String, Integer> graph; 23 private boolean main_graph_output; 24 private boolean cluster_graph_output; 21 25 private boolean isGraphDirected; 22 26 private int minvolume; 23 LocalSpectral<String, Edge<String>> clusterer;27 LocalSpectral<String,String> clusterer; 24 28 25 public GraphClusterer(String path, boolean isGraphDirected,int min_volume ) {29 public GraphClusterer(String path, boolean isGraphDirected,int min_volume, boolean graph_output) { 26 30 global_ranking = null; 27 31 this.currentElement = 0; 28 32 GraphBuilder builder = new GraphBuilder(isGraphDirected); 29 builder.buildGraphFrom ARFF(path, 1000000);33 builder.buildGraphFromCVS(path, 1); 30 34 graph = builder.getGraph(); 31 viewGraph = new Viewer(graph, this); 32 graph_output = true; 35 if(graph_output){ 36 viewGraph = new Viewer(graph, this); 37 this.main_graph_output = true; 38 } 39 else 40 this.main_graph_output = false; 33 41 this.isGraphDirected = isGraphDirected; 34 42 this.minvolume = min_volume; 43 System.out.println("Graph data: nodes "+graph.getVertexCount()+" edges "+graph.getEdgeCount()); 44 System.out.flush(); 45 //System.out.println("What is the min cluster volume? "); 46 //Scanner in = new Scanner(System.in); 47 //this.minvolume = in.nextInt(); 35 48 this.clusterer = new LocalSpectral(graph); 36 49 } 37 50 38 51 public void actionPerformed(ActionEvent ae) { 39 System.out.println("Clicked!");40 52 this.visualNextCluster(); 41 53 } 42 54 43 public void clusterize(boolean graph_output){55 public void clusterize(boolean main_graph_output, boolean cluster_graph_output){ 44 56 45 57 global_ranking = clusterer.getGlobalRank(); 46 this.graph_output = graph_output; 58 this.main_graph_output = main_graph_output; 59 this.cluster_graph_output = cluster_graph_output; 47 60 48 61 System.out.println("GLOBAL RANKING"); … … 50 63 System.out.println(v.toString()); 51 64 52 if (graph_output) 53 viewGraph.viewGraphRank(global_ranking,null); 65 if (main_graph_output){ 66 viewGraph.setText("Nodes: "+graph.getVertexCount() + "\nEdges: "+graph.getEdgeCount()); 67 viewGraph.viewGraphRank(global_ranking,null); 68 } 69 else{ 70 for(int i=0; i< this.global_ranking.size(); i++) 71 visualNextCluster(); 72 } 73 } 74 75 public float getConductance(Graph<String,Integer> graph, List<String> cluster){ 76 int volume_graph = 2 * graph.getEdgeCount(); 77 int volume_cluster = 0; 78 int edge_boundary = 0; 79 for(String vertex : cluster){ 80 volume_cluster += graph.getInEdges(vertex).size(); 81 for(Integer e : graph.getOutEdges(vertex)){ 82 String opposite = graph.getOpposite(vertex, e); 83 if(!cluster.contains(opposite)) 84 edge_boundary++; 85 } 86 87 } 88 if(volume_cluster > 0){ 89 double volume = volume_cluster; 90 if (volume_cluster > (volume_graph - volume_cluster)) 91 volume = volume_graph - volume_cluster; 92 double conductance = edge_boundary / volume; 93 return (float) conductance; 94 } 95 else 96 return 0; 97 54 98 } 55 99 … … 57 101 if(this.currentElement > global_ranking.size()-1) 58 102 return; 59 60 List<String> cut = clusterer.clusterPageRankPriors(global_ranking.get(currentElement).getVertex(),this.minvolume); 103 104 List<String> seed = new ArrayList<String>(); 105 seed.add(global_ranking.get(currentElement).getVertex()); 106 List<String> cut = clusterer.clusterPageRankPriors(seed,this.minvolume); 61 107 Graph cut_graph; 62 108 if (isGraphDirected) … … 65 111 cut_graph = new SparseGraph<String, Edge<String>>(); 66 112 for(String vertex : cut){ 67 Collection< Edge<String>> out_edges = graph.getOutEdges(vertex);68 for( Edge<String>edge : out_edges){113 Collection<Integer> out_edges = graph.getOutEdges(vertex); 114 for(Integer edge : out_edges){ 69 115 String out_node = graph.getOpposite(vertex, edge); 70 116 if (cut.contains(out_node)){ 71 cut_graph.addEdge(edge, edge.getVertex1(),edge.getVertex2()); 117 Pair<String> edge_nodes = graph.getEndpoints(edge); 118 cut_graph.addEdge(edge,edge_nodes.getFirst() ,edge_nodes.getSecond()); 72 119 } 73 120 } 74 121 } 75 viewGraph.setGraph(cut_graph); 76 float vertex_rank = (float) global_ranking.get(this.currentElement).getScore(); 77 viewGraph.setText("Page Rank Value: "+vertex_rank); 78 if (graph_output) 79 viewGraph.viewGraphRank(global_ranking, global_ranking.get(this.currentElement).getVertex()); 122 if (cluster_graph_output){ 123 viewGraph.setGraph(cut_graph); 124 float vertex_rank = (float) global_ranking.get(this.currentElement).getScore(); 125 viewGraph.setText("Nodes: "+cut_graph.getVertexCount() + "\nEdges: "+cut_graph.getEdgeCount() 126 + "\nPage Rank Value: "+vertex_rank + "\nConductance: "+this.getConductance(graph, cut) + "\nSeed Element: "+global_ranking.get(this.currentElement).getVertex()); 127 //viewGraph.viewClusterRankedInGraph(global_ranking, global_ranking.get(this.currentElement).getVertex(),cut); 128 viewGraph.viewGraphRank(global_ranking, global_ranking.get(this.currentElement).getVertex()); 129 } 80 130 81 131 this.currentElement++; -
branches/localSpectral/src/clustering/LocalSpectral.java
r34 r36 49 49 rank.evaluate(); 50 50 Collection<V> vertexs = graph.getVertices(); 51 Iterator< ?> vertexsIterator = vertexs.iterator();51 Iterator<V> vertexsIterator = vertexs.iterator(); 52 52 ArrayList<VertexScore<V>> vertexsScore = new ArrayList<VertexScore<V>>(); 53 53 while(vertexsIterator.hasNext()){ 54 V vertex = (V)vertexsIterator.next();54 V vertex = vertexsIterator.next(); 55 55 Double score = (Double) rank.getVertexScore(vertex); 56 VertexScore<V> vertexscore = new VertexScore<V>(vertex,score );56 VertexScore<V> vertexscore = new VertexScore<V>(vertex,score.floatValue()); 57 57 vertexsScore.add(vertexscore); 58 58 } … … 62 62 } 63 63 64 public List<V> clusterPageRankPriors( Vseed, double min_volume){64 public List<V> clusterPageRankPriors(List<V> seed, double min_volume){ 65 65 PageRankWithPriors rank; 66 66 if(seed != null){ 67 Transformer transf = new SeedTransformer(seed); 68 rank = new PageRankWithPriors(graph, transf, alpha); 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 69 76 } 70 77 else{ … … 79 86 V vertex = (V) vertexsIterator.next(); 80 87 Double score = (Double) rank.getVertexScore(vertex); 81 int degree = graph. getIncidentEdges(vertex).size();82 VertexScore<V> vertexscore = new VertexScore<V>(vertex,score /degree);88 int degree = graph.inDegree(vertex); 89 VertexScore<V> vertexscore = new VertexScore<V>(vertex,score.floatValue()/degree); 83 90 vertexsScore.add(vertexscore); 84 91 } … … 103 110 int edge_boundary = 0; 104 111 for(int j=0; j<i; j++){ 105 volume_subset += graph. getIncidentEdges(vertexsScore.get(j).getVertex()).size();112 volume_subset += graph.inDegree(vertexsScore.get(j).getVertex()); 106 113 for(E out_edge : graph.getOutEdges(vertexsScore.get(j).getVertex())){ 107 114 V opposite = graph.getOpposite(vertexsScore.get(j).getVertex(), out_edge); … … 117 124 118 125 if ((volume_subset > min_volume) && (volume_subset < max_volume)){ 119 //if (volume_subset < min_volume){120 126 if (conductance < min_conductance_subset){ 121 127 min_conductance_subset = conductance; … … 143 149 } 144 150 151 145 152 } -
branches/localSpectral/src/clustering/VertexScore.java
r27 r36 5 5 6 6 private V vertex; 7 private doublescore;7 private float score; 8 8 9 public VertexScore(V vertex, doublescore) {9 public VertexScore(V vertex, float score) { 10 10 this.vertex = vertex; 11 11 this.score = score; … … 16 16 } 17 17 18 public void setScore( doublescore) {18 public void setScore(float score) { 19 19 this.score = score; 20 20 }
Note: See TracChangeset
for help on using the changeset viewer.