[Data Structures and Algorithms] dfs depth-first search and breadth-first search bfs

 The first step, a structure diagram, a structure as in FIG.

   

 

 This embodiment has two kinds of configuration in FIG. A two-dimensional array A + Map list, as FIG sparsely constructed using Map plus chain, the code shown below

private static Map<Character,List<Character>> buildGraph(){
        Map<Character, List<Character>> graph = new HashMap<>();
        List<Character> aList = new LinkedList<>(Arrays.asList('B','C'));
        List<Character> bList = new LinkedList<>(Arrays.asList('A','C','D'));
        List<Character> cList = new LinkedList<>(Arrays.asList('A','B','D','E'));
        List<Character> dList = new LinkedList<>(Arrays.asList('B','F','E','C'));
        List<Character> eList = new LinkedList<>(Arrays.asList('C','D'));
        List<Character> fList = new LinkedList<>(Arrays.asList('D'));
        graph.put('A',aList);
        graph.put('B',bList);
        graph.put('C',cList);
        graph.put('D',dList);
        graph.put('E',eList);
        graph.put('F',fList);
        return graph;
    }

  Map representation of key nodes, Value is a list of adjacent nodes.

BFS bfs: The idea is to access the current node, then visit the adjacent node of the current node, and so on. Using FIFO queue characteristics, it may be traversed by the layer.

Depth-first search bfs: The idea is to access the current node and has access to go until the road barrier and then go back.

 

public static void main(String[] args) {

        Map<Character, List<Character>> graph = buildGraph();

        dfs(graph, 'C');

        bfs(graph, 'D');
    }

    private static Map<Character,List<Character>> buildGraph(){
        Map<Character, List<Character>> graph = new HashMap<>();
        List<Character> aList = new LinkedList<>(Arrays.asList('B','C'));
        List<Character> bList = new LinkedList<>(Arrays.asList('A','C','D'));
        List<Character> cList = new LinkedList<>(Arrays.asList('A','B','D','E'));
        List<Character> dList = new LinkedList<>(Arrays.asList('B','F','E','C'));
        List<Character> eList = new LinkedList<>(Arrays.asList('C','D'));
        List <Character> = new new FLIST the LinkedList <> (Arrays.asList ( 'D'));
        graph.put ( 'A', of aList); 
        graph.put ( 'B', Blist); 
        graph.put ( 'C', CLIST); 
        graph.put ( 'D', DLIST); 
        graph.put ( 'E ', ELIST); 
        graph.put (' F. ', FLIST); 
        return Graph; 
    } 

    / ** 
     * depth first search 
     * @param graph to traverse FIG 
     * @param s starting point 
     * / 
    public static void DFS (the Map < Character, List <>> Graph Character, Character S) { 
        // node through 
        the Set <Character> = new new visited HashSet <> (); 
        Stack <Character> = new new Stack Stack <> (); 
        stack.push (S ); 

        the while (Stack!.empty()){
            Character accessC = stack.pop();
            if(!visited.contains(accessC)){
                System.out.print("->"+accessC);
                visited.add(accessC);
            }
            graph.get(accessC).forEach(c ->{
                if(!visited.contains(c)){
                    stack.push(c);
                }
            });
        }
    }

    public static void bfs(Map<Character,List<Character>> graph, Character s){
        //走过的节点
        Set<Character> visited = new HashSet<>();
        Queue<Character> queue = new LinkedList<>();

        queue.offer(s);

        while (!queue.isEmpty()){
            Character accessC = queue.poll();
            if(!visited.contains(accessC)){
                System.out.print("->"+accessC);
                visited.add(accessC);
            }
            graph.get(accessC).forEach(c ->{
                if(!visited.contains(c)){
                    queue.offer(c);
                }
            });
        }
    }

  

Guess you like

Origin www.cnblogs.com/zhengwangzw/p/11482085.html