广度优先算法代码示例

简介: 广度优先算法代码示例

广度优先算法代码示例:

using System;
using System.Collections.Generic;
public class DijkstraAlgorithm
{
    // Dijkstra算法主要逻辑
    public static void Dijkstra(Dictionary<int, List<Edge>> graph, int startNode, int endNode)
    {
        // 存储节点到起点的距离
        Dictionary<int, int> distances = new Dictionary<int, int>();
        // 存储节点的父节点,用于构建最短路径
        Dictionary<int, int> parents = new Dictionary<int, int>();
        // 优先队列,用于按照节点距离从小到大的顺序处理
        PriorityQueue priorityQueue = new PriorityQueue();
        // 初始化distances和parents字典
        foreach (int node in graph.Keys)
        {
            distances[node] = int.MaxValue; // 将所有节点的距离初始化为无穷大
            parents[node] = -1; // 将所有节点的父节点初始化为-1
        }
        distances[startNode] = 0; // 将起始节点的距离设置为0
        priorityQueue.Enqueue(new Node(startNode, 0)); // 将起始节点加入优先队列
        // 开始执行Dijkstra算法
        while (priorityQueue.Count > 0) // 当优先队列不为空时
        {
            Node currentNode = priorityQueue.Dequeue();
            
            // 如果当前节点的距离值大于distances中对应节点的距离值,说明当前节点已经被处理过,直接跳过
            if (currentNode.Value > distances[currentNode.Vertex])
                continue;
            // 对当前节点的所有相邻边进行遍历
            foreach (Edge edge in graph[currentNode.Vertex])
            {
                // 计算从起始节点经过当前节点到目标节点的新距离
                int newDistance = distances[currentNode.Vertex] + edge.Weight;
                // 如果新距离小于distances中对应节点的距离值
                if (newDistance < distances[edge.Destination])
                {
                    distances[edge.Destination] = newDistance; // 更新目标节点的距离值
                    parents[edge.Destination] = currentNode.Vertex; // 更新目标节点的父节点
                    priorityQueue.Enqueue(new Node(edge.Destination, newDistance)); // 将目标节点加入优先队列
                }
            }
        }
        // 判断是否存在从起点到终点的路径
        if (distances[endNode] == int.MaxValue)
        {
            Console.WriteLine("There is no path from startNode to endNode.");
        }
        else
        {
            // 获取最短路径
            List<int> shortestPath = GetShortestPath(parents, startNode, endNode);
            Console.WriteLine($"Shortest distance from {startNode} to {endNode}: {distances[endNode]}");
            Console.WriteLine("Shortest path: " + string.Join(" -> ", shortestPath));
        }
    }
    // 构建最短路径
    public static List<int> GetShortestPath(Dictionary<int, int> parents, int startNode, int endNode)
    {
        List<int> path = new List<int>();
        int currentNode = endNode;
        while (currentNode != -1)
        {
            path.Insert(0, currentNode); // 将节点插入到路径列表的开头
            currentNode = parents[currentNode]; // 更新当前节点为父节点
        }
        return path;
    }
    public static void Main(string[] args)
    {
        Dictionary<int, List<Edge>> graph = new Dictionary<int, List<Edge>>();
        // 创建图形
        // 每个键表示一个节点,每个值表示与该节点相连的所有边
        graph[0] = new List<Edge>
        {
            new Edge(1, 2),
            new Edge(2, 5),
            new Edge(3, 9)
        };
        graph[1] = new List<Edge>
        {
            new Edge(4, 12)
        };
        graph[2] = new List<Edge>
        {
            new Edge(4, 5),
            new Edge(5, 6)
        };
        graph[3] = new List<Edge>
        {
            new Edge(5, 10)
        };
        graph[4] = new List<Edge>
        {
            new Edge(6, 6)
        };
        graph[5] = new List<Edge>
        {
            new Edge(6, 5),
            new Edge(7, 4)
        };
        graph[6] = new List<Edge>
        {
            new Edge(8, 8)
        };
        graph[7] = new List<Edge>
        {
            new Edge(8, 5),
            new Edge(9, 7)
        };
        graph[8] = new List<Edge>
        {
            new Edge(9, 8)
        };
        graph[9] = new List<Edge>();
        int startNode = 0;
        int endNode = 9;
        // 执行Dijkstra算法
        Dijkstra(graph, startNode, endNode);
    }
}
// 边类,表示节点之间的连接关系
public class Edge
{
    public int Destination { get; } // 目标节点
    public int Weight { get; } // 边的权重
    public Edge(int destination, int weight)
    {
        Destination = destination;
        Weight = weight;
    }
}
// 节点类,包含节点编号和距离值
public class Node
{
    public int Vertex { get; } // 节点编号
    public int Value { get; } // 节点距离值
    public Node(int vertex, int value)
    {
        Vertex = vertex;
        Value = value;
    }
}
// 优先队列类,用于保存节点,并确保按照节点的距离值从小到大排序
public class PriorityQueue
{
    private List<Node> heap; // 存放节点的列表
    public int Count => heap.Count;
    public PriorityQueue()
    {
        heap = new List<Node>();
    }
    // 入队操作,将节点加入优先队列并保持有序
    public void Enqueue(Node item)
    {
        heap.Add(item);
        int currentIndex = heap.Count - 1;
        while (currentIndex > 0)
        {
            int parentIndex = (currentIndex - 1) / 2;
            if (heap[currentIndex].Value >= heap[parentIndex].Value)
                break;
            Swap(currentIndex, parentIndex);
            currentIndex = parentIndex;
        }
    }
    // 出队操作,取出距离值最小的节点
    public Node Dequeue()
    {
        if (Count == 0)
            throw new InvalidOperationException("PriorityQueue is empty.");
        Node item = heap[0];
        heap[0] = heap[Count - 1];
        heap.RemoveAt(Count - 1);
        int currentIndex = 0;
        while (currentIndex < Count)
        {
            int leftChildIndex = 2 * currentIndex + 1;
            int rightChildIndex = 2 * currentIndex + 2;
            if (leftChildIndex >= Count)
                break;
            int smallerChildIndex = leftChildIndex;
            if (rightChildIndex < Count && heap[rightChildIndex].Value < heap[leftChildIndex].Value)
                smallerChildIndex = rightChildIndex;
            if (heap[currentIndex].Value <= heap[smallerChildIndex].Value)
                break;
            Swap(currentIndex, smallerChildIndex);
            currentIndex = smallerChildIndex;
        }
        return item;
    }
    // 交换两个位置的节点
    private void Swap(int index1, int index2)
    {
        Node temp = heap[index1];
        heap[index1] = heap[index2];
        heap[index2] = temp;
    }
}

注释很详细!

相关文章
|
4天前
|
算法 PHP
【php经典算法】冒泡排序,冒泡排序原理,冒泡排序执行逻辑,执行过程,执行结果 代码
【php经典算法】冒泡排序,冒泡排序原理,冒泡排序执行逻辑,执行过程,执行结果 代码
6 1
|
11天前
|
机器学习/深度学习 算法 数据挖掘
Python机器学习10大经典算法的讲解和示例
为了展示10个经典的机器学习算法的最简例子,我将为每个算法编写一个小的示例代码。这些算法将包括线性回归、逻辑回归、K-最近邻(KNN)、支持向量机(SVM)、决策树、随机森林、朴素贝叶斯、K-均值聚类、主成分分析(PCA)、和梯度提升(Gradient Boosting)。我将使用常见的机器学习库,如 scikit-learn,numpy 和 pandas 来实现这些算法。
|
12天前
|
人工智能 算法 Java
java中经典算法代码整理
java中经典算法代码整理
18 0
|
12天前
|
算法 IDE 开发工具
c语言的经典算法代码
c语言进阶11-经典算法代码
|
13天前
|
算法
数据结构和算法常见的问题和代码
数据结构和算法常见的问题和代码
10 0
|
1天前
|
机器学习/深度学习 算法 调度
Matlab|基于改进鲸鱼优化算法的微网系统能量优化管理matlab-源码
基于改进鲸鱼优化算法的微网系统能量管理源码实现,结合LSTM预测可再生能源和负荷,优化微网运行成本与固定成本。方法应用于冷热电联供微网,结果显示经济成本平均降低4.03%,提高经济效益。代码包括数据分段、LSTM网络定义及训练,最终展示了一系列运行结果图表。
|
7天前
|
算法 安全 数据库
基于结点电压法的配电网状态估计算法matlab仿真
**摘要** 该程序实现了基于结点电压法的配电网状态估计算法,旨在提升数据的准确性和可靠性。在MATLAB2022a中运行,显示了状态估计过程中的电压和相位估计值,以及误差随迭代变化的图表。算法通过迭代计算雅可比矩阵,结合基尔霍夫定律解决线性方程组,估算网络节点电压。状态估计过程中应用了高斯-牛顿或莱文贝格-马夸尔特法,处理量测数据并考虑约束条件,以提高估计精度。程序结果以图形形式展示电压幅值和角度估计的比较,以及估计误差的演变,体现了算法在处理配电网状态估计问题的有效性。
|
3天前
|
数据采集 存储 算法
基于BP算法的SAR成像matlab仿真
**摘要:** 基于BP算法的SAR成像研究,利用MATLAB2022a进行仿真。SAR系统借助相对运动合成大孔径,提供高分辨率图像。BP算法执行回波数据预处理、像素投影及图像重建,实现精确成像。优点是高精度和强适应性,缺点是计算量大、内存需求高。代码示例展示了回波生成、数据处理到插值显示的全过程。
|
11天前
|
机器学习/深度学习 自然语言处理 算法
m基于深度学习的OFDM+QPSK链路信道估计和均衡算法误码率matlab仿真,对比LS,MMSE及LMMSE传统算法
**摘要:** 升级版MATLAB仿真对比了深度学习与LS、MMSE、LMMSE的OFDM信道估计算法,新增自动样本生成、复杂度分析及抗频偏性能评估。深度学习在无线通信中,尤其在OFDM的信道估计问题上展现潜力,解决了传统方法的局限。程序涉及信道估计器设计,深度学习模型通过学习导频信息估计信道响应,适应频域变化。核心代码展示了信号处理流程,包括编码、调制、信道模拟、降噪、信道估计和解调。
38 8
|
4天前
|
算法 vr&ar
基于自适应波束成形算法的matlab性能仿真,对比SG和RLS两种方法
```markdown - MATLAB2022a中比较SG与RLS自适应波束成形算法。核心程序实现阵列信号处理,强化期望信号,抑制干扰。RLS以其高效计算权重,而SG则以简单和低计算复杂度著称。[12345] [6666666666] [777777] ```

热门文章

最新文章