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