Changeset 9 for src/main/java/weka/clusterers/forMetisMQI/Subgraph.java
- Timestamp:
- Sep 14, 2010, 2:11:28 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/weka/clusterers/forMetisMQI/Subgraph.java
r8 r9 2 2 3 3 import java.util.ArrayList; 4 import java.util.BitSet;5 4 import java.util.HashMap; 5 import java.util.HashSet; 6 6 import java.util.Iterator; 7 7 import java.util.List; 8 import java.util.Set; 8 9 import java.util.SortedSet; 9 10 import java.util.TreeSet; … … 12 13 public class Subgraph { 13 14 14 private Graph g = null; 15 private BitSet nodes = null; 16 private List<Integer> listOfNodes = null; 17 private HashMap<Integer,Integer> ID = null; 18 private HashMap<Integer,Integer> ED = null; 19 private HashMap<Integer,List<Integer>> bucketGain = null; 15 private UndirectedGraph g = null; 16 17 private Set<Node> nodes = null; 18 19 // private BitSet nodes = null; 20 // private List<Integer> listOfNodes = null; 21 private HashMap<Node,Integer> ID = null; 22 private HashMap<Node,Integer> ED = null; 23 private HashMap<Integer,List<Node>> bucketGain = null; 20 24 private SortedSet<Integer> gainSet = null; 21 25 private boolean recomputeGain = true; 22 26 23 public Subgraph( Graph g){27 public Subgraph(UndirectedGraph g){ 24 28 this.g = g; 25 nodes = new BitSet(g.size()); 26 listOfNodes = new ArrayList<Integer>(); 27 ID = new HashMap<Integer,Integer>(); 28 ED = new HashMap<Integer,Integer>(); 29 bucketGain = new HashMap<Integer, List<Integer>>(); 29 nodes = new HashSet<Node>(); 30 ID = new HashMap<Node,Integer>(); 31 ED = new HashMap<Node,Integer>(); 32 bucketGain = new HashMap<Integer, List<Node>>(); 30 33 gainSet = new TreeSet<Integer>(); 31 34 } 32 35 33 public Graph getGraph() {36 public UndirectedGraph getGraph() { 34 37 return g; 35 38 } 36 39 37 public void addNode(int u) { 38 nodes.set(g.getIndex(u)); 39 listOfNodes.add(u); 40 public void addVertex(Node u) { 41 nodes.add(u); 40 42 ID.put(u, 0); 41 43 ED.put(u, 0); … … 44 46 45 47 private void computeDegree() { 46 for(int i = 0; i < size(); i++) { 47 int u = listOfNodes.get(i); 48 Iterator<Integer> it = g.getNeighbors(u).iterator(); 48 Iterator<Node> subgraphIterator = nodes.iterator(); 49 while(subgraphIterator.hasNext()) { 50 Node u = subgraphIterator.next(); 51 Iterator<Node> nborsIterator = g.getNeighbors(u).iterator(); 49 52 int newID = 0; 50 53 int newED = 0; 51 while( it.hasNext()) {52 int v = it.next();54 while(nborsIterator.hasNext()) { 55 Node v = nborsIterator.next(); 53 56 if(contains(v)) 54 newID = newID + g. getWeight(u, v);57 newID = newID + g.findEdge(u, v).getWeight(); 55 58 else 56 newED = newED + g. getWeight(u, v);59 newED = newED + g.findEdge(u, v).getWeight(); 57 60 ID.put(u, newID); 58 61 ED.put(u, newED); … … 67 70 ED.clear(); 68 71 computeDegree(); 69 for(int i = 0; i < size(); i++) { 70 int u = listOfNodes.get(i); 72 Iterator<Node> subgraphIterator = nodes.iterator(); 73 while(subgraphIterator.hasNext()) { 74 Node u = subgraphIterator.next(); 71 75 int gainU = ED.get(u) - ID.get(u); 72 76 if(!bucketGain.containsKey(gainU)) { 73 bucketGain.put(gainU, new ArrayList< Integer>());77 bucketGain.put(gainU, new ArrayList<Node>()); 74 78 } 75 79 bucketGain.get(gainU).add(u); … … 79 83 } 80 84 81 public intgetCandidate() {82 return getCandidate(new ArrayList<Integer>());85 public Node getCandidate() { 86 return getCandidate(new HashSet<Node>()); 83 87 } 84 88 … … 86 90 * 87 91 * @param marked 88 * @return the candidate node for swap or -1if there aren't available nodes.92 * @return the candidate node for swap or <code>null</code> if there aren't available nodes. 89 93 */ 90 public int getCandidate(List<Integer> marked) {91 if(recomputeGain) 92 computeGain(); 93 int candidate = -1;94 public Node getCandidate(Set<Node> marked) { 95 if(recomputeGain) 96 computeGain(); 97 Node candidate = null; 94 98 Iterator<Integer> iterator = ((TreeSet<Integer>)gainSet).descendingIterator(); 95 while(iterator.hasNext() && (candidate == -1)) {99 while(iterator.hasNext() && (candidate == null)) { 96 100 int gain = iterator.next(); 97 Iterator< Integer> nodes = bucketGain.get(gain).iterator();98 while(nodes.hasNext() && (candidate == -1)) {99 intu = nodes.next();101 Iterator<Node> nodes = bucketGain.get(gain).iterator(); 102 while(nodes.hasNext() && (candidate == null)) { 103 Node u = nodes.next(); 100 104 if(!marked.contains(u)) 101 105 candidate = u; … … 105 109 } 106 110 107 public int gain( intu){111 public int gain(Node u){ 108 112 if(recomputeGain) 109 113 computeGain(); … … 111 115 } 112 116 113 public void remove (intu) {117 public void removeVertex(Node u) { 114 118 if(recomputeGain) 115 119 computeGain(); 116 120 int gainU = gain(u); 117 nodes.set(g.getIndex(u), false); 118 listOfNodes.remove(listOfNodes.indexOf(u)); 121 nodes.remove(u); 119 122 ID.remove(u); 120 123 ED.remove(u); 121 List< Integer> l = bucketGain.get(gainU);124 List<Node> l = bucketGain.get(gainU); 122 125 l.remove(l.indexOf(u)); 123 126 if(l.size() == 0) { … … 128 131 } 129 132 130 public int size() { 131 return listOfNodes.size(); 132 } 133 134 public int getWeight(int u, int v) { 133 public int getVertexCount() { 134 return nodes.size(); 135 } 136 137 public Edge findEdge(Node v1, Node v2) { 138 Edge e = null; 139 if(contains(v1) && contains(v2)) 140 e = g.findEdge(v1, v2); 141 return e; 142 } 143 144 public int getWeight(Node u, Node v) { 135 145 int ret = -1; 136 if( isEdge(u,v))137 ret = g. getWeight(u, v);146 if(containsEdge(u,v)) 147 ret = g.findEdge(u, v).getWeight(); 138 148 return ret; 139 149 } 140 150 141 public Iterator< Integer> iterator() {142 return listOfNodes.iterator();143 } 144 145 public boolean isEdge(int u, intv) {146 return nodes.get(g.getIndex(u)) && nodes.get(g.getIndex(v)) && g.isEdge(u, v);147 } 148 149 public boolean contains( intu) {150 return nodes. get(g.getIndex(u));151 } 152 153 public List< Integer> getNeighbours(intu) {154 List< Integer> neighbors = new ArrayList<Integer>();155 Iterator< Integer> iterator = g.getNeighbors(u).iterator();151 public Iterator<Node> iterator() { 152 return nodes.iterator(); 153 } 154 155 public boolean containsEdge(Node u, Node v) { 156 return contains(u) && contains(v) && g.containsEdge(u, v); 157 } 158 159 public boolean contains(Node u) { 160 return nodes.contains(u); 161 } 162 163 public List<Node> getNeighbors(Node u) { 164 List<Node> neighbors = new ArrayList<Node>(); 165 Iterator<Node> iterator = g.getNeighbors(u).iterator(); 156 166 while(iterator.hasNext()) { 157 intv = iterator.next();158 if(contains(v) && isEdge(u, v))167 Node v = iterator.next(); 168 if(contains(v) && containsEdge(u, v)) 159 169 neighbors.add(v); 160 170 } … … 175 185 public String toString() { 176 186 String out = "["; 177 Iterator< Integer> it = listOfNodes.iterator();187 Iterator<Node> it = nodes.iterator(); 178 188 while(it.hasNext()) { 179 intu = it.next();189 Node u = it.next(); 180 190 out = out + u + ","; 181 191 } … … 184 194 } 185 195 186 public List< Integer> getBoundary() {187 if(recomputeGain) 188 computeGain(); 189 Iterator<Entry< Integer,Integer>> EDIterator = ED.entrySet().iterator();190 List< Integer> boundary = new ArrayList<Integer>();196 public List<Node> getBoundary() { 197 if(recomputeGain) 198 computeGain(); 199 Iterator<Entry<Node,Integer>> EDIterator = ED.entrySet().iterator(); 200 List<Node> boundary = new ArrayList<Node>(); 191 201 while(EDIterator.hasNext()) { 192 Entry< Integer,Integer> entry = EDIterator.next();202 Entry<Node,Integer> entry = EDIterator.next(); 193 203 if(entry.getValue() > 0) 194 204 boundary.add(entry.getKey()); … … 206 216 clone.g = g.clone(); 207 217 208 clone.nodes = (BitSet)nodes.clone(); 209 210 clone.listOfNodes = new ArrayList<Integer>(); 211 for(int i = 0; i < listOfNodes.size(); i++) { 212 clone.listOfNodes.add(listOfNodes.get(i)); 213 } 214 215 clone.ID = new HashMap<Integer, Integer>(); 216 Iterator<Entry<Integer,Integer>> i = ID.entrySet().iterator(); 217 while(i.hasNext()) { 218 Entry<Integer,Integer> entry = i.next(); 219 clone.ID.put(entry.getKey(), entry.getValue()); 220 } 221 222 clone.ED = new HashMap<Integer, Integer>(); 223 i = ED.entrySet().iterator(); 224 while(i.hasNext()) { 225 Entry<Integer,Integer> entry = i.next(); 226 clone.ED.put(entry.getKey(), entry.getValue()); 227 } 228 229 clone.bucketGain = new HashMap<Integer, List<Integer>>(); 230 Iterator<Entry<Integer,List<Integer>>>it = bucketGain.entrySet().iterator(); 231 while(it.hasNext()) { 232 Entry<Integer,List<Integer>> entry = it.next(); 233 List<Integer> bucketClone = new ArrayList<Integer>(); 234 List<Integer> bucket = entry.getValue(); 235 for(int j = 0; j < bucket.size(); j++) { 236 bucketClone.add(bucket.get(j)); 237 } 238 clone.bucketGain.put(entry.getKey(), bucketClone); 239 } 240 218 clone.nodes = new HashSet<Node>(); 219 Iterator<Node> graphIterator =clone.g.getVertices().iterator(); 220 while(graphIterator.hasNext()) { 221 Node u = graphIterator.next(); 222 if(contains(u)) 223 clone.nodes.add(u); 224 } 225 226 clone.bucketGain = new HashMap<Integer, List<Node>>(); 241 227 clone.gainSet = new TreeSet<Integer>(); 242 Iterator<Integer> gainIt = gainSet.iterator(); 243 while(gainIt.hasNext()) { 244 gainSet.add(gainIt.next()); 245 } 246 247 clone.recomputeGain = recomputeGain; 228 clone.ID = new HashMap<Node, Integer>(); 229 clone.ED = new HashMap<Node, Integer>(); 230 231 clone.computeGain(); 248 232 249 233 return clone;
Note: See TracChangeset
for help on using the changeset viewer.