-2

我正在尝试使用 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);
    }
}
4

1 回答 1

0

你的代码让我有点困惑,所以让我们看看我是怎么做的......

首先,当你已经有一个非常好的搜索实例时,为什么还要创建一个新的搜索实例?制作execute()构造函数。更改new Search().execute()execute().

其次,您是否还打算创建具有相同数量的新节点或重复使用相同的节点,因为对我来说似乎所有内容都链接到单个节点,而不是我假设您想要的并将节点相互链接和将所有内容链接到解析它。你甚至没有在任何地方包含你的根节点。

如果这是你想要的,请原谅我。正如我所说,你的代码让我感到困惑。

于 2012-12-14T14:01:50.417 回答