AlgDeikstra.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace MethodDeikstraEXZ
  7. {
  8. internal class AlgDeikstra
  9. {
  10. int[][] matrix;
  11. int[] route;
  12. bool[] boolCheckNode;
  13. int startNode;
  14. int amountNode;
  15. public AlgDeikstra(int[][] matrix)
  16. {
  17. this.matrix = matrix;
  18. amountNode = matrix.Count();
  19. startNode = 0;
  20. boolCheckNode = new bool[amountNode];
  21. route = new int[amountNode];
  22. for (int i = 0; i < amountNode; i++)
  23. {
  24. boolCheckNode[i] = false;
  25. route[i] = int.MaxValue;
  26. }
  27. }
  28. public void RealizationAlg()
  29. {
  30. int minValueNode;
  31. route[startNode] = 0;
  32. for (int i = 0; i < amountNode - 1; i++)
  33. {
  34. minValueNode = int.MaxValue;
  35. int indexNode = -1;
  36. for (int j = 0; j < amountNode; j++)
  37. {
  38. if (!boolCheckNode[j] & route[j] < minValueNode)
  39. {
  40. minValueNode = route[j];
  41. indexNode = j;
  42. }
  43. }
  44. boolCheckNode[indexNode] = true;
  45. for (int j = 0; j < amountNode; j++)
  46. {
  47. int sumNodeAndRoute = matrix[indexNode][j] + minValueNode;
  48. if (!boolCheckNode[j] & matrix[indexNode][j]!=0 & sumNodeAndRoute < route[j])
  49. route[j] = sumNodeAndRoute;
  50. }
  51. }
  52. }
  53. public override string ToString()
  54. {
  55. string routeOutput = string.Empty;
  56. int index = 1;
  57. foreach (int item in route)
  58. {
  59. routeOutput += $"1->{index}: {item}\n";
  60. index++;
  61. }
  62. return routeOutput;
  63. }
  64. }
  65. }