1 25 39 package org.jgrapht.experimental.isomorphism; 40 41 import java.util.*; 42 43 import org.jgrapht.*; 44 import org.jgrapht.experimental.equivalence.*; 45 import org.jgrapht.experimental.permutation.*; 46 47 48 57 class EquivalenceIsomorphismInspector<V, E> 58 extends AbstractExhaustiveIsomorphismInspector<V, E> 59 { 60 61 63 73 public EquivalenceIsomorphismInspector( 74 Graph<V, E> graph1, 75 Graph<V, E> graph2, 76 77 EquivalenceComparator<? super V, ? super Graph<? super V, ? super E>> vertexChecker, 80 EquivalenceComparator<? super E, ? super Graph<? super V, ? super E>> edgeChecker) 81 { 82 super(graph1, graph2, vertexChecker, edgeChecker); 83 } 84 85 90 public EquivalenceIsomorphismInspector( 91 Graph<V, E> graph1, 92 Graph<V, E> graph2) 93 { 94 super(graph1, graph2); 95 } 96 97 99 134 @SuppressWarnings ("unchecked") 135 protected CollectionPermutationIter<V> createPermutationIterator( 136 Set<V> vertexSet1, 137 Set<V> vertexSet2) 138 { 139 if (vertexSet1.size() != vertexSet2.size()) { 140 return null; } 146 147 EquivalenceSet [] eqGroupArray1 = 149 EquivalenceSetCreator.createEqualityGroupOrderedArray( 150 vertexSet1, 151 this.vertexComparator, 152 this.graph1); 153 154 EquivalenceSet [] eqGroupArray2 = 156 EquivalenceSetCreator.createEqualityGroupOrderedArray( 157 vertexSet2, 158 this.vertexComparator, 159 this.graph2); 160 161 boolean reorderSuccess = 163 reorderTargetArrayToMatchSourceOrder(eqGroupArray1, eqGroupArray2); if (!reorderSuccess) { 165 return null; 167 } 168 169 174 V [] reorderingVertexSet1Temp = (V []) new Object [vertexSet1.size()]; 175 fillElementsflatArray(eqGroupArray1, reorderingVertexSet1Temp); 176 vertexSet1.clear(); 177 vertexSet1.addAll(Arrays.asList(reorderingVertexSet1Temp)); 178 179 186 V [] flatVertexArray = (V []) new Object [vertexSet2.size()]; 187 fillElementsflatArray(eqGroupArray2, flatVertexArray); 188 189 int [] groupSizesArray = new int [eqGroupArray1.length]; 191 192 for ( 194 int eqGroupCounter = 0; 195 eqGroupCounter < eqGroupArray2.length; 196 eqGroupCounter++) { 197 groupSizesArray[eqGroupCounter] = 199 eqGroupArray2[eqGroupCounter].size(); 200 } 201 202 ArrayPermutationsIter arrayPermIter = 203 PermutationFactory.createByGroups(groupSizesArray); 204 CollectionPermutationIter<V> vertexPermIter = 205 new CollectionPermutationIter<V>( 206 Arrays.asList(flatVertexArray), 207 arrayPermIter); 208 209 return vertexPermIter; 210 } 211 212 232 private boolean reorderTargetArrayToMatchSourceOrder( 233 EquivalenceSet [] sourceArray, 234 EquivalenceSet [] targetArray) 235 { 236 boolean result = true; 237 for ( 238 int sourceIndex = 0; 239 sourceIndex < sourceArray.length; 240 sourceIndex++) { 241 int currTargetIndex = sourceIndex; 242 243 EquivalenceSet sourceEqGroup = sourceArray[sourceIndex]; 245 EquivalenceSet targetEqGroup = targetArray[currTargetIndex]; 246 if (!sourceEqGroup.equals(targetEqGroup)) { 247 boolean foundMatch = false; 250 int sourceSize = sourceEqGroup.size(); 251 int sourceHashCode = sourceEqGroup.hashCode(); 252 while ( 253 (targetEqGroup.size() == sourceSize) 254 && (targetEqGroup.hashCode() == sourceHashCode) 255 && (currTargetIndex < targetArray.length)) { 256 currTargetIndex++; 257 targetEqGroup = targetArray[currTargetIndex]; 258 if (targetEqGroup.equals(sourceEqGroup)) { 259 foundMatch = true; 260 261 targetArray[currTargetIndex] = targetArray[sourceIndex]; 264 targetArray[sourceIndex] = targetEqGroup; 265 } 266 } 267 if (!foundMatch) { 268 result = false; 272 break; 273 } 274 } 275 } 276 return result; 277 } 278 279 283 protected void fillElementsflatArray( 284 EquivalenceSet [] eqGroupArray, 285 Object [] flatVertexArray) 286 { 287 int flatVertexArrayNextFree = 0; 289 for ( 291 int eqGroupCounter = 0; 292 eqGroupCounter < eqGroupArray.length; 293 eqGroupCounter++) { 294 Object [] currGroupArray = eqGroupArray[eqGroupCounter].toArray(); 295 296 System.arraycopy( 299 currGroupArray, 0, flatVertexArray, flatVertexArrayNextFree, currGroupArray.length ); 305 flatVertexArrayNextFree += currGroupArray.length; 306 } 307 } 308 309 316 protected boolean areVertexSetsOfTheSameEqualityGroup( 317 Set vertexSet1, 318 Set vertexSet2) 319 { 320 return true; 321 } 322 } 323 | Popular Tags |