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 import org.jgrapht.util.*; 47 48 49 58 abstract class AbstractExhaustiveIsomorphismInspector<V, E> 59 implements GraphIsomorphismInspector<IsomorphismRelation> 60 { 61 62 64 public static EquivalenceComparator<Object , Object > edgeDefaultIsomorphismComparator = 65 new UniformEquivalenceComparator<Object , Object >(); 66 public static EquivalenceComparator<Object , Object > vertexDefaultIsomorphismComparator = 67 new UniformEquivalenceComparator<Object , Object >(); 68 69 71 protected EquivalenceComparator<? super E, ? super Graph<V, ? super E>> edgeComparator; 72 protected EquivalenceComparator<? super V, ? super Graph<? super V, E>> vertexComparator; 73 74 protected Graph<V, E> graph1; 75 protected Graph<V, E> graph2; 76 77 private PrefetchIterator<IsomorphismRelation> nextSupplier; 78 79 private GraphOrdering lableGraph1; 81 private LinkedHashSet<V> graph1VertexSet; 82 private LinkedHashSet<E> graph2EdgeSet; 83 private CollectionPermutationIter<V> vertexPermuteIter; 84 private Set<V> currVertexPermutation; 86 88 90 100 public AbstractExhaustiveIsomorphismInspector( 101 Graph<V, E> graph1, 102 Graph<V, E> graph2, 103 104 EquivalenceComparator<? super V, ? super Graph<? super V, ? super E>> vertexChecker, 107 EquivalenceComparator<? super E, ? super Graph<? super V, ? super E>> edgeChecker) 108 { 109 this.graph1 = graph1; 110 this.graph2 = graph2; 111 112 if (vertexChecker != null) { 113 this.vertexComparator = vertexChecker; 114 } else { 115 this.vertexComparator = vertexDefaultIsomorphismComparator; 116 } 117 118 122 if (edgeChecker != null) { 123 this.edgeComparator = edgeChecker; 124 } 125 126 init(); 127 } 128 129 137 public AbstractExhaustiveIsomorphismInspector( 138 Graph<V, E> graph1, 139 Graph<V, E> graph2) 140 { 141 this( 142 graph1, 143 graph2, 144 edgeDefaultIsomorphismComparator, 145 vertexDefaultIsomorphismComparator); 146 } 147 148 150 164 private void init() 165 { 166 this.nextSupplier = 167 new PrefetchIterator<IsomorphismRelation>( 168 new NextFunctor()); 170 171 this.graph1VertexSet = new LinkedHashSet<V>(this.graph1.vertexSet()); 172 173 this.vertexPermuteIter = 175 createPermutationIterator( 176 this.graph1VertexSet, 177 this.graph2.vertexSet()); 178 179 this.lableGraph1 = 180 new GraphOrdering<V, E>( 181 this.graph1, 182 this.graph1VertexSet, 183 this.graph1.edgeSet()); 184 185 this.graph2EdgeSet = new LinkedHashSet<E>(this.graph2.edgeSet()); 186 } 187 188 197 protected abstract CollectionPermutationIter<V> createPermutationIterator( 198 Set<V> vertexSet1, 199 Set<V> vertexSet2); 200 201 242 private IsomorphismRelation<V, E> findNextIsomorphicGraph() 243 { 244 boolean result = false; 245 IsomorphismRelation<V, E> resultRelation = null; 246 if (this.vertexPermuteIter != null) { 247 while (this.vertexPermuteIter.hasNext()) { 249 currVertexPermutation = this.vertexPermuteIter.getNextSet(); 250 251 if ( 253 !areVertexSetsOfTheSameEqualityGroup( 254 this.graph1VertexSet, 255 currVertexPermutation)) { 256 continue; } 258 259 GraphOrdering<V, E> currPermuteGraph = 261 new GraphOrdering<V, E>( 262 this.graph2, 263 currVertexPermutation, 264 this.graph2EdgeSet); 265 266 if (this.lableGraph1.equalsByEdgeOrder(currPermuteGraph)) { 268 resultRelation = 270 new IsomorphismRelation<V, E>( 271 new ArrayList<V>(graph1VertexSet), 272 new ArrayList<V>(currVertexPermutation), 273 graph1, 274 graph2); 275 276 boolean edgeEq = 278 areAllEdgesEquivalent( 279 resultRelation, 280 this.edgeComparator); 281 if (edgeEq) { 283 result = true; 284 break; 285 } 286 } 287 } 288 } 289 290 if (result == true) { 291 return resultRelation; 292 } else { 293 return null; 294 } 295 } 296 297 307 protected abstract boolean areVertexSetsOfTheSameEqualityGroup( 308 Set<V> vertexSet1, 309 Set<V> vertexSet2); 310 311 317 protected boolean areAllEdgesEquivalent( 318 IsomorphismRelation<V, E> resultRelation, 319 EquivalenceComparator<? super E, ? super Graph<V, E>> edgeComparator) 320 { 321 boolean checkResult = true; 322 323 if (edgeComparator == null) { 324 return true; 326 } 327 328 try { 329 Set<E> edgeSet = this.graph1.edgeSet(); 330 331 for (E currEdge : edgeSet) { 332 E correspondingEdge = 333 resultRelation.getEdgeCorrespondence(currEdge, true); 334 335 if ( 337 !edgeComparator.equivalenceCompare( 338 currEdge, 339 correspondingEdge, 340 this.graph1, 341 this.graph2)) { 342 checkResult = false; 343 break; 344 } 345 } 346 } catch (IllegalArgumentException illegal) { 347 checkResult = false; 348 } 349 350 return checkResult; 351 } 352 353 356 public IsomorphismRelation nextIsoRelation() 357 { 358 return next(); 359 } 360 361 368 public boolean isIsomorphic() 369 { 370 return !(this.nextSupplier.isEnumerationStartedEmpty()); 371 } 372 373 376 public boolean hasNext() 377 { 378 boolean result = this.nextSupplier.hasMoreElements(); 379 380 return result; 381 } 382 383 386 public IsomorphismRelation next() 387 { 388 return this.nextSupplier.nextElement(); 389 } 390 391 394 public void remove() 395 { 396 throw new UnsupportedOperationException ( 397 "remove() method is not supported in AdaptiveIsomorphismInspectorFactory." 398 + " There is no meaning to removing an isomorphism result."); 399 } 400 401 403 private class NextFunctor 404 implements PrefetchIterator.NextElementFunctor<IsomorphismRelation> 405 { 406 public IsomorphismRelation nextElement() 407 throws NoSuchElementException 408 { 409 IsomorphismRelation resultRelation = findNextIsomorphicGraph(); 410 if (resultRelation != null) { 411 return resultRelation; 412 } else { 413 throw new NoSuchElementException( 414 "IsomorphismInspector does not have any more elements"); 415 } 416 } 417 } 418 } 419 | Popular Tags |