[BAEL-19135] - Move search articles to a new module

This commit is contained in:
catalin-burcea
2019-11-19 17:58:32 +02:00
parent dd4aae6139
commit 5e7d875d0f
31 changed files with 744 additions and 327 deletions

View File

@@ -6,4 +6,3 @@ This module contains articles about data structures in Java
- [The Trie Data Structure in Java](https://www.baeldung.com/trie-java)
- [Implementing a Binary Tree in Java](https://www.baeldung.com/java-binary-tree)
- [Depth First Search in Java](https://www.baeldung.com/java-depth-first-search)

View File

@@ -1,74 +0,0 @@
package com.baeldung.graph;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Stack;
public class Graph {
private Map<Integer, List<Integer>> adjVertices;
public Graph() {
this.adjVertices = new HashMap<Integer, List<Integer>>();
}
public void addVertex(int vertex) {
adjVertices.putIfAbsent(vertex, new ArrayList<>());
}
public void addEdge(int src, int dest) {
adjVertices.get(src).add(dest);
}
public void dfsWithoutRecursion(int start) {
Stack<Integer> stack = new Stack<Integer>();
boolean[] isVisited = new boolean[adjVertices.size()];
stack.push(start);
while (!stack.isEmpty()) {
int current = stack.pop();
isVisited[current] = true;
visit(current);
for (int dest : adjVertices.get(current)) {
if (!isVisited[dest])
stack.push(dest);
}
}
}
public void dfs(int start) {
boolean[] isVisited = new boolean[adjVertices.size()];
dfsRecursive(start, isVisited);
}
private void dfsRecursive(int current, boolean[] isVisited) {
isVisited[current] = true;
visit(current);
for (int dest : adjVertices.get(current)) {
if (!isVisited[dest])
dfsRecursive(dest, isVisited);
}
}
public List<Integer> topologicalSort(int start) {
LinkedList<Integer> result = new LinkedList<Integer>();
boolean[] isVisited = new boolean[adjVertices.size()];
topologicalSortRecursive(start, isVisited, result);
return result;
}
private void topologicalSortRecursive(int current, boolean[] isVisited, LinkedList<Integer> result) {
isVisited[current] = true;
for (int dest : adjVertices.get(current)) {
if (!isVisited[dest])
topologicalSortRecursive(dest, isVisited, result);
}
result.addFirst(current);
}
private void visit(int value) {
System.out.print(" " + value);
}
}

View File

@@ -1,40 +0,0 @@
package com.baeldung.graph;
import java.util.List;
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();
List<Integer> list = graph.topologicalSort(0);
System.out.println(list);
}
private Graph createDirectedGraph() {
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);
graph.addEdge(2, 3);
graph.addEdge(3, 4);
graph.addEdge(4, 5);
return graph;
}
}