From 5b5733239c06b595649324d744e97c6a2dc97d68 Mon Sep 17 00:00:00 2001 From: DOHA Date: Thu, 25 Jul 2019 23:11:26 +0300 Subject: [PATCH 1/2] depth first search --- .../main/java/com/baeldung/graph/Graph.java | 71 +++++++++++++++++++ .../java/com/baeldung/tree/BinaryTree.java | 63 ++++++++++++++++ .../com/baeldung/graph/GraphUnitTest.java | 31 ++++++++ .../com/baeldung/tree/BinaryTreeUnitTest.java | 6 ++ 4 files changed, 171 insertions(+) create mode 100644 data-structures/src/main/java/com/baeldung/graph/Graph.java create mode 100644 data-structures/src/test/java/com/baeldung/graph/GraphUnitTest.java diff --git a/data-structures/src/main/java/com/baeldung/graph/Graph.java b/data-structures/src/main/java/com/baeldung/graph/Graph.java new file mode 100644 index 0000000000..b20301f86f --- /dev/null +++ b/data-structures/src/main/java/com/baeldung/graph/Graph.java @@ -0,0 +1,71 @@ +package com.baeldung.graph; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class Graph { + + private List> neighbours; + private int n; + + public Graph(int n) { + this.n = n; + this.neighbours = new ArrayList>(n); + for (int i = 0; i < n; i++) { + this.neighbours.add(new ArrayList()); + } + } + + public void addEdge(int src, int dest) { + this.neighbours.get(src) + .add(dest); + } + + public void dfsWithoutRecursion(int start) { + Stack stack = new Stack(); + boolean[] isVisited = new boolean[n]; + stack.push(start); + while (!stack.isEmpty()) { + int current = stack.pop(); + isVisited[current] = true; + System.out.print(" " + current); + for (int dest : neighbours.get(current)) { + if (!isVisited[dest]) + stack.push(dest); + } + } + } + + public void dfs(int start) { + boolean[] isVisited = new boolean[n]; + dfsRecursive(start, isVisited); + } + + private void dfsRecursive(int current, boolean[] isVisited) { + isVisited[current] = true; + System.out.print(" " + current); + for (int dest : neighbours.get(current)) { + if (!isVisited[dest]) + dfsRecursive(dest, isVisited); + } + } + + public void topologicalSort(int start) { + Stack result = new Stack(); + boolean[] isVisited = new boolean[n]; + topologicalSortRecursive(start, isVisited, result); + while (!result.isEmpty()) { + System.out.print(" " + result.pop()); + } + } + + private void topologicalSortRecursive(int current, boolean[] isVisited, Stack result) { + isVisited[current] = true; + for (int dest : neighbours.get(current)) { + if (!isVisited[dest]) + topologicalSortRecursive(dest, isVisited, result); + } + result.push(current); + } +} diff --git a/data-structures/src/main/java/com/baeldung/tree/BinaryTree.java b/data-structures/src/main/java/com/baeldung/tree/BinaryTree.java index f435e41afa..ff73ee8e54 100644 --- a/data-structures/src/main/java/com/baeldung/tree/BinaryTree.java +++ b/data-structures/src/main/java/com/baeldung/tree/BinaryTree.java @@ -2,6 +2,7 @@ package com.baeldung.tree; import java.util.LinkedList; import java.util.Queue; +import java.util.Stack; public class BinaryTree { @@ -147,6 +148,68 @@ public class BinaryTree { } } + + public void traverseInOrderWithoutRecursion() { + Stack stack = new Stack(); + Node current = root; + stack.push(root); + while(! stack.isEmpty()) { + while(current.left != null) { + current = current.left; + stack.push(current); + } + current = stack.pop(); + System.out.print(" " + current.value); + if(current.right != null) { + current = current.right; + stack.push(current); + } + } + } + + public void traversePreOrderWithoutRecursion() { + Stack stack = new Stack(); + Node current = root; + stack.push(root); + while(! stack.isEmpty()) { + current = stack.pop(); + System.out.print(" " + current.value); + + if(current.right != null) + stack.push(current.right); + + if(current.left != null) + stack.push(current.left); + } + } + + public void traversePostOrderWithoutRecursion() { + Stack stack = new Stack(); + Node prev = root; + Node current = root; + stack.push(root); + + while (!stack.isEmpty()) { + current = stack.peek(); + boolean hasChild = (current.left != null || current.right != null); + boolean isPrevLastChild = (prev == current.right || (prev == current.left && current.right == null)); + + if (!hasChild || isPrevLastChild) { + current = stack.pop(); + System.out.print(" " + current.value); + prev = current; + } else { + if (current.right != null) { + stack.push(current.right); + } + if (current.left != null) { + stack.push(current.left); + } + } + } + } + + class Node { int value; Node left; diff --git a/data-structures/src/test/java/com/baeldung/graph/GraphUnitTest.java b/data-structures/src/test/java/com/baeldung/graph/GraphUnitTest.java new file mode 100644 index 0000000000..837f83b494 --- /dev/null +++ b/data-structures/src/test/java/com/baeldung/graph/GraphUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.graph; + +import org.junit.Test; + +public class GraphUnitTest { + + @Test + public void givenDirectedGraph_whenDFS_thenPrintAllValues() { + Graph graph = createDirectedGraph(); + graph.dfs(0); + System.out.println(); + graph.dfsWithoutRecursion(0); + } + + @Test + public void givenDirectedGraph_whenGetTopologicalSort_thenPrintValuesSorted() { + Graph graph = createDirectedGraph(); + graph.topologicalSort(0); + } + + private Graph createDirectedGraph() { + Graph graph = new Graph(6); + graph.addEdge(0, 1); + graph.addEdge(0, 2); + graph.addEdge(1, 3); + graph.addEdge(2, 3); + graph.addEdge(3, 4); + graph.addEdge(4, 5); + return graph; + } +} diff --git a/data-structures/src/test/java/com/baeldung/tree/BinaryTreeUnitTest.java b/data-structures/src/test/java/com/baeldung/tree/BinaryTreeUnitTest.java index f81247b74d..f99cb52ed7 100644 --- a/data-structures/src/test/java/com/baeldung/tree/BinaryTreeUnitTest.java +++ b/data-structures/src/test/java/com/baeldung/tree/BinaryTreeUnitTest.java @@ -87,6 +87,8 @@ public class BinaryTreeUnitTest { BinaryTree bt = createBinaryTree(); bt.traverseInOrder(bt.root); + System.out.println(); + bt.traverseInOrderWithoutRecursion(); } @Test @@ -95,6 +97,8 @@ public class BinaryTreeUnitTest { BinaryTree bt = createBinaryTree(); bt.traversePreOrder(bt.root); + System.out.println(); + bt.traversePreOrderWithoutRecursion(); } @Test @@ -103,6 +107,8 @@ public class BinaryTreeUnitTest { BinaryTree bt = createBinaryTree(); bt.traversePostOrder(bt.root); + System.out.println(); + bt.traversePostOrderWithoutRecursion(); } @Test From 24135c03cfea302b60a9cd66fc18735f407d5592 Mon Sep 17 00:00:00 2001 From: DOHA Date: Sun, 28 Jul 2019 00:01:06 +0300 Subject: [PATCH 2/2] modify graph implementation --- .../main/java/com/baeldung/graph/Graph.java | 33 ++++++++++--------- .../com/baeldung/graph/GraphUnitTest.java | 8 ++++- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/data-structures/src/main/java/com/baeldung/graph/Graph.java b/data-structures/src/main/java/com/baeldung/graph/Graph.java index b20301f86f..16b7e04297 100644 --- a/data-structures/src/main/java/com/baeldung/graph/Graph.java +++ b/data-structures/src/main/java/com/baeldung/graph/Graph.java @@ -1,36 +1,36 @@ package com.baeldung.graph; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Stack; public class Graph { - private List> neighbours; - private int n; + private Map> adjVertices; - public Graph(int n) { - this.n = n; - this.neighbours = new ArrayList>(n); - for (int i = 0; i < n; i++) { - this.neighbours.add(new ArrayList()); - } + public Graph() { + this.adjVertices = new HashMap>(); + } + + public void addVertex(int vertex) { + adjVertices.putIfAbsent(vertex, new ArrayList<>()); } public void addEdge(int src, int dest) { - this.neighbours.get(src) - .add(dest); + adjVertices.get(src).add(dest); } public void dfsWithoutRecursion(int start) { Stack stack = new Stack(); - boolean[] isVisited = new boolean[n]; + boolean[] isVisited = new boolean[adjVertices.size()]; stack.push(start); while (!stack.isEmpty()) { int current = stack.pop(); isVisited[current] = true; System.out.print(" " + current); - for (int dest : neighbours.get(current)) { + for (int dest : adjVertices.get(current)) { if (!isVisited[dest]) stack.push(dest); } @@ -38,14 +38,14 @@ public class Graph { } public void dfs(int start) { - boolean[] isVisited = new boolean[n]; + boolean[] isVisited = new boolean[adjVertices.size()]; dfsRecursive(start, isVisited); } private void dfsRecursive(int current, boolean[] isVisited) { isVisited[current] = true; System.out.print(" " + current); - for (int dest : neighbours.get(current)) { + for (int dest : adjVertices.get(current)) { if (!isVisited[dest]) dfsRecursive(dest, isVisited); } @@ -53,7 +53,7 @@ public class Graph { public void topologicalSort(int start) { Stack result = new Stack(); - boolean[] isVisited = new boolean[n]; + boolean[] isVisited = new boolean[adjVertices.size()]; topologicalSortRecursive(start, isVisited, result); while (!result.isEmpty()) { System.out.print(" " + result.pop()); @@ -62,10 +62,11 @@ public class Graph { private void topologicalSortRecursive(int current, boolean[] isVisited, Stack result) { isVisited[current] = true; - for (int dest : neighbours.get(current)) { + for (int dest : adjVertices.get(current)) { if (!isVisited[dest]) topologicalSortRecursive(dest, isVisited, result); } result.push(current); } + } diff --git a/data-structures/src/test/java/com/baeldung/graph/GraphUnitTest.java b/data-structures/src/test/java/com/baeldung/graph/GraphUnitTest.java index 837f83b494..249cb6e093 100644 --- a/data-structures/src/test/java/com/baeldung/graph/GraphUnitTest.java +++ b/data-structures/src/test/java/com/baeldung/graph/GraphUnitTest.java @@ -19,7 +19,13 @@ public class GraphUnitTest { } private Graph createDirectedGraph() { - Graph graph = new Graph(6); + Graph graph = new Graph(); + graph.addVertex(0); + graph.addVertex(1); + graph.addVertex(2); + graph.addVertex(3); + graph.addVertex(4); + graph.addVertex(5); graph.addEdge(0, 1); graph.addEdge(0, 2); graph.addEdge(1, 3);