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
82 changes: 80 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>> 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 @@ -23,7 +36,23 @@ 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>> visited = new HashSet<>();
return longestWord(vertex, visited);
}

public static String longestWord(Vertex<String> vertex, Set<Vertex<String>> visited) {
if (vertex == null || visited.contains(vertex)) return "";
visited.add(vertex);

String longestWord = vertex.data;

for (Vertex<String> neighbor : vertex.neighbors) {
// longestWord = vertex.data.length() > longestWord(neighbor, visited).length() ? vertex.data : longestWord(neighbor, visited);
String longestNeighborWord = longestWord(neighbor, visited);
if (longestNeighborWord.length() > longestWord.length()) longestWord = longestNeighborWord;
}

return longestWord;
}

/**
Expand All @@ -34,6 +63,19 @@ 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>> 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 @@ -45,9 +87,21 @@ 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> visited = new HashSet<>();
canReach(start, destination, visited);
if (visited.contains(destination)) return true;
return false;
}

public static void canReach(Airport start, Airport destination, Set<Airport> visited) {
if (start == null || visited.contains(start)) return;
visited.add(start);

for (Airport outbound : start.getOutboundFlights()) {
canReach(outbound, destination, visited);
}
}

/**
* 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 @@ -57,7 +111,31 @@ 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
*/

//traverse every possible value reachable from starting
//add each value to a reachable set
//itterate map keys
//add to unreachable set if not in reachable set
public static <T> Set<T> unreachable(Map<T, List<T>> graph, T starting) {
return new HashSet<>();
Set<T> reachable = new HashSet<>();
Set<T> unreachable = new HashSet<>();
unreachableHelper(graph, starting, reachable);

for (T key : graph.keySet()) {
if (!reachable.contains(key)) unreachable.add(key);
}

return unreachable;
}

public static <T> void unreachableHelper(Map<T, List<T>> graph, T starting, Set<T> reachable) {
if (starting == null || reachable.contains(starting)) return;
reachable.add(starting);

if (graph.get(starting) != null && !graph.get(starting).isEmpty()) {
for (T neighbor : graph.get(starting)) {
unreachableHelper(graph, neighbor, reachable);
}
}
}
}