Skip to content

Commit 86a73c6

Browse files
committed
minor dijkstra refactoring
1 parent d575a30 commit 86a73c6

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212

1313
class Dijkstra {
1414

15+
// Infinity as a default vertex cost value.
1516
private static final double INFINITY = Double.POSITIVE_INFINITY;
1617

1718

1819
public static <T> List<T> dijkstra(Map<T, Map<T, Double>> graph, T root, T target) {
19-
if (!graph.containsKey(root)) throw new IllegalArgumentException("Invalid root.");
20+
if (!graph.containsKey(root)) throw new IllegalArgumentException("Invalid root vertex.");
2021

2122
var costs = new HashMap<>(graph.get(root));
2223
var explored = new HashSet<T>();
@@ -55,7 +56,7 @@ private static <T> T findMinCostNode(Map<T, Double> costs, Set<T> explored) {
5556
}
5657

5758
private static <T> List<T> buildPathToNode(T target, Map<T, T> parents) {
58-
if (!parents.containsKey(target)) throw new IllegalArgumentException("Invalid target.");
59+
if (!parents.containsKey(target)) throw new IllegalArgumentException("Invalid target vertex.");
5960

6061
var path = new LinkedList<T>();
6162
T node = target;

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package sequential.graph.dijkstra
22

33

4-
/**
5-
* Weighted graph representation using hash table.
6-
* @param T graph value type.
7-
*/
8-
private typealias Graph<T> = Map<T, Map<T, Double>>
4+
// Weighted graph map representation where `key` = source vertex, `value` = destination vertices with edge weights.
5+
private typealias Graph<T> = Map<T, Edges<T>>
6+
// Weighted edges to destination vertices.
7+
private typealias Edges<T> = Map<T, Weight>
8+
// Edge weight.
9+
private typealias Weight = Double
910

10-
/**
11-
* Infinity value which is used as graph edge weight.
12-
*/
11+
12+
// Infinity as a default vertex cost value.
1313
private val INFINITY = Double.POSITIVE_INFINITY
1414

1515

@@ -18,7 +18,7 @@ private val INFINITY = Double.POSITIVE_INFINITY
1818
*/
1919
fun <T: Any> Graph<T>.dijkstra(root: T, target: T): Collection<T> {
2020
val graph = this
21-
require(root in graph) { "Invalid root." }
21+
require(root in graph) { "Invalid root vertex." }
2222

2323
val costs = graph[root]!!.toMutableMap()
2424
val explored = mutableSetOf<T>()
@@ -43,7 +43,7 @@ fun <T: Any> Graph<T>.dijkstra(root: T, target: T): Collection<T> {
4343
}
4444

4545
private fun <T> Map<T, Double>.findMinCostNode(explored: Collection<T>): T? =
46-
// turning costs to sequence to improve performance of further operations.
46+
// turning costs to sequence for faster further operations.
4747
asSequence()
4848
// ignoring already explored nodes.
4949
.filter { (node, _) -> node !in explored }
@@ -56,7 +56,7 @@ private fun <T> Map<T, Double>.findMinCostNode(explored: Collection<T>): T? =
5656
* Build path from root to target node.
5757
*/
5858
private fun <T: Any> buildPathToNode(target: T, parents: Map<T, T>): List<T> {
59-
require(target in parents) { "Invalid target."}
59+
require(target in parents) { "Invalid target vertex."}
6060

6161
return generateSequence(target) { node -> parents[node] }
6262
.toList()
@@ -65,7 +65,7 @@ private fun <T: Any> buildPathToNode(target: T, parents: Map<T, T>): List<T> {
6565

6666

6767
fun main() {
68-
val graph: Graph<String> = mapOf(
68+
val graph = mapOf(
6969
"Start" to mapOf("A" to 5.0),
7070
"A" to mapOf("B" to 7.0, "C" to 4.0),
7171
"B" to mapOf("Finish" to 4.0),

src/python/sequential/graph/dijkstra/dijkstra.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@ def dijkstra(graph, root, target):
99
explored = set()
1010
parents = {node: root for node in graph[root]}
1111

12-
node = find_min_cost_node(costs, explored)
12+
node = _find_min_cost_node(costs, explored)
1313
while node:
1414
explored.add(node)
1515
node_cost = costs[node]
1616

1717
successors = graph.get(node, {})
18-
for (succ, edge_cost) in successors.items():
19-
if node_cost + edge_cost < (costs.get(succ) or infinity):
20-
costs[succ] = node_cost + edge_cost
18+
for (succ, edge_weight) in successors.items():
19+
if node_cost + edge_weight < (costs.get(succ) or infinity):
20+
costs[succ] = node_cost + edge_weight
2121
parents[succ] = node
2222

23-
node = find_min_cost_node(costs, explored)
23+
node = _find_min_cost_node(costs, explored)
2424

25-
return build_path_to_node(target, parents)
25+
return _build_path_to_node(target, parents)
2626

27-
def find_min_cost_node(costs, explored):
27+
def _find_min_cost_node(costs, explored):
28+
# ignoring already explored nodes.
2829
costs = {node: cost for (node, cost) in costs.items() if node not in explored}
30+
# returning the min cost node or `None` if not found. `costs.get` corresponds to node cost.
2931
return min(costs, key=costs.get, default=None)
3032

31-
def build_path_to_node(target, parents):
33+
def _build_path_to_node(target, parents):
3234
if target not in parents:
3335
return None
3436

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
/// Weighted graph representation using dictionary.
2-
typealias Graph<T: Hashable> = [T: [T: Double]]
1+
/// Weighted graph dictionary representation where `key` = source vertex, `value` = destination vertices edge weights.
2+
typealias Graph<T: Hashable> = [T: Edges<T>]
3+
/// Weighted edges to destination vertices.
4+
typealias Edges<T: Hashable> = [T: Weight]
5+
/// Edge weight.
6+
typealias Weight = Double
37

48

5-
/// Infinity as a default node cost value.
9+
/// Infinity as a default vertex cost value.
610
let infinity = Double.infinity
711

812

@@ -71,11 +75,9 @@ private func findMinCostNode<T>(in costs: [T: Double], except explored: Set<T>)
7175
private func buildPathTo<T>(_ target: T, with parents: [T: T]) -> [T]? {
7276
guard parents[target] != nil else { return nil }
7377

74-
var path = [T]()
75-
var node: T? = target
76-
while node != nil {
77-
path.append(node!)
78-
node = parents[node!]
78+
var path = [target]
79+
while let node = parents[path.last!] {
80+
path.append(node)
7981
}
8082
return path.reversed()
8183
}

0 commit comments

Comments
 (0)