Skip to content

Commit 5a6e8aa

Browse files
committed
added grid bfs, groovy draft
1 parent 98944e0 commit 5a6e8aa

File tree

8 files changed

+104
-1
lines changed

8 files changed

+104
-1
lines changed

src/groovy/.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+
build --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java11

src/groovy/BUILD

Whitespace-only changes.

src/groovy/WORKSPACE

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
workspace(name = "algorithms_groovy")
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
http_archive(
5+
name = "io_bazel_rules_groovy",
6+
url = "https://github.com/bazelbuild/rules_groovy/archive/0.0.6.tar.gz",
7+
sha256 = "21c7172786623f280402d3b3a2fc92f36568afad5a4f6f5ea38fd1c6897aecf8",
8+
strip_prefix = "rules_groovy-0.0.6",
9+
)
10+
load("@io_bazel_rules_groovy//groovy:repositories.bzl", "rules_groovy_dependencies")
11+
rules_groovy_dependencies()

src/groovy/sequential/graph/bfs/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@io_bazel_rules_groovy//groovy:groovy.bzl", "groovy_binary")
2+
3+
groovy_binary(
4+
name = "bfs",
5+
srcs = glob(["*.groovy"]),
6+
main_class = "sequential.graph.bfs.BreadthFirstTraversal",
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package sequential.graph.bfs
2+
3+
// def hello() {
4+
// println "Hello World"
5+
// }
6+
7+
// hello()

src/kotlin/sequential/graph/bfs/GridBreadthFirstTraversal.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ fun main() {
7474
)
7575

7676
println(
77-
graph.bfs(0, 0)
77+
graph.bfs(rootRow = 0, rootCol = 0)
7878
)
7979
}

src/swift/sequential/graph/bfs/BUILD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,12 @@ swift_binary(
66
"BreadthFirstTraversal.swift",
77
"main.swift",
88
],
9+
)
10+
11+
swift_binary(
12+
name = "bfs_grid",
13+
srcs = [
14+
"GridBreadthFirstTraversal.swift",
15+
"main.swift",
16+
],
917
)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
typealias Graph<T> = [[T]]
2+
3+
4+
let directions = [(0, -1), (-1, 0), (0, 1), (1, 0)]
5+
6+
7+
func bfs<T>(in graph: Graph<T?>, withRoot root: (row: Int, col: Int)) -> [T]? {
8+
guard isValid(graph, startingAt: root) else { return nil }
9+
10+
let rows = graph.count
11+
let cols = graph[0].count
12+
let rowBounds = 0..<rows
13+
let colBounds = 0..<cols
14+
15+
var explored = Array(repeating: Array(repeating: false, count: cols), count: rows)
16+
var traversed = [T]()
17+
var searchQueue = [root]
18+
var queuePopIndex = 0
19+
20+
while queuePopIndex < searchQueue.count {
21+
let (row, col) = searchQueue[queuePopIndex]
22+
queuePopIndex += 1
23+
24+
if !explored[row][col], let value = graph[row][col] {
25+
explored[row][col] = true
26+
traversed.append(value)
27+
28+
for (stepRow, stepCol) in directions {
29+
let nextRow = row + stepRow
30+
let nextCol = col + stepCol
31+
32+
if rowBounds ~= nextRow && colBounds ~= nextCol {
33+
searchQueue.append((nextRow, nextCol))
34+
}
35+
}
36+
}
37+
}
38+
39+
return traversed
40+
}
41+
42+
func isValid<T>(_ graph: Graph<T?>, startingAt root: (row: Int, col: Int)) -> Bool {
43+
if graph.isEmpty || graph[0].isEmpty { return false }
44+
45+
let rows = graph.count
46+
let cols = graph[0].count
47+
48+
if !(0..<rows).contains(root.row) || !(0..<cols).contains(root.col) { return false }
49+
50+
for row in graph {
51+
if row.count != cols { return false }
52+
}
53+
return true
54+
}
55+
56+
57+
func mainBfs() {
58+
let graph = [
59+
[1, 5, 6],
60+
[9, 2, 4],
61+
[9, 7, nil],
62+
]
63+
64+
65+
print(
66+
bfs(in: graph, withRoot: (0, 0)) ?? "Invalid graph"
67+
)
68+
}

0 commit comments

Comments
 (0)