rybufc

Diikstra

Feb 4th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.65 KB | None | 0 0
  1. ?uses System.Collections.Generic;
  2.  
  3. const
  4.   MaxN = 1000;
  5.  
  6. type
  7.   Point = class
  8.  
  9.   private
  10.     _index: integer;
  11.     _previous: Point;
  12.     _value: integer
  13.  
  14.   public
  15.     constructor(index: integer);
  16.     begin
  17.       _index := index;
  18.       _previous := nil;
  19.     end;
  20.    
  21.     procedure SetPrevious(previous: Point);
  22.     procedure SetValue(value: integer);
  23.     function ToString(): string;
  24.     property Value: integer read _value;
  25.     property Index: integer read _index;
  26.     property Previous: Point read _previous;
  27.   end;
  28.  
  29. function Point.ToString(): string;
  30. begin
  31.   if (not (previous = nil)) then
  32.     result := 'index: ' + _index + #10 + 'value ' + _value + ' ;' + #10
  33.       + 'previous: ' + previous.ToString()
  34.   else
  35.     result := 'index: ' + _index + #10 + 'value ' + _index;
  36. end;
  37.  
  38. procedure Point.SetPrevious(previous: Point);
  39. begin
  40.   _previous := previous;
  41. end;
  42.  
  43. procedure Point.SetValue(value: integer);
  44. begin
  45.   _value := value;
  46. end;
  47.  
  48. function Diikstra(startPoint: integer;
  49.               params graph: Array[,] of integer): Array of Point;
  50. begin
  51.   var queue: Queue<Point>;
  52.   queue := new Queue<Point>;
  53.  
  54.   var calculatedGraph: Array of Point;
  55.   calculatedGraph := new Point[Length(graph, 0)];
  56.   for var i := 0 to Length(calculatedGraph, 0) - 1 do
  57.   begin
  58.     var temp: Point;
  59.     temp := new Point(i);
  60.     temp.SetValue(integer.MaxValue);
  61.     calculatedGraph[i] := temp;
  62.   end;
  63.  
  64.   calculatedGraph[startPoint].SetValue(0);
  65.  
  66.   var visited: Array[0..MaxN] of boolean;  
  67.   queue.Enqueue(calculatedGraph[startPoint]);
  68.  
  69.   while queue.Count <> 0 do
  70.   begin
  71.     var current: Point;
  72.     current := queue.Dequeue();
  73.     visited[current.Index] := true;
  74.    
  75.     for var i := 0 to Length(graph, 1) - 1 do
  76.     begin
  77.       if (graph[current.Index, i] > 0) and (not visited[i]) then
  78.       begin
  79.         if(calculatedGraph[i].Value >
  80.           current.Value + graph[current.Index, i]) then
  81.           begin
  82.             calculatedGraph[i].SetValue(current.Value + graph[current.Index, i]);
  83.             calculatedGraph[i].SetPrevious(current);
  84.           end;
  85.         queue.Enqueue(calculatedGraph[i]);
  86.       end;
  87.     end;
  88.   end;
  89.   result := calculatedGraph;
  90. end;
  91.  
  92. begin
  93.   var graph: array[,] of integer;
  94.   graph := new integer[10, 10];
  95.   for var i := 0 to Length(graph, 0) - 1 do
  96.     for var j := 0 to Length(graph, 1) - 1 do
  97.       graph[i, j] := 0;
  98.   var startPoint, targetPoint: integer;
  99.  
  100.   startPoint := 0;  
  101.   graph[0, 3] := 1;
  102.   graph[3, 5] := 1;
  103.   graph[0, 5] := 10;
  104.   var calculatedGraph: Array of Point;
  105.   calculatedGraph := Diikstra(startPoint, graph);
  106.   Writeln(calculatedGraph[5].ToString());
  107. end.
Advertisement