From ce6b8239c6092d775e8bad4fb96077ee4bdd3edc Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Tue, 10 Jun 2025 12:01:21 -0700 Subject: [PATCH 1/5] oddVertices and sortedReachable passing tests --- src/Practice.java | 63 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 6f1383b..cfc8e21 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,10 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Queue; import java.util.Set; public class Practice { @@ -24,8 +29,36 @@ 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; + int count = 0; + + if (starting == null) return count; + + Set> visited = new HashSet<>(); + Queue> queue = new LinkedList<>(); + queue.add(starting); + + while (!queue.isEmpty()) { + Vertex current = queue.poll(); + + if (current != null && !visited.contains(current)) { + visited.add(current); + + if (current.data % 2 != 0) count++; + + if (current.neighbors != null && !current.neighbors.isEmpty()) { + for (Vertex vertex : current.neighbors) { + if (!visited.contains(vertex)) { + queue.add(vertex); + } + } + } + } + } + + + return count; } /** @@ -47,7 +80,31 @@ public static int oddVertices(Vertex starting) { * @return a sorted list of all reachable vertex values by */ public static List sortedReachable(Vertex starting) { - return null; + List sorted = new ArrayList<>(); + + if (starting == null) return sorted; + + Set> visited = new HashSet<>(); + + sortedReachable(starting, sorted, visited); + + Collections.sort(sorted); + + return sorted; + } + + public static void sortedReachable(Vertex current, List sorted, Set> visited) { + if (current == null || visited.contains(current)) return; + + sorted.add(current.data); + visited.add(current); + + for (Vertex neighbor : current.neighbors) { + if (!visited.contains(neighbor)) { + sortedReachable(neighbor, sorted, visited); + } + } + } /** @@ -64,6 +121,8 @@ public static List sortedReachable(Map> graph, in return null; } + + /** * Returns true if and only if it is possible both to reach v2 from v1 and to reach v1 from v2. * A vertex is always considered reachable from itself. From 9e571d5a9dd9a3fc4ce73e3f873e6c71839117c8 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Tue, 10 Jun 2025 12:11:27 -0700 Subject: [PATCH 2/5] sortedReachable (graphs) added --- src/Practice.java | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index cfc8e21..b0ded1a 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -118,10 +118,31 @@ public static void sortedReachable(Vertex current, List sorted * @return a sorted list of all reachable vertex values */ public static List sortedReachable(Map> graph, int starting) { - return null; - } + List sorted = new ArrayList<>(); + if (!graph.containsKey(starting)) return sorted; + + Set visited = new HashSet<>(); + Queue queue = new LinkedList<>(); - + queue.add(starting); + while (!queue.isEmpty()) { + int current = queue.poll(); + + if (!visited.contains(current)) { + sorted.add(current); + visited.add(current); + } + + for (Integer number : graph.get(current)) { + if (!visited.contains(number)) { + queue.add(number); + } + } + } + + Collections.sort(sorted); + return sorted; + } /** * Returns true if and only if it is possible both to reach v2 from v1 and to reach v1 from v2. From 562d00904791bd77b47335380623c783bd24e1fe Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Wed, 11 Jun 2025 12:26:33 -0700 Subject: [PATCH 3/5] positivePathExists completed --- src/Practice.java | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index b0ded1a..2f92fa8 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -159,6 +159,31 @@ 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> firstPath = new HashSet<>(); + boolean canReachFirst = twoWayHelper(v1, v2, firstPath); + + Set> secondPath = new HashSet<>(); + boolean canReachSecond = twoWayHelper(v2, v1, secondPath); + + if (canReachFirst && canReachSecond) return true; + + return false; + } + + public static boolean twoWayHelper(Vertex current, Vertex goal, Set> visited) { + if (current == goal) return true; + if (current == null || visited.contains(current)) return false; + + visited.add(current); + for (Vertex vertex : current.neighbors) { + if (!visited.contains(vertex)) { + if (twoWayHelper(vertex, goal, visited)) return true; + } + } + return false; } @@ -175,9 +200,27 @@ 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) { - return false; + if (starting < 0 || ending < 0 || !graph.containsKey(starting) || !graph.containsKey(ending)) return false; + if (starting == ending) return true; + + Set visited = new HashSet<>(); + + return positivePathExists(graph, starting, ending, visited); } + public static boolean positivePathExists(Map> graph, int starting, int ending, Set visited) { + if (starting < 0 || ending < 0 || !graph.containsKey(starting) || !graph.containsKey(ending) || visited.contains(starting)) return false; + if (starting == ending) return true; + + visited.add(starting); + for (int num : graph.get(starting)) { + if (!visited.contains(num)) { + if (positivePathExists(graph, num, ending, visited)) return true; + } + } + + return false; + } /** * Returns true if a professional has anyone in their extended network (reachable through any number of links) * that works for the given company. The search includes the professional themself. From 08ea7e10c5acd973bd501e96082dd058488a1a19 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Wed, 11 Jun 2025 12:38:22 -0700 Subject: [PATCH 4/5] extended connections completed --- src/Practice.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index 2f92fa8..ac9a309 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -231,6 +231,27 @@ 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; + if (person.getCompany() == companyName) return true; + + Set network = new HashSet<>(); + return hasExtendedConnectionAtCompany(person, companyName, network); + + } + + public static boolean hasExtendedConnectionAtCompany(Professional person, String companyName, Set network) { + if (person == null || network.contains(person)) return false; + if (person.getCompany() == companyName) return true; + + network.add(person); + Set connections = person.getConnections(); + + for (Professional professional : connections) { + if (!network.contains(professional)) { + if (hasExtendedConnectionAtCompany(professional, companyName, network)) return true; + } + } + return false; } From 17d73c511edf06a2caff46424a132b2e67e27cf8 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Wed, 11 Jun 2025 12:51:25 -0700 Subject: [PATCH 5/5] nextMoves passing tests --- src/Practice.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ac9a309..76e6490 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -323,6 +323,22 @@ 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 r = current[0]; + int c = current[1]; + + List moves = new ArrayList<>(); + + for (int[] direction : directions) { + int newR = r + direction[0]; + int newC = c + direction[1]; + + if (newR >= 0 && newR < board.length && + newC >= 0 && newC < board[newR].length && + board[newR][newC] == ' ') { + moves.add(new int[] {newR, newC}); + } + } + + return moves; } }