Skip to content

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")
        }
    }

}