1 | package weka.clusterers.forMetisMQI.graph; |
---|
2 | |
---|
3 | import java.util.HashSet; |
---|
4 | import java.util.Iterator; |
---|
5 | import java.util.Set; |
---|
6 | |
---|
7 | import weka.clusterers.forMetisMQI.util.Random; |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | public class Bisection { |
---|
12 | |
---|
13 | private Subgraph a = null; |
---|
14 | |
---|
15 | private Subgraph b = null; |
---|
16 | |
---|
17 | private Set<Node> marked = null; |
---|
18 | |
---|
19 | private UndirectedGraph g = null; |
---|
20 | |
---|
21 | private Bisection() { |
---|
22 | } |
---|
23 | |
---|
24 | /** |
---|
25 | * Initialize the bisection with a given subgraph. |
---|
26 | * @param s |
---|
27 | */ |
---|
28 | public Bisection(Subgraph s) { |
---|
29 | g = s.getGraph(); |
---|
30 | a = s; |
---|
31 | b = new Subgraph(g); |
---|
32 | Iterator<Node> graphIterator = g.getVertices().iterator(); |
---|
33 | while(graphIterator.hasNext()) { |
---|
34 | Node u = graphIterator.next(); |
---|
35 | if(!s.contains(u)) |
---|
36 | b.addVertex(u); |
---|
37 | } |
---|
38 | marked = new HashSet<Node>(); |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * Creates a bisection choosing randomly the nodes for each subgraph. |
---|
43 | * @param g |
---|
44 | */ |
---|
45 | public Bisection(UndirectedGraph g){ |
---|
46 | double limitingProbabilities = 0.1; |
---|
47 | this.g = g; |
---|
48 | a = new Subgraph(g); |
---|
49 | b = new Subgraph(g); |
---|
50 | Iterator<Node> graph = g.vtxsPermutation().iterator(); |
---|
51 | while(graph.hasNext()) { |
---|
52 | Node u = graph.next(); |
---|
53 | if(Random.instance().nextDouble() < limitingProbabilities) |
---|
54 | a.addVertex(u); |
---|
55 | else |
---|
56 | b.addVertex(u); |
---|
57 | } |
---|
58 | marked = new HashSet<Node>(); |
---|
59 | } |
---|
60 | |
---|
61 | public UndirectedGraph getGraph() { |
---|
62 | return g; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * Returns the node marked as candidate for swapping or <code>null</code> if there aren't node available |
---|
67 | * for swapping. |
---|
68 | * @return |
---|
69 | */ |
---|
70 | public Node getCandidate() { |
---|
71 | Node u; |
---|
72 | if(a.getVertexCount() > b.getVertexCount()) { |
---|
73 | u = a.getCandidate(marked); |
---|
74 | if(u == null) |
---|
75 | u = b.getCandidate(marked); |
---|
76 | } else { |
---|
77 | u = b.getCandidate(marked); |
---|
78 | if(u == null) |
---|
79 | u = a.getCandidate(marked); |
---|
80 | } |
---|
81 | if(u != null) { |
---|
82 | marked.add(u); |
---|
83 | } |
---|
84 | return u; |
---|
85 | } |
---|
86 | |
---|
87 | public void swap(Node u) { |
---|
88 | Subgraph from = fromSubgraph(u); |
---|
89 | Subgraph to = toSubgraph(u); |
---|
90 | from.removeVertex(u); |
---|
91 | to.addVertex(u); |
---|
92 | } |
---|
93 | |
---|
94 | private Subgraph fromSubgraph(Node u) { |
---|
95 | Subgraph ret = null; |
---|
96 | if(a.contains(u)) |
---|
97 | ret = a; |
---|
98 | if(b.contains(u)) |
---|
99 | ret = b; |
---|
100 | return ret; |
---|
101 | } |
---|
102 | |
---|
103 | private Subgraph toSubgraph(Node u) { |
---|
104 | Subgraph ret = null; |
---|
105 | if(!a.contains(u)) |
---|
106 | ret = a; |
---|
107 | if(!b.contains(u)) |
---|
108 | ret = b; |
---|
109 | return ret; |
---|
110 | } |
---|
111 | |
---|
112 | public int edgeCut() { |
---|
113 | int acc = a.getExternalDegree() + b.getExternalDegree(); |
---|
114 | return acc; |
---|
115 | } |
---|
116 | |
---|
117 | public Subgraph getSubgraph() { |
---|
118 | return a; |
---|
119 | } |
---|
120 | |
---|
121 | public Subgraph getComplement() { |
---|
122 | return b; |
---|
123 | } |
---|
124 | |
---|
125 | public Bisection clone(){ |
---|
126 | Bisection clone = new Bisection(); |
---|
127 | clone.a = (Subgraph) a.clone(); |
---|
128 | clone.b = (Subgraph) b.clone(); |
---|
129 | clone.g = g.clone(); |
---|
130 | clone.marked = new HashSet<Node>(); |
---|
131 | return clone; |
---|
132 | } |
---|
133 | |
---|
134 | @Override |
---|
135 | public String toString(){ |
---|
136 | String out = a.toString(); |
---|
137 | out = out + "\n"; |
---|
138 | out = out + b.toString(); |
---|
139 | return out; |
---|
140 | } |
---|
141 | |
---|
142 | public Subgraph getLargerSubgraph() { |
---|
143 | if(a.getVertexCount() < b.getVertexCount()) |
---|
144 | return b; |
---|
145 | else |
---|
146 | return a; |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * Returns the smaller subgraph of this bisection, null otherwise. |
---|
151 | * @return |
---|
152 | */ |
---|
153 | public Subgraph getSmallerSubgraph() { |
---|
154 | if(a.getVertexCount() < b.getVertexCount()) |
---|
155 | return a; |
---|
156 | else |
---|
157 | return b; |
---|
158 | } |
---|
159 | } |
---|