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.KKLayout; |
---|
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 KKLayout<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 | vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Edge>()); |
---|
26 | |
---|
27 | JFrame frame = new JFrame("Simple Graph View"); |
---|
28 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
---|
29 | frame.getContentPane().add(vv); |
---|
30 | frame.pack(); |
---|
31 | frame.setVisible(true); |
---|
32 | } |
---|
33 | |
---|
34 | static public void start(KLPartition partition) { |
---|
35 | Subgraph A = null; |
---|
36 | Subgraph B = null; |
---|
37 | if(partition.getSubgraph().getVertexCount() > partition.getComplement().getVertexCount()) { |
---|
38 | A = partition.getSubgraph(); |
---|
39 | B = partition.getComplement(); |
---|
40 | } |
---|
41 | else { |
---|
42 | A = partition.getComplement(); |
---|
43 | B = partition.getSubgraph(); |
---|
44 | } |
---|
45 | int a = A.getVertexCount(); |
---|
46 | int c = partition.edgeCut(); |
---|
47 | |
---|
48 | |
---|
49 | Graph<Node,Edge> g = new DirectedSparseGraph<Node, Edge>(); |
---|
50 | Iterator<Node> nodes = A.iterator(); |
---|
51 | while(nodes.hasNext()) { |
---|
52 | Node u = nodes.next(); |
---|
53 | g.addVertex(u); |
---|
54 | } |
---|
55 | |
---|
56 | nodes = A.iterator(); |
---|
57 | int id = 0; |
---|
58 | while(nodes.hasNext()) { |
---|
59 | Node u = nodes.next(); |
---|
60 | Iterator<Node> neighbors = A.getNeighbors(u).iterator(); |
---|
61 | while(neighbors.hasNext()) { |
---|
62 | Node v = neighbors.next(); |
---|
63 | g.addEdge(new Edge(Integer.toString(id),A.getWeight(u, v),a),u,v); |
---|
64 | id++; |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | Node source = new Node("S"); |
---|
69 | Node sink = new Node("T"); |
---|
70 | g.addVertex(source); |
---|
71 | g.addVertex(sink); |
---|
72 | |
---|
73 | |
---|
74 | nodes = B.iterator(); |
---|
75 | while(nodes.hasNext()) { |
---|
76 | Node u = nodes.next(); |
---|
77 | Iterator<Node> neighbors = B.getGraph().getNeighbors(u).iterator(); |
---|
78 | while(neighbors.hasNext()) { |
---|
79 | Node v = neighbors.next(); |
---|
80 | if(A.contains(v)) { |
---|
81 | g.addEdge(new Edge(Integer.toString(id),1,a),source,v); |
---|
82 | id++; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | nodes = A.iterator(); |
---|
88 | while(nodes.hasNext()) { |
---|
89 | Node u = nodes.next(); |
---|
90 | g.addEdge(new Edge(Integer.toString(id),1,c),u,sink); |
---|
91 | id++; |
---|
92 | } |
---|
93 | |
---|
94 | // viewGraph(g); |
---|
95 | System.out.println(g.toString()); |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | } |
---|