我正在使用矩阵,我需要获取从源索引到目标索引的所有路径(其中索引值不为 0),同时我可以选择是否按真/假计算对角线)。
我可以通过这种方式将变量发送到方法:(src, dest, indlueDiagnoal)
作为回报,我需要该方法提供所有路径的列表。
这是我拥有的类,它执行 TLS(线程本地存储)DFS,并且需要使方法遍历适应我拥有的变量:
public class ThreadLocalDfsVisit<T> {
protected final ThreadLocal<Stack<Node<T>>> stackThreadLocal
= ThreadLocal.withInitial(Stack::new);
protected final ThreadLocal<Set<Node<T>>> setThreadLocal =
ThreadLocal.withInitial(() -> new LinkedHashSet<>());
protected void threadLocalPush(Node<T> node) {
stackThreadLocal.get().push(node);
}
protected Node<T> threadLocalPop() {
return stackThreadLocal.get().pop();
}
public Set<T> traverse(HashSet<Index> visitedIndexes, Traversable<T> partOfGraph) {
threadLocalPush(partOfGraph.getOrigin());
while (!stackThreadLocal.get().isEmpty()) {
Node<T> poppedNode = threadLocalPop();
setThreadLocal.get().add(poppedNode);
Collection<Node<T>> reachableNodes = partOfGraph.getReachableNodes(poppedNode, true);
for (Node<T> singleReachableNode : reachableNodes) {
if (!setThreadLocal.get().contains(singleReachableNode) &&
!stackThreadLocal.get().contains(singleReachableNode)) {
threadLocalPush(singleReachableNode);
}
}
}
HashSet<T> blackList = new HashSet<>();
for (Node<T> node : setThreadLocal.get()) {
visitedIndexes.add((Index) node.getData());
blackList.add(node.getData());
}
stackThreadLocal.remove();
setThreadLocal.remove();
return blackList;
}
}
它应该以一种我可以从这个方法调用它的方式进行调整:
public List<List<Index>> getMinimumPath(Index src, Index dest, boolean indlueDiagnoal) {
List<List<Index>> allPathes = mThreadLocalDfsAlgorithem.traverse(src, dest, indlueDiagnoal); //find all paths
return getShortetPathWithWeight(allPathes);