| 1 | package weka.clusterers.forMetisMQI; |
|---|
| 2 | |
|---|
| 3 | import java.awt.Dimension; |
|---|
| 4 | import java.util.Iterator; |
|---|
| 5 | |
|---|
| 6 | import javax.swing.JFrame; |
|---|
| 7 | |
|---|
| 8 | import edu.uci.ics.jung.algorithms.layout.CircleLayout; |
|---|
| 9 | import edu.uci.ics.jung.algorithms.layout.Layout; |
|---|
| 10 | import edu.uci.ics.jung.graph.DirectedSparseGraph; |
|---|
| 11 | import edu.uci.ics.jung.graph.Graph; |
|---|
| 12 | import edu.uci.ics.jung.visualization.BasicVisualizationServer; |
|---|
| 13 | import edu.uci.ics.jung.visualization.decorators.ToStringLabeller; |
|---|
| 14 | |
|---|
| 15 | public class MQI { |
|---|
| 16 | |
|---|
| 17 | public static void viewGraph(Graph<Node, Edge> g){ |
|---|
| 18 | Layout<Node, Edge> layout = new CircleLayout<Node, Edge>(g); |
|---|
| 19 | layout.setSize(new Dimension(300,300)); // sets the initial size of the space |
|---|
| 20 | // The BasicVisualizationServer<V,E> is parameterized by the edge types |
|---|
| 21 | BasicVisualizationServer<Node,Edge> vv = |
|---|
| 22 | new BasicVisualizationServer<Node,Edge>(layout); |
|---|
| 23 | vv.setPreferredSize(new Dimension(350,350)); //Sets the viewing area size |
|---|
| 24 | vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>()); |
|---|
| 25 | JFrame frame = new JFrame("Simple Graph View"); |
|---|
| 26 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|---|
| 27 | frame.getContentPane().add(vv); |
|---|
| 28 | frame.pack(); |
|---|
| 29 | frame.setVisible(true); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | static public void start(KLPartition partition) { |
|---|
| 33 | Subgraph A = null; |
|---|
| 34 | Subgraph B = null; |
|---|
| 35 | if(partition.getSubgraph().size() > partition.getComplement().size()) { |
|---|
| 36 | A = partition.getSubgraph(); |
|---|
| 37 | B = partition.getComplement(); |
|---|
| 38 | } |
|---|
| 39 | else { |
|---|
| 40 | A = partition.getComplement(); |
|---|
| 41 | B = partition.getSubgraph(); |
|---|
| 42 | } |
|---|
| 43 | int a = A.size(); |
|---|
| 44 | int c = partition.edgeCut(); |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | Graph<Node,Edge> g = new DirectedSparseGraph<Node, Edge>(); |
|---|
| 48 | Iterator<Integer> nodes = A.iterator(); |
|---|
| 49 | while(nodes.hasNext()) { |
|---|
| 50 | int u = nodes.next(); |
|---|
| 51 | g.addVertex(new Node(Integer.toString(u))); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | nodes = A.iterator(); |
|---|
| 55 | int id = 0; |
|---|
| 56 | while(nodes.hasNext()) { |
|---|
| 57 | int u = nodes.next(); |
|---|
| 58 | Iterator<Integer> neighbors = A.getNeighbours(u).iterator(); |
|---|
| 59 | while(neighbors.hasNext()) { |
|---|
| 60 | int v = neighbors.next(); |
|---|
| 61 | g.addEdge(new Edge(Integer.toString(id),A.getWeight(u, v),a),new Node(Integer.toString(u)),new Node(Integer.toString(v))); |
|---|
| 62 | id++; |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | Node source = new Node("S"); |
|---|
| 67 | Node sink = new Node("T"); |
|---|
| 68 | g.addVertex(source); |
|---|
| 69 | g.addVertex(sink); |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | nodes = B.iterator(); |
|---|
| 73 | while(nodes.hasNext()) { |
|---|
| 74 | int u = nodes.next(); |
|---|
| 75 | Iterator<Integer> neighbors = B.getGraph().getNeighbors(u).iterator(); |
|---|
| 76 | while(neighbors.hasNext()) { |
|---|
| 77 | int v = neighbors.next(); |
|---|
| 78 | if(A.contains(v)) { |
|---|
| 79 | g.addEdge(new Edge(Integer.toString(id),1,a),source,new Node(Integer.toString(v))); |
|---|
| 80 | id++; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | nodes = A.iterator(); |
|---|
| 86 | while(nodes.hasNext()) { |
|---|
| 87 | int u = nodes.next(); |
|---|
| 88 | g.addEdge(new Edge(Integer.toString(id),1,c),new Node(Integer.toString(u)),sink); |
|---|
| 89 | id++; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | viewGraph(g); |
|---|
| 93 | System.out.println(g.toString()); |
|---|
| 94 | |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | } |
|---|