diff --git a/src/Practice.java b/src/Practice.java index 6f1383b..bb15c07 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,6 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -25,7 +28,19 @@ public class Practice { * @return the number of vertices with odd values reachable from the starting vertex */ public static int oddVertices(Vertex starting) { - return 0; + Set> visited = new HashSet<>(); + return oddVertices(visited, starting); + } + + public static int oddVertices(Set> visited, Vertex start) { + if(start == null || visited.contains(start)) return 0; + visited.add(start); + int count = 0; + if(start.data % 2 == 1) count++; + for( Vertex neighbor: start.neighbors) { + count += oddVertices(visited, neighbor); + } + return count; } /** @@ -47,7 +62,20 @@ public static int oddVertices(Vertex starting) { * @return a sorted list of all reachable vertex values by */ public static List sortedReachable(Vertex starting) { - return null; + Set> visited = new HashSet<>(); + List reachable = new ArrayList<>(); + sortedReachable(visited, reachable, starting); + Collections.sort(reachable); + return reachable; + } + + public static void sortedReachable( Set> visited, List reachable, Vertex starting) { + if(starting == null || visited.contains(starting)) return; + visited.add(starting); + reachable.add(starting.data); + for( Vertex neighbor: starting.neighbors) { + sortedReachable(visited, reachable, neighbor); + } } /** @@ -61,7 +89,20 @@ public static List sortedReachable(Vertex starting) { * @return a sorted list of all reachable vertex values */ public static List sortedReachable(Map> graph, int starting) { - return null; + Set visited = new HashSet<>(); + List reachable = new ArrayList<>(); + sortedReachable(visited, reachable, graph, starting); + Collections.sort(reachable); + return reachable; + } + + public static void sortedReachable(Set visited, List reachable, Map> graph, int starting) { + if(graph.isEmpty() || !graph.containsKey(starting) || visited.contains(starting)) return; + visited.add(starting); + reachable.add(starting); + for(Integer neighbor : graph.get(starting)) { + sortedReachable(visited, reachable, graph, neighbor); + } } /** @@ -79,6 +120,23 @@ public static List sortedReachable(Map> graph, in * @return true if there is a two-way connection between v1 and v2, false otherwise */ public static boolean twoWay(Vertex v1, Vertex v2) { + if (v1 == null || v2 == null) return false; + if (v1 == v2) return true; + Set> visited1 = new HashSet<>(); + Set> visited2 = new HashSet<>(); + boolean test1 = twoWay(v1, v2, visited1); + boolean test2 = twoWay(v2, v1, visited2); + return test1 && test2; + } + + private static boolean twoWay(Vertex start, Vertexend, Set> visited) { + if (start == end) return true; + if (visited.contains(start)) return false; + visited.add(start); + + for (Vertex neighbor : start.neighbors) { + if (twoWay(neighbor, end, visited)) return true; + } return false; } @@ -95,6 +153,18 @@ public static boolean twoWay(Vertex v1, Vertex v2) { * @return whether there exists a valid positive path from starting to ending */ public static boolean positivePathExists(Map> graph, int starting, int ending) { + if(starting < 0 || ending < 0 || !graph.containsKey(starting) || !graph.containsKey(ending)) return false; + Set visited = new HashSet<>(); + return positivePathExists(visited, graph, starting, ending); + } + + public static boolean positivePathExists(Set visited, Map> graph, int starting, int ending) { + if(starting == ending) return true; + if(visited.contains(starting)) return false; + visited.add(starting); + for(Integer neighbor : graph.get(starting)) { + if(neighbor > 0 && positivePathExists(visited, graph, neighbor, ending)) return true; + } return false; } @@ -108,6 +178,18 @@ public static boolean positivePathExists(Map> graph, int s * @return true if a person in the extended network works at the specified company, false otherwise */ public static boolean hasExtendedConnectionAtCompany(Professional person, String companyName) { + if(person == null) return false; + Set visited = new HashSet<>(); + return hasExtendedConnectionAtCompany(visited, person, companyName); + } + + public static boolean hasExtendedConnectionAtCompany(Set visited, Professional person, String companyName) { + if(visited.contains(person)) return false; + if(person.getCompany().equals(companyName)) return true; + visited.add(person); + for(Professional connection : person.getConnections()) { + if(hasExtendedConnectionAtCompany(visited, connection, companyName)) return true; + } return false; } @@ -179,6 +261,25 @@ 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; + List moves = new ArrayList<>(); + int curR = current[0]; + int curC = current[1]; + + for(int[] direction : directions) { + int changeR = direction[0]; + int changeC = direction[1]; + int newR = curR + changeR; + int newC = curC + changeC; + + if(newR >= 0 && newR < board.length && + newC >= 0 && newC < board[newR].length && + board[newR][newC] != 'X') { + + int[] location = {newR, newC}; + moves.add(location); + } + } + return moves; } + }