Skip to content

Commit 089e724

Browse files
committed
added swift
1 parent dc0e3cc commit 089e724

File tree

11 files changed

+161
-3
lines changed

11 files changed

+161
-3
lines changed

src/kotlin/sequential/graph/bfs/BUILD

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")
22

33
kt_jvm_library(
44
name = "bfs_lib",
5-
srcs = glob([
6-
"*.kt",
7-
]),
5+
srcs = glob(["*.kt"]),
86
)
97

108
java_binary(
File renamed without changes.

src/swift/WORKSPACE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
workspace(name = "algorithms_swift")
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
5+
http_archive(
6+
name = "build_bazel_rules_swift",
7+
sha256 = "d2f38c33dc82cf3160c59342203d31a030e53ebe8f4c7365add7a549223f9c62",
8+
url = "https://github.com/bazelbuild/rules_swift/releases/download/0.15.0/rules_swift.0.15.0.tar.gz",
9+
)
10+
11+
load(
12+
"@build_bazel_rules_swift//swift:repositories.bzl",
13+
"swift_rules_dependencies",
14+
)
15+
16+
swift_rules_dependencies()
17+
18+
load(
19+
"@build_bazel_rules_swift//swift:extras.bzl",
20+
"swift_rules_extra_dependencies",
21+
)
22+
23+
swift_rules_extra_dependencies()

src/swift/sequential/graph/bfs/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary")
2+
3+
swift_binary(
4+
name = "bfs",
5+
srcs = [
6+
"BreadthFirstTraversal.swift",
7+
"main.swift",
8+
],
9+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
typealias Graph<T: Hashable> = [T: [T]]
2+
3+
4+
func bfs<T>(in graph: Graph<T>, withRoot root: T) -> [T] {
5+
var explored = Set<T>()
6+
var traversed = [T]()
7+
var searchQueue = [root]
8+
var queuePopIndex = 0
9+
10+
while searchQueue.count > queuePopIndex {
11+
let node = searchQueue[queuePopIndex]
12+
queuePopIndex += 1
13+
14+
if !explored.contains(node) {
15+
explored.insert(node)
16+
traversed.append(node)
17+
if let successors = graph[node] {
18+
searchQueue += successors
19+
}
20+
}
21+
}
22+
return traversed
23+
}
24+
25+
26+
func bfsMain() {
27+
let graph = [
28+
"you": ["alice", "bob", "clair"],
29+
"alice": ["peggy"],
30+
"clair": ["thom", "jonny"],
31+
"bob": ["anuj", "peggy"]
32+
]
33+
34+
print(
35+
bfs(in: graph, withRoot: "you")
36+
)
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bfsMain()

src/swift/sequential/graph/dfs/BUILD

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary")
2+
3+
swift_binary(
4+
name = "dfs_iterative",
5+
srcs = [
6+
"IterativeBreadthFirstTraversal.swift",
7+
"main.swift",
8+
],
9+
)
10+
11+
swift_binary(
12+
name = "dfs_recursive",
13+
srcs = [
14+
"RecursiveBreadthFirstTraversal.swift",
15+
"main.swift",
16+
],
17+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
typealias Graph<T: Hashable> = [T: [T]]
2+
3+
4+
func dfs<T>(in graph: Graph<T>, withRoot root: T) -> [T] {
5+
var explored = Set<T>()
6+
var traversed = [T]()
7+
var searchStack = [root]
8+
9+
while !searchStack.isEmpty {
10+
let node = searchStack.removeLast()
11+
12+
if !explored.contains(node) {
13+
explored.insert(node)
14+
traversed.append(node)
15+
if let successors = graph[node] {
16+
searchStack += successors
17+
}
18+
}
19+
}
20+
return traversed
21+
}
22+
23+
24+
func dfsMain() {
25+
let graph = [
26+
"you": ["alice", "bob", "clair"],
27+
"alice": ["peggy"],
28+
"clair": ["thom", "jonny"],
29+
"bob": ["anuj", "peggy"]
30+
]
31+
32+
print(
33+
dfs(in: graph, withRoot: "you")
34+
)
35+
}

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

Whitespace-only changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
typealias Graph<T: Hashable> = [T: [T]]
2+
3+
4+
func dfs<T>(in graph: Graph<T>, withRoot root: T) -> [T] {
5+
var explored = Set<T>()
6+
var traversed = [T]()
7+
8+
func explore(_ node: T) {
9+
explored.insert(node)
10+
traversed.append(node)
11+
12+
guard let successors = graph[node] else { return }
13+
14+
for succ in successors {
15+
if !explored.contains(succ) {
16+
explore(succ)
17+
}
18+
}
19+
}
20+
explore(root)
21+
22+
return traversed
23+
}
24+
25+
26+
func dfsMain() {
27+
let graph = [
28+
"you": ["alice", "bob", "clair"],
29+
"alice": ["peggy"],
30+
"clair": ["thom", "jonny"],
31+
"bob": ["anuj", "peggy"]
32+
]
33+
34+
print(
35+
dfs(in:graph, withRoot: "you")
36+
)
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dfsMain()

0 commit comments

Comments
 (0)