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
126 changes: 124 additions & 2 deletions src/Build.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -14,6 +15,31 @@ public class Build {
* @param k the maximum word length (exclusive)
*/
public static void printShortWords(Vertex<String> vertex, int k) {
Set<Vertex<String>> set = new HashSet<>();

printSHortWordsHelper(set, k, vertex);
}

public static void printSHortWordsHelper(Set<Vertex<String>> current, int k, Vertex<String> vertex)
{
if(vertex == null || current.contains(vertex))
{
return;
}

current.add(vertex);

String name = vertex.data;
if(name.length() < k)
{
System.out.println(name);
}

for(Vertex<String> item : vertex.neighbors)
{
printSHortWordsHelper(current, k, item);
}

}

/**
Expand All @@ -23,7 +49,40 @@ 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 "";
if(vertex == null) return "";

Set<Vertex<String>> set = new HashSet<>();

return longestWordHelper(set, vertex, vertex.data);
}

public static String longestWordHelper(Set<Vertex<String>> set, Vertex<String> vertex, String word)
{

if(vertex == null || set.contains(vertex)) return "";

set.add(vertex);

String newWord = "";
if(word.length() < vertex.data.length())
{
newWord = vertex.data;
}
else
{
newWord = word;
}

for(Vertex<String> item : vertex.neighbors)
{
String longestNeighbor = longestWordHelper(set, item, newWord);
if(longestNeighbor.length() > newWord.length())
{
newWord = longestNeighbor;
}
}

return newWord;
}

/**
Expand All @@ -34,6 +93,29 @@ 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) {
if(vertex == null) return;

Set<Vertex<T>> set = new HashSet<>();

printSelfLoopersHelper(vertex, set);
}


public static <T> void printSelfLoopersHelper(Vertex<T> vertex, Set<Vertex<T>> set)
{
if(set.contains(vertex)) return;

set.add(vertex);

if(vertex.neighbors.contains(vertex))
{
System.out.println(vertex.data);
}

for(Vertex<T> item : vertex.neighbors)
{
printSelfLoopersHelper(item, set);
}
}

/**
Expand All @@ -45,6 +127,27 @@ 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> set = new HashSet<>();

return canReachHelper(start, destination, set);
}

public static boolean canReachHelper(Airport start, Airport des, Set<Airport> set)
{
if(start == des) return true;

set.add(start);

for(Airport item: start.getOutboundFlights())
{
if(!set.contains(item) && item.getAirportCode() != start.getAirportCode())
{
if(canReachHelper(item, des, set) == true)
{
return true;
}
}
}
return false;
}

Expand All @@ -58,6 +161,25 @@ 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> set = new HashSet<>();
unreachableHelper(starting, graph, set);

Set<T> unreachable = new HashSet<>(graph.keySet());
unreachable.removeAll(set);

return unreachable;
}

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

visted.add(current);

for(T item: graph.getOrDefault(current, Collections.emptyList()))
{
unreachableHelper(item, graph, visted);
}
}

}