我正在尝试使用 Java 实现 BFS。我从这个问题中得到了一个代码,我对其进行了如下修改。
我将要存储的字符串类型更改为对象类型。它不工作。如果它是一个字符串,它将起作用。谁能告诉我为什么?
我的代码如下。
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);
}
}