1 | package weka.clusterers.forMetisMQI; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.Iterator; |
---|
5 | import java.util.List; |
---|
6 | |
---|
7 | |
---|
8 | public class KLPartition { |
---|
9 | |
---|
10 | private Subgraph a = null; |
---|
11 | |
---|
12 | private Subgraph b = null; |
---|
13 | |
---|
14 | private List<Integer> marked = null; |
---|
15 | |
---|
16 | private KLPartition() { |
---|
17 | } |
---|
18 | |
---|
19 | public KLPartition(Subgraph s) { |
---|
20 | Graph g = s.getGraph(); |
---|
21 | a = s; |
---|
22 | b = new Subgraph(g); |
---|
23 | Iterator<Integer> graphIterator = g.iterator(); |
---|
24 | while(graphIterator.hasNext()) { |
---|
25 | int u = graphIterator.next(); |
---|
26 | if(!s.contains(u)) |
---|
27 | b.addNode(u); |
---|
28 | } |
---|
29 | marked = new ArrayList<Integer>(); |
---|
30 | } |
---|
31 | |
---|
32 | public KLPartition(Graph g){ |
---|
33 | a = new Subgraph(g); |
---|
34 | b = new Subgraph(g); |
---|
35 | Iterator<Integer> graph = g.vtxsPermutation().iterator(); |
---|
36 | int i = 0; |
---|
37 | while(graph.hasNext()) { |
---|
38 | int u = graph.next(); |
---|
39 | if((i%2)==0) |
---|
40 | a.addNode(u); |
---|
41 | else |
---|
42 | b.addNode(u); |
---|
43 | i++; |
---|
44 | } |
---|
45 | marked = new ArrayList<Integer>(); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Returns the node marked as candidate for swapping or -1 if there aren't node available |
---|
50 | * for swapping. |
---|
51 | * @return |
---|
52 | */ |
---|
53 | public int getCandidate() { |
---|
54 | int u; |
---|
55 | if(a.size() > b.size()) { |
---|
56 | u = a.getCandidate(marked); |
---|
57 | if(u == -1) |
---|
58 | u = b.getCandidate(marked); |
---|
59 | } else { |
---|
60 | u = b.getCandidate(marked); |
---|
61 | if(u == -1) |
---|
62 | u = a.getCandidate(marked); |
---|
63 | } |
---|
64 | if(u != -1) { |
---|
65 | marked.add(u); |
---|
66 | } |
---|
67 | return u; |
---|
68 | } |
---|
69 | |
---|
70 | public void swap(int u) { |
---|
71 | Subgraph from = fromSubgraph(u); |
---|
72 | Subgraph to = toSubgraph(u); |
---|
73 | from.remove(u); |
---|
74 | to.addNode(u); |
---|
75 | } |
---|
76 | |
---|
77 | private Subgraph fromSubgraph(int 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(int 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 | @Override |
---|
109 | public KLPartition clone(){ |
---|
110 | KLPartition clone = new KLPartition(); |
---|
111 | clone.a = (Subgraph) a.clone(); |
---|
112 | clone.b = (Subgraph) b.clone(); |
---|
113 | clone.marked = new ArrayList<Integer>(); |
---|
114 | for(int i = 0; i < marked.size(); i++) { |
---|
115 | clone.marked.add(marked.get(i)); |
---|
116 | } |
---|
117 | return clone; |
---|
118 | } |
---|
119 | |
---|
120 | @Override |
---|
121 | public String toString(){ |
---|
122 | String out = a.toString(); |
---|
123 | out = out + "\n"; |
---|
124 | out = out + b.toString(); |
---|
125 | return out; |
---|
126 | } |
---|
127 | } |
---|