From 02e49e7fdb598b12b8b6e914446d6d6b409cdcc8 Mon Sep 17 00:00:00 2001
From: Manish Raj <2200032955@kluniversity.in>
Date: Thu, 17 Oct 2024 00:16:07 +0530
Subject: [PATCH 1/4] Optimize BreadthFirstSearch implementation with bug fixes
and improvements
- Fix potential infinite loop by adding cycle detection using HashSet
- Add null value and null node handling for better robustness
- Optimize visited node lookups from O(n) to O(1) using HashSet
- Clear previous search results for multiple search support
- Add defensive copying in getVisited() to prevent external state modification
- Improve code organization and documentation
- Add consistent node visitation handling
- Fix root node visitation order inconsistency
- Prevent redundant node processing
---
.../searches/BreadthFirstSearch.java | 54 +++++++++++++------
1 file changed, 37 insertions(+), 17 deletions(-)
diff --git a/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java b/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java
index debab98c67a8..8b0115e15d79 100644
--- a/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java
+++ b/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java
@@ -3,47 +3,67 @@
import com.thealgorithms.datastructures.Node;
import java.util.ArrayDeque;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Queue;
+import java.util.Set;
/**
- * @author: caos321
- * @date: 31 October 2021 (Sunday)
- * @wiki: https://en.wikipedia.org/wiki/Breadth-first_search
+ * Breadth-First Search implementation for tree/graph traversal.
+ * @author caos321
+ * @co-author manishraj27
+ * @see Breadth-first search
*/
public class BreadthFirstSearch {
private final List visited = new ArrayList<>();
+ private final Set visitedSet = new HashSet<>();
- public Optional> search(final Node node, final T value) {
- if (node == null) {
+ /**
+ * Performs a breadth-first search to find a node with the given value.
+ *
+ * @param root The root node to start the search from
+ * @param value The value to search for
+ * @return Optional containing the found node, or empty if not found
+ */
+ public Optional> search(final Node root, final T value) {
+ if (value == null || root == null) {
return Optional.empty();
}
- if (node.getValue().equals(value)) {
- // add root node to visited
- visited.add(value);
- return Optional.of(node);
- }
- visited.add(node.getValue());
- Queue> queue = new ArrayDeque<>(node.getChildren());
+ visited.clear();
+ visitedSet.clear();
+
+ Queue> queue = new ArrayDeque<>();
+ queue.offer(root);
+ visitedSet.add(root.getValue());
+ visited.add(root.getValue());
while (!queue.isEmpty()) {
final Node current = queue.poll();
- visited.add(current.getValue());
- if (current.getValue().equals(value)) {
+ if (value.equals(current.getValue())) {
return Optional.of(current);
}
- queue.addAll(current.getChildren());
+ for (Node child : current.getChildren()) {
+ if (child != null && !visitedSet.contains(child.getValue())) {
+ queue.offer(child);
+ visitedSet.add(child.getValue());
+ visited.add(child.getValue());
+ }
+ }
}
-
return Optional.empty();
}
+ /**
+ * Returns the list of nodes in the order they were visited.
+ *
+ * @return A new list containing the visited nodes
+ */
public List getVisited() {
- return visited;
+ return new ArrayList<>(visited);
}
}
From 18267b66627dce440be67c27bb2846a2f552b63d Mon Sep 17 00:00:00 2001
From: Manish Raj <2200032955@kluniversity.in>
Date: Thu, 17 Oct 2024 00:25:18 +0530
Subject: [PATCH 2/4] Fixede clear() calls
---
.../searches/BreadthFirstSearch.java | 53 +++++++++++++------
1 file changed, 36 insertions(+), 17 deletions(-)
diff --git a/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java b/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java
index 8b0115e15d79..821d0b66c86b 100644
--- a/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java
+++ b/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java
@@ -28,42 +28,61 @@ public class BreadthFirstSearch {
* @return Optional containing the found node, or empty if not found
*/
public Optional> search(final Node root, final T value) {
- if (value == null || root == null) {
+ // Handle null root
+ if (root == null) {
return Optional.empty();
}
- visited.clear();
- visitedSet.clear();
+ // Check root value first
+ if (value == null) {
+ if (root.getValue() == null) {
+ visited.add(null);
+ return Optional.of(root);
+ }
+ visited.add(root.getValue());
+ return Optional.empty();
+ }
- Queue> queue = new ArrayDeque<>();
- queue.offer(root);
- visitedSet.add(root.getValue());
+ // Check root node
+ if (value.equals(root.getValue())) {
+ visited.add(root.getValue());
+ return Optional.of(root);
+ }
+
+ // Add root to visited
visited.add(root.getValue());
+ visitedSet.add(root.getValue());
+ // Process remaining nodes
+ Queue> queue = new ArrayDeque<>(root.getChildren());
while (!queue.isEmpty()) {
final Node current = queue.poll();
+ T currentValue = current.getValue();
+
+ // Skip if already visited
+ if (visitedSet.contains(currentValue)) {
+ continue;
+ }
+
+ visited.add(currentValue);
+ visitedSet.add(currentValue);
- if (value.equals(current.getValue())) {
+ if (value.equals(currentValue)) {
return Optional.of(current);
}
- for (Node child : current.getChildren()) {
- if (child != null && !visitedSet.contains(child.getValue())) {
- queue.offer(child);
- visitedSet.add(child.getValue());
- visited.add(child.getValue());
- }
- }
+ queue.addAll(current.getChildren());
}
+
return Optional.empty();
}
/**
* Returns the list of nodes in the order they were visited.
*
- * @return A new list containing the visited nodes
+ * @return List containing the visited nodes
*/
public List getVisited() {
- return new ArrayList<>(visited);
+ return visited;
}
-}
+}
\ No newline at end of file
From c7d4af5d5fbfa929c4c0b95363dd6234e6256dc9 Mon Sep 17 00:00:00 2001
From: Manish Raj <2200032955@kluniversity.in>
Date: Thu, 17 Oct 2024 00:33:13 +0530
Subject: [PATCH 3/4] Fixed test cases
---
.../searches/BreadthFirstSearch.java | 29 ++++---------------
1 file changed, 6 insertions(+), 23 deletions(-)
diff --git a/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java b/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java
index 821d0b66c86b..b13eb3bcb4f1 100644
--- a/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java
+++ b/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java
@@ -12,11 +12,10 @@
/**
* Breadth-First Search implementation for tree/graph traversal.
* @author caos321
- * @co-author manishraj27
+ * @co-author @manishraj27
* @see Breadth-first search
*/
public class BreadthFirstSearch {
-
private final List visited = new ArrayList<>();
private final Set visitedSet = new HashSet<>();
@@ -28,38 +27,22 @@ public class BreadthFirstSearch {
* @return Optional containing the found node, or empty if not found
*/
public Optional> search(final Node root, final T value) {
- // Handle null root
if (root == null) {
return Optional.empty();
}
- // Check root value first
- if (value == null) {
- if (root.getValue() == null) {
- visited.add(null);
- return Optional.of(root);
- }
- visited.add(root.getValue());
- return Optional.empty();
- }
+ visited.add(root.getValue());
+ visitedSet.add(root.getValue());
- // Check root node
- if (value.equals(root.getValue())) {
- visited.add(root.getValue());
+ if (root.getValue() == value) {
return Optional.of(root);
}
- // Add root to visited
- visited.add(root.getValue());
- visitedSet.add(root.getValue());
-
- // Process remaining nodes
Queue> queue = new ArrayDeque<>(root.getChildren());
while (!queue.isEmpty()) {
final Node current = queue.poll();
T currentValue = current.getValue();
-
- // Skip if already visited
+
if (visitedSet.contains(currentValue)) {
continue;
}
@@ -67,7 +50,7 @@ public Optional> search(final Node root, final T value) {
visited.add(currentValue);
visitedSet.add(currentValue);
- if (value.equals(currentValue)) {
+ if (currentValue == value || (value != null && value.equals(currentValue))) {
return Optional.of(current);
}
From 285365fd265e687fe050821931da9e2da24d0976 Mon Sep 17 00:00:00 2001
From: Manish Raj <2200032955@kluniversity.in>
Date: Thu, 17 Oct 2024 00:39:14 +0530
Subject: [PATCH 4/4] Optimised
---
.../java/com/thealgorithms/searches/BreadthFirstSearch.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java b/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java
index b13eb3bcb4f1..7ac9c7b01526 100644
--- a/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java
+++ b/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java
@@ -68,4 +68,4 @@ public Optional> search(final Node root, final T value) {
public List getVisited() {
return visited;
}
-}
\ No newline at end of file
+}