最短路径(Dijkstra)算法 之 .Net 4.0 Parallel 实现
来源:互联网 发布:数据结构排序算法口诀 编辑:程序博客网 时间:2024/05/18 02:22
Program.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ParallelDijkstra{ class Program { static void Main(string[] args) { int[,] c=new int[5,5]; Parallel.For(0, 5, (i) => { Parallel.For(0, 5, (j) => { c[i,j] = Dijkstra.MaxInt; } ); } ); c[0, 1] = c[1, 0] = 10; c[0, 3] = c[3, 0] = 30; c[0, 4] = c[4, 0] = 100; c[1, 2] = c[2, 1] = 50; c[2, 4] = c[4, 2] = 10; c[3, 2] = c[2, 3] = 20; c[3, 4] = c[4, 3] = 60; int[] dist; int[] prev; Dijkstra.Execute(5, 0, c, out dist, out prev); string path = Dijkstra.ParsePath(prev,0,4); Console.WriteLine(path); Console.ReadKey(); } }}
Dijkstra.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ParallelDijkstra{ public class Dijkstra { public static int MaxInt = 999999; /// <summary> /// 执行最短路径分析 /// </summary> /// <param name="n">结点个数</param> /// <param name="v">源点</param> /// <param name="c">路径权值矩阵</param> /// <param name="dist">从源点出发至各节点的最短路径值列表</param> /// <param name="prev">记录每一个结点的前一个最短路径结点</param> public static void Execute(int n, int v, int[,] c,out int[] dist, out int[] prev) { int[] tmpDist=new int[n]; int[] tmpPrev=new int[n]; bool[] s = new bool[100]; // 判断是否已存入该点到S集合中 Parallel.For(0, n, (i) => { tmpDist[i] = c[v,i]; s[i] = false; if (tmpDist[i] == MaxInt) tmpPrev[i] = -1; else tmpPrev[i] = v; } ); tmpDist[v] = 0; s[v] = true; //依次将未放入S集合的结点中,取dist[]最小值的结点,放入集合S之中 //一旦S包含了所有V中顶点,dist就记录了从源点到所有其他顶点之间的最短路径长度 Parallel.For(1, n, (i) => { int tmp = MaxInt; int u = v; // 找出当前未使用的点j的dist[j]最小值 Parallel.For(0, n, (j) => { if (!s[j] && tmpDist[j] < tmp) { u = j; //u保存当前邻接点中距离最小的点的号码 tmp=tmpDist[j]; } } ); s[u] = true; //表示u点已存入S集合中 //更新dist Parallel.For(0, n, (j) => { if (!s[j] && c[u,j] < MaxInt) { int newDist = tmpDist[u] + c[u,j]; if (newDist < tmpDist[j]) { tmpDist[j] = newDist; tmpPrev[j] = u; } } } ); } ); dist=tmpDist; prev=tmpPrev; } /// <summary> /// 获取从点v至点u的最短路径描述字符串 /// </summary> /// <param name="prev">记录每一个结点的前一个最短路径结点</param> /// <param name="v">起点</param> /// <param name="u">终点</param> /// <returns>从点v至点u的最短路径描述字符串</returns> public static String ParsePath(int[] prev, int v, int u) { String path = "没有有效路径."; int tot=0; List<int> que = new List<int>(); que.Add(u); tot++; int tmp=prev[u]; while (tmp != v && tmp != -1) { que.Add(tmp); tot++; tmp=prev[tmp]; } if (tmp != -1) { que.Add(v); path=String.Empty; for (int i = tot; i > 0; i--) { path += que[i].ToString() + "-->"; } path += que[0].ToString(); } return path; } }}
输出结果:
0-->3-->2-->4
- 最短路径(Dijkstra)算法 之 .Net 4.0 Parallel 实现
- 最短路径算法之Dijkstra算法(java实现)
- 最短路径算法之Dijkstra算法(邻接矩阵实现)
- 最短路径算法之Dijkstra算法_Java实现
- 最短路径之dijkstra算法的C语言实现
- 动手实现 算法 之 “Dijkstra 最短路径”
- 最短路径之Dijkstra算法 C语言实现
- 最短路径之Dijkstra算法的概念与实现
- 算法实现Java之最短路径迪杰斯特拉(Dijkstra)
- Dijkstra 最短路径算法C#实现
- 最短路径实现(Dijkstra算法)
- Dijkstra算法的最短路径实现
- dijkstra最短路径算法C实现
- 最短路径Dijkstra算法实现
- dijkstra最短路径算法的实现
- 最短路径之Dijkstra算法
- 最短路径之Dijkstra+Floyd算法
- 三、最短路径之Dijkstra算法
- 简单比较 JDO、EJB-CMP、Hibernate、Amber
- JavaScript encodeURIComponent() 函数
- Ext.Panel
- .net 防止页面刷新重复提交(表单提交后保留上一次数据缓存,刷新提交)
- SHELL编程基础
- 最短路径(Dijkstra)算法 之 .Net 4.0 Parallel 实现
- WMI使用的WIN32_类库名
- History 对象
- windows下,多线程c代码嵌入python例子,测试通过
- myeclipse8.5无法加载工程
- 实现字符串的语序翻转
- merge 用法 先查询库中是否有此值,如果没有的话添加
- $(document).ready(function() 具体介绍
- Myeclipse下java.lang.OutOfMemoryError: Java heap space的解决