|
| 1 | +import java.util.*; |
| 2 | +import org.jgrapht.*; |
| 3 | +import org.jgrapht.alg.interfaces.MatchingAlgorithm.*; |
| 4 | +import org.jgrapht.graph.*; |
| 5 | + |
| 6 | +public class MSC_distance { |
| 7 | + MSC_distance(ArrayList<User> input){ |
| 8 | + Set<User> set = new HashSet<User>(input); |
| 9 | + see(set); |
| 10 | + Set<User> OutputUser = main(set); |
| 11 | + see(OutputUser); |
| 12 | + |
| 13 | + } |
| 14 | + |
| 15 | + public void see(Set<User> set) { |
| 16 | + if( set == null ) { |
| 17 | + System.out.println( "This set is null" ); |
| 18 | + return; |
| 19 | + } |
| 20 | + for(User a: set) { |
| 21 | + for(Integer aa: a.interest) { |
| 22 | + System.out.print(aa+":"); |
| 23 | + } |
| 24 | + |
| 25 | + System.out.println("#"+a.x+"::"+a.y+"#"); |
| 26 | + } |
| 27 | + System.out.println("-----------------------"); |
| 28 | + } |
| 29 | + public void see(User a) { |
| 30 | + if( a == null ) { |
| 31 | + System.out.println( "This user is null" ); |
| 32 | + return; |
| 33 | + } |
| 34 | + for(Integer aa: a.interest) { |
| 35 | + System.out.print(aa+":"); |
| 36 | + } |
| 37 | + System.out.println(""); |
| 38 | + } |
| 39 | + public void isee( Set<Integer> set ) { |
| 40 | + for( Integer i: set ) |
| 41 | + System.out.print( i + ":" ); |
| 42 | + System.out.println( "#" ); |
| 43 | + } |
| 44 | + |
| 45 | + public Set<User> main(Set<User> set) { |
| 46 | + |
| 47 | + if(set.size()==0) { |
| 48 | + return new HashSet<User>(); |
| 49 | + } |
| 50 | + |
| 51 | + set = DelSubUser(set); |
| 52 | + |
| 53 | + Set<User> unique = UniqueElement(set); |
| 54 | + if( unique.size() > 0 ) { |
| 55 | + set = delete( set, unique ); |
| 56 | + unique.addAll(main(set)); |
| 57 | + return unique; |
| 58 | + } |
| 59 | + |
| 60 | + User label = ChooseMaxSizeUser(set); |
| 61 | + |
| 62 | + if( label.interest.size()==2) { |
| 63 | + return Condition2msc(set); |
| 64 | + } |
| 65 | + |
| 66 | + set.remove(label); |
| 67 | + Set<User> a = main(set); |
| 68 | + set = delete( set, new HashSet<User>( Collections.singleton( label ) ) ); |
| 69 | + Set<User> b = main(set); |
| 70 | + |
| 71 | + if( a.size() != 1 + b.size() ) |
| 72 | + return a.size() < 1 + b.size()? a : b; |
| 73 | + else |
| 74 | + return CountDistance( a ) < CountDistance( b )? a : b; |
| 75 | + } |
| 76 | + |
| 77 | + public double CountDistance(Set<User> InputUser) { |
| 78 | + double max=0; |
| 79 | + for(User a: InputUser) { |
| 80 | + for(User b: InputUser) { |
| 81 | + double trial = Math.pow(Math.pow(a.x-b.x, 2)+Math.pow(a.y-b.y, 2),(0.5)); |
| 82 | + if(trial>max) { |
| 83 | + max = trial; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + return max; |
| 89 | + } |
| 90 | + |
| 91 | + public Set<User> DelSubUser(Set<User> InputUser) { |
| 92 | + Set<User> ForDelete = new HashSet<>(); |
| 93 | + for(User a: InputUser) { |
| 94 | + for(User b: InputUser) { |
| 95 | + if(isSubSet(a.interest,b.interest)&& !a.equals(b)) { |
| 96 | + if(!ForDelete.contains(b)) { |
| 97 | + ForDelete.add(b); |
| 98 | + //System.out.println(a.interest.size()+"::"+b.interest.size()); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + for(User del: ForDelete) { |
| 104 | + InputUser.remove(del); |
| 105 | + } |
| 106 | + return InputUser; |
| 107 | + } |
| 108 | + |
| 109 | + public boolean isSubSet(Set<Integer> a, Set<Integer> b) { |
| 110 | + |
| 111 | + for(Integer bb : b) { |
| 112 | + if(!a.contains(bb)) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + } |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + public Set<User> UniqueElement(Set<User> InputUser) { |
| 120 | + Set<Integer> UniqueInterest = new HashSet<>(); |
| 121 | + Set<Integer> DualInterest= new HashSet<>(); |
| 122 | + Set<User> OutputUser = new HashSet<>(); |
| 123 | + |
| 124 | + for(User a: InputUser) { |
| 125 | + for(Integer aa: a.interest) { |
| 126 | + if(!UniqueInterest.contains(aa)) { |
| 127 | + UniqueInterest.add(aa); |
| 128 | + }else { |
| 129 | + DualInterest.add(aa); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + UniqueInterest.removeAll(DualInterest); |
| 134 | + for(User a: InputUser) { |
| 135 | + for(Integer aa: a.interest) { |
| 136 | + if(UniqueInterest.contains(aa) && !OutputUser.contains(a)) { |
| 137 | + OutputUser.add(a); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + return OutputUser; |
| 142 | + } |
| 143 | + |
| 144 | + public Set<User> delete( Set<User> user, Set<User> uniq ) { |
| 145 | + Set<User> ret = new HashSet<>(); |
| 146 | + Set<Integer> element = new HashSet<>(); |
| 147 | + |
| 148 | + for( User u: uniq ) |
| 149 | + for( Integer i: u.interest ) |
| 150 | + element.add( i ); |
| 151 | + for( User u: user ) { |
| 152 | + User n = new User(); |
| 153 | + for( Integer i: u.interest ) |
| 154 | + if( !element.contains( i ) ) |
| 155 | + n.interest.add( i ); |
| 156 | + if( n.interest.size() > 0 ) |
| 157 | + ret.add( n ); |
| 158 | + } |
| 159 | + |
| 160 | + return ret; |
| 161 | + } |
| 162 | + |
| 163 | + public User ChooseMaxSizeUser(Set<User> InputUser) { |
| 164 | + User max = null; |
| 165 | + int size=0; |
| 166 | + for(User user: InputUser) { |
| 167 | + if(user.interest.size()>size) { |
| 168 | + size = user.interest.size(); |
| 169 | + max = user; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return max; |
| 174 | + } |
| 175 | + public Set<User> Condition2msc(Set<User> InputUser) { |
| 176 | + |
| 177 | + Graph<Integer, DefaultEdge> g = new SimpleGraph<>(DefaultEdge.class); |
| 178 | + Set<User> OutputUser = new HashSet<User>(); |
| 179 | + |
| 180 | + for(User a: InputUser) { |
| 181 | + Integer[] arr = new Integer[a.interest.size()]; |
| 182 | + System.arraycopy(a.interest.toArray(), 0, arr, 0, a.interest.size()); |
| 183 | + g.addVertex(arr[0]); |
| 184 | + g.addVertex(arr[1]); |
| 185 | + g.addEdge(arr[0], arr[1]); |
| 186 | + } |
| 187 | + |
| 188 | + MatchingImpl m = new MatchingImpl(g,g.edgeSet(),0); |
| 189 | + |
| 190 | + if(m.isPerfect()) { |
| 191 | + for(User a:InputUser) { |
| 192 | + OutputUser.add(a); |
| 193 | + } |
| 194 | + return OutputUser; |
| 195 | + } |
| 196 | + else { |
| 197 | + Set<Integer> insert = new HashSet<Integer>(); |
| 198 | + for(Integer v: g.vertexSet()) { |
| 199 | + if(!m.isMatched(v) && !insert.contains(v)) { |
| 200 | + for(User a: InputUser) { |
| 201 | + for(Integer aa: a.interest) { |
| 202 | + if(v.equals(aa)) { |
| 203 | + OutputUser.add(a); |
| 204 | + insert.add(v); |
| 205 | + break; |
| 206 | + } |
| 207 | + } |
| 208 | + if( insert.contains(v) ) |
| 209 | + break; |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + Set<DefaultEdge> edge = m.getEdges(); |
| 214 | + for(DefaultEdge e: edge ) { |
| 215 | + Integer a = g.getEdgeSource(e); |
| 216 | + Integer b = g.getEdgeTarget(e); |
| 217 | + for(User user: InputUser) { |
| 218 | + if(user.interest.contains(a) && user.interest.contains(b)) { |
| 219 | + OutputUser.add(user); |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + return OutputUser; |
| 225 | + } |
| 226 | +} |
0 commit comments