Skip to content

Commit d575a30

Browse files
committed
java dijkstra improvements
1 parent a03b204 commit d575a30

File tree

5 files changed

+20
-25
lines changed

5 files changed

+20
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ An encyclopedia of computer science algorithms and data structures.
1111
## Algorithms
1212
...
1313

14-
All the links refer to `Kotlin` implementations by default. If you prefer another language feel free to check the list of [available languages](#available-languages).
14+
By default all the links below refer to `Kotlin` algorithm implementations. If you prefer another language feel free to check the list of [available languages](#available-languages).
1515

1616
`B` - Base, `A` - Advanced
1717
### By category

src/java/sequential/graph/dijkstra/Dijkstra.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
import java.util.Map;
88
import java.util.Set;
99

10+
import static java.util.Map.Entry.comparingByValue;
1011

11-
class Dijkstra<T> {
12+
13+
class Dijkstra {
1214

1315
private static final double INFINITY = Double.POSITIVE_INFINITY;
1416

15-
17+
1618
public static <T> List<T> dijkstra(Map<T, Map<T, Double>> graph, T root, T target) {
17-
if(!graph.containsKey(root)) throw new IllegalArgumentException("Invalid root.");
19+
if (!graph.containsKey(root)) throw new IllegalArgumentException("Invalid root.");
1820

1921
var costs = new HashMap<>(graph.get(root));
2022
var explored = new HashSet<T>();
@@ -43,16 +45,13 @@ public static <T> List<T> dijkstra(Map<T, Map<T, Double>> graph, T root, T targe
4345
}
4446

4547
private static <T> T findMinCostNode(Map<T, Double> costs, Set<T> explored) {
46-
var minCost = INFINITY;
47-
T minCostNode = null;
48-
for (var node : costs.keySet()) {
49-
var cost = costs.get(node);
50-
if (!explored.contains(node) && cost < minCost) {
51-
minCost = cost;
52-
minCostNode = node;
53-
}
54-
}
55-
return minCostNode;
48+
return costs.entrySet().stream()
49+
// ignoring already explored nodes.
50+
.filter(node -> !explored.contains(node.getKey()))
51+
// finding the min cost node. `value` corresponds to node cost.
52+
.min(comparingByValue())
53+
// returning node object or `null` if the min node was not found.
54+
.map(node -> node.getKey()).orElse(null);
5655
}
5756

5857
private static <T> List<T> buildPathToNode(T target, Map<T, T> parents) {

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ private val INFINITY = Double.POSITIVE_INFINITY
1616
/**
1717
* Finds shortest path to the [target] node using Dijkstra algorithm.
1818
*/
19-
fun <T> Graph<T>.dijkstra(root: T, target: T): Collection<T> {
19+
fun <T: Any> Graph<T>.dijkstra(root: T, target: T): Collection<T> {
2020
val graph = this
2121
require(root in graph) { "Invalid root." }
2222

@@ -55,16 +55,12 @@ private fun <T> Map<T, Double>.findMinCostNode(explored: Collection<T>): T? =
5555
/**
5656
* Build path from root to target node.
5757
*/
58-
private fun <T> buildPathToNode(target: T, parents: Map<T, T>): List<T> {
58+
private fun <T: Any> buildPathToNode(target: T, parents: Map<T, T>): List<T> {
5959
require(target in parents) { "Invalid target."}
6060

61-
val path = mutableListOf<T>()
62-
var node: T? = target
63-
while (node != null) {
64-
path += node
65-
node = parents[node]
66-
}
67-
return path.reversed()
61+
return generateSequence(target) { node -> parents[node] }
62+
.toList()
63+
.asReversed()
6864
}
6965

7066

src/swift/sequential/graph/bfs/BreadthFirstTraversal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func bfs<T>(in graph: Graph<T>, withRoot root: T) -> [T] {
77
var searchQueue = [root]
88
var queuePopIndex = 0
99

10-
while searchQueue.count > queuePopIndex {
10+
while queuePopIndex < searchQueue.count {
1111
let node = searchQueue[queuePopIndex]
1212
queuePopIndex += 1
1313

src/swift/sequential/graph/dijkstra/Dijkstra.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private func findMinCostNode<T>(in costs: [T: Double], except explored: Set<T>)
5555
.filter { node, cost in !explored.contains(node) }
5656
// finding the min cost node. `value` corresponds to node cost.
5757
.min { cost1, cost2 in cost1.value < cost2.value}?
58-
// returning node object or `nil` if the node was not found.
58+
// returning node object or `nil` if the min node was not found.
5959
.key
6060
}
6161

0 commit comments

Comments
 (0)