Skip to content

Commit dc0e3cc

Browse files
committed
minor fixes
1 parent 1d4cee6 commit dc0e3cc

File tree

6 files changed

+57
-48
lines changed

6 files changed

+57
-48
lines changed

src/kotlin/_util/CollectionUtils.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package _util
22

3-
import sequential.shuffling.fisheryates.fisherYatesShuffle
43
import kotlin.random.Random
54

65

76
private const val MAX_LIST_VALUE = 999
87
private const val LIST_SIZE = 50
98

10-
fun randomList(size: Int = LIST_SIZE, range: IntRange = 0..MAX_LIST_VALUE) =
11-
List(size) {
12-
Random.nextInt(range.first, range.last + 1)
9+
fun randomList(size: Int = LIST_SIZE, range: IntRange = 0..MAX_LIST_VALUE) =
10+
List(size) {
11+
Random.nextInt(range.first, range.last + 1)
1312
}
1413

15-
fun <T: Comparable<T>> Collection<T>.isSorted(): Boolean =
14+
fun <T : Comparable<T>> Collection<T>.isSorted(): Boolean =
1615
asSequence().zipWithNext { a, b -> a <= b }.all { it }

src/kotlin/_util/IntArrayUtils.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import kotlin.random.Random
77
private const val MAX_ARRAY_VALUE = 999
88
private const val ARRAY_SIZE = 50
99

10-
fun randomIntArray(size: Int = ARRAY_SIZE, range: IntRange = 0..MAX_ARRAY_VALUE) =
11-
IntArray(size) {
12-
Random.nextInt(range.first, range.last + 1)
10+
fun randomIntArray(size: Int = ARRAY_SIZE, range: IntRange = 0..MAX_ARRAY_VALUE) =
11+
IntArray(size) {
12+
Random.nextInt(range.first, range.last + 1)
1313
}
1414

1515
fun IntArray.shuffle() {

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,15 @@ private typealias Queue<T> = ArrayDeque<T>
1111
fun <T> Graph<T>.bfs(root: T): Collection<T> {
1212
val graph = this
1313

14-
val explored = mutableSetOf<T>()
14+
val explored = linkedSetOf<T>()
1515
val searchQueue = Queue<T>()
1616

17-
searchQueue.addLast(root)
17+
searchQueue += root
1818
while (searchQueue.isNotEmpty()) {
1919
val node = searchQueue.removeFirst()
20-
explored.add(node)
21-
22-
val successors = graph[node].orEmpty()
23-
for (succ in successors) {
24-
if (succ !in explored) searchQueue.addLast(succ)
20+
if (node !in explored) {
21+
explored += node
22+
searchQueue += graph[node].orEmpty()
2523
}
2624
}
2725

@@ -30,5 +28,14 @@ fun <T> Graph<T>.bfs(root: T): Collection<T> {
3028

3129

3230
fun main() {
33-
// TODO
31+
val socialNetwork: Graph<String> = mapOf(
32+
"you" to listOf("tony", "steve", "nick"),
33+
"tony" to listOf("clint"),
34+
"nick" to listOf("thor", "natasha"),
35+
"steve" to listOf("phil", "clint")
36+
)
37+
38+
println(
39+
socialNetwork.bfs("you")
40+
)
3441
}

src/kotlin/sequential/graph/dfs/IterativeDepthFirstTraversal.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@ private typealias Graph<T> = Map<T, List<T>>
88
private typealias Stack<T> = ArrayDeque<T>
99

1010

11-
fun <T> Graph<T>.iterativeDfs(root: T): Collection<T> {
11+
fun <T> Graph<T>.dfs(root: T): Collection<T> {
1212
val graph = this
1313

1414
val explored = mutableSetOf<T>()
15-
val stack = Stack<T>()
16-
17-
stack.addLast(root)
18-
while (stack.isNotEmpty()) {
19-
val node = stack.removeLast()
20-
explored.add(node)
21-
22-
val successors = graph[node].orEmpty()
23-
for (succ in successors) {
24-
if (succ !in explored) stack.addLast(succ)
15+
val searchStack = Stack<T>()
16+
17+
searchStack.addLast(root)
18+
while (searchStack.isNotEmpty()) {
19+
val node = searchStack.removeLast()
20+
if (node !in explored) {
21+
explored += node
22+
searchStack += graph[node].orEmpty()
2523
}
2624
}
2725

src/kotlin/sequential/graph/dijkstra/Dijkstra.kt

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,41 @@ private val INFINITY = Double.POSITIVE_INFINITY
1919
fun <T> Graph<T>.dijkstra(root: T, target: T): Collection<T> {
2020
val graph = this
2121

22-
val costs = graph.getValue(root).toMutableMap()
22+
val costs = graph[root].orEmpty().toMutableMap()
2323
val explored = mutableSetOf<T>()
24-
val parents = graph.getValue(root).mapValues { root }.toMutableMap()
24+
val parents = graph[root].orEmpty().mapValues { root }.toMutableMap()
2525

26-
var node = costs.smallestCostNode(explored)
26+
var node = costs.minCostNode(explored)
2727
while (node != null) {
2828
val nodeCost = costs[node]!!
29-
val successors = graph[node].orEmpty()
3029

30+
val successors = graph[node].orEmpty()
3131
for ((succ, edgeWeight) in successors) {
3232
if (nodeCost + edgeWeight < costs[succ] ?: INFINITY) {
3333
costs[succ] = nodeCost + edgeWeight
3434
parents[succ] = node
3535
}
3636
}
37-
38-
explored.add(node)
39-
node = costs.smallestCostNode(explored)
37+
explored += node
38+
node = costs.minCostNode(explored)
4039
}
4140

4241
return pathToNode(target, parents)
4342
}
4443

45-
private fun <T> Map<T, Double>.smallestCostNode(explored: Collection<T>): T? = asSequence()
46-
.filter { (node, _) -> node !in explored }
47-
.minBy { (_, cost) -> cost }
48-
?.key
44+
private fun <T> Map<T, Double>.minCostNode(explored: Collection<T>): T? {
45+
val costs = this
46+
47+
var minCostNode: T? = null
48+
var minCost = INFINITY
49+
for ((node, cost) in costs) {
50+
if (node !in explored && cost < minCost) {
51+
minCostNode = node
52+
minCost = cost
53+
}
54+
}
55+
return minCostNode
56+
}
4957

5058
/**
5159
* Build path from root to target node.
@@ -56,23 +64,22 @@ private fun <T> pathToNode(target: T, parents: Map<T, T>): List<T> {
5664
val path = mutableListOf<T>()
5765
var node: T? = target
5866
while (node != null) {
59-
path.add(node)
67+
path += node
6068
node = parents[node]
6169
}
6270
return path.reversed()
6371
}
6472

6573

6674
fun main() {
67-
6875
val waypoints: Graph<String> = mapOf(
69-
"Start" to mapOf("A" to 5.0),
70-
"A" to mapOf("B" to 4.0, "C" to 7.0),
71-
"B" to mapOf("Finish" to 4.0),
72-
"C" to mapOf("Finish" to 3.0)
76+
"Start" to mapOf("A" to 5.0),
77+
"A" to mapOf("B" to 7.0, "C" to 4.0),
78+
"B" to mapOf("Finish" to 4.0),
79+
"C" to mapOf("Finish" to 3.0)
7380
)
7481

7582
println(
76-
waypoints.dijkstra("Start", "Finish")
83+
waypoints.dijkstra("Start", "Finish")
7784
)
7885
}

src/python/sequential/graph/bfs/breadth_first_traversal.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ def main():
2828
"bob": ["anuj", "peggy"],
2929
}
3030

31-
print(
32-
bfs(graph, "you")
33-
)
31+
print(bfs(graph, "you"))
3432

3533
if __name__ == "__main__":
3634
main()

0 commit comments

Comments
 (0)