Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions src/Build.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ public class Build {
* @param k the maximum word length (exclusive)
*/
public static void printShortWords(Vertex<String> vertex, int k) {
Set<Vertex<String>> reachable = new HashSet<>();
printShortWords(vertex, k, reachable);
}

public static <T> void printShortWords(Vertex<String> vertex, int k, Set<Vertex<String>> reachable) {
if(vertex == null || reachable.contains(vertex)) return;
reachable.add(vertex);
if(vertex.data.length() < k) System.out.println(vertex.data);
for(Vertex<String> neighbor : vertex.neighbors) {
printShortWords(neighbor, k, reachable);
}
}

/**
Expand All @@ -23,7 +34,20 @@ public static void printShortWords(Vertex<String> vertex, int k) {
* @return the longest reachable word, or an empty string if the vertex is null
*/
public static String longestWord(Vertex<String> vertex) {
return "";
Set<Vertex<String>> reachable = new HashSet<>();
String longest = "";
return longestWord(vertex, reachable, longest);
}

public static String longestWord(Vertex<String> vertex, Set<Vertex<String>> 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<String> neighbor : vertex.neighbors) {
String temp = longestWord(neighbor, reachable, longest);
if(temp.length() > longest.length()) longest = temp;
}
return longest;
}

/**
Expand All @@ -34,6 +58,17 @@ public static String longestWord(Vertex<String> vertex) {
* @param <T> the type of values stored in the vertices
*/
public static <T> void printSelfLoopers(Vertex<T> vertex) {
Set<Vertex<T>> reachable = new HashSet<>();
printSelfLoopers(vertex, reachable);
}

public static <T> void printSelfLoopers(Vertex<T> vertex, Set<Vertex<T>> reachable ) {
if(vertex == null || reachable.contains(vertex)) return;
reachable.add(vertex);
if(vertex.neighbors.contains(vertex)) System.out.println(vertex.data);
for(Vertex<T> neighbor : vertex.neighbors) {
printSelfLoopers(neighbor, reachable);
}
}

/**
Expand All @@ -45,9 +80,20 @@ public static <T> void printSelfLoopers(Vertex<T> vertex) {
* @return true if the destination is reachable from the start, false otherwise
*/
public static boolean canReach(Airport start, Airport destination) {
return false;
Set<Airport> reachable = new HashSet<>();
return canReach(start, destination, reachable);
}

public static boolean canReach(Airport start, Airport destination, Set<Airport> 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.
Expand All @@ -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 <T> Set<T> unreachable(Map<T, List<T>> graph, T starting) {
return new HashSet<>();
if(!graph.keySet().contains(starting)) return graph.keySet();
Set<T> reachable = new HashSet<>();
Set<T> unreachable = new HashSet<>();
reachable(graph, starting, reachable);
for(T value : graph.keySet()) {
if(!reachable.contains(value)) unreachable.add(value);
}
return unreachable;
}
public static <T> void reachable(Map<T, List<T>> graph, T starting, Set<T> reachable) {
if(starting == null || reachable.contains(starting)) return;
reachable.add(starting);
for(T value : graph.get(starting)) {
reachable(graph, value, reachable);
}
}
}