Changeset 11 for src/main/java/weka/clusterers/forMetisMQI/MQI.java
- Timestamp:
- Sep 16, 2010, 10:44:40 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/weka/clusterers/forMetisMQI/MQI.java
r10 r11 2 2 3 3 import java.awt.Dimension; 4 import java.util.HashMap; 4 5 import java.util.Iterator; 6 import java.util.Map; 7 import java.util.Set; 5 8 6 9 import javax.swing.JFrame; 7 10 11 import org.apache.commons.collections15.Factory; 12 import org.apache.commons.collections15.Transformer; 13 14 import weka.clusterers.forMetisMQI.graph.Bisection; 15 import weka.clusterers.forMetisMQI.graph.Edge; 16 import weka.clusterers.forMetisMQI.graph.Node; 17 import weka.clusterers.forMetisMQI.graph.Subgraph; 18 import weka.clusterers.forMetisMQI.graph.UndirectedGraph; 19 import edu.uci.ics.jung.algorithms.flows.EdmondsKarpMaxFlow; 8 20 import edu.uci.ics.jung.algorithms.layout.KKLayout; 9 21 import edu.uci.ics.jung.algorithms.layout.Layout; 22 import edu.uci.ics.jung.graph.DirectedGraph; 10 23 import edu.uci.ics.jung.graph.DirectedSparseGraph; 11 24 import edu.uci.ics.jung.graph.Graph; … … 15 28 public class MQI { 16 29 30 static int i = -1; 31 17 32 public static void viewGraph(Graph<Node, Edge> g){ 18 33 Layout<Node, Edge> layout = new KKLayout<Node, Edge>(g); 19 layout.setSize(new Dimension( 300,300)); // sets the initial size of the space34 layout.setSize(new Dimension(800,600)); // sets the initial size of the space 20 35 // The BasicVisualizationServer<V,E> is parameterized by the edge types 21 36 BasicVisualizationServer<Node,Edge> vv = 22 37 new BasicVisualizationServer<Node,Edge>(layout); 23 vv.setPreferredSize(new Dimension( 350,350)); //Sets the viewing area size38 vv.setPreferredSize(new Dimension(800,600)); //Sets the viewing area size 24 39 vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>()); 25 40 vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Edge>()); … … 31 46 frame.setVisible(true); 32 47 } 48 49 /** 50 * Given a bisection, returns the cardinality of the larger subgraph. 51 * @param b 52 * @return 53 */ 54 static private int getMaxCardinality(Bisection b) { 55 Subgraph A = null; 56 if(b.getSubgraph().getVertexCount() > b.getComplement().getVertexCount()) { 57 A = b.getSubgraph(); 58 } 59 else { 60 A = b.getComplement(); 61 } 62 return A.getVertexCount(); 63 } 33 64 34 static p ublic void start(KLPartition partition) {65 static private DirectedGraph<Node, Edge> prepareDirectedGraph(Bisection partition, Node source, Node sink) { 35 66 Subgraph A = null; 36 67 Subgraph B = null; … … 44 75 } 45 76 int a = A.getVertexCount(); 46 int c = partition.edgeCut() ;77 int c = partition.edgeCut() / 2; 47 78 48 79 49 Graph<Node,Edge> g = new DirectedSparseGraph<Node, Edge>();80 DirectedGraph<Node,Edge> g = new DirectedSparseGraph<Node, Edge>(); 50 81 Iterator<Node> nodes = A.iterator(); 51 82 while(nodes.hasNext()) { … … 66 97 } 67 98 68 Node source = new Node("S");69 Node sink = new Node("T");70 99 g.addVertex(source); 71 100 g.addVertex(sink); … … 91 120 id++; 92 121 } 93 94 // viewGraph(g); 95 System.out.println(g.toString()); 96 122 return g; 123 } 124 125 static public Bisection mqi(Bisection partition) { 126 boolean finished = false; 127 Bisection bisection = partition; 128 int maxFlowThreshold = Integer.MAX_VALUE; 129 while (!finished) { 130 UndirectedGraph startingGraph = partition.getGraph(); 131 Node source = new Node("S"); 132 Node sink = new Node("T"); 133 DirectedGraph<Node, Edge> g = prepareDirectedGraph(bisection, source, sink); 134 Transformer<Edge, Number> capTransformer = new Transformer<Edge, Number>() { 135 public Double transform(Edge e) { 136 return (double) e.getCapacity(); 137 } 138 }; 139 Map<Edge, Number> edgeFlowMap = new HashMap<Edge, Number>(); 140 // This Factory produces new edges for use by the algorithm 141 i=-1; 142 Factory<Edge> edgeFactory = new Factory<Edge>() { 143 public Edge create() { 144 i++; 145 return new Edge(Integer.toString(i), 1, 1); 146 } 147 }; 148 EdmondsKarpMaxFlow<Node, Edge> alg = new EdmondsKarpMaxFlow<Node, Edge>( 149 g, source, sink, capTransformer, edgeFlowMap, edgeFactory); 150 151 maxFlowThreshold = getMaxCardinality(bisection) * bisection.edgeCut() / 2; 152 alg.evaluate(); 153 if(alg.getMaxFlow() < maxFlowThreshold) { 154 Set<Node> sinkPartition = alg.getNodesInSinkPartition(); 155 Set<Node> sourcePartition = alg.getNodesInSourcePartition(); 156 bisection = prepareBisection(startingGraph, sourcePartition, sinkPartition); 157 } else 158 finished = true; 159 } 160 return bisection; 161 } 162 163 private static Bisection prepareBisection(UndirectedGraph g, Set<Node> sourcePartition, Set<Node> sinkPartition) { 164 Bisection b = null; 165 Subgraph sourceSubgraph = new Subgraph(g, sourcePartition); 166 Subgraph sinkSubgraph = new Subgraph(g, sinkPartition); 167 Subgraph subgraph = null; 168 if(sourceSubgraph.getVertexCount() > sinkSubgraph.getVertexCount()) 169 subgraph =sourceSubgraph; 170 else 171 subgraph = sinkSubgraph; 172 b = new Bisection(subgraph); 173 return b; 97 174 } 98 175
Note: See TracChangeset
for help on using the changeset viewer.