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
71 changes: 69 additions & 2 deletions src/Build.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ public class Build {
* @param k the maximum word length (exclusive)
*/
public static void printShortWords(Vertex<String> vertex, int k) {
Set<Vertex<String>> visit = new HashSet<>();
printShortWords(vertex, k, visit);
}
public static void printShortWords(Vertex<String> vertex, int k, Set<Vertex<String>> visited) {
if(vertex == null || visited.contains(vertex)) return;
visited.add(vertex);
String word = vertex.data;
if(word.toCharArray().length < k) {
System.out.println(word);
}
for(Vertex<String> newWords : vertex.neighbors) {
printShortWords(newWords, k, visited);
}
}

/**
Expand All @@ -23,7 +36,18 @@ 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>> visit = new HashSet<>();
return longestWord(vertex, visit, "");
}
public static String longestWord(Vertex<String> vertex, Set<Vertex<String>> visited, String max) {
if(vertex == null || visited.contains(vertex)) return "";
visited.add(vertex);
if(max.length() < vertex.data.length()) max = vertex.data;
for(Vertex<String> word : vertex.neighbors) {
String newWord = longestWord(word, visited, max);
if(newWord.length() > max.length()) max = newWord;
}
return max;
}

/**
Expand All @@ -34,6 +58,18 @@ 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>> set = new HashSet<>();
printSelfLoopers(vertex, set);
}
public static <T> void printSelfLoopers(Vertex<T> vertex, Set<Vertex<T>> visit) {
if(vertex == null || visit.contains(vertex)) return;
visit.add(vertex);
for(Vertex<T> neighbor : vertex.neighbors) {
if(neighbor == vertex) {
System.out.println(vertex.data);
}
printSelfLoopers(neighbor, visit);
}
}

/**
Expand All @@ -45,6 +81,16 @@ 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) {
Set<Airport> visit = new HashSet<>();
return canReach(start, destination, visit);
}
public static boolean canReach(Airport start, Airport destination, Set<Airport> visited) {
if(start == null || destination == null || visited.contains(start)) return false;
visited.add(start);
for(Airport next : start.getOutboundFlights()) {
canReach(next, destination, visited);
}
if(visited.contains(destination)) return true;
return false;
}

Expand All @@ -58,6 +104,27 @@ 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<>();
Set<T> seen = new HashSet<>();
Set<T> unseen = new HashSet<>();
return unreachable(graph, starting, seen, unseen);
}
public static <T> Set<T> unreachable(Map<T, List<T>> graph, T starting, Set<T> visited, Set<T> unseen) {
if(graph == null || starting == null || visited.contains(starting)) return new HashSet<>();
if(!graph.containsKey(starting)) {
return graph.keySet();
}

visited.add(starting);
for(T next : graph.get(starting)) {
unreachable(graph, next, visited, unseen);
}

unseen.clear();
for(T allPoint : graph.keySet()) {
if(!visited.contains(allPoint)) {
unseen.add(allPoint);
}
}
return unseen;
}
}