Advertisement
Guest User

DijsktrasAlgorithm

a guest
Sep 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using PriorityQueue;
  3.  
  4. namespace Pathfinding
  5. {
  6.     public class DijsktraAlgorithm
  7.     {
  8.         Dictionary<Node, int> totalCost = new Dictionary<Node, int>();
  9.         Dictionary<Node, Node> prevNodes = new Dictionary<Node, Node>();
  10.         PriorityQueue<Node> queue = new PriorityQueue<Node>();
  11.         HashSet<Node> visited = new HashSet<Node>();
  12.  
  13.         public DijsktraAlgorithm(){}
  14.  
  15.         public List<object> RunAlgorithm(Graph graph, Node start)
  16.         {
  17.             totalCost.Clear();
  18.             prevNodes.Clear();
  19.             queue.Clear();
  20.             visited.Clear();
  21.  
  22.             totalCost.Add(start, 0);
  23.             queue.Enqueue(start, 0);
  24.  
  25.             foreach (Node n in graph.nodes)
  26.             {
  27.                 if (n != start)
  28.                 {
  29.                     totalCost.Add(n, int.MaxValue);
  30.                 }
  31.             }
  32.  
  33.             while (queue.Count > 0)
  34.             {
  35.                 if (queue.Count == 0)
  36.                 {
  37.  
  38.                     break;
  39.                 }
  40.                 Node newSmallest = queue.Dequeue();
  41.  
  42.                 visited.Add(newSmallest);
  43.  
  44.                 foreach (Node neighbor in newSmallest.neighbors)
  45.                 {
  46.                     if (!queue.Contains(neighbor) && !visited.Contains(neighbor))
  47.                     {
  48.                         int priority = (int)UnityEngine.Vector3.Distance(newSmallest.transform.position,
  49.                             neighbor.transform.position);
  50.  
  51.                         queue.Enqueue(neighbor, priority);
  52.                     }
  53.                 }
  54.  
  55.                 foreach (Node neighbor in newSmallest.neighbors)
  56.                 {
  57.                     if (!visited.Contains(neighbor))
  58.                     {
  59.                         int altPath = totalCost[newSmallest] +
  60.                             (int)UnityEngine.Vector3.Distance(newSmallest.transform.position,
  61.                             neighbor.transform.position);
  62.  
  63.                         if (altPath < totalCost[neighbor])
  64.                         {
  65.                             if (!totalCost.ContainsKey(neighbor))
  66.                             {
  67.                                 totalCost.Add(neighbor, altPath);
  68.                             }
  69.                             else
  70.                             {
  71.                                 totalCost[neighbor] = altPath;
  72.                             }
  73.  
  74.                             if (!prevNodes.ContainsKey(neighbor))
  75.                             {
  76.                                 prevNodes.Add(neighbor, newSmallest);
  77.                             }
  78.                             else
  79.                             {
  80.                                 prevNodes[neighbor] = newSmallest;
  81.                             }
  82.  
  83.                             if (queue.Contains(neighbor))
  84.                             {
  85.                                 queue.UpdatePriority(neighbor, altPath);
  86.                             }
  87.                         }
  88.                     }
  89.                 }
  90.             }
  91.  
  92.             List<object> result = new List<object>();
  93.  
  94.             result.Add(totalCost); result.Add(prevNodes);
  95.  
  96.             return result;
  97.  
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement