Skip to content
Open
Show file tree
Hide file tree
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
194 changes: 186 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,46 @@ public class Build {
* @param vertex the starting vertex
* @param k the maximum word length (exclusive)
*/
public static void printShortWords(Vertex<String> vertex, int k) {
//vertex is the node of a tree
//graph is the tree
public static void printShortWords(Vertex<String> vertex, int k)
{
//basecase vertex and the data cannot be null
if (vertex == null || vertex.data == null) return;

//create a set called visited to keep track of visited vertices
//avoid loops
//set that contains vertex that have to contain strings
Set<Vertex<String>> visited = new HashSet<>();
//three arguments dfs search
//calls the recursive version of the method
dfsPrintShortWords(vertex, k, visited);
}

//dfs recursive helper method to graph
private static void dfsPrintShortWords(Vertex<String> current, int k, Set<Vertex<String>> visited)
{
//if visited move on
if (visited.contains(current)) return;
//add it to keep track of visited
visited.add(current);

//if the data is shorter than k
//print
if (current.data.length() < k)
{
System.out.println(current.data);
}

//visit neighbors
//vertex of strings called neighbor
//call the helper method again but allows us to enter all the vertexs in the neighbors list
//for each vertext that contains a string like current.neighbors we want to assign it to neighbor
for (Vertex<String> neighbor : current.neighbors)
{
//neighbor is now used pasesd into this method call
dfsPrintShortWords(neighbor, k, visited);
}
}

/**
Expand All @@ -22,8 +62,43 @@ 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)
{
//vertex cannot be empty or return empty str
if (vertex == null) return "";

//create a set of string called visited to track the vertices visited
Set<Vertex<String>> visited = new HashSet<>();
//dfs search
return dfsLongest(vertex, visited);
}

//helper method to do dfs and return longest word
private static String dfsLongest(Vertex<String> current, Set<Vertex<String>> visited)
{
//if visited continruw on
if (visited.contains(current)) return "";
//track it if visited
visited.add(current);

//current data/word is longest
String longest = current.data;

//traverse through neighbors
//right side is the datastructre and datatype that it contains plus a temp variable name,
for (Vertex<String> neighbor : current.neighbors)
{
//get the longest word from the neighbor
String candidate = dfsLongest(neighbor, visited);
if (candidate.length() > longest.length())
{
//updated longest
longest = candidate;
}
}
//return longest
return longest;

}

/**
Expand All @@ -33,7 +108,36 @@ 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)
{
//null return nothing
if (vertex == null) return;

//track visited nodes called visited with a set
Set<Vertex<T>> visited = new HashSet<>();
//dfs search
dfsPrintSelfLoopers(vertex, visited);
}

//helper method to do dfs search to print neighbors that contains them
private static <T> void dfsPrintSelfLoopers(Vertex<T> current, Set<Vertex<T>> visited)
{
//if visited contains current then we have visited
if (visited.contains(current)) return;
//track it by adding to tracker
visited.add(current);

//check if current is its neighbor
if (current.neighbors.contains(current))
{
System.out.println(current.data);
}

//visit the neighbors
for (Vertex<T> neighbor : current.neighbors)
{
dfsPrintSelfLoopers(neighbor, visited);
}
}

/**
Expand All @@ -44,8 +148,45 @@ 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) {
//start from ariport
public static boolean canReach(Airport start, Airport destination)
{
//basecase start or destination is null return false dont continue
if (start == null || destination == null) return false;
//if start and destination is true then true
if (start == destination) return true;

//set called visited to track visited airports
Set<Airport> visited = new HashSet<>();
//start the dfs
return dfsCanReach(start, destination, visited);
}

//helper method to check airports that have been reached
private static boolean dfsCanReach(Airport current, Airport target, Set<Airport> visited)
{
//if visited already then carry on
//visited contains current then false
if (visited.contains(current)) return false;
//add it current to the tracker
visited.add(current);

//current is target so we reached the target
if (current == target) return true;

//vistited neighbor connected airport
for (Airport neighbor : current.getOutboundFlights())
{
//found valid reachable
if (dfsCanReach(neighbor, target, visited))
{
return true;
}
}

//not reachable
return false;

}

/**
Expand All @@ -57,7 +198,44 @@ 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<>();
//check to see if nodes in a graph is unreachable
//<T> are generaic they can be string, Integer, airport etc
public static <T> Set<T> unreachable(Map<T, List<T>> graph, T starting)
{
//basecase
//set called visited to track visited nodes
Set<T> visited = new HashSet<>();
//dfs search
dfsGraph(starting, graph, visited);

//start with all nodes
Set<T> unreachable = new HashSet<>(graph.keySet());
//remove reachable nodes
unreachable.removeAll(visited);
//return the unreachable
return unreachable;
}

//dfs helper to find reachable nodes
//<T> are generaic they can be string, Integer, airport etc
private static <T> void dfsGraph(T current, Map<T, List<T>> graph, Set<T> visited)
{
//skipt of visited and track it
if (visited.contains(current)) return;
//if visited add it to tracked
visited.add(current);

//get neighbor of current node
List<T> neighbors = graph.get(current);

//if no neighbors stop and dont continue
if (neighbors == null) return;

//recurse and visited neighbors that hasnt been visited
for (T neighbor : neighbors)
{
//recusre dfs
dfsGraph(neighbor, graph, visited);
}
}
}
8 changes: 6 additions & 2 deletions src/Vertex.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import java.util.ArrayList;
import java.util.List;

public class Vertex<T> {
public class Vertex<T>
{
//data is data in teh vertex
T data;
//neighbors is a list of vertexs that come after the current vertexes
List<Vertex<T>> neighbors;

public Vertex(T data) {
this(data, new ArrayList<>());
}

public Vertex(T data, List<Vertex<T>> neighbors) {
public Vertex(T data, List<Vertex<T>> neighbors)
{
this.data = data;
this.neighbors = neighbors;
}
Expand Down