1 | package weka.clusterers.forMetisMQI; |
---|
2 | |
---|
3 | import java.util.HashSet; |
---|
4 | import java.util.Iterator; |
---|
5 | import java.util.Set; |
---|
6 | |
---|
7 | |
---|
8 | public class KLPartition { |
---|
9 | |
---|
10 | private Subgraph a = null; |
---|
11 | |
---|
12 | private Subgraph b = null; |
---|
13 | |
---|
14 | private Set<Node> marked = null; |
---|
15 | |
---|
16 | private KLPartition() { |
---|
17 | } |
---|
18 | |
---|
19 | public KLPartition(Subgraph s) { |
---|
20 | UndirectedGraph g = s.getGraph(); |
---|
21 | a = s; |
---|
22 | b = new Subgraph(g); |
---|
23 | Iterator<Node> graphIterator = g.getVertices().iterator(); |
---|
24 | while(graphIterator.hasNext()) { |
---|
25 | Node u = graphIterator.next(); |
---|
26 | if(!s.contains(u)) |
---|
27 | b.addVertex(u); |
---|
28 | } |
---|
29 | marked = new HashSet<Node>(); |
---|
30 | } |
---|
31 | |
---|
32 | public KLPartition(UndirectedGraph g){ |
---|
33 | a = new Subgraph(g); |
---|
34 | b = new Subgraph(g); |
---|
35 | Iterator<Node> graph = g.vtxsPermutation().iterator(); |
---|
36 | int i = 0; |
---|
37 | while(graph.hasNext()) { |
---|
38 | Node u = graph.next(); |
---|
39 | if((i%2)==0) |
---|
40 | a.addVertex(u); |
---|
41 | else |
---|
42 | b.addVertex(u); |
---|
43 | i++; |
---|
44 | } |
---|
45 | marked = new HashSet<Node>(); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Returns the node marked as candidate for swapping or <code>null</code> if there aren't node available |
---|
50 | * for swapping. |
---|
51 | * @return |
---|
52 | */ |
---|
53 | public Node getCandidate() { |
---|
54 | Node u; |
---|
55 | if(a.getVertexCount() > b.getVertexCount()) { |
---|
56 | u = a.getCandidate(marked); |
---|
57 | if(u == null) |
---|
58 | u = b.getCandidate(marked); |
---|
59 | } else { |
---|
60 | u = b.getCandidate(marked); |
---|
61 | if(u == null) |
---|
62 | u = a.getCandidate(marked); |
---|
63 | } |
---|
64 | if(u != null) { |
---|
65 | marked.add(u); |
---|
66 | } |
---|
67 | return u; |
---|
68 | } |
---|
69 | |
---|
70 | public void swap(Node u) { |
---|
71 | Subgraph from = fromSubgraph(u); |
---|
72 | Subgraph to = toSubgraph(u); |
---|
73 | from.removeVertex(u); |
---|
74 | to.addVertex(u); |
---|
75 | } |
---|
76 | |
---|
77 | private Subgraph fromSubgraph(Node u) { |
---|
78 | Subgraph ret = null; |
---|
79 | if(a.contains(u)) |
---|
80 | ret = a; |
---|
81 | if(b.contains(u)) |
---|
82 | ret = b; |
---|
83 | return ret; |
---|
84 | } |
---|
85 | |
---|
86 | private Subgraph toSubgraph(Node u) { |
---|
87 | Subgraph ret = null; |
---|
88 | if(!a.contains(u)) |
---|
89 | ret = a; |
---|
90 | if(!b.contains(u)) |
---|
91 | ret = b; |
---|
92 | return ret; |
---|
93 | } |
---|
94 | |
---|
95 | public int edgeCut() { |
---|
96 | int acc = a.getExternalDegree() + b.getExternalDegree(); |
---|
97 | return (int)Math.round(0.5 * acc); |
---|
98 | } |
---|
99 | |
---|
100 | public Subgraph getSubgraph() { |
---|
101 | return a; |
---|
102 | } |
---|
103 | |
---|
104 | public Subgraph getComplement() { |
---|
105 | return b; |
---|
106 | } |
---|
107 | |
---|
108 | public KLPartition copy(){ |
---|
109 | KLPartition clone = new KLPartition(); |
---|
110 | clone.a = (Subgraph) a.clone(); |
---|
111 | clone.b = (Subgraph) b.clone(); |
---|
112 | clone.marked = new HashSet<Node>(); |
---|
113 | return clone; |
---|
114 | } |
---|
115 | |
---|
116 | @Override |
---|
117 | public String toString(){ |
---|
118 | String out = a.toString(); |
---|
119 | out = out + "\n"; |
---|
120 | out = out + b.toString(); |
---|
121 | return out; |
---|
122 | } |
---|
123 | } |
---|