C#实现的最短路径分析

所属分类: 软件编程 / C#教程 阅读数: 94
收藏 0 赞 0 分享
复制代码 代码如下:

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

 namespace ConsoleApplication1
 {
     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, 1, path1);
             Console.WriteLine("点0到点5路径:");
             for (int i = 0; i < path1.Length; i++)
                 Console.Write(path1[i].ToString() + " "); 
             Console.WriteLine("长度:" + dist1);

 
             Console.WriteLine("\r\n-----------------------------------------\r\n");

             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;

         }

 
     }
 }
更多精彩内容其他人还在看

C# SendInput 模拟鼠标操作的实现方法

C# SendInput 模拟鼠标操作的实现方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中 paint()与Onpaint()的区别

paint是事件onpaint方法onpaint方法是调用paint事件的,用哪一个,效果是一样,就看那一个方便了内部是这样实现的:
收藏 0 赞 0 分享

c#中GetType()与Typeof()的区别

c#中GetType()与Typeof()的区别,需要的朋友可以参考一下
收藏 0 赞 0 分享

将字符串转换成System.Drawing.Color类型的方法

将字符串转换成System.Drawing.Color类型的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# 抓取网页内容的方法

C# 抓取网页内容的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于C#后台调用跨域MVC服务及带Cookie验证的实现

本篇文章介绍了,基于C#后台调用跨域MVC服务及带Cookie验证的实现。需要的朋友参考下
收藏 0 赞 0 分享

使用C#获取远程图片 Form用户名与密码Authorization认证的实现

本篇文章介绍了,使用C#获取远程图片 Form用户名与密码Authorization认证的实现。需要的朋友参考下
收藏 0 赞 0 分享

Winform跨线程操作的简单方法

线程间操作无效:从不是创建控件“label1”的线程访问它
收藏 0 赞 0 分享

C# WINFORM 强制让窗体获得焦点的方法代码

C# WINFORM 强制让窗体获得焦点的方法代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中方括号[]的语法及作用介绍

C#中方括号[]可用于数组,索引、属性,更重要的是用于外部DLL类库的引用。
收藏 0 赞 0 分享
查看更多