diff --git a/src/search/iterativedeepening/IterativeDeepening.java b/src/search/iterativedeepening/IterativeDeepening.java new file mode 100644 index 0000000..ac932f5 --- /dev/null +++ b/src/search/iterativedeepening/IterativeDeepening.java @@ -0,0 +1,45 @@ +package search.iterativedeepening; + +import search.Node; + +public class IterativeDeepening +{ + public Node iterativeDeepening(Node node, Node target) + { + int lowBarrier = 0; + + Node resultNode; + + do + { + resultNode = depthFirstSearch(node, target, 0, lowBarrier++); + } + while (resultNode == null); + + return resultNode; + } + + private Node depthFirstSearch(Node node, Node target, int depth, int barrier) + { + if (node.isTargetReached(target)) + { + return node; + } + + var newNodes = node.generateSuccessors(); + + while (!newNodes.isEmpty() && depth < barrier) + { + var resultNode = depthFirstSearch(newNodes.get(0), target, ++depth, barrier); + + if (resultNode != null) + { + return resultNode; + } + + newNodes.remove(0); + } + + return null; + } +} diff --git a/test/search/depthfirstsearch/DepthFirstSearchTest.java b/test/search/depthfirstsearch/DepthFirstSearchTest.java index 333a371..0c73d5c 100644 --- a/test/search/depthfirstsearch/DepthFirstSearchTest.java +++ b/test/search/depthfirstsearch/DepthFirstSearchTest.java @@ -28,26 +28,4 @@ class DepthFirstSearchTest printSolution(actual); } - -// @Test -// void shouldReturnCorrectTargetChubekNode() -// { -// int[][] state = { -// {2, 0, 4}, -// {6, 7, 1}, -// {8, 5, 3} -// }; -// var root = new EightPuzzleNode(state); -// -// int[][] targetState = { -// {1, 2, 3}, -// {4, 5, 6}, -// {7, 8, 0} -// }; -// var expected = new EightPuzzleNode(targetState); -// -// var actual = new DepthFirstSearch().depthFirstSearch(root, expected); -// -// printSolution(actual); -// } } \ No newline at end of file diff --git a/test/search/iterativedeepening/IterativeDeepeningTest.java b/test/search/iterativedeepening/IterativeDeepeningTest.java new file mode 100644 index 0000000..8d509e0 --- /dev/null +++ b/test/search/iterativedeepening/IterativeDeepeningTest.java @@ -0,0 +1,57 @@ +package search.iterativedeepening; + +import org.junit.jupiter.api.Test; +import search.EightPuzzleNode; +import search.breadthfirstsearch.BreadthFirstSearch; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static search.SearchTestUtils.printSolution; + +class IterativeDeepeningTest +{ + @Test + void shouldReturnCorrectTarget() + { + int[][] state = { + {5, 0, 3}, + {2, 1, 6}, + {4, 7, 8} + }; + var root = new EightPuzzleNode(state); + + int[][] targetState = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 0} + }; + var expected = new EightPuzzleNode(targetState); + + var actual = new IterativeDeepening().iterativeDeepening(root, expected); + + printSolution(actual); + } + + @Test + void shouldReturnCorrectTargetChubekNode() + { + int[][] state = { + {2, 0, 4}, + {6, 7, 1}, + {8, 5, 3} + }; + var root = new EightPuzzleNode(state); + + int[][] targetState = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 0} + }; + var expected = new EightPuzzleNode(targetState); + + var actual = new IterativeDeepening().iterativeDeepening(root, expected); + + printSolution(actual); + } +} \ No newline at end of file