コード例 #1
0
        //BFS
        public static Boolean searchBFS(Graph g, Node start, Node end)
        {
            foreach(Node node in g.getNodes()){
                node.state = State.Unvisited;
            }

            Queue<Node> queue = new Queue<Node>();
            start.state = State.Visiting;
            queue.Enqueue(start);
            Node u;
            while(queue.Count != 0){
                u = queue.Dequeue();
                if( u != null){
                    foreach(Node n in u.getAdjacent()){
                        if(n.state == State.Unvisited){
                            if( n == end)
                                return true;
                            else{
                                n.state = State.Visiting;
                                queue.Enqueue(n);
                            }
                        }
                    }
                    u.state = State.Visited;
                }
            }
            return false;
        }
コード例 #2
0
        //DFS
        public static Boolean searchDFS(Graph g, Node start, Node end)
        {
            foreach (Node node in g.getNodes())
            {
                node.state = State.Unvisited;
            }

            return searchDFS(start, false, end);
        }
コード例 #3
0
        public static Graph createNewGraph()
        {
            Graph g = new Graph();
            Node[] temp = new Node[6];

            temp[0] = new Node("a", 3);
            temp[1] = new Node("b", 0);
            temp[2] = new Node("c", 0);
            temp[3] = new Node("d", 1);
            temp[4] = new Node("e", 1);
            temp[5] = new Node("f", 0);

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

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

            return g;
        }