diff --git a/src/Build.java b/src/Build.java index 70810d4..476bdf5 100644 --- a/src/Build.java +++ b/src/Build.java @@ -14,6 +14,17 @@ public class Build { * @param k the maximum word length (exclusive) */ public static void printShortWords(Vertex vertex, int k) { + Set> reachable = new HashSet<>(); + printShortWords(vertex, k, reachable); + } + + public static void printShortWords(Vertex vertex, int k, Set> reachable) { + if(vertex == null || reachable.contains(vertex)) return; + reachable.add(vertex); + if(vertex.data.length() < k) System.out.println(vertex.data); + for(Vertex neighbor : vertex.neighbors) { + printShortWords(neighbor, k, reachable); + } } /** @@ -23,7 +34,20 @@ public static void printShortWords(Vertex vertex, int k) { * @return the longest reachable word, or an empty string if the vertex is null */ public static String longestWord(Vertex vertex) { - return ""; + Set> reachable = new HashSet<>(); + String longest = ""; + return longestWord(vertex, reachable, longest); + } + + public static String longestWord(Vertex vertex, Set> reachable, String longest) { + if(vertex == null || reachable.contains(vertex)) return longest; + reachable.add(vertex); + if(vertex.data.length() > longest.length()) longest = vertex.data; + for(Vertex neighbor : vertex.neighbors) { + String temp = longestWord(neighbor, reachable, longest); + if(temp.length() > longest.length()) longest = temp; + } + return longest; } /** @@ -34,6 +58,17 @@ public static String longestWord(Vertex vertex) { * @param the type of values stored in the vertices */ public static void printSelfLoopers(Vertex vertex) { + Set> reachable = new HashSet<>(); + printSelfLoopers(vertex, reachable); + } + + public static void printSelfLoopers(Vertex vertex, Set> reachable ) { + if(vertex == null || reachable.contains(vertex)) return; + reachable.add(vertex); + if(vertex.neighbors.contains(vertex)) System.out.println(vertex.data); + for(Vertex neighbor : vertex.neighbors) { + printSelfLoopers(neighbor, reachable); + } } /** @@ -45,9 +80,20 @@ public static void printSelfLoopers(Vertex vertex) { * @return true if the destination is reachable from the start, false otherwise */ public static boolean canReach(Airport start, Airport destination) { - return false; + Set reachable = new HashSet<>(); + return canReach(start, destination, reachable); } + public static boolean canReach(Airport start, Airport destination, Set reachable) { + if(start == null || reachable.contains(start)) return false; + if(start == destination ) return true; + reachable.add(start); + for(Airport airport : start.getOutboundFlights()) { + if(canReach(airport, destination, reachable)) return true; + } + return false; + } + /** * Returns the set of all values in the graph that cannot be reached from the given starting value. * The graph is represented as a map where each vertex is associated with a list of its neighboring values. @@ -58,6 +104,20 @@ public static boolean canReach(Airport start, Airport destination) { * @return a set of values that cannot be reached from the starting value */ public static Set unreachable(Map> graph, T starting) { - return new HashSet<>(); + if(!graph.keySet().contains(starting)) return graph.keySet(); + Set reachable = new HashSet<>(); + Set unreachable = new HashSet<>(); + reachable(graph, starting, reachable); + for(T value : graph.keySet()) { + if(!reachable.contains(value)) unreachable.add(value); + } + return unreachable; + } + public static void reachable(Map> graph, T starting, Set reachable) { + if(starting == null || reachable.contains(starting)) return; + reachable.add(starting); + for(T value : graph.get(starting)) { + reachable(graph, value, reachable); + } } }