[35] | 1 | |
---|
| 2 | package clustering; |
---|
| 3 | |
---|
| 4 | import data.GraphBuilder; |
---|
| 5 | import edu.uci.ics.jung.graph.DirectedSparseGraph; |
---|
| 6 | import edu.uci.ics.jung.graph.Graph; |
---|
| 7 | import edu.uci.ics.jung.graph.SparseGraph; |
---|
[36] | 8 | import edu.uci.ics.jung.graph.util.Pair; |
---|
[35] | 9 | import java.awt.event.ActionEvent; |
---|
| 10 | import java.awt.event.ActionListener; |
---|
[36] | 11 | import java.util.ArrayList; |
---|
[35] | 12 | import java.util.Collection; |
---|
| 13 | import java.util.List; |
---|
[36] | 14 | import java.util.Scanner; |
---|
[35] | 15 | import view.Viewer; |
---|
| 16 | |
---|
| 17 | public class GraphClusterer implements ActionListener{ |
---|
| 18 | |
---|
| 19 | private List<VertexScore<String>> global_ranking; |
---|
| 20 | private int currentElement; |
---|
| 21 | private Viewer viewGraph; |
---|
[36] | 22 | private Graph<String, Integer> graph; |
---|
| 23 | private boolean main_graph_output; |
---|
| 24 | private boolean cluster_graph_output; |
---|
[35] | 25 | private boolean isGraphDirected; |
---|
| 26 | private int minvolume; |
---|
[36] | 27 | LocalSpectral<String,String> clusterer; |
---|
[35] | 28 | |
---|
[36] | 29 | public GraphClusterer(String path, boolean isGraphDirected,int min_volume, boolean graph_output) { |
---|
[35] | 30 | global_ranking = null; |
---|
| 31 | this.currentElement = 0; |
---|
| 32 | GraphBuilder builder = new GraphBuilder(isGraphDirected); |
---|
[36] | 33 | builder.buildGraphFromCVS(path, 1); |
---|
[35] | 34 | graph = builder.getGraph(); |
---|
[36] | 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; |
---|
[35] | 41 | this.isGraphDirected = isGraphDirected; |
---|
| 42 | this.minvolume = min_volume; |
---|
[36] | 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); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | public void actionPerformed(ActionEvent ae) { |
---|
| 52 | this.visualNextCluster(); |
---|
| 53 | } |
---|
| 54 | |
---|
[36] | 55 | public void clusterize(boolean main_graph_output, boolean cluster_graph_output){ |
---|
[35] | 56 | |
---|
| 57 | global_ranking = clusterer.getGlobalRank(); |
---|
[36] | 58 | this.main_graph_output = main_graph_output; |
---|
| 59 | this.cluster_graph_output = cluster_graph_output; |
---|
[35] | 60 | |
---|
| 61 | System.out.println("GLOBAL RANKING"); |
---|
| 62 | for(VertexScore<String> v : global_ranking) |
---|
| 63 | System.out.println(v.toString()); |
---|
| 64 | |
---|
[36] | 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 | } |
---|
[35] | 73 | } |
---|
| 74 | |
---|
[36] | 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 | |
---|
| 98 | } |
---|
| 99 | |
---|
[35] | 100 | public void visualNextCluster(){ |
---|
| 101 | if(this.currentElement > global_ranking.size()-1) |
---|
| 102 | return; |
---|
[36] | 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); |
---|
[35] | 107 | Graph cut_graph; |
---|
| 108 | if (isGraphDirected) |
---|
| 109 | cut_graph = new DirectedSparseGraph<String, Edge<String>>(); |
---|
| 110 | else |
---|
| 111 | cut_graph = new SparseGraph<String, Edge<String>>(); |
---|
| 112 | for(String vertex : cut){ |
---|
[36] | 113 | Collection<Integer> out_edges = graph.getOutEdges(vertex); |
---|
| 114 | for(Integer edge : out_edges){ |
---|
[35] | 115 | String out_node = graph.getOpposite(vertex, edge); |
---|
| 116 | if (cut.contains(out_node)){ |
---|
[36] | 117 | Pair<String> edge_nodes = graph.getEndpoints(edge); |
---|
| 118 | cut_graph.addEdge(edge,edge_nodes.getFirst() ,edge_nodes.getSecond()); |
---|
[35] | 119 | } |
---|
| 120 | } |
---|
| 121 | } |
---|
[36] | 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 | } |
---|
[35] | 130 | |
---|
| 131 | this.currentElement++; |
---|
| 132 | |
---|
| 133 | } |
---|
| 134 | } |
---|