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; }
    }
    
}
相关推荐
CoovallyAIHub9 小时前
中科大DSAI Lab团队多篇论文入选ICCV 2025,推动三维视觉与泛化感知技术突破
深度学习·算法·计算机视觉
NAGNIP10 小时前
Serverless 架构下的大模型框架落地实践
算法·架构
moonlifesudo10 小时前
半开区间和开区间的两个二分模版
算法
moonlifesudo10 小时前
300:最长递增子序列
算法
小码编匠11 小时前
WPF 中的高级交互通过右键拖动实现图像灵活缩放
后端·c#·.net
CoovallyAIHub15 小时前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
CoovallyAIHub16 小时前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉
唐青枫18 小时前
C#.NET 定时任务与队列利器:Hangfire 完整教程
c#·.net
hez20101 天前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
聚客AI1 天前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm