Вопрос

The depth first search does not function properly. Honestly I am not sure if this is the current way of implementation. I am new to implementing graph and want to be a pro at it. Where am I going wrong and I do not understand how do I print the elements either in the dfs() function so I can know how dfs should be like.

Would getter & setter method for child elements is recommended?

Here is my code:

    package graphs;

    public enum State {

        Unvisited,Visiting,Visited;

    }

    package graphs;

    public class Node {

        public Node[] adjacent;
        public int adjacentCount;
        private String vertex;
        public graphs.State state;

        public Node(String vertex)
        {
            this.vertex = vertex;
        }
        public Node(String vertex, int adjacentlen)
        {
            this.vertex = vertex;
            adjacentCount = 0;
            adjacent = new Node[adjacentlen];
        }

        public void addAdjacent(Node adj)
        {
            if(adjacentCount < 30)
            {
                this.adjacent[adjacentCount] = adj;
                adjacentCount++;
            }
        }

        public Node[] getAdjacent()
        {
            return adjacent;

        }

        public String getVertex()
        {
            return vertex;
        }

    }

    public class Graph {

        public int count; // num of vertices
        private Node vertices[];

        public Graph()
        {
            vertices = new Node[8];
            count = 0;
        }

        public void addNode(Node n)
        {
            if(count < 10)
            {
                vertices[count] = n;
                count++;
            }
            else
            {
                System.out.println("graph full");
            }
        }

        public Node[] getNode()
        {
            return vertices;
        }
    }


    package graphs;

    import java.util.Stack;
    import graphs.State;
    public class Dfs {

        /**
         * @param args
         */

        static boolean visited[];

        public void dfs(Node root)
        {       
            if(root == null) return;

            Stack<Node> s = new Stack<Node>();
            s.push(root);
            root.state = State.Visited;

            System.out.println("root:"+root.getVertex() + "\t");
            while(!s.isEmpty())
            {
                Node u = s.pop();
                for(Node n: u.getAdjacent())
                {

                    if(n.state != State.Visited)
                    {
                        dfs(n);
                    }
                }
            }
        }

        public static Graph createNewGraph()
        {
            Graph g = new Graph();        
            Node[] temp = new Node[8];

            temp[0] = new Node("A", 3);
    temp[1] = new Node("B", 3);
    temp[2] = new Node("C", 1);
    temp[3] = new Node("D", 1);
    temp[4] = new Node("E", 1);
    temp[5] = new Node("F", 1);

    temp[0].addAdjacent(temp[1]);
    temp[0].addAdjacent(temp[2]);
    temp[0].addAdjacent(temp[3]);

    temp[1].addAdjacent(temp[0]);
    temp[1].addAdjacent(temp[4]);
    temp[1].addAdjacent(temp[5]);

    temp[2].addAdjacent(temp[0]);
    temp[3].addAdjacent(temp[0]);
    temp[4].addAdjacent(temp[1]);
    temp[5].addAdjacent(temp[1]);

            for (int i = 0; i < 7; i++) 
            {
                g.addNode(temp[i]);
            }
            return g;
        }

        public static void main(String[] args) {


            Graph g = createNewGraph();
            Dfs s = new Dfs();
            //Node[] n = g.getNode();

            s.dfs(g.getNode()[0]);

        }

    }
Это было полезно?

Решение

You don't need stack here. Only recursion:

public void dfs(Node node) {
    if (node == null) {
        return;
    }

    System.out.println("Node: " + node.getVertex());
    node.state = State.Visited;

    for (Node n : node.getAdjacent()) {
        if (n.state != State.Visited) {
            dfs(n);
        }
    }
}

UPDATE

To check path existence:

public boolean isPath(Graph graph, Node start, Node target) {
    for (Node node : graph.getNode()) {
        if (node != null) {
            node.state = State.Unvisited;
        }
    }

    dfs(start);

    return target.state == State.Visited;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top