| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using System.Collections.Generic;
- using System.Linq;
- namespace PathfindingPCL
- {
- public class Dijkstra : Algorithm
- {
- public Dijkstra(Graph _graph, IHM _ihm) : base(_graph, _ihm) { }
- protected override void Run()
- {
- // Initialisation
- List<Node> nodesToVisit = graph.NodesList();
- bool exitReached = false;
- // Boucle principale
- while (nodesToVisit.Count != 0 && !exitReached)
- {
- Node currentNode = nodesToVisit.FirstOrDefault();
- foreach (Node newNode in nodesToVisit)
- {
- if (newNode.DistanceFromBegin < currentNode.DistanceFromBegin)
- {
- currentNode = newNode;
- }
- }
-
- if (currentNode == graph.ExitNode())
- {
- exitReached = true;
- }
- else
- {
- List<Arc> arcsFromCurrentNode = graph.ArcsList(currentNode);
- foreach (Arc arc in arcsFromCurrentNode)
- {
- if (arc.FromNode.DistanceFromBegin + arc.Cost < arc.ToNode.DistanceFromBegin)
- {
- arc.ToNode.DistanceFromBegin = arc.FromNode.DistanceFromBegin + arc.Cost;
- arc.ToNode.Precursor = arc.FromNode;
- }
- }
- nodesToVisit.Remove(currentNode);
- }
- }
- }
- }
- }
|