Dijkstra 最短路径算法C#实现

算法不用细说,学过数据结构的同仁们都知道,前两天用到,用C#写出来了。

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

namespace ShortedPath
{
    class Program
    {
        static int length=6;
        static string[] shortedPath=new string[length];
        static int noPath=2000;
        static int MaxSize = 1000;
        static int [,] G={{noPath,noPath,10,noPath,30,100},{noPath,noPath,5,noPath,noPath,noPath},{noPath,noPath,noPath,50,noPath,noPath},{noPath,noPath,noPath,noPath,noPath,10},{noPath,noPath,noPath,20,noPath,60},{noPath,noPath,noPath,noPath,noPath,noPath}};
        static string[] PathResult = new string[length];

        static int[] path1 = new int[length];
        static int[,] path2 = new int[length, length];
        static int[] distance2 = new int[length];

        static void Main(string[] args)
        {

            int dist1= getShortedPath(G,0,5,path1);
            Console.WriteLine("点0到点5路径:");
            for (int i=0; i < path1.Length; i++)
                Console.Write(path1[i].ToString()+" ");
            Console.WriteLine("长度:" + dist1);

           
            int [] pathdist = getShortedPath(G, 0, path2);
            Console.WriteLine("点0到任意点的路径:");
            for (int j = 0; j < pathdist.Length; j++)
            {
                Console.WriteLine("点0到" + j + "的路径:");
                for (int i = 0; i < length; i++)
                    Console.Write(path2[j, i].ToString() + " ");
                Console.WriteLine("长度:" + pathdist[j]);
            }
            Console.ReadKey();
        }

        //从某一源点出发,找到到某一结点的最短路径
        static int getShortedPath(int[,]G, int start, int end,int [] path)
        {
            bool[] s = new bool[length]; //表示找到起始结点与当前结点间的最短路径
            int min;  //最小距离临时变量
            int curNode=0; //临时结点,记录当前正计算结点
            int[] dist = new int[length];
            int[] prev = new int[length];

            //初始结点信息
            for (int v = 0; v < length; v++)
            {
                s[v] = false;
                dist[v] = G[start, v];
                if (dist[v] > MaxSize)
                    prev[v] = 0;
                else
                    prev[v] = start;
            }
            path[0] = end;
            dist[start] = 0;
            s[start] = true;
            //主循环
            for (int i = 1; i < length; i++)
            {
                min = MaxSize;
                for (int w = 0; w < length; w++)
                {
                    if (!s[w] && dist[w] < min)
                    {
                        curNode = w;
                        min = dist[w];
                    }
                }

                s[curNode] = true;

                for (int j = 0; j < length; j++)
                    if (!s[j] && min + G[curNode, j] < dist[j])
                    {
                        dist[j] = min + G[curNode, j];
                        prev[j] = curNode;
                    }

            }
            //输出路径结点
            int e = end, step = 0;
            while (e != start)
            {
                step++;
                path[step] = prev[e];
                e = prev[e];
            }
            for (int i = step; i > step/2; i--)
            {
                int temp = path[step - i];
                path[step - i] = path[i];
                path[i] = temp;
            }
            return dist[end];
        }

        //从某一源点出发,找到到所有结点的最短路径
        static int[] getShortedPath(int[,] G, int start, int[,] path)
        {
            int[] PathID = new int[length];//路径(用编号表示)
            bool[] s = new bool[length]; //表示找到起始结点与当前结点间的最短路径
            int min;  //最小距离临时变量
            int curNode = 0; //临时结点,记录当前正计算结点
            int[] dist = new int[length];
            int[] prev = new int[length];
            //初始结点信息

            for (int v = 0; v < length; v++)
            {
                s[v] = false;
                dist[v] = G[start, v];
                if (dist[v] > MaxSize)
                    prev[v] = 0;
                else
                    prev[v] = start;
                path[v,0] = v;
            }
           
            dist[start] = 0;
            s[start] = true;
            //主循环
            for (int i = 1; i < length; i++)
            {
                min = MaxSize;
                for (int w = 0; w < length; w++)
                {
                    if (!s[w] && dist[w] < min)
                    {
                        curNode = w;
                        min = dist[w];
                    }
                }

                s[curNode] = true;

                for (int j = 0; j < length; j++)
                    if (!s[j] && min + G[curNode, j] < dist[j])
                    {
                        dist[j] = min + G[curNode, j];
                        prev[j] = curNode;
                    }

            }
            //输出路径结点
            for (int k = 0; k < length; k++)
            {
                int e = k, step = 0;
                while (e != start)
                {
                    step++;
                    path[k,step] = prev[e];
                    e = prev[e];
                }
                for (int i = step; i > step / 2; i--)
                {
                    int temp = path[k,step - i];
                    path[k,step - i] = path[k,i];
                    path[k,i] = temp;
                }
            }
            return dist;

        }
    }
}
 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
你好!下面是使用C#实现Dijkstra算法来输出最短路径的示例代码: ```c# using System; using System.Collections.Generic; public class DijkstraAlgorithm { private int numVertices; private List<List<Node>> adjacencyList; private class Node { public int Vertex { get; } public int Weight { get; } public Node(int vertex, int weight) { Vertex = vertex; Weight = weight; } } public DijkstraAlgorithm(int numVertices) { this.numVertices = numVertices; adjacencyList = new List<List<Node>>(numVertices); for (int i = 0; i < numVertices; i++) { adjacencyList.Add(new List<Node>()); } } public void AddEdge(int source, int destination, int weight) { adjacencyList[source].Add(new Node(destination, weight)); } public void ShortestPath(int source) { int[] distances = new int[numVertices]; bool[] visited = new bool[numVertices]; for (int i = 0; i < numVertices; i++) { distances[i] = int.MaxValue; visited[i] = false; } distances[source] = 0; for (int i = 0; i < numVertices - 1; i++) { int minDistanceVertex = MinimumDistance(distances, visited); visited[minDistanceVertex] = true; foreach (Node node in adjacencyList[minDistanceVertex]) { if (!visited[node.Vertex] && distances[minDistanceVertex] != int.MaxValue && distances[minDistanceVertex] + node.Weight < distances[node.Vertex]) { distances[node.Vertex] = distances[minDistanceVertex] + node.Weight; } } } PrintShortestPaths(distances, source); } private int MinimumDistance(int[] distances, bool[] visited) { int min = int.MaxValue; int minIndex = -1; for (int i = 0; i < numVertices; i++) { if (!visited[i] && distances[i] <= min) { min = distances[i]; minIndex = i; } } return minIndex; } private void PrintShortestPaths(int[] distances, int source) { Console.WriteLine("Vertex\tDistance from Source"); for (int i = 0; i < numVertices; i++) { Console.WriteLine("{0}\t\t{1}", i, distances[i]); } } public static void Main(string[] args) { DijkstraAlgorithm graph = new DijkstraAlgorithm(6); graph.AddEdge(0, 1, 4); graph.AddEdge(0, 2, 1); graph.AddEdge(1, 3, 1); graph.AddEdge(2, 1, 2); graph.AddEdge(2, 3, 5); graph.AddEdge(3, 4, 3); graph.AddEdge(4, 1, 3); graph.AddEdge(4, 0, 2); int source = 0; graph.ShortestPath(source); } } ``` 这是一个简单的示例,你可以根据自己的需求进行修改。在示例代码中,我们首先定义了一个 `DijkstraAlgorithm` 类来实现Dijkstra算法。然后,我们可以使用 `AddEdge` 方法添加图的边。最后,在 `Main` 方法中,我们创建了一个具有6个顶点的图,并计算从指定源点到其他各个顶点的最短路径。 希望能帮到你!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值