1 25 39 package org.jgrapht.experimental.isomorphism; 40 41 import org.jgrapht.*; 42 import org.jgrapht.experimental.equivalence.*; 43 44 45 58 public class VertexDegreeEquivalenceComparator<V, E> 59 implements EquivalenceComparator<V, Graph<V, E>> 60 { 61 62 64 66 public VertexDegreeEquivalenceComparator() 67 { 68 } 69 70 72 81 public boolean equivalenceCompare( 82 V vertex1, 83 V vertex2, 84 Graph<V, E> context1, 85 Graph<V, E> context2) 86 { 87 InOutDegrees inOut1 = getInOutDegrees(context1, vertex1); 90 InOutDegrees inOut2 = getInOutDegrees(context2, vertex2); 91 boolean result = inOut1.equals(inOut2); 92 return result; 93 } 94 95 100 public int equivalenceHashcode(V vertex, Graph<V, E> context) 101 { 102 InOutDegrees inOut = getInOutDegrees(context, vertex); 103 104 StringBuffer sb = new StringBuffer (); 106 sb.append(String.valueOf(inOut.inDegree)); 107 sb.append("-"); sb.append(String.valueOf(inOut.outDegree)); 109 return sb.toString().hashCode(); 110 } 111 112 120 protected InOutDegrees getInOutDegrees( 121 Graph<V, E> aContextGraph, 122 V vertex) 123 { 124 int inVertexDegree = 0; 125 int outVertexDegree = 0; 126 if (aContextGraph instanceof UndirectedGraph) { 127 UndirectedGraph<V, E> undirectedGraph = 128 (UndirectedGraph<V, E>) aContextGraph; 129 inVertexDegree = undirectedGraph.degreeOf(vertex); 130 outVertexDegree = inVertexDegree; } else if (aContextGraph instanceof DirectedGraph) { 132 DirectedGraph<V, E> directedGraph = 133 (DirectedGraph<V, E>) aContextGraph; 134 inVertexDegree = directedGraph.inDegreeOf(vertex); 135 outVertexDegree = directedGraph.outDegreeOf(vertex); 136 } else { 137 throw new RuntimeException ( 138 "contextGraph is of unsupported type . It must be one of these two :" 139 + " UndirectedGraph or DirectedGraph"); 140 } 141 return new InOutDegrees(inVertexDegree, outVertexDegree); 142 } 143 144 146 154 protected class InOutDegrees 155 { 156 public int inDegree; 157 public int outDegree; 158 159 public InOutDegrees(int aInDegree, int aOutDegree) 160 { 161 this.inDegree = aInDegree; 162 this.outDegree = aOutDegree; 163 } 164 165 171 public boolean equals(Object obj) 172 { 173 InOutDegrees other = (InOutDegrees) obj; 174 return 175 ((this.inDegree == other.inDegree) 176 && (this.outDegree == other.outDegree)); 177 } 178 } 179 } 180 | Popular Tags |