From 577757f5bd05421d41335296245fdcca7c3ba8e1 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Wed, 11 Jun 2025 21:39:41 -0700 Subject: [PATCH 1/4] test odd complete --- src/Practice.java | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 6f1383b..57d92d9 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,27 @@ 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(starting, visited); + } + + public static int oddVertices(Vertex starting, Set> visited) { + if (starting == null || visited.contains(starting)) { + return 0; + } + + visited.add(starting); + int count = 0; + + if (starting.data % 2 != 0) { + count = 1; + } + + for (Vertex neighbor : starting.neighbors) { + count += oddVertices(neighbor, visited); + } + + return count; } /** From 81b514e7f12dc4ec3f7a54a92ea66cbd35ef2b21 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Wed, 11 Jun 2025 22:13:26 -0700 Subject: [PATCH 2/4] crazy idea didn't work, here a typical graph and map version --- src/Practice.java | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 57d92d9..909ec19 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,7 +1,11 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.Stack; public class Practice { @@ -68,7 +72,26 @@ public static int oddVertices(Vertex starting, Set> vis * @return a sorted list of all reachable vertex values by */ public static List sortedReachable(Vertex starting) { - return null; + Set> visited = new HashSet<>(); + List list = sortReachable(starting, visited); + Collections.sort(list); + return list; + } + + public static List sortReachable(Vertex starting, Set> visited) { + List list = new ArrayList<>(); + if (starting == null || visited.contains(starting)) { + return list; + } + + visited.add(starting); + list.add(starting.data); + + for (Vertex neighbor : starting.neighbors) { + list.addAll(sortReachable(neighbor, visited)); + } + + return list; } /** @@ -82,9 +105,28 @@ 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 list = sortedReachable(graph, starting, visited); + Collections.sort(list); + return list; } + public static List sortedReachable(Map> graph, int starting, Set visited) { + List list = new ArrayList<>(); + if (visited.contains(starting) || !graph.containsKey(starting)) { + return list; + } + + visited.add(starting); + list.add(starting); + + for (int neighbor : graph.get(starting)) { + list.addAll(sortedReachable(graph, neighbor, visited)); + } + + return list; + } + /** * 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 f3d439172f002fa043b8c3b875a8c72d4e769a09 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Wed, 11 Jun 2025 22:49:57 -0700 Subject: [PATCH 3/4] +pathExist, and 2way done --- src/Practice.java | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index 909ec19..1e40fce 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -142,6 +142,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> pathOne = new HashSet<>(); + boolean reachFirst = twoWay(v1, v2, pathOne); + + Set> pathTwo = new HashSet<>(); + boolean reachSecond = twoWay(v2, v1, pathTwo); + + if (reachFirst && reachSecond) return true; + + return false; + } + + public static boolean twoWay(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 (twoWay(vertex, goal, visited)) return true; + } + } + return false; } @@ -158,6 +183,21 @@ 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) { + 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; } From 925fd823d84490220c125edba8ba62e670b9cd15 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Wed, 11 Jun 2025 23:14:59 -0700 Subject: [PATCH 4/4] done --- src/Practice.java | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 1e40fce..ce508c9 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -211,6 +211,20 @@ 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) { + Set visited = new HashSet<>(); + return hasExtendedConnectionAtCompany(person, companyName, visited); + } + + public static boolean hasExtendedConnectionAtCompany(Professional person, String companyName, Set visited) { + if (person == null || visited.contains(person)) return false; + if (person.getCompany().equals(companyName)) return true; + + visited.add(person); + + for (Professional coworker : person.getConnections()) { + if (hasExtendedConnectionAtCompany(coworker, companyName, visited)) return true; + } + return false; } @@ -282,6 +296,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; + return possibleMoves(board, current, directions); + } + + public static List possibleMoves(char[][] board, int[] current, int[][] directions) { + List moves = new ArrayList<>(); + + int newR = 0; + int newC = 0; + + for (int[] direction : directions) { + newR = current[0] + direction[0]; + newC = current[1] + direction[1]; + + if (newR >= 0 && newR < board.length && newC >= 0 && newC < board[newR].length && board[newR][newC] != 'X') { + int[] newMove = {newR, newC}; + moves.add(newMove); + } + } + + return moves; } }