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
79 changes: 71 additions & 8 deletions src/Build.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import java.util.Set;


public class Build {
public class Build
{

/**
* Prints words that are reachable from the given vertex and are strictly shorter than k characters.
Expand All @@ -13,7 +14,19 @@ public class Build {
* @param vertex the starting vertex
* @param k the maximum word length (exclusive)
*/
public static void printShortWords(Vertex<String> vertex, int k) {
public static void printShortWords(Vertex<String> vertex, int k)
{
Set<Vertex<String>> visited = new HashSet<>();
printShortWords(vertex, k, visited);

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

}

/**
Expand All @@ -22,8 +35,19 @@ public static void printShortWords(Vertex<String> vertex, int k) {
* @param vertex the starting vertex
* @return the longest reachable word, or an empty string if the vertex is null
*/
public static String longestWord(Vertex<String> vertex) {
return "";
public static String longestWord(Vertex<String> vertex)
{
Set<Vertex<String>> visited = new HashSet<>();
String longest = "";
return longestWord(vertex, visited, longest);
}
public static String longestWord(Vertex<String> vertex, Set<Vertex<String>> visited, String longest)
{
if (vertex == null || visited.contains(vertex)) return longest;
visited.add(vertex);
if (vertex.data.length() > longest.length()) longest = vertex.data;
for (Vertex<String> neighbor : vertex.neighbors) return longestWord(neighbor, visited, longest);
return longest;
}

/**
Expand All @@ -33,7 +57,19 @@ public static String longestWord(Vertex<String> vertex) {
* @param vertex the starting vertex
* @param <T> the type of values stored in the vertices
*/
public static <T> void printSelfLoopers(Vertex<T> vertex) {
public static <T> void printSelfLoopers(Vertex<T> vertex)
{
Set<Vertex<T>> visited = new HashSet<>();
printSelfLoopers(vertex, visited);

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

}

/**
Expand All @@ -44,8 +80,22 @@ public static <T> void printSelfLoopers(Vertex<T> vertex) {
* @param destination the destination airport
* @return true if the destination is reachable from the start, false otherwise
*/
public static boolean canReach(Airport start, Airport destination) {
public static boolean canReach(Airport start, Airport destination)
{
Set<Airport> visited = new HashSet<>();
return canReach(start, destination, visited);
}
public static boolean canReach(Airport start, Airport destination, Set<Airport> visited)
{
if (start == destination) return true;
if (start == null || destination == null || visited.contains(start)) return false;
visited.add(start);
for (Airport flight : start.getOutboundFlights())
{
if (canReach(flight, destination, visited)) return true;
}
return false;

}

/**
Expand All @@ -57,7 +107,20 @@ public static boolean canReach(Airport start, Airport destination) {
* @param <T> the type of values stored in the graph
* @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<>();
public static <T> Set<T> unreachable(Map<T, List<T>> graph, T starting)
{
if (graph == null) return new HashSet<>();
Set<T> unvisited = new HashSet<>();
for (T key : graph.keySet()) unvisited.add(key);
if (!graph.containsKey(starting)) return unvisited;
return unreachable(graph, starting, unvisited);
}
public static <T> Set<T> unreachable(Map<T, List<T>> graph, T starting, Set<T> unvisited)
{
if (starting == null || !unvisited.contains(starting)) return unvisited;
unvisited.remove(starting);
for (T nextPoint : graph.get(starting)) unreachable(graph, nextPoint, unvisited);
return unvisited;
}

}