| 1 | package weka.clusterers.forMetisMQI; |
|---|
| 2 | |
|---|
| 3 | import java.io.PrintStream; |
|---|
| 4 | import java.util.ArrayList; |
|---|
| 5 | import java.util.HashSet; |
|---|
| 6 | import java.util.Iterator; |
|---|
| 7 | import java.util.List; |
|---|
| 8 | import java.util.Set; |
|---|
| 9 | import java.util.Stack; |
|---|
| 10 | |
|---|
| 11 | public class Coarse { |
|---|
| 12 | |
|---|
| 13 | private static boolean debug = true; |
|---|
| 14 | private static PrintStream debugStream = System.err; |
|---|
| 15 | private static int finerSize = 5; |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * Return true if the vertex v is matched |
|---|
| 19 | * |
|---|
| 20 | * @param g graph |
|---|
| 21 | * @param v |
|---|
| 22 | * the index of the vertex |
|---|
| 23 | * @return true if the vertex v is matched, false o.w. |
|---|
| 24 | */ |
|---|
| 25 | private static boolean isMatched(Graph g, List<Integer> match, int v) { |
|---|
| 26 | return match.get(g.getIndex(v)) != g.getIndex(v); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | private static void RMMatch(Graph g, List<Integer> match, List<Integer> map) { |
|---|
| 30 | int labelCounter = 0; |
|---|
| 31 | for (int i = 0; i < g.size(); i++) { |
|---|
| 32 | match.add(i); |
|---|
| 33 | map.add(i); |
|---|
| 34 | } |
|---|
| 35 | g.adjncyPermutation(); |
|---|
| 36 | Iterator<Integer> nodeIterator = g.vtxsPermutation().iterator(); |
|---|
| 37 | while (nodeIterator.hasNext()) { |
|---|
| 38 | int u = nodeIterator.next(); |
|---|
| 39 | if (debug) |
|---|
| 40 | debugStream.println("Visiting node " + u + " Matched = " + isMatched(g,match,u)); |
|---|
| 41 | if (!isMatched(g,match,u)) { |
|---|
| 42 | boolean foundMatch = false; |
|---|
| 43 | int matchedNode = -1; |
|---|
| 44 | Iterator<Integer> iterator = g.getNeighbors(u).iterator(); |
|---|
| 45 | while(iterator.hasNext() && !foundMatch){ |
|---|
| 46 | int v = iterator.next(); |
|---|
| 47 | if (!isMatched(g,match,v)) { |
|---|
| 48 | matchedNode = v; |
|---|
| 49 | foundMatch = true; |
|---|
| 50 | if (debug) |
|---|
| 51 | debugStream.println("Found a match with node " |
|---|
| 52 | + matchedNode); |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | if (debug && !foundMatch) |
|---|
| 56 | debugStream.println("There aren't unmatched neighbors."); |
|---|
| 57 | if(foundMatch) { |
|---|
| 58 | match.set(g.getIndex(u), g.getIndex(matchedNode)); |
|---|
| 59 | match.set(g.getIndex(matchedNode), g.getIndex(u)); |
|---|
| 60 | map.set(g.getIndex(u), labelCounter); |
|---|
| 61 | map.set(g.getIndex(matchedNode), labelCounter); |
|---|
| 62 | if(debug) debugStream.println("Contracting node " + u + " with " + matchedNode + ". Node id: " + getMappedNode(g, map, u)); |
|---|
| 63 | } else { |
|---|
| 64 | map.set(g.getIndex(u), labelCounter); |
|---|
| 65 | if(debug) debugStream.println("Node " + u + " with " + " new node id: " + getMappedNode(g, map, u)); |
|---|
| 66 | } |
|---|
| 67 | labelCounter++; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | private static int getMatchedNode(Graph g, List<Integer> match, int u) { |
|---|
| 73 | return g.getLabel(match.get(g.getIndex(u))); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | private static int getMappedNode(Graph g, List<Integer> map, int u) { |
|---|
| 77 | return map.get(g.getIndex(u)); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * Return a new contracted graph. |
|---|
| 82 | */ |
|---|
| 83 | private static Graph contract(Graph g, List<Integer> match, List<Integer> map) { |
|---|
| 84 | Graph output = new Graph(); |
|---|
| 85 | Iterator<Integer> iterator = g.iterator(); |
|---|
| 86 | while(iterator.hasNext()) { |
|---|
| 87 | int u = iterator.next(); |
|---|
| 88 | output.addNode(getMappedNode(g,map,u)); |
|---|
| 89 | Iterator<Integer> it = g.getNeighbors(u).iterator(); |
|---|
| 90 | Set<Integer> neighbors = new HashSet<Integer>(); |
|---|
| 91 | while(it.hasNext()) { |
|---|
| 92 | int v = it.next(); |
|---|
| 93 | neighbors.add(getMappedNode(g,map,v)); |
|---|
| 94 | } |
|---|
| 95 | it = g.getNeighbors(getMatchedNode(g,match,u)).iterator(); |
|---|
| 96 | while(it.hasNext()) { |
|---|
| 97 | int v = it.next(); |
|---|
| 98 | neighbors.add(getMappedNode(g,map,v)); |
|---|
| 99 | } |
|---|
| 100 | neighbors.remove(getMappedNode(g,map,u)); |
|---|
| 101 | it = neighbors.iterator(); |
|---|
| 102 | while(it.hasNext()) { |
|---|
| 103 | int v = it.next(); |
|---|
| 104 | output.addNode(v); |
|---|
| 105 | output.addEdgeUndirected(getMappedNode(g,map,u), v, 0); |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | //calcolo dei pesi del nuovo grafo: per ogni arco (u,v) && u < v. |
|---|
| 109 | //w(map(u),map(v)) += w(u,v). |
|---|
| 110 | for(int i=0; i < g.size(); i++) { |
|---|
| 111 | int u = g.getLabel(i); |
|---|
| 112 | Iterator<Integer> it = g.getNeighbors(u).iterator(); |
|---|
| 113 | while(it.hasNext()) { |
|---|
| 114 | int v = it.next(); |
|---|
| 115 | if(getMappedNode(g,map,u) != getMappedNode(g,map,v) && output.isEdge(getMappedNode(g,map,u), getMappedNode(g,map,v)) && u < v) { |
|---|
| 116 | output.setWeight(getMappedNode(g,map,u), getMappedNode(g,map,v), output.getWeight(getMappedNode(g,map,u), getMappedNode(g,map,v)) + g.getWeight(u, v)); |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | for(int i=0; i < g.size(); i++) { |
|---|
| 121 | int u = g.getLabel(i); |
|---|
| 122 | if(isMatched(g,match,u)) { |
|---|
| 123 | int v = getMatchedNode(g,match,u); |
|---|
| 124 | if(u < v) { |
|---|
| 125 | output.getNode(getMappedNode(g,map,u)).vwgt = g.getNode(u).vwgt + g.getNode(v).vwgt; |
|---|
| 126 | output.getNode(getMappedNode(g,map,u)).cewgt = g.getNode(u).cewgt + g.getNode(v).cewgt + |
|---|
| 127 | g.getWeight(u, v); |
|---|
| 128 | output.getNode(getMappedNode(g,map,u)).adjwgt = g.getNode(u).adjwgt + g.getNode(v).adjwgt - 2 * |
|---|
| 129 | g.getWeight(u, v); |
|---|
| 130 | } |
|---|
| 131 | } else { |
|---|
| 132 | output.getNode(getMappedNode(g,map,u)).vwgt = g.getNode(u).vwgt; |
|---|
| 133 | output.getNode(getMappedNode(g,map,u)).cewgt = g.getNode(u).cewgt; |
|---|
| 134 | output.getNode(getMappedNode(g,map,u)).adjwgt = g.getNode(u).adjwgt; |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | return output; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /** |
|---|
| 141 | * Performs the first stage of the METIS algorithm, using RM. |
|---|
| 142 | */ |
|---|
| 143 | public static CoarserGraphElement coarseOneStep(Graph g) { |
|---|
| 144 | Graph projected = g; |
|---|
| 145 | Graph contracted = null; |
|---|
| 146 | List<Integer> match = new ArrayList<Integer>(); |
|---|
| 147 | List<Integer> map = new ArrayList<Integer>(); |
|---|
| 148 | RMMatch(g,match,map); |
|---|
| 149 | contracted = contract(g,match,map); |
|---|
| 150 | return new CoarserGraphElement(contracted, projected, match, map); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | public static Stack<CoarserGraphElement> coarse(Graph g) { |
|---|
| 154 | Stack<CoarserGraphElement> stack = new Stack<CoarserGraphElement>(); |
|---|
| 155 | CoarserGraphElement e; |
|---|
| 156 | Graph curr = g; |
|---|
| 157 | do { |
|---|
| 158 | if(debug) |
|---|
| 159 | debugStream.println("-----------------------------------------------------"); |
|---|
| 160 | e = coarseOneStep(curr); |
|---|
| 161 | stack.push(e); |
|---|
| 162 | curr = e.getContracted(); |
|---|
| 163 | if(debug) |
|---|
| 164 | debugStream.println("-----------------------------------------------------"); |
|---|
| 165 | } while(e.getProjected().size() > e.getContracted().size() && e.getContracted().size() > finerSize); |
|---|
| 166 | return stack; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | public static void setFinerSize(int i) { |
|---|
| 170 | finerSize = i; |
|---|
| 171 | } |
|---|
| 172 | } |
|---|