using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MethodDeikstraEXZ { internal class AlgDeikstra { int[][] matrix; int[] route; bool[] boolCheckNode; int startNode; int amountNode; public AlgDeikstra(int[][] matrix) { this.matrix = matrix; amountNode = matrix.Count(); startNode = 0; boolCheckNode = new bool[amountNode]; route = new int[amountNode]; for (int i = 0; i < amountNode; i++) { boolCheckNode[i] = false; route[i] = int.MaxValue; } } public void RealizationAlg() { int minValueNode; route[startNode] = 0; for (int i = 0; i < amountNode - 1; i++) { minValueNode = int.MaxValue; int indexNode = -1; for (int j = 0; j < amountNode; j++) { if (!boolCheckNode[j] & route[j] < minValueNode) { minValueNode = route[j]; indexNode = j; } } boolCheckNode[indexNode] = true; for (int j = 0; j < amountNode; j++) { int sumNodeAndRoute = matrix[indexNode][j] + minValueNode; if (!boolCheckNode[j] & matrix[indexNode][j]!=0 & sumNodeAndRoute < route[j]) route[j] = sumNodeAndRoute; } } } public override string ToString() { string routeOutput = string.Empty; int index = 1; foreach (int item in route) { routeOutput += $"1->{index}: {item}\n"; index++; } return routeOutput; } } }