我正在尝试使用Java实现BFS。 我从this question获得了一个代码,我将其修改如下。
我将字符串类型更改为存储到对象类型。 它不起作用。如果它是一个字符串,它将起作用。谁能告诉我为什么?
我的代码如下。
package bfs;
import java.util.*;
/**
*
* @author Harikrishnan
*/
public class BFS {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Search.execute();
}
}
class Graph {
private Map <Node, LinkedHashSet<Node>> map = new HashMap();
public void addEdge(Node node1, Node node2) {
LinkedHashSet<Node> adjacent = map.get(node1);
if(adjacent==null) {
adjacent = new LinkedHashSet();
map.put(node1, adjacent);
}
adjacent.add(node2);
}
public void addTwoWayVertex(Node node1, Node node2) {
addEdge(node1, node2);
addEdge(node2, node1);
}
public boolean isConnected(Node node1, Node node2) {
Set adjacent = map.get(node1);
if(adjacent==null) {
return false;
}
return adjacent.contains(node2);
}
public LinkedList<Node> adjacentNodes(Node last) {
LinkedHashSet<Node> adjacent = map.get(last);
if(adjacent==null) {
return new LinkedList();
}
return new LinkedList<Node>(adjacent);
}
}
class Search {
private static final Node START = new Node("1");
private static final Node END = new Node("4");
public static void execute() {
// this graph is directional
Graph graph = new Graph();
graph.addEdge(new Node("1"), new Node( "2"));
graph.addEdge(new Node("2"), new Node( "1"));
graph.addEdge(new Node("2"), new Node( "3"));
graph.addEdge(new Node("2"), new Node( "4"));
graph.addEdge(new Node("2"), new Node( "7"));
graph.addEdge(new Node("3"), new Node("5"));
graph.addEdge(new Node("3"), new Node( "6"));
graph.addEdge(new Node("3"), new Node( "2"));
graph.addEdge(new Node("4"), new Node( "2"));
graph.addEdge(new Node("4"), new Node( "7"));
graph.addEdge(new Node("4"), new Node( "8"));
graph.addEdge(new Node("5"), new Node( "3"));
graph.addEdge(new Node("5"), new Node( "6"));
graph.addEdge(new Node("5"), new Node("9"));
graph.addEdge(new Node("6"), new Node( "3"));
graph.addEdge(new Node("6"), new Node("7"));
graph.addEdge(new Node("6"), new Node("5"));
graph.addEdge(new Node("6"), new Node("9"));
graph.addEdge(new Node("7"), new Node("2"));
graph.addEdge(new Node("7"), new Node("6"));
graph.addEdge(new Node("7"), new Node("8"));
graph.addEdge(new Node("7"), new Node("10"));
graph.addEdge(new Node("8"), new Node("4"));
graph.addEdge(new Node("8"), new Node("7"));
graph.addEdge(new Node("8"), new Node("10"));
graph.addEdge(new Node("9"), new Node("5"));
graph.addEdge(new Node("9"), new Node("6"));
graph.addEdge(new Node("9"), new Node("10"));
graph.addEdge(new Node("10"), new Node( "9"));
graph.addEdge(new Node("10"), new Node("7"));
graph.addEdge(new Node("10"), new Node("8"));
LinkedList<Node> visited = new LinkedList();
visited.add(START);
new Search().breadthFirst(graph, visited);
}
private void breadthFirst(Graph graph, LinkedList<Node> visited) {
LinkedList<Node> nodes = graph.adjacentNodes(visited.getLast());
// examine adjacent nodes
for (Node node : nodes) {
if (visited.contains(node)) {
continue;
}
if (node.equals(END)) {
visited.add(node);
printPath(visited);
visited.removeLast();
break;
}
}
// in breadth-first, recursion needs to come after visiting adjacent nodes
for (Node node : nodes) {
if (visited.contains(node) || node.equals(END)) {
continue;
}
visited.addLast(node);
breadthFirst(graph, visited);
visited.removeLast();
}
}
private void printPath(LinkedList<Node> visited) {
for (Node node : visited) {
System.out.print(node);
System.out.print(" ");
}
System.out.println();
}
}
class Node
{
public String name;
public int x;
public int y;
public Node(String name)
{
this.name = name;
}
@Override
public String toString()
{
return name;
}
@Override
public boolean equals(Object n)
{
return ((Node)n).name.equals(name);
}
}
答案 0 :(得分:0)
你的代码让我感到困惑,所以让我们看看我做了什么......
首先,当你已经拥有一个非常好的搜索时,你为什么要创建一个新的搜索实例?使execute()
构造函数。将new Search().execute()
更改为execute()
。
其次,您是否也打算创建具有相同数字的新节点或重用相同的节点,因为对我来说似乎所有内容都链接到单个节点而不是我假设您打算并将节点链接到彼此并将所有内容链接起来解析它。你甚至不在任何地方包含你的根节点。
如果这是你的意图,请原谅我。正如我所说,你的代码让我困惑。