-2

I am trying to implement BFS using Java. I got a code from this question and I modified it as follows.

I changes the string type to store to an object type. It is not working. If it is a string, it will work. Can anybody tell me why?

My code is given below.

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);
    }
}
Community
  • 1
  • 1
Harikrishnan
  • 3,664
  • 7
  • 48
  • 77

1 Answers1

0

Your code confuses me a little bit so lets see what i make of it...

Firstly, why are you making a new instance of search when you already have a perfectly good one there? Make execute() the constructor. Change new Search().execute() to just execute().

Secondly, Are you also meant to be creating new nodes with the same number or reusing the same nodes because to me is seems like everything is linked to a single node rather than what I assume you are intending and to link the nodes to each other and have everything linked to parse it. You don;t even include your root node anywhere.

Forgive me if this is what you intended. As I said, your code confuses me.

Skepi
  • 478
  • 3
  • 12