作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个图,其节点具有以下结构:
Class Node:
int source;
int next;
所以,我有以下节点:[(1,2), (2,3), (3,4), (1,4)]
我想列出从 1 到 3 的所有可能路径,它将列出:[[(1,2),(2,3)],[(1,4),(4,3)]。
我正在尝试使用这段代码,但我遗漏了一些东西:
public List<Node> getNodeNeighbors(Node n) {
List<Node> filteredNodes = new ArrayList<Node>();
List<Node> allNodes = (List<Node>) nodesRepository.findAll();
for (Node node: allNodes) {
if (node.source == n.next) {
filteredNodes.add(node);
}
}
return filteredNodes;
}
public List<Node> bfs(Node n, String destinationNodeNumber, List<Node> path) {
visitedX.add(n); //visitedX is a global List to control visited nodes
path.add(n); //local path to be listed
List<Node> neighbors = getNodeNeighbors(n); //function to get node neighbors
if (n.next.equals(destinationNodeNumber)) {
allPaths.add(paths); //all paths to be listed
path.remove(n);
}
for (Node nNode: neighbors) {
if(!visitedX.contains(nNode)) {
bfs(nNode, destinationNodeNumber, path);
}
}
return null;
}
最佳答案
您的代码中有很多缺陷:
Node
具有误导性:Edge
是一个更好的名称,getNodeNeighbors
仅考虑每条边的一个方向aCompany
和 anotherCompany
字段是什么?我假设您指的是source
和next
?Contract
?destinationNodeNumber
是一个String
;它应该是一个int
。visitedX
集可防止两条路径使用相同的边;您只需要确保一条边在单个路径中出现的次数不会超过一次。路径
添加到allPaths
;您应该复印一份。这是一个类Edge
:
public class Edge {
final int source;
final int next;
Edge(int source, int next) {
this.source = source;
this.next = next;
}
@Override
public String toString() {
return "(" + source + "," + next + ')';
}
}
然后是包含搜索算法的Graph
类:
public class Graph {
private final Iterable<Edge> allNodes;
public Graph(Iterable<Edge> allNodes) {
this.allNodes = allNodes;
}
public List<Edge> edgesFrom(int vertex) {
List<Edge> filteredNodes = new ArrayList<Edge>();
for (Edge node : allNodes) {
if (node.source == vertex || node.next == vertex) {
filteredNodes.add(node);
}
}
return filteredNodes;
}
public List<List<Edge>> allPaths(int source, int dest) {
List<Edge> path = new ArrayList<>();
List<List<Edge>> allPaths = new ArrayList<>();
for (Edge n: edgesFrom(source)) {
searchPaths(n, source, dest, path, allPaths);
}
return allPaths;
}
private void searchPaths(Edge n, int source, int dest, List<Edge> path,
List<List<Edge>> allPaths) {
path.add(n); //local path to be listed
int next = n.source == source ? n.next : n.source;
List<Edge> neighbors = edgesFrom(next); //function to get node neighbors
if (next == dest) {
allPaths.add(new ArrayList<>(path)); //all paths to be listed
}
for (Edge nNode : neighbors) {
if (!path.contains(nNode)) {
searchPaths(nNode, next, dest, path, allPaths);
}
}
path.remove(n);
}
}
这是使用这些类的示例:
Graph graph = new Graph(Arrays.asList(
new Edge(1,2), new Edge(2,3), new Edge(3,4), new Edge(1,4)));
List<List<Edge>> allPaths = graph.allPaths(1,3);
for (List<Edge> path: allPaths) {
System.out.println(path);
}
关于java - 如何列出无向图中从一个节点到另一个节点的所有路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56015504/
我是一名优秀的程序员,十分优秀!