Skip to content

Commit f817de8

Browse files
committed
added java algorithms
1 parent 88dff94 commit f817de8

File tree

10 files changed

+140
-0
lines changed

10 files changed

+140
-0
lines changed

src/java/.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build --java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
2+
--host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java11
File renamed without changes.

src/java/WORKSPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
workspace(name = "algorithms_java")

src/java/sequential/graph/bfs/BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
java_binary(
2+
name = "bfs",
3+
main_class = "sequential.graph.bfs.BreadthFirstTraversal",
4+
srcs = glob(["*.java"]),
5+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package sequential.graph.bfs;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Collection;
5+
import java.util.LinkedHashSet;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
10+
public class BreadthFirstTraversal {
11+
12+
public static <T> Collection<T> bfs(Map<T, List<T>> graph, T root) {
13+
var explored = new LinkedHashSet<T>();
14+
var searchQueue = new ArrayDeque<T>();
15+
16+
searchQueue.add(root);
17+
while (!searchQueue.isEmpty()) {
18+
var node = searchQueue.removeFirst();
19+
if (!explored.contains(node)) {
20+
explored.add(node);
21+
searchQueue.addAll(graph.getOrDefault(node, List.of()));
22+
}
23+
}
24+
return explored;
25+
}
26+
27+
28+
public static void main(String[] args) {
29+
var graph = Map.of(
30+
"you", List.of("alice", "bob", "clair"),
31+
"alice", List.of("peggy"),
32+
"clair", List.of("thom", "jonny"),
33+
"bob", List.of("anuj", "paggy")
34+
);
35+
36+
System.out.println(
37+
bfs(graph, "you")
38+
);
39+
}
40+
}

src/java/sequential/graph/bfs/README.md

Whitespace-only changes.

src/java/sequential/graph/dfs/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
java_binary(
2+
name = "dfs_iterative",
3+
main_class = "sequential.graph.dfs.IterativeDepthFirstTraversal",
4+
srcs = ["IterativeDepthFirstTraversal.java"],
5+
)
6+
7+
java_binary(
8+
name = "dfs_recursive",
9+
main_class = "sequential.graph.dfs.RecursiveDepthFirstTraversal",
10+
srcs = ["RecursiveDepthFirstTraversal.java"],
11+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package sequential.graph.dfs;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Collection;
5+
import java.util.LinkedHashSet;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
10+
public class IterativeDepthFirstTraversal {
11+
12+
public static <T> Collection<T> dfs(Map<T, List<T>> graph, T root) {
13+
var searchStack = new ArrayDeque<T>();
14+
var explored = new LinkedHashSet<T>();
15+
16+
searchStack.add(root);
17+
while (!searchStack.isEmpty()) {
18+
var node = searchStack.removeLast();
19+
if (!explored.contains(node)) {
20+
explored.add(node);
21+
searchStack.addAll(graph.getOrDefault(node, List.of()));
22+
}
23+
}
24+
return explored;
25+
}
26+
27+
28+
public static void main(String[] args) {
29+
var graph = Map.of(
30+
"you", List.of("alice", "bob", "clair"),
31+
"alice", List.of("peggy"),
32+
"clair", List.of("thom", "jonny"),
33+
"bob", List.of("anuj", "paggy")
34+
);
35+
36+
System.out.println(
37+
dfs(graph, "you")
38+
);
39+
}
40+
}

src/java/sequential/graph/dfs/README.md

Whitespace-only changes.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package sequential.graph.dfs;
2+
3+
import java.util.Collection;
4+
import java.util.LinkedHashSet;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Set;
8+
9+
10+
class RecursiveDepthFirstTraversal {
11+
12+
public static <T> Collection<T> dfs(Map<T, List<T>> graph, T root) {
13+
var explored = new LinkedHashSet<T>();
14+
15+
explore(root, graph, explored);
16+
17+
return explored;
18+
}
19+
20+
private static <T> void explore(T node, Map<T, List<T>> graph, Set<T> explored) {
21+
explored.add(node);
22+
23+
var successors = graph.getOrDefault(node, List.of());
24+
for (var succ : successors)
25+
if (!explored.contains(succ)) explore(node, graph, explored);
26+
}
27+
28+
29+
public static void main(String[] args) {
30+
var graph = Map.of(
31+
"you", List.of("alice", "bob", "clair"),
32+
"alice", List.of("peggy"),
33+
"clair", List.of("thom", "jonny"),
34+
"bob", List.of("anuj", "paggy")
35+
);
36+
37+
System.out.println(
38+
dfs(graph, "you")
39+
);
40+
}
41+
}

0 commit comments

Comments
 (0)