Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. package ee.ttu.algoritmid.bond;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class AL08 {
  7.  
  8.     public enum Network {
  9.         FRIENDLY, UNFRIENDLY, UNKNOWN;
  10.     }
  11.     Map<String, Network> people;
  12.  
  13.     private DisjointSubsets disjointSubsets = new DisjointSubsets();
  14.  
  15.     public AL08() {
  16.         people = new HashMap<>();
  17.         friendly("A");
  18.         unfriendly("U");
  19.     }
  20.  
  21.     public DisjointSubsets getDisjointSubsets() {
  22.         return disjointSubsets;
  23.     }
  24.  
  25.     public void talkedToEachOther(String name1, String name2) {
  26.         disjointSubsets.union(name1,name2);
  27.         String rootOne = disjointSubsets.find(name1);
  28.         String rootTwo = disjointSubsets.find(name2);
  29.         if(people.get(rootOne) != people.get(rootTwo))
  30.         {
  31.             if(people.get(rootOne) == Network.UNKNOWN)
  32.             {
  33.                 people.put(rootOne, people.get(rootTwo));
  34.             }
  35.             else
  36.             {
  37.                 people.put(rootTwo, people.get(rootOne));
  38.             }
  39.         }
  40.     }
  41.  
  42.     public void addPerson(String name) {
  43.         disjointSubsets.addSubset(name);
  44.         people.put(name, Network.UNKNOWN);
  45.     }
  46.  
  47.     public void friendly(String name) {
  48.         disjointSubsets.addSubset(name);
  49.         people.put(name, Network.FRIENDLY);
  50.     }
  51.  
  52.     public void unfriendly(String name) {
  53.         disjointSubsets.addSubset(name);
  54.         people.put(name, Network.UNFRIENDLY);
  55.     }
  56.  
  57.     public Network memberOfNetwork(String name) {
  58.         String root = disjointSubsets.find(name);
  59.  
  60.         return people.get(root);
  61.     }
  62.  
  63.     public static void main(String[] args) {
  64.  
  65.     }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement