// Josh Ervin, thanks to Erika Wolfe :) // This program reads a Graphviz graph file of friendships and allows the user // to find the distance between two people in the graph. import java.io.*; import java.util.*; public class Friends { public static void main(String[] args) throws FileNotFoundException { System.out.println("Welcome to the cse143 friend finder."); Scanner input = new Scanner(new File("friends.dot")); Map> friends = readFile(input); System.out.println("On average, each TA has " + computeAverage(friends) + " friends"); Scanner console = new Scanner(System.in); System.out.print("starting name? "); String name1 = console.next(); if (!friends.containsKey(name1)) { System.out.println("That person is not in the data file."); } else { System.out.print("target name? "); String name2 = console.next(); showMatches(friends, name1, name2); } } // pre : friends is a mapping from a person to a set of their friends // post: computes the average number of friends that each person has public static double computeAverage(Map> friends) { int total = 0; for (String name : friends.keySet()) { Set theirFriends = friends.get(name); total += theirFriends.size(); } return (double) total / friends.size(); } // pre : input is open and contains a legal Graphviz file of friendship // connections where each friend is listed as a single token // post: returns a map that contains for each person the set of friends for // that person public static Map> readFile(Scanner input) { Map> friends = new TreeMap>(); while (input.hasNextLine()) { String line = input.nextLine(); if (line.contains("--")) { Scanner lineData = new Scanner(line); String name1 = lineData.next(); lineData.next(); // this skips the "--" token String name2 = lineData.next(); // Added since Wednesday's lecture addTo(friends, name1, name2); addTo(friends, name2, name1); } } return friends; } // post: computes the distance between two people, printing the various // lists of friends with their distance from name1 // showMatches(friends, "hunter", "mitchell"); public static void showMatches(Map> friends, String name1, String name2) { Set currGroup = new TreeSet<>(); Set visited = new HashSet<>(); currGroup.add(name1); int distance = 0; while (!currGroup.isEmpty() && !currGroup.contains(name2)) { Set nextGroup = new TreeSet<>(); for (String person : currGroup) { Set neighbors = friends.get(person); for (String neighbor : neighbors) { if (!visited.contains(neighbor)) { nextGroup.add(neighbor); } } } visited.addAll(currGroup); currGroup = nextGroup; distance++; System.out.println(" " + distance + " away: " + currGroup); } } // post: records a friendship for name1. If name1 is not in the map, a new // set is added to the map public static void addTo(Map> friends, String name1, String name2) { /* if (!friends.containsKey(name1)) { Set theFriends = new TreeSet(); friends.put(name1, theFriends); theFriends.add(name2); } else { Set theFriends = friends.get(name1); theFriends.add(name2); } */ if (!friends.containsKey(name1)) { friends.put(name1, new TreeSet()); } friends.get(name1).add(name2); } }