1 | package weka.clusterers.forMetisMQI; |
---|
2 | |
---|
3 | import java.io.PrintStream; |
---|
4 | import java.util.HashMap; |
---|
5 | import java.util.HashSet; |
---|
6 | import java.util.Iterator; |
---|
7 | import java.util.Map; |
---|
8 | import java.util.Set; |
---|
9 | import java.util.Stack; |
---|
10 | |
---|
11 | import edu.uci.ics.jung.graph.util.Pair; |
---|
12 | |
---|
13 | public class Coarse { |
---|
14 | |
---|
15 | private static boolean debug = true; |
---|
16 | private static PrintStream debugStream = System.err; |
---|
17 | private static int finerSize = 5; |
---|
18 | |
---|
19 | /** |
---|
20 | * Return true if the vertex v is matched |
---|
21 | * |
---|
22 | * @param g graph |
---|
23 | * @param v |
---|
24 | * the index of the vertex |
---|
25 | * @return true if the vertex v is matched, false o.w. |
---|
26 | */ |
---|
27 | private static boolean isMatched(Map<Node,Node> match, Node v) { |
---|
28 | if(match.containsKey(v)) |
---|
29 | return !match.get(v).equals(v); |
---|
30 | else |
---|
31 | return false; |
---|
32 | } |
---|
33 | |
---|
34 | private static void RMMatch(UndirectedGraph g, Map<Node,Node> match, Map<Node,Node> map) { |
---|
35 | int labelCounter = 0; |
---|
36 | Iterator<Node> nodeIterator = g.vtxsPermutation().iterator(); |
---|
37 | while (nodeIterator.hasNext()) { |
---|
38 | Node u = nodeIterator.next(); |
---|
39 | if (debug) |
---|
40 | debugStream.println("Visiting node " + u + " Matched = " + isMatched(match,u)); |
---|
41 | if (!isMatched(match,u)) { |
---|
42 | boolean foundMatch = false; |
---|
43 | Node matchedNode = null; |
---|
44 | Iterator<Node> iterator = g.getNeighborsPermutation(u).iterator(); |
---|
45 | while(iterator.hasNext() && !foundMatch){ |
---|
46 | Node v = iterator.next(); |
---|
47 | if (!isMatched(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 | Node newNode = new Node(Integer.toString(labelCounter)); |
---|
58 | if(foundMatch) { |
---|
59 | match.put(u, matchedNode); |
---|
60 | match.put(matchedNode, u); |
---|
61 | map.put(u, newNode); |
---|
62 | map.put(matchedNode, newNode); |
---|
63 | if(debug) debugStream.println("Contracting node " + u + " with " + matchedNode + ". Node id: " + getMappedNode(map, u)); |
---|
64 | } else { |
---|
65 | match.put(u, u); |
---|
66 | map.put(u, newNode); |
---|
67 | if(debug) debugStream.println("Node " + u + " with " + " new node id: " + getMappedNode(map, u)); |
---|
68 | } |
---|
69 | labelCounter++; |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | private static Node getMatchedNode(Map<Node,Node> match, Node u) { |
---|
75 | return match.get(u); |
---|
76 | } |
---|
77 | |
---|
78 | private static Node getMappedNode(Map<Node,Node> map, Node u) { |
---|
79 | return map.get(u); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Return a new contracted graph. |
---|
84 | */ |
---|
85 | private static UndirectedGraph contract(UndirectedGraph g, Map<Node,Node> match, Map<Node,Node> map) { |
---|
86 | UndirectedGraph output = new UndirectedGraph(); |
---|
87 | Iterator<Node> iterator = g.getVertices().iterator(); |
---|
88 | int i = 0; |
---|
89 | while(iterator.hasNext()) { |
---|
90 | Node u = iterator.next(); |
---|
91 | output.addVertex(getMappedNode(map,u)); |
---|
92 | |
---|
93 | Set<Node> neighbors = new HashSet<Node>(); |
---|
94 | Iterator<Node> it = g.getNeighbors(u).iterator(); |
---|
95 | while(it.hasNext()) |
---|
96 | neighbors.add(getMappedNode(map, it.next())); |
---|
97 | it = g.getNeighbors(getMatchedNode(match, u)).iterator(); |
---|
98 | while(it.hasNext()) |
---|
99 | neighbors.add(getMappedNode(map, it.next())); |
---|
100 | |
---|
101 | // Set<Node> neighbors = new HashSet<Node>(); |
---|
102 | // while(it.hasNext()) { |
---|
103 | // Node v = it.next(); |
---|
104 | // neighbors.add(getMappedNode(map,v)); |
---|
105 | // } |
---|
106 | // it = g.getNeighbors(getMappedNode(match,u)).iterator(); |
---|
107 | // while(it.hasNext()) { |
---|
108 | // Node v = it.next(); |
---|
109 | // neighbors.add(getMappedNode(map,v)); |
---|
110 | // } |
---|
111 | neighbors.remove(getMappedNode(map,u)); |
---|
112 | it = neighbors.iterator(); |
---|
113 | while(it.hasNext()) { |
---|
114 | Node v = it.next(); |
---|
115 | output.addVertex(v); |
---|
116 | output.addEdge(new Edge(Integer.toString(i),0,0),getMappedNode(map,u), v); |
---|
117 | i++; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | //calcolo dei pesi del nuovo grafo: per ogni arco (u,v) && u < v. |
---|
122 | //w(map(u),map(v)) += w(u,v). |
---|
123 | |
---|
124 | Iterator<Edge> edgeIterator = g.getEdges().iterator(); |
---|
125 | while(edgeIterator.hasNext()) { |
---|
126 | Edge oldEdge = edgeIterator.next(); |
---|
127 | Pair<Node> srcDst = g.getEndpoints(oldEdge); |
---|
128 | Node src = srcDst.getFirst(); |
---|
129 | Node dst = srcDst.getFirst(); |
---|
130 | Node srcMapped = getMappedNode(map, src); |
---|
131 | Node dstMapped = getMappedNode(map, dst); |
---|
132 | if(!srcMapped.equals(dstMapped) && output.containsEdge(srcMapped, dstMapped)) { |
---|
133 | Edge newEdge = output.findEdge(srcMapped, dstMapped); |
---|
134 | newEdge.setWeight(newEdge.getWeight() + oldEdge.getWeight()); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | // for(int i=0; i < g.size(); i++) { |
---|
140 | // int u = g.getLabel(i); |
---|
141 | // Iterator<Integer> it = g.getNeighbors(u).iterator(); |
---|
142 | // while(it.hasNext()) { |
---|
143 | // int v = it.next(); |
---|
144 | // if(getMappedNode(g,map,u) != getMappedNode(g,map,v) && output.isEdge(getMappedNode(g,map,u), getMappedNode(g,map,v)) && u < v) { |
---|
145 | // output.setWeight(getMappedNode(g,map,u), getMappedNode(g,map,v), output.getWeight(getMappedNode(g,map,u), getMappedNode(g,map,v)) + g.getWeight(u, v)); |
---|
146 | // } |
---|
147 | // } |
---|
148 | // } |
---|
149 | |
---|
150 | |
---|
151 | iterator = g.getVertices().iterator(); |
---|
152 | Set<Node> nodesComplete = new HashSet<Node>(); |
---|
153 | while(iterator.hasNext()) { |
---|
154 | Node u = iterator.next(); |
---|
155 | if(isMatched(match,u)) { |
---|
156 | Node v = getMatchedNode(match,u); |
---|
157 | if(!nodesComplete.contains(u)) { |
---|
158 | getMappedNode(map,u).setVwgt(u.getVwgt() + v.getVwgt()); |
---|
159 | getMappedNode(map,u).setCewgt(u.getCewgt() + v.getCewgt() + g.findEdge(u, v).getWeight()); |
---|
160 | getMappedNode(map,u).setAdjwgt(u.getAdjwgt() + v.getAdjwgt() - 2 * g.findEdge(u, v).getWeight()); |
---|
161 | nodesComplete.add(u); |
---|
162 | nodesComplete.add(v); |
---|
163 | } |
---|
164 | } else { |
---|
165 | getMappedNode(map,u).setVwgt(u.getVwgt()); |
---|
166 | getMappedNode(map,u).setCewgt(u.getCewgt()); |
---|
167 | getMappedNode(map,u).setAdjwgt(u.getAdjwgt()); |
---|
168 | } |
---|
169 | } |
---|
170 | return output; |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * Performs the first stage of the METIS algorithm, using RM. |
---|
175 | */ |
---|
176 | public static CoarserGraphElement coarseOneStep(UndirectedGraph g) { |
---|
177 | UndirectedGraph projected = g; |
---|
178 | UndirectedGraph contracted = null; |
---|
179 | Map<Node,Node> match = new HashMap<Node,Node>(); |
---|
180 | Map<Node,Node> map = new HashMap<Node,Node>(); |
---|
181 | RMMatch(g,match,map); |
---|
182 | contracted = contract(g,match,map); |
---|
183 | return new CoarserGraphElement(contracted, projected, match, map); |
---|
184 | } |
---|
185 | |
---|
186 | public static Stack<CoarserGraphElement> coarse(UndirectedGraph g) { |
---|
187 | Stack<CoarserGraphElement> stack = new Stack<CoarserGraphElement>(); |
---|
188 | CoarserGraphElement e; |
---|
189 | UndirectedGraph curr = g; |
---|
190 | do { |
---|
191 | if(debug) |
---|
192 | debugStream.println("-----------------------------------------------------"); |
---|
193 | e = coarseOneStep(curr); |
---|
194 | stack.push(e); |
---|
195 | curr = e.getContracted(); |
---|
196 | if(debug) |
---|
197 | debugStream.println("-----------------------------------------------------"); |
---|
198 | } while(e.getProjected().getVertexCount() > e.getContracted().getVertexCount() && e.getContracted().getVertexCount() > finerSize); |
---|
199 | return stack; |
---|
200 | } |
---|
201 | |
---|
202 | public static void setFinerSize(int i) { |
---|
203 | finerSize = i; |
---|
204 | } |
---|
205 | } |
---|