1 25 39 package org.jgrapht.experimental.isomorphism; 40 41 import org.jgrapht.*; 42 import org.jgrapht.experimental.equivalence.*; 43 import org.jgrapht.graph.*; 44 45 46 64 public class AdaptiveIsomorphismInspectorFactory 65 { 66 67 69 public static final int GRAPH_TYPE_ARBITRARY = 0; 70 public static final int GRAPH_TYPE_PLANAR = 1; 71 public static final int GRAPH_TYPE_TREE = 2; 72 public static final int GRAPH_TYPE_MULTIGRAPH = 3; 73 74 76 85 public static <V, E> GraphIsomorphismInspector createIsomorphismInspector( 86 Graph<V, E> graph1, 87 Graph<V, E> graph2, 88 EquivalenceComparator<? super V, ? super Graph<V, E>> vertexChecker, 89 EquivalenceComparator<? super E, ? super Graph<V, E>> edgeChecker) 90 { 91 int graphType = checkGraphsType(graph1, graph2); 92 return 93 createAppropriateConcreteInspector( 94 graphType, 95 graph1, 96 graph2, 97 vertexChecker, 98 edgeChecker); 99 } 100 101 110 public static <V, E> GraphIsomorphismInspector createIsomorphismInspector( 111 Graph<V, E> graph1, 112 Graph<V, E> graph2) 113 { 114 return createIsomorphismInspector(graph1, graph2, null, null); 115 } 116 117 127 public static <V, E> GraphIsomorphismInspector 128 createIsomorphismInspectorByType( 129 int type, 130 Graph<V, E> graph1, 131 Graph<V, E> graph2, 132 EquivalenceComparator<? super V, ? super Graph<V, E>> vertexChecker, 133 EquivalenceComparator<? super E, ? super Graph<V, E>> edgeChecker) 134 { 135 return 136 createAppropriateConcreteInspector( 137 type, 138 graph1, 139 graph2, 140 vertexChecker, 141 edgeChecker); 142 } 143 144 155 public static <V, E> GraphIsomorphismInspector 156 createIsomorphismInspectorByType( 157 int type, 158 Graph<V, E> graph1, 159 Graph<V, E> graph2) 160 { 161 return 162 createAppropriateConcreteInspector( 163 type, 164 graph1, 165 graph2, 166 null, 167 null); 168 } 169 170 181 protected static <V, E> GraphIsomorphismInspector 182 createAppropriateConcreteInspector( 183 int graphType, 184 Graph<V, E> graph1, 185 Graph<V, E> graph2, 186 EquivalenceComparator<? super V, ? super Graph<V, E>> vertexChecker, 187 EquivalenceComparator<? super E, ? super Graph<V, E>> edgeChecker) 188 { 189 assertUnsupportedGraphTypes(graph1); 190 assertUnsupportedGraphTypes(graph2); 191 GraphIsomorphismInspector currentInspector = null; 192 193 switch (graphType) { 194 case GRAPH_TYPE_PLANAR: 195 case GRAPH_TYPE_TREE: 196 case GRAPH_TYPE_ARBITRARY: 197 currentInspector = 198 createTopologicalExhaustiveInspector( 199 graph1, 200 graph2, 201 vertexChecker, 202 edgeChecker); 203 break; 204 205 default: 206 207 throw new IllegalArgumentException ( 208 "The type was not one of the supported types."); 209 } 210 return currentInspector; 211 } 212 213 223 protected static void assertUnsupportedGraphTypes(Graph g) 224 throws IllegalArgumentException 225 { 226 if ( 227 (g instanceof Multigraph) 228 || (g instanceof DirectedMultigraph) 229 || (g instanceof Pseudograph)) { 230 throw new IllegalArgumentException ( 231 "graph type not supported for the graph" + g); 232 } 233 } 234 235 protected static int checkGraphsType(Graph graph1, Graph graph2) 236 { 237 return GRAPH_TYPE_ARBITRARY; 238 } 239 240 245 @SuppressWarnings ("unchecked") 246 protected static <V, E> GraphIsomorphismInspector 247 createTopologicalExhaustiveInspector( 248 Graph<V, E> graph1, 249 Graph<V, E> graph2, 250 EquivalenceComparator<? super V, ? super Graph<V, E>> vertexChecker, 251 EquivalenceComparator<? super E, ? super Graph<V, E>> edgeChecker) 252 { 253 VertexDegreeEquivalenceComparator<V, E> degreeComparator = 254 new VertexDegreeEquivalenceComparator<V, E>(); 255 EquivalenceComparatorChain<V, Graph<V, E>> vertexChainedChecker = 256 new EquivalenceComparatorChainBase<V, Graph<V, E>>( 257 degreeComparator); 258 vertexChainedChecker.appendComparator(vertexChecker); 259 260 GraphIsomorphismInspector inspector = 261 262 new EquivalenceIsomorphismInspector( 264 graph1, 265 graph2, 266 vertexChainedChecker, 267 edgeChecker); 268 return inspector; 269 } 270 } 271 | Popular Tags |