Changeset 15 for src/main/java/weka/clusterers/forMetisMQI/MQI.java
- Timestamp:
- Sep 17, 2010, 6:04:18 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/weka/clusterers/forMetisMQI/MQI.java
r14 r15 1 1 package weka.clusterers.forMetisMQI; 2 2 3 import java.awt.Color;4 import java.awt.Dimension;5 import java.awt.Paint;6 3 import java.util.Collection; 7 4 import java.util.HashMap; … … 12 9 import java.util.Stack; 13 10 14 import javax.swing.JFrame;15 16 11 import org.apache.commons.collections15.Factory; 17 12 import org.apache.commons.collections15.Transformer; … … 22 17 import weka.clusterers.forMetisMQI.graph.Subgraph; 23 18 import weka.clusterers.forMetisMQI.graph.UndirectedGraph; 19 import weka.clusterers.forMetisMQI.util.Util; 24 20 import edu.uci.ics.jung.algorithms.flows.EdmondsKarpMaxFlow; 25 import edu.uci.ics.jung.algorithms.layout.FRLayout;26 import edu.uci.ics.jung.algorithms.layout.KKLayout;27 import edu.uci.ics.jung.algorithms.layout.Layout;28 21 import edu.uci.ics.jung.graph.DirectedGraph; 29 22 import edu.uci.ics.jung.graph.DirectedSparseGraph; 30 import edu.uci.ics.jung.graph.Graph;31 import edu.uci.ics.jung.visualization.BasicVisualizationServer;32 import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;33 23 34 24 public class MQI { 35 25 36 26 static int i = -1; 27 28 static private Set<Node> reversedLookup(Node sink, DirectedGraph<Node, Edge> g, Map<Edge, Number> edgeFlowMap) { 29 Set<Node> result = new HashSet<Node>(); 30 Collection<Edge> inEdges = g.getInEdges(sink); 31 Iterator<Edge> inEdgesIterator = inEdges.iterator(); 32 while (inEdgesIterator.hasNext()) { 33 Edge edge = inEdgesIterator.next(); 34 Node src = g.getSource(edge); 35 Edge reverseEdge = g.findEdge(src, sink); 36 if ((reverseEdge != null) 37 && ((Integer) edgeFlowMap.get(reverseEdge) < reverseEdge 38 .getCapacity())) { 39 result.add(src); 40 } 41 } 42 return result; 43 } 44 45 static private Set<Node> DFSReversed(Node sink, DirectedGraph<Node, Edge> g, Map<Edge, Number> edgeFlowMap) { 46 Stack<Node> stack = new Stack<Node>(); 47 stack.push(sink); 48 Set<Node> result = new HashSet<Node>(); 49 Set<Node> discardedNodes = new HashSet<Node>(); 50 result.add(sink); 51 while (!stack.empty()) { 52 boolean founded = false; 53 Node currentNode = stack.pop(); 54 discardedNodes.add(currentNode); 55 Collection<Edge> inEdges = g.getInEdges(currentNode); 56 Iterator<Edge> inEdgesIterator = inEdges.iterator(); 57 while (inEdgesIterator.hasNext()) { 58 Edge edge = inEdgesIterator.next(); 59 Node src = g.getSource(edge); 60 Edge reverseEdge = g.findEdge(src, currentNode); 61 if ((reverseEdge != null) && !discardedNodes.contains(src) 62 && ((Integer) edgeFlowMap.get(reverseEdge) < reverseEdge 63 .getCapacity()) && !founded) { 64 founded=true; 65 stack.push(src); 66 result.add(src); 67 } 68 discardedNodes.add(src); 69 } 70 } 71 return result; 72 } 37 73 38 74 static private Set<Node> BFSReversed(Node sink, DirectedGraph<Node, Edge> g, Map<Edge, Number> edgeFlowMap) { … … 132 168 boolean finished = false; 133 169 Bisection bisection = partition; 134 Set<Node> cluster = new HashSet<Node>(partition.getS ubgraph().createInducedSubgraph().getVertices());170 Set<Node> cluster = new HashSet<Node>(partition.getSmallerNotEmptySubgraph().createInducedSubgraph().getVertices()); 135 171 int maxFlowThreshold = Integer.MAX_VALUE; 136 172 while (!finished) { … … 138 174 Node sink = new Node("T"); 139 175 DirectedGraph<Node, Edge> directedGraph = prepareDirectedGraph(bisection, source, sink); 176 // Util.viewGraph(directedGraph); 140 177 Transformer<Edge, Number> capTransformer = new Transformer<Edge, Number>() { 141 178 public Double transform(Edge e) { … … 144 181 }; 145 182 Map<Edge, Number> edgeFlowMap = new HashMap<Edge, Number>(); 183 i=-1; 146 184 // This Factory produces new edges for use by the algorithm 147 i=-1;148 185 Factory<Edge> edgeFactory = new Factory<Edge>() { 149 186 public Edge create() { 150 187 i++; 151 return new Edge( Integer.toString(i), 1, 1);188 return new Edge("f" + Integer.toString(i), 1, 1); 152 189 } 153 190 }; … … 155 192 directedGraph, source, sink, capTransformer, edgeFlowMap, edgeFactory); 156 193 157 maxFlowThreshold = bisection.getSmaller Subgraph().getVertexCount() * bisection.edgeCut() / 2;194 maxFlowThreshold = bisection.getSmallerNotEmptySubgraph().getVertexCount() * bisection.edgeCut() / 2; 158 195 alg.evaluate(); 196 Util.viewFlowGraph(directedGraph, edgeFlowMap); 159 197 System.out.println("MAX FLOW: " + alg.getMaxFlow() + " THRESHOLD: " + maxFlowThreshold); 160 198 if(alg.getMaxFlow() < maxFlowThreshold) { 161 Set<Node> sinkPartition = alg.getNodesInSinkPartition(); 162 System.out.println(sinkPartition); 163 Set<Node> sourcePartition = alg.getNodesInSourcePartition(); 164 System.out.println(sourcePartition); 165 bisection = prepareBisection(bisection.getSmallerSubgraph().createInducedSubgraph(), sourcePartition, sinkPartition); 166 // bisection = new Bisection(new Subgraph(bisection.getSmallerSubgraph().createInducedSubgraph(), BFSReversed(sink, directedGraph, edgeFlowMap))); 199 // bisection = prepareBisection(bisection.getSmallerNotEmptySubgraph().createInducedSubgraph(), sourcePartition, sinkPartition); 200 cluster = BFSReversed(sink, directedGraph, edgeFlowMap); 201 // System.out.println("DFSReversed: " + DFSReversed(sink, directedGraph, edgeFlowMap)); 202 // bisection = new Bisection(new Subgraph(bisection.getSmallerNotEmptySubgraph().createInducedSubgraph(), DFSReversed(sink, directedGraph, edgeFlowMap))); 203 // cluster = reversedLookup(sink, directedGraph, edgeFlowMap); 204 cluster.remove(sink); 205 bisection = new Bisection(new Subgraph(bisection.getSmallerNotEmptySubgraph().createInducedSubgraph(), cluster)); 167 206 System.out.println("NEW BISECTION: " + bisection.toString()); 168 cluster = sinkPartition;169 207 } else 170 208 finished = true;
Note: See TracChangeset
for help on using the changeset viewer.