BFS广度优先搜索

广度优先搜索(Breadth-First Search, BFS)是一种用于遍历或搜索树或图的算法。

它从根节点开始,逐层访问每个节点,并在访问完一层后才访问下一层。BFS常用于寻找最短路径的问题。

下面将用实例一和实例二来实现BFS广度优先搜索

实例一

使用BFS来遍历一个无向图,这里我们将使用邻接表来表示图。

cs 复制代码
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        // 创建一个图实例
        Graph graph = new Graph(5);

        // 添加边到图中
        graph.AddEdge(0, 1);
        graph.AddEdge(0, 2);
        graph.AddEdge(1, 2);
        graph.AddEdge(2, 0);
        graph.AddEdge(2, 3);
        graph.AddEdge(3, 3);

        Console.WriteLine("广度优先搜索结果 (从顶点 2 开始):");
        graph.BFS(2);
    }
}

public class Graph
{
    private int V; 
    private List<int>[] adj; 

    public Graph(int v)
    {
        V = v;
        adj = new List<int>[v];
        for (int i = 0; i < v; ++i)
        {
            adj[i] = new List<int>();
        }
    }
    public void AddEdge(int v, int w)
    {
        adj[v].Add(w); 
        adj[w].Add(v); 
    }
    public void BFS(int s)
    {
        bool[] visited = new bool[V]; 

        Queue<int> queue = new Queue<int>();

        visited[s] = true;
        queue.Enqueue(s);

        while (queue.Count != 0)
        {
            s = queue.Dequeue();
            Console.Write(s + " ");
            foreach (var adjacent in adj[s])
            {
                if (!visited[adjacent])
                {
                    visited[adjacent] = true;
                    queue.Enqueue(adjacent);
                }
            }
        }
    }
}

实例二:

使用队列(Queue)来实现广泛搜索(BFS)

给定一个简单的二叉树节点类TreeNode(包含Value属性和LeftRight子节点引用),使用Queue<TreeNode>实现二叉树的广度优先搜索,打印出节点的值。

cs 复制代码
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 二叉树节点
{
    internal class Program
    {
        static void Main(string[] args)
        {
            TreeNode root= new TreeNode { value = 1 };
            root.Left=new TreeNode { value = 2 };
            root.Right=new TreeNode { value = 3 };
            root.Left.Left=new TreeNode { value = 4 };
            root.Left.Right=new TreeNode { value = 5 };
            BFS(root);
        }
        static void BFS(TreeNode root)
        {
            if (root == null)
            {
                return;
            }
            Queue<TreeNode> queue = new Queue<TreeNode>();
            queue.Enqueue(root);
            while (queue.Count>0)
            {
                TreeNode currert = queue.Dequeue();
                Console.Write(currert.value+" ");
                if (currert.Left != null)
                {
                    queue.Enqueue(currert.Left);
                }
                if (currert.Right != null)
                {
                    queue.Enqueue(currert.Right);
                }
            }
        }
    }
    class TreeNode
    {
        public int value;
        public TreeNode Left { get; set; }
        public TreeNode Right { get; set; }
    }
    
}
相关推荐
Evand J几秒前
【MATLAB例程】自适应渐消卡尔曼滤波,背景为二维雷达目标跟踪,基于扩展卡尔曼(EKF)|附完整代码的下载链接
开发语言·matlab·目标跟踪·1024程序员节
百锦再11 分钟前
低代码开发的约束性及ABP框架的实践解析
android·开发语言·python·低代码·django·virtualenv·rxjava
csbysj202039 分钟前
Scala 字符串
开发语言
自动化小秋葵1 小时前
Python入门经典题目
开发语言·python
居7然1 小时前
DeepSeek OCR:重新定义AI文档处理的“降本增效”新范式
人工智能·算法·语言模型·自然语言处理·大模型·ocr
while(1){yan}1 小时前
数据结构之堆
数据结构·python·算法
编程岁月2 小时前
java面试-0305-java线程调度方法?sleep()和wait()区别?
java·开发语言·面试
凌晨一点的秃头猪2 小时前
Python 常见 bug 总结和异常处理
开发语言·python·bug
SleepyWhite0012 小时前
代码随想录Day61|Floyd 算法精讲、A * 算法精讲
算法·floyd算法·astar算法
Miraitowa_cheems2 小时前
LeetCode算法日记 - Day 84: 乘积为正数的最长子数组长度
数据结构·算法·leetcode·贪心算法·线性回归·深度优先·动态规划