diff --git a/src/Practice.java b/src/Practice.java index 6f1383b..581e237 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,5 @@ +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; @@ -24,8 +26,19 @@ public class Practice { * @param starting the starting vertex (may be null) * @return the number of vertices with odd values reachable from the starting vertex */ - public static int oddVertices(Vertex starting) { - return 0; + public static int oddVertices(Vertex starting, List> seen, List odds) { + if (seen.contains(starting)||starting==null) return 0; + seen.add(starting); + if (starting.data%2==1) odds.add(starting.data); + for (Vertex neighbor : starting.neighbors) { + oddVertices(neighbor,seen,odds); + } + return odds.size(); + } + public static int oddVertices(Vertex starting) { + List> seen = new ArrayList<>(); + List odds = new ArrayList<>(); + return oddVertices(starting,seen,odds); } /** @@ -46,8 +59,20 @@ public static int oddVertices(Vertex starting) { * @param starting the starting vertex (may be null) * @return a sorted list of all reachable vertex values by */ - public static List sortedReachable(Vertex starting) { - return null; + public static List sortedReachable(Vertex current, List> seen, List reachable) { + if (current==null||seen.contains(current)) return List.of(); + seen.add(current); + reachable.add(current.data); + for (Vertex neighbor : current.neighbors) { + sortedReachable(neighbor,seen,reachable); + } + Collections.sort(reachable); + return reachable; + } + public static List sortedReachable(Vertex starting) { + List> seen = new ArrayList<>(); + List reachable = new ArrayList<>(); + return sortedReachable(starting,seen,reachable); } /** @@ -60,8 +85,20 @@ public static List sortedReachable(Vertex starting) { * @param starting the starting vertex value * @return a sorted list of all reachable vertex values */ + public static List sortedReachable(Map> graph, int starting, List sorted) { + if (!graph.containsKey(starting)) return List.of(); + if (sorted.contains(starting)) return sorted; + sorted.add(starting); + for (Integer neighbor : graph.get(starting)) { + sortedReachable(graph, neighbor, sorted); + } + return sorted; + } public static List sortedReachable(Map> graph, int starting) { - return null; + List sorted = new ArrayList<>(); + sortedReachable(graph,starting,sorted); + Collections.sort(sorted); + return sorted; } /** @@ -78,9 +115,19 @@ public static List sortedReachable(Map> graph, in * @param v2 the target vertex * @return true if there is a two-way connection between v1 and v2, false otherwise */ - public static boolean twoWay(Vertex v1, Vertex v2) { + public static boolean twoWay(Vertex v1, Vertex v2, List> seen) { + if (v1==null||v2==null) return false; + if (v1==v2) return true; + seen.add(v1); + for (Vertex neighbor : v1.neighbors) { + return twoWay(neighbor, v2, seen); + } return false; } + public static boolean twoWay(Vertex v1, Vertex v2) { + List> seen = new ArrayList<>(); + return (twoWay(v1, v2, seen)==twoWay(v2, v1, seen)); + } /** * Returns whether there exists a path from the starting to ending vertex that includes only positive values. @@ -94,9 +141,19 @@ public static boolean twoWay(Vertex v1, Vertex v2) { * @param ending the ending vertex value * @return whether there exists a valid positive path from starting to ending */ - public static boolean positivePathExists(Map> graph, int starting, int ending) { + public static boolean positivePathExists(Map> graph, int starting, int ending, List seen) { + if (starting<0||ending<0||!graph.containsKey(starting)) return false; + if (starting==ending) return true; + seen.add(starting); + for (Integer neighbor : graph.get(starting)) { + if (!seen.contains(neighbor)&&neighbor>0) return positivePathExists(graph, neighbor, ending, seen); + } return false; } + public static boolean positivePathExists(Map> graph, int starting, int ending) { + List seen = new ArrayList<>(); + return positivePathExists(graph, starting, ending, seen); + } /** * Returns true if a professional has anyone in their extended network (reachable through any number of links) @@ -107,9 +164,20 @@ public static boolean positivePathExists(Map> graph, int s * @param companyName the name of the company to check for employment * @return true if a person in the extended network works at the specified company, false otherwise */ - public static boolean hasExtendedConnectionAtCompany(Professional person, String companyName) { + public static boolean hasExtendedConnectionAtCompany(Professional person, String companyName, List seen) { + if (person==null||seen.contains(person)) return false; + if (person.getCompany()==companyName) return true; + seen.add(person); + for (Professional connection : person.getConnections()) { + if (connection.getCompany()==companyName) return true; + return hasExtendedConnectionAtCompany(connection, companyName, seen); + } return false; } + public static boolean hasExtendedConnectionAtCompany(Professional person, String companyName) { + List seen = new ArrayList<>(); + return hasExtendedConnectionAtCompany(person, companyName, seen); + } /** * Returns a list of possible next moves starting from a given position. @@ -179,6 +247,16 @@ public static boolean hasExtendedConnectionAtCompany(Professional person, String * @return an unsorted list of next moves */ public static List nextMoves(char[][] board, int[] current, int[][] directions) { - return null; + int currROW = current[0]; + int currCOL = current[1]; + List possibleMoves = new ArrayList<>(); + for (int[] dir : directions) { + int newROW = currROW + dir[0]; + int newCOL = currCOL + dir[1]; + if (newROW>=0&&newROW=0&&newCOL