You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

kriegaex/jgrapht中CyclicTransitiveReduction报错:未找到哈密顿环(HC)

JGraphT CyclicTransitiveReduction 抛出无哈密顿环异常问题

问题场景

测试JGraphT的CyclicTransitiveReduction类时,针对一个强连通有向图调用reduce()方法,抛出运行时异常,提示未找到哈密顿环,但该图实际为强连通图,按照算法逻辑应当存在哈密顿环。同时无法在kriegaex/jgrapht仓库找到提交Issue的入口。

测试代码

Graph<String, DefaultEdge> graph = GraphTypeBuilder
  .<String, DefaultEdge>directed()
  .allowingMultipleEdges(false)
  .allowingSelfLoops(false)
  .edgeClass(DefaultEdge.class)
  .buildGraph();

graph.addVertex("a");
graph.addVertex("b");
graph.addVertex("c");
graph.addVertex("d");
graph.addEdge("a", "b");
graph.addEdge("b", "c");
graph.addEdge("c", "a");
graph.addEdge("c", "d");
graph.addEdge("d", "b");

new CyclicTransitiveReduction<>(graph).reduce();

抛出的异常

java.lang.RuntimeException:
  No Hamiltonian cycle (HC) found.
  This should never happen and indicates an error in the algorithm,
  because the graph is strongly connected and a HC must therefore exist.

预期结果

执行CyclicTransitiveReduction.reduce()操作时不应抛出该异常。

内容的提问来源于stack exchange,提问作者sht

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 20:12:39