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 | Random r = Random.instance(); |
---|
36 | for(int i=0;i<g.size();i++) { |
---|
37 | if(r.nextBoolean()) |
---|
38 | a.addNode(g.getLabel(i)); |
---|
39 | else |
---|
40 | b.addNode(g.getLabel(i)); |
---|
41 | } |
---|
42 | marked = new ArrayList<Integer>(); |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Returns the node marked as candidate for swapping or -1 if there aren't node available |
---|
47 | * for swapping. |
---|
48 | * @return |
---|
49 | */ |
---|
50 | public int getCandidate() { |
---|
51 | int u; |
---|
52 | if(a.size() > b.size()) { |
---|
53 | u = a.getCandidate(marked); |
---|
54 | if(u == -1) |
---|
55 | u = b.getCandidate(marked); |
---|
56 | } else { |
---|
57 | u = b.getCandidate(marked); |
---|
58 | if(u == -1) |
---|
59 | u = a.getCandidate(marked); |
---|
60 | } |
---|
61 | if(u != -1) { |
---|
62 | marked.add(u); |
---|
63 | } |
---|
64 | return u; |
---|
65 | } |
---|
66 | |
---|
67 | public void swap(int u) { |
---|
68 | Subgraph from = fromSubgraph(u); |
---|
69 | Subgraph to = toSubgraph(u); |
---|
70 | from.remove(u); |
---|
71 | to.addNode(u); |
---|
72 | } |
---|
73 | |
---|
74 | private Subgraph fromSubgraph(int u) { |
---|
75 | Subgraph ret = null; |
---|
76 | if(a.contains(u)) |
---|
77 | ret = a; |
---|
78 | if(b.contains(u)) |
---|
79 | ret = b; |
---|
80 | return ret; |
---|
81 | } |
---|
82 | |
---|
83 | private Subgraph toSubgraph(int u) { |
---|
84 | Subgraph ret = null; |
---|
85 | if(!a.contains(u)) |
---|
86 | ret = a; |
---|
87 | if(!b.contains(u)) |
---|
88 | ret = b; |
---|
89 | return ret; |
---|
90 | } |
---|
91 | |
---|
92 | public int edgeCut() { |
---|
93 | int acc = a.getExternalDegree() + b.getExternalDegree(); |
---|
94 | return (int)Math.round(0.5 * acc); |
---|
95 | } |
---|
96 | |
---|
97 | public Subgraph getSubgraph() { |
---|
98 | return a; |
---|
99 | } |
---|
100 | |
---|
101 | public Subgraph getComplement() { |
---|
102 | return b; |
---|
103 | } |
---|
104 | |
---|
105 | @Override |
---|
106 | public KLPartition clone(){ |
---|
107 | KLPartition clone = new KLPartition(); |
---|
108 | clone.a = (Subgraph) a.clone(); |
---|
109 | clone.b = (Subgraph) b.clone(); |
---|
110 | clone.marked = new ArrayList<Integer>(); |
---|
111 | for(int i = 0; i < marked.size(); i++) { |
---|
112 | clone.marked.add(marked.get(i)); |
---|
113 | } |
---|
114 | return clone; |
---|
115 | } |
---|
116 | |
---|
117 | @Override |
---|
118 | public String toString(){ |
---|
119 | String out = a.toString(); |
---|
120 | out = out + "\n"; |
---|
121 | out = out + b.toString(); |
---|
122 | return out; |
---|
123 | } |
---|
124 | } |
---|