Graph Theory Algorithm (5): Graph Breadth-First Traversal BFS

The content of this chapter is implemented in java, Github code warehouse: https://github.com/ZhekaiLi/Code/tree/main/Graph/src

Viewing the pictures in the article may require scientific Internet access!Because github is used to manage pictures, if there is a situation that cannot be loaded, please turn over the wall

[Reference] imooc Mr. Bobo: Fun Algorithm Series - Graph Theory Essentials for Interview and Promotion (Java Edition)

[Links to previous blogs]
Graph Theory Algorithms (1, 2): Classification of Graphs, Basic Concepts of Graphs (Undirected Graphs and Directed Graphs, Unweighted Graphs, Acyclic Graphs, Complete Graphs, Bipartite Graphs; Simple Graphs, Connected Graphs Components, spanning tree of graphs, subgraphs and parent graphs)
graph theory algorithm (3): basic representation of graphs (adjacency matrix, adjacency list, comparison of adjacency matrix and adjacency list)
graph theory algorithm (4): depth first of graphs Traversal DFS
Graph Theory Algorithm (5): Graph Breadth First Traversal BFS
Graph Theory Algorithm (6): LeetCode Graph Theory Algorithm Exercise (785. Judgment of Bipartite Graph, 695. Maximum Area of ​​Island, Floodfill Algorithm, Union Check)

5. Breadth-first traversal of graph BFS

Let's first look at the breadth-first traversal of the tree . The following figure shows the general process of using the queue to perform the BFS of the tree.



bfs(root); // 从根结点开始遍历

bfs(TreeNode node)
    queue.add(node);
    while(!queue.isEmpty)
        v = queue.remove();
        list.add(v);
        for(w: v.son())
            queue.add(w)

The breadth-first traversal of the graph is logically the same as the BFS of the tree, except that a judgment of whether the node w has been visited needs to be added queue.add(w)before . The time complexity is also O ( V + E ) O(V+E)O ( V+E)

java implementation: GraphBFS.java

Most of the problems that DFS can solve can also be solved by BFS

5.1 Ex: Find the path between two points

Similarly Section 4.2

pre[0...V-1] = -1;
s = 0; // 自定义的起始点
t = 5; // 自定义的终止点

bfs(s); // 从根结点开始遍历

bfs(int v)
    queue.add(v);
    pre[v] = v;
    while(!queue.isEmpty)
        v = queue.pop();
        list.add(v);
        for(w: v.son())
            queue.add(w);
            pre[w] = v;

java implementation: SingleSourcePathBFS.java

5.2 Properties: Unweighted Graph Shortest Path

The same is from 0 → 6 0\to606. The path of BFS is shorter than that of DFS. This property also applies to any point, that is,BFS can find the shortest path between any point and the root node.



This is because BFS is a layer-order traversal , starting from the root node and traversing from near to far, and each traversal is looking for the nearest node (it can be understood as a greedy algorithm, when the edge does not contain weight, greedy = the most excellent)

If the distance information is directly recorded during the traversal process, dis[]the shortest distance from the root node to any node can be directly read out



java implementation: USSSPath.java

5.3 Comparison: DFS vs. BFS

Comparing BFS and DFS (non-recursive), we find that the only difference is that DFS uses stacks and BFS uses queues (DFS head in and head out when processing nodes, BFS tail in and head out) ( statck.add(s)corresponding stack.push(), statck.remove()corresponding stack.pop())



Further, we can also use any structure to replace the above figure stack/ queueto achieve a custom traversal method.

For example, a random container (random queue) can be used to generate a random maze by random access to edges



Larger and better looking example:



Guess you like

Origin blog.csdn.net/weixin_43728138/article/details/118937900