From 9a69c19f97a8ac8b531720b0f2ce0eb80533026a Mon Sep 17 00:00:00 2001 From: Ben Mojo <155706745+oakes777@users.noreply.github.com> Date: Thu, 13 Mar 2025 21:08:04 -0700 Subject: [PATCH 1/5] JS initial commit completedx oddVertices, tests passed --- src/Practice.java | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 1dcdfb1..2fbebe2 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,6 +1,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.HashSet; public class Practice { @@ -25,7 +26,31 @@ public class Practice { * @return the number of vertices with odd values reachable from the starting vertex */ public static int oddVertices(Vertex starting) { - return 0; + if (starting == null) return 0; + + //initialize visited Set + Set> visited = new HashSet<>(); + + //start recursive traversal with starting vertex + return oddVerticesHelper(starting, visited); + } + //helper method- + //no extra shared mutable variables needed—recursion handles + //accumulation through returns + private static int oddVerticesHelper(Vertex current, Set> visited) { + //base case-if we've already visited this vertex return immediately + if(visited.contains(current)) return 0; + //mark current as visited to avoid infinite loops + visited.add(current); + //count this vertex.data if odd + int count = (current.data % 2 != 0) ? 1 : 0; + //for-each iterates each neighbor of current + for (Vertex neighbor : current.neighbors) { + //recursion step adds up counts returned from neighbors + count += oddVerticesHelper(neighbor, visited); + } + //return total count accumulated from this vertex downward + return count; } /** From d44e24c4e44a07619705255212bf789f46863892 Mon Sep 17 00:00:00 2001 From: Ben Mojo <155706745+oakes777@users.noreply.github.com> Date: Fri, 14 Mar 2025 00:02:42 -0700 Subject: [PATCH 2/5] JS 2nd commit sortedReachable/helper; tests specific to this method are passing currently --- src/Practice.java | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 2fbebe2..49f2422 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,6 +1,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; public class Practice { @@ -73,7 +75,28 @@ private static int oddVerticesHelper(Vertex current, Set sortedReachable(Vertex starting) { // Unimplemented: perform a depth-first search and sort the collected values. - return null; + //base case + if (starting == null) return new ArrayList<>(); + //initialize empty visited Set/empty results List + Set> visited = new HashSet<>(); + List results = new ArrayList<>(); + // + sortedReachableHelper(starting, visited, results); + //sort results List into asc order + Collections.sort(results); + return results; + } + private static void sortedReachableHelper(Vertex current, Set> visited, List results) { + //base case + if (visited.contains(current)) return; + //add current and current.data to visited and results trackers + visited.add(current); + results.add(current.data); + //for each to iterate through each neighbor of current + for (Vertex neighbor : current.neighbors) { + //recurse all neighbors + sortedReachableHelper(neighbor, visited, results); + } } /** From ec9a6710df84c78340c940cf662d16a6e7e2a1bd Mon Sep 17 00:00:00 2001 From: Ben Mojo <155706745+oakes777@users.noreply.github.com> Date: Fri, 14 Mar 2025 01:39:32 -0700 Subject: [PATCH 3/5] JS 3rd commit finished sortedReachable Map version, tests passing --- src/Practice.java | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 49f2422..73c69cf 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -105,14 +105,37 @@ private static void sortedReachableHelper(Vertex current, Set sortedReachable(Map> graph, int starting) { - return null; + //base case ( more generic: return Collections.emptySet(); ) + if (graph == null || !graph.containsKey(starting)) return new ArrayList<>(); + //instantiate visited Set + Set visited = new HashSet<>(); + + sortedReachableHelper(graph, starting, visited); + + //convert visited data into List format + List sorted = new ArrayList<>(visited); + //sort newly formed List + Collections.sort(sorted); + //return List of sorted reachable 'vertices' + return sorted; } + private static void sortedReachableHelper(Map> graph, int current, Set visited) { + //base case - if we've been here already then return immediately + if (visited.contains(current)) return; + //add current to visited + visited.add(current); + //for each avoids getting a null pointer exception or needing + //an extra check for null values explicitly + for (Integer neighbor : graph.getOrDefault(current, Collections.emptySet())) { //recursively transverse to fill up visited Set + sortedReachableHelper(graph, neighbor, visited); + } + } /** * 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 6816cf147db96cd7407570544bef99518807897d Mon Sep 17 00:00:00 2001 From: Ben Mojo <155706745+oakes777@users.noreply.github.com> Date: Fri, 14 Mar 2025 02:36:22 -0700 Subject: [PATCH 4/5] JS completed no5, tests passing --- src/Practice.java | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index 73c69cf..576515c 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -151,6 +151,29 @@ private static void sortedReachableHelper(Map> graph, int * @return true if there is a two-way connection between v1 and v2, false otherwise */ public static boolean twoWay(Vertex v1, Vertex v2) { + //base case + if (v1 == null || v2 == null) return false; + //instantiate Set inside helper method + //call helper method + boolean oneWay = twoWayHelper(v1, v2, new HashSet<>()); + //repeat for opposite direction + + //call helper method + boolean otherWay = twoWayHelper(v2, v1, new HashSet<>()); + return oneWay && otherWay; + } + private static boolean twoWayHelper(Vertex current, Vertex v2, Set> visited) { + //base case -> successfully reached target vertex + if (current == v2) return true; + //already visited? stop recursion! + if (visited.contains(current)) return false; + //add current to visited to avoid looping + visited.add(current); + //for each iterate all neighbors of current + for (Vertex neighbor : current.neighbors) { + //recursively transverse each neighbor of current + if (twoWayHelper(neighbor, v2, visited)) return true; + } return false; } @@ -167,6 +190,22 @@ 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) { + //base case + if (starting < 0 || ending < 0 || graph == null || !graph.containsKey(starting) || !graph.containsKey(ending)) return false; + //call helper method inside boolean solution return + return positivePathExistsHelper(graph, starting, ending, new HashSet<>()); + } + private static boolean positivePathExistsHelper(Map> graph, int current, int ending, Set visited) { + //base case -> have we reached ending via current? + if (current == ending) return true; + //have we been here already? if so return false + if (visited.contains(current)) return false; + //add current to visited as tracking to avoid looping + visited.add(current); + //for each to iterate neighbors of current + for (Integer neighbor : graph.getOrDefault(current, Collections.emptySet())) { + if (neighbor > 0 && positivePathExistsHelper(graph, neighbor, ending, visited)) return true; + } return false; } @@ -180,6 +219,9 @@ 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) { + //base case + if (person == null) return false; + return false; } } From 798950d2239b4c8fe1b237563c1f813b470a4261 Mon Sep 17 00:00:00 2001 From: Ben Mojo <155706745+oakes777@users.noreply.github.com> Date: Fri, 14 Mar 2025 03:02:42 -0700 Subject: [PATCH 5/5] JS finished problem #6 re Professional person and matching contact working same company; all unit tests passing currently. This was a blast --- src/Practice.java | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 576515c..3160f4d 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -221,7 +221,25 @@ private static boolean positivePathExistsHelper(Map> graph public static boolean hasExtendedConnectionAtCompany(Professional person, String companyName) { //base case if (person == null) return false; - + //call helper + return hasExtendedConnectionAtCompanyHelper(person, companyName, new HashSet<>()); + } + private static boolean hasExtendedConnectionAtCompanyHelper(Professional currentPerson, String companyName, Set visited) { + //base case: if we've reached via DFS a person + //working at same company as starting person + //return true; **use .equals() for String comparisons + if (currentPerson.getCompany().equals(companyName)) return true; + //have we visited this person already? return false! + if (visited.contains(currentPerson)) return false; + //add currentPerson to visited Set + visited.add(currentPerson); + //for-each iterates all connections currentPerson has + for (Professional connection : currentPerson.getConnections()) { + //if recursion step is true, return true + if (hasExtendedConnectionAtCompanyHelper(connection, companyName, visited)) { + return true; + } + } return false; } }