From 0d20d483ac101e5cc585565ada3e7c31b0a60ea4 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 11 Jun 2025 15:14:46 -0700 Subject: [PATCH 1/7] completed oddVertices --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 6f1383b..2a506cb 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -25,7 +26,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; } /** From 9a7a4b2184b8d23dee640fd95ecf2ad792fe67bc Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 11 Jun 2025 15:30:14 -0700 Subject: [PATCH 2/7] completed sortedReachable --- src/Practice.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 2a506cb..925e2b3 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,5 @@ +import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -60,7 +62,20 @@ public static int oddVertices(Set> visited, Vertex star * @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); + } } /** From 72b4cd52bcfea05c6de50821ab70c8dec4d57093 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 11 Jun 2025 16:03:12 -0700 Subject: [PATCH 3/7] completed sortedReachable (MapGraph) --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 925e2b3..eb1f21c 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -89,7 +89,20 @@ public static void sortedReachable( Set> visited, List * @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); + } } /** From c7c72e1ade3cd9b08fef3d03ae48acea4d7a60d8 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 11 Jun 2025 16:27:42 -0700 Subject: [PATCH 4/7] completed twoWay --- src/Practice.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index eb1f21c..1aa7060 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -120,6 +120,23 @@ public static void sortedReachable(Set visited, List reachable * @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; } From 26580a96bd90de6fc991fdaa5df7c592be7c7d5a Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 11 Jun 2025 16:46:06 -0700 Subject: [PATCH 5/7] completed positivePathExists --- src/Practice.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index 1aa7060..16694b9 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -153,6 +153,18 @@ private static boolean twoWay(Vertex start, Vertexend, Set> * @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; } From b5fb2325d89a5e582f2a33832511a9f27fc57e20 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 11 Jun 2025 19:05:17 -0700 Subject: [PATCH 6/7] completed hasExtendedConnectionAtCompany --- src/Practice.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index 16694b9..4a8d192 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -178,6 +178,18 @@ public static boolean positivePathExists(Set visited, Map 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; } From 49b4ffe1af9af00befa21a337b28d03d9957c7eb Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Wed, 11 Jun 2025 19:33:42 -0700 Subject: [PATCH 7/7] completed nextMoves --- src/Practice.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 4a8d192..bb15c07 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -261,6 +261,25 @@ public static boolean hasExtendedConnectionAtCompany(Set visited, * @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; } + }