Graph's Algorithms
图中的算法
图的基础算法校验
abstract class GraphCompute {
protected val graph: Graph
constructor(graph: Graph) {
this.graph = graph
}
fun checkEmpty(): GraphCompute {
require(!graph.isEmpty()) {
"Graph is empty"
}
return this
}
fun checkDirected(expectedDirected: Boolean): GraphCompute {
require(graph.isDirected() == expectedDirected) {
"Graph is not ${if (expectedDirected) "directed" else "undirected"}"
}
return this
}
fun checkWeighted(expectedWeighted: Boolean): GraphCompute {
require(graph.isWeighted() == expectedWeighted) {
"Graph is not ${if (expectedWeighted) "weighted" else "unweighted"}"
}
return this
}
fun checkVertex(name: String, required: Boolean): Vertex {
return graph.vertexIndex().getVertex(name) ?: if (required) {
throw IllegalArgumentException("Vertex '$name' not found")
} else {
throw NoSuchElementException("Vertex '$name' not found")
}
}
}
27 January 2026