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; }
    }
    
}
相关推荐
screenCui1 小时前
macOS运行python程序遇libiomp5.dylib库冲突错误解决方案
开发语言·python·macos
linux kernel1 小时前
第七讲:C++中的string类
开发语言·c++
jz_ddk1 小时前
[实战]调频(FM)和调幅(AM)信号生成(完整C语言实现)
c语言·算法·信号处理
玩代码1 小时前
Java线程池原理概述
java·开发语言·线程池
CloudAce云一2 小时前
谷歌云代理商:谷歌云TPU/GPU如何加速您的AI模型训练和推理
算法
水果里面有苹果2 小时前
20-C#构造函数--虚方法
java·前端·c#
泰勒疯狂展开2 小时前
Java研学-MongoDB(三)
java·开发语言·mongodb
zzywxc7872 小时前
AI技术通过提示词工程(Prompt Engineering)正在深度重塑职场生态和行业格局,这种变革不仅体现在效率提升,更在重构人机协作模式。
java·大数据·开发语言·人工智能·spring·重构·prompt
高hongyuan2 小时前
Go语言教程-占位符及演示代码
开发语言·后端·golang
轻语呢喃2 小时前
每日LeetCode : 杨辉三角
javascript·后端·算法