Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. class DijkstraAlgorithm
  2.     {
  3.         private Graph graph;
  4.         private const int INF = unchecked((int)10e9);
  5.  
  6.         public DijkstraAlgorithm(ref Graph graph)
  7.         {
  8.             this.graph = graph;
  9.         }
  10.  
  11.         public List<Vertex> GetShortRoute(int first_id, int second_id)
  12.         {
  13.             int arrival = graph.GetPositionById(second_id);
  14.             int n = graph.Length;
  15.             List<List<Pair>> g = new List<List<Pair>>();
  16.             for (int i = 0; i < n; ++i)
  17.                 g.Add(new List<Pair>());
  18.  
  19.             for (int i = 0; i < n; ++i)
  20.                 g[i] = graph.GetVertexEdgePositions(graph[i].ID);
  21.  
  22.  
  23.             int s = graph.GetPositionById(first_id);
  24.             List<int> d = new List<int>(), p = new List<int>();
  25.             List<bool> used = new List<bool>();
  26.             for (int i = 0; i < n; ++i)
  27.             {
  28.                 d.Add(INF);
  29.                 p.Add(-1);
  30.                 used.Add(false);
  31.             }
  32.             d[s] = 0;
  33.  
  34.             for (int i = 0; i < n; ++i)
  35.             {
  36.                 int v = -1;
  37.                 for (int j = 0; j < n; ++j)
  38.                     if (!used[j] && (v == -1 || d[j] < d[v]))
  39.                         v = j;
  40.                 if (d[v] == INF)
  41.                     break;
  42.                 used[v] = true;
  43.  
  44.                 for (int j = 0; j < g[v].Count; ++j)
  45.                 {
  46.                     int to = g[v][j].First,
  47.                         len = g[v][j].Second;
  48.                     if (d[v] + len < d[to])
  49.                     {
  50.                         d[to] = d[v] + len;
  51.                         p[to] = v;
  52.                     }
  53.                 }
  54.             }
  55.  
  56.             List<Vertex> way = GetWayByPositions(p, s, arrival);
  57.  
  58.  
  59.             return way;
  60.         }
  61.  
  62.         private List<Vertex> GetWayByPositions(List<int> p, int s, int arrival)
  63.         {
  64.             List<Vertex> way = new List<Vertex>();
  65.             way.Add(graph[arrival]);
  66.             int cur = arrival;
  67.             while (cur != s)
  68.             {
  69.                 cur = p[cur];
  70.                 if (cur != -1)
  71.                     way.Add(graph[cur]);
  72.                 else
  73.                 {
  74.                     way.Clear();
  75.                     break;
  76.                 }
  77.             }
  78.  
  79.             return way;
  80.         }
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement