C#,圖算法——以鄰接節點表示的圖最短路徑的迪杰斯特拉(Dijkstra)算法C#程序

1 文本格式

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
?? ?public class Node ?// : IComparable<Node>
?? ?{
?? ??? ?private int vertex, weight;
?? ??? ?public Node(int v, int w)
?? ??? ?{
?? ??? ??? ?vertex = v;
?? ??? ??? ?weight = w;
?? ??? ?}
?? ??? ?public int getVertex() { return vertex; }
?? ??? ?public int getWeight() { return weight; }
?? ??? ?public int CompareTo(Node other)
?? ??? ?{
?? ??? ??? ?return weight - other.weight;
?? ??? ?}
?? ?}

?? ?public class Dijkstra_Algorithm
?? ?{
?? ??? ?// Function to find the shortest distance of all the
?? ??? ?// vertices from the source vertex S.
?? ??? ?public static int[] dijkstra(int V, List<List<Node>> graph, int src)
?? ??? ?{
?? ??? ??? ?int[] distance = new int[V];
?? ??? ??? ?for (int i = 0; i < V; i++)
?? ??? ??? ?{
?? ??? ??? ??? ?distance[i] = Int32.MaxValue;
?? ??? ??? ?}
?? ??? ??? ?distance[src] = 0;

?? ??? ??? ?SortedSet<Node> pq = new SortedSet<Node>();
?? ??? ??? ?pq.Add(new Node(src, 0));

?? ??? ??? ?while (pq.Count > 0)
?? ??? ??? ?{
?? ??? ??? ??? ?Node current = pq.First();
?? ??? ??? ??? ?pq.Remove(current);

?? ??? ??? ??? ?foreach (Node n in graph[current.getVertex()])
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if (distance[current.getVertex()] + n.getWeight() < distance[n.getVertex()])
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?distance[n.getVertex()] = n.getWeight() + distance[current.getVertex()];
?? ??? ??? ??? ??? ??? ?pq.Add(new Node(n.getVertex(), distance[n.getVertex()]));
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?// If you want to calculate distance from source to
?? ??? ??? ?// a particular target, you can return
?? ??? ??? ?// distance[target]
?? ??? ??? ?return distance;
?? ??? ?}

?? ??? ?public static string Drive()
?? ??? ?{
?? ??? ??? ?int V = 9;
?? ??? ??? ?List<List<Node>> graph = new List<List<Node>>();
?? ??? ??? ?for (int i = 0; i < V; i++)
?? ??? ??? ?{
?? ??? ??? ??? ?graph.Add(new List<Node>());
?? ??? ??? ?}
?? ??? ??? ?int source = 0;
?? ??? ??? ?graph[0].Add(new Node(1, 4));
?? ??? ??? ?graph[0].Add(new Node(7, 8));
?? ??? ??? ?graph[1].Add(new Node(2, 8));
?? ??? ??? ?graph[1].Add(new Node(7, 11));
?? ??? ??? ?graph[1].Add(new Node(0, 7));
?? ??? ??? ?graph[2].Add(new Node(1, 8));
?? ??? ??? ?graph[2].Add(new Node(3, 7));
?? ??? ??? ?graph[2].Add(new Node(8, 2));
?? ??? ??? ?graph[2].Add(new Node(5, 4));
?? ??? ??? ?graph[3].Add(new Node(2, 7));
?? ??? ??? ?graph[3].Add(new Node(4, 9));
?? ??? ??? ?graph[3].Add(new Node(5, 14));
?? ??? ??? ?graph[4].Add(new Node(3, 9));
?? ??? ??? ?graph[4].Add(new Node(5, 10));
?? ??? ??? ?graph[5].Add(new Node(4, 10));
?? ??? ??? ?graph[5].Add(new Node(6, 2));
?? ??? ??? ?graph[6].Add(new Node(5, 2));
?? ??? ??? ?graph[6].Add(new Node(7, 1));
?? ??? ??? ?graph[6].Add(new Node(8, 6));
?? ??? ??? ?graph[7].Add(new Node(0, 8));
?? ??? ??? ?graph[7].Add(new Node(1, 11));
?? ??? ??? ?graph[7].Add(new Node(6, 1));
?? ??? ??? ?graph[7].Add(new Node(8, 7));
?? ??? ??? ?graph[8].Add(new Node(2, 2));
?? ??? ??? ?graph[8].Add(new Node(6, 6));
?? ??? ??? ?graph[8].Add(new Node(7, 1));

?? ??? ??? ?int[] distance = dijkstra(V, graph, source);
?? ??? ??? ?// Printing the Output
?? ??? ??? ?StringBuilder sb = new StringBuilder();
?? ??? ??? ?sb.AppendLine("Vertex " + " Distance from Source");
?? ??? ??? ?for (int i = 0; i < V; i++)
?? ??? ??? ?{
?? ??? ??? ??? ?sb.AppendLine(i + "\t" + distance[i]);
?? ??? ??? ?}
?? ??? ??? ?return sb.ToString();
?? ??? ?}
?? ?}
}
?

2 代碼格式

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;namespace Legalsoft.Truffer.Algorithm
{public class Node{private int vertex, weight;public Node(int v, int w){vertex = v;weight = w;}public int getVertex() { return vertex; }public int getWeight() { return weight; }public int CompareTo(Node other){return weight - other.weight;}}public class Dijkstra_Algorithm{// Function to find the shortest distance of all the// vertices from the source vertex S.public static int[] dijkstra(int V, List<List<Node>> graph, int src){int[] distance = new int[V];for (int i = 0; i < V; i++){distance[i] = Int32.MaxValue;}distance[src] = 0;SortedSet<Node> pq = new SortedSet<Node>();pq.Add(new Node(src, 0));while (pq.Count > 0){Node current = pq.First();pq.Remove(current);foreach (Node n in graph[current.getVertex()]){if (distance[current.getVertex()] + n.getWeight() < distance[n.getVertex()]){distance[n.getVertex()] = n.getWeight() + distance[current.getVertex()];pq.Add(new Node(n.getVertex(), distance[n.getVertex()]));}}}// If you want to calculate distance from source to// a particular target, you can return// distance[target]return distance;}public static string Drive(){int V = 9;List<List<Node>> graph = new List<List<Node>>();for (int i = 0; i < V; i++){graph.Add(new List<Node>());}int source = 0;graph[0].Add(new Node(1, 4));graph[0].Add(new Node(7, 8));graph[1].Add(new Node(2, 8));graph[1].Add(new Node(7, 11));graph[1].Add(new Node(0, 7));graph[2].Add(new Node(1, 8));graph[2].Add(new Node(3, 7));graph[2].Add(new Node(8, 2));graph[2].Add(new Node(5, 4));graph[3].Add(new Node(2, 7));graph[3].Add(new Node(4, 9));graph[3].Add(new Node(5, 14));graph[4].Add(new Node(3, 9));graph[4].Add(new Node(5, 10));graph[5].Add(new Node(4, 10));graph[5].Add(new Node(6, 2));graph[6].Add(new Node(5, 2));graph[6].Add(new Node(7, 1));graph[6].Add(new Node(8, 6));graph[7].Add(new Node(0, 8));graph[7].Add(new Node(1, 11));graph[7].Add(new Node(6, 1));graph[7].Add(new Node(8, 7));graph[8].Add(new Node(2, 2));graph[8].Add(new Node(6, 6));graph[8].Add(new Node(7, 1));int[] distance = dijkstra(V, graph, source);// Printing the OutputStringBuilder sb = new StringBuilder();sb.AppendLine("Vertex " + " Distance from Source");for (int i = 0; i < V; i++){sb.AppendLine(i + "\t" + distance[i]);}return sb.ToString();}}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/211288.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/211288.shtml
英文地址,請注明出處:http://en.pswp.cn/news/211288.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

第7章-使用統計方法進行變量有效性測試-7.5.4-模型評估

目錄 混淆矩陣 準確率 定義 局限性 精準率 定義 局限性

【分布式微服務專題】從單體到分布式(一、SpringCloud項目初步升級)

目錄 前言閱讀對象閱讀導航前置知識筆記正文一、單體服務介紹二、服務拆分三、分布式微服務升級前的思考3.1 關于SpringBoot/SpringCloud的思考【有點門檻】 四、SpringCloud升級整合4.1 新建父子項目 學習總結感謝 前言 從本節課開始&#xff0c;我將自己手寫一個基于SpringC…

如何輕松恢復 Windows 中刪除的文件夾

我們都曾經歷過這樣的事&#xff0c;而且我們中的大多數人可能很快就會再次這樣做。我們討論的是在 Windows 中按“Delete”或“ShiftDelete”鍵意外刪除重要文件夾的情況。 如果您剛剛按下刪除鍵且未超過 30 天&#xff0c;或者尚未清空回收站&#xff0c;則可以恢復文件夾。…

操作系統學習筆記---內存管理

目錄 概念 功能 內存空間的分配和回收 地址轉換 邏輯地址&#xff08;相對地址&#xff09; 物理地址&#xff08;絕對地址&#xff09; 內存空間的擴充 內存共享 存儲保護 方式 源程序變為可執行程序步驟 鏈接方式 裝入方式 覆蓋 交換 連續分配管理方式 單一連…

python安裝與工具PyCharm

摘要&#xff1a; 周末閑來無事學習一下python&#xff01;不是你菜雞&#xff0c;只不過是對手太強了&#xff01;所以你要不斷努力&#xff0c;去追求更高的未來&#xff01;下面先了解python與環境的安裝與工具的配置&#xff01; python安裝&#xff1a; 官網 進入官網下載…

lua腳本串口收發與CRC16校驗及使用方法

lua腳本CRC16校驗 --calculate CRC16校驗 --data : t, data to be verified --n : number of verified --return : check result function add_crc16(start, n, data)local carry_flag, a 0local result 0xfffflocal i startwhile(true)doresult result ~ data[i]for j…

git 關于分支、merge、commit提交

最近開始用git終端提交代碼&#xff0c;梳理了一些知識點 一 關于分支 關于分支&#xff0c;git的分支分為本地分支遠程分支兩種分支&#xff0c;在上傳代碼時&#xff0c;我們要確保當前本地分支連接了一個遠程分支。 我們可以通過下面代碼查看當前的本地分支&#xff1a; g…

迅為3588開發板 sudo: 無法解析主機:/DNS配置

環境申明 RK3588 ubuntu 22.04 jammy 迅為開發板 hostname 看是否有Host .&#xff0c;如果沒有&#xff0c; sudo vim /etc/hostname在里面加一行&#xff0c;我這就這一個 iTOP-RK3588hosts 修改本地hosts sudo vim /etc/hosts127.0.0.1 localhost localhost iTOP-RK3…

2.postman環境變量及接口關聯

一、環境變量以及全局變量 操作流程 1.點擊environment 2.點擊environment右側號&#xff0c;新增環境變量 3.在變量中輸入變量名以及變量值 4.回到collection頁面&#xff0c;修改變量環境 5.在collection中通過{{變量名}}調用變量 變量定義 環境變量&#xff1a;環境變量…

vue 限制在指定容器內可拖拽的div

<template><div class"container" id"container"><div class"drag-box center" v-drag v-if"isShowDrag"><div>無法拖拽出容器的div浮窗</div></div></div> </template><script&g…

P11 Linux進程編程exec族函數

前言 &#x1f3ac; 個人主頁&#xff1a;ChenPi &#x1f43b;推薦專欄1: 《Linux C應用編程&#xff08;概念類&#xff09;_ChenPi的博客-CSDN博客》??? &#x1f525; 推薦專欄2: 《C_ChenPi的博客-CSDN博客》??? &#x1f6f8;推薦專欄3: ??????《鏈表_C…

Java 簡易版 UDP 多人聊天室

服務端 import java.io.*; import java.net.*; import java.util.ArrayList; public class Server{public static ServerSocket server_socket;public static ArrayList<Socket> socketListnew ArrayList<Socket>(); public static void main(String []args){try{…

算法通關村第五關—LRU的設計與實現(黃金)

LRU的設計與實現 一、理解LRU的原理 LeetCode146:運用你所掌握的數據結構&#xff0c;設計和實現一個LRU(最近最少使用)緩存機制 實現LRUCache類&#xff1a; LRUCache(int capacity) 以正整數作為容量capacity初始化 LRU 緩存 int get(int key) 如果關鍵字key存在于緩存中&a…

數據可視化|jupyter notebook運行pyecharts,無法正常顯示“可視化圖形”,怎么解決?

前言 本文是該專欄的第39篇,后面會持續分享python數據分析的干貨知識,記得關注。 相信有些同學在本地使用jupyter notebook運行pyecharts的時候,在代碼沒有任何異常的情況下,無論是html還是notebook區域,都無法顯示“可視化圖形”,界面區域只有空白一片。遇到這種情況,…

SQL(COALESCE)

zstarling 非空值查找及替換COALESCE 非空值查找及替換COALESCE 新語法SQL COALESCE(staff_no,staff_no1,)詳解 在SQL中&#xff0c;COALESCE函數用于返回一組表達式中的第一個非NULL值。它接受兩個或多個參數&#xff0c;并按參數順序依次判斷每個參數是否為NULL&#xff0c…

如何才能保證績效考核的有效性呢?

績效管理是現代人力資源管理的核心&#xff0c;做好績效考核是做好績效管理的重要手段。但企業績效考核的設計往往缺乏針對性和科學性&#xff0c;績效考核工作也常常停留在形式上&#xff0c;最終沒能為提高組織效率提供幫助&#xff0c;還消耗員工與管理者的時間、精力。于是…

Nginx服務優化以及防盜鏈

1. 隱藏版本號 以在 CentOS 中使用命令 curl -I http://192.168.66.10 顯示響應報文首部信息。 查看版本號 curl -I http://192.168.66.10 1. 修改配置文件 vim /usr/local/nginx/conf/nginx.conf http {include mime.types;default_type application/octet-stream;…

京東數據運營(京東API接口):10月投影儀店鋪數據分析

鯨參謀監測的京東平臺10月份投影儀市場銷售數據已出爐&#xff01; 10月份&#xff0c;環同比來看&#xff0c;投影儀市場銷售均上漲。鯨參謀數據顯示&#xff0c;今年10月&#xff0c;京東平臺投影儀的銷量為16萬&#xff0c;環比增長約22%&#xff0c;同比增長約8%&#xff1…

鴻蒙應用開發ArkTS基礎組件的使用

語雀知識庫地址&#xff1a;語雀HarmonyOS知識庫 飛書知識庫地址&#xff1a;飛書HarmonyOS知識庫 本文示例代碼地址&#xff1a;Gitee 倉庫地址 嗨&#xff0c;各位好呀&#xff0c;我是小白 上一篇文章我為大家介紹了如何使用 ArkTS 開發鴻蒙應用&#xff0c;對 HarmonyOS 項…

大文件分割,合并------C++ ------fstream

將一個大文件(這里測試文件為5.2G)切分為指定大小的文件,然后在把分割后的文件拼接合并為分割前的源文件 #include <boost/timer.hpp> // 計時函數#include <filesystem> #include <fstream> #include <vector> // 分隔后文件夾的格式, 原文件名_chun…