Slavik9510

Untitled

May 9th, 2023
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace DO_Lab6
  8. {
  9.     internal class DijkstraMethod
  10.     {
  11.         int[,] weigthMatrix;
  12.         int[,] currentTable;
  13.         List<int> revisedVerticies;
  14.         int initialVertex;
  15.         public DijkstraMethod(string path, int initialVertex = 1)
  16.         {
  17.             string[] data = File.ReadAllLines(path);
  18.             weigthMatrix = new int[data.Length, data[0].Split(' ').Count()];
  19.             currentTable = new int[2, weigthMatrix.GetLength(1)];
  20.             revisedVerticies = new List<int>();
  21.  
  22.             for (int i = 0; i < weigthMatrix.GetLength(0); i++)
  23.             {
  24.                 string[] current = data[i].Split(" ");
  25.                 for (int j = 0; j < weigthMatrix.GetLength(1); j++)
  26.                 {
  27.                     if (current[j] == "-")
  28.                     {
  29.                         weigthMatrix[i, j] = -1;
  30.                     }
  31.                     else
  32.                     {
  33.                         weigthMatrix[i, j] = int.Parse(current[j]);
  34.                     }
  35.                 }
  36.             }
  37.  
  38.             for (int i = 0; i < currentTable.GetLength(0); i++)
  39.             {
  40.                 for (int j = 0; j < currentTable.GetLength(1); j++)
  41.                 {
  42.                     currentTable[i, j] = int.MaxValue;
  43.                 }
  44.             }
  45.             this.initialVertex = initialVertex - 1;
  46.             currentTable[0, this.initialVertex] = 0;
  47.         }
  48.         private void printArr()
  49.         {
  50.             Console.Write("".PadRight(4));
  51.             for (int i = 0; i < weigthMatrix.GetLength(0); i++)
  52.             {
  53.                 Console.Write($"{i + 1}".PadRight(4));
  54.             }
  55.             Console.WriteLine();
  56.  
  57.             for (int i = 0; i < weigthMatrix.GetLength(0); i++)
  58.             {
  59.                 Console.Write($"{i + 1}".PadRight(4));
  60.  
  61.                 for (int j = 0; j < weigthMatrix.GetLength(1); j++)
  62.                 {
  63.                     if (weigthMatrix[i, j] == -1)
  64.                     {
  65.                         Console.Write($"-".PadRight(4));
  66.                     }
  67.                     else
  68.                     {
  69.                         Console.Write($"{weigthMatrix[i, j]}".PadRight(4));
  70.                     }
  71.                 }
  72.                 Console.WriteLine();
  73.             }
  74.         }
  75.         private void printTable()
  76.         {
  77.             for (int i = 0; i < weigthMatrix.GetLength(0); i++)
  78.             {
  79.                 Console.Write($"{i + 1}".PadRight(4));
  80.             }
  81.             Console.WriteLine();
  82.  
  83.             for (int i = 0; i < currentTable.GetLength(0); i++)
  84.             {
  85.                 for (int j = 0; j < currentTable.GetLength(1); j++)
  86.                 {
  87.                     if (currentTable[i, j] == int.MaxValue)
  88.                     {
  89.                         Console.Write($"-".PadRight(4));
  90.                     }
  91.                     else
  92.                     {
  93.                         if (i == 0)
  94.                         {
  95.                             Console.Write($"{currentTable[i, j]}".PadRight(4));
  96.                         }
  97.                         else
  98.                         {
  99.                             Console.Write($"{currentTable[i, j] + 1}".PadRight(4));
  100.                         }
  101.                     }
  102.                 }
  103.                 Console.WriteLine();
  104.             }
  105.         }
  106.         private List<int> getAdjacentVertices(int vertexIndex)
  107.         {
  108.             List<int> adjacentVertices = new List<int>();
  109.  
  110.             for (int j = 0; j < weigthMatrix.GetLength(1); j++)
  111.             {
  112.                 if (weigthMatrix[vertexIndex, j] > 0)
  113.                 {
  114.                     adjacentVertices.Add(j);
  115.                 }
  116.             }
  117.             return adjacentVertices;
  118.         }
  119.         private int selectCheapestVertex()
  120.         {
  121.             int cheapest = int.MaxValue;
  122.             int selected = -1;
  123.             for (int j = 0; j < currentTable.GetLength(1); j++)
  124.             {
  125.                 if (currentTable[0, j] < cheapest && !revisedVerticies.Contains(j))
  126.                 {
  127.                     cheapest = currentTable[0, j];
  128.                     selected = j;
  129.                 }
  130.             }
  131.             if (selected != -1)
  132.             {
  133.                 revisedVerticies.Add(selected);
  134.             }
  135.  
  136.             return selected;
  137.         }
  138.         private void printAdjacentVertices(List<int> adjacentVertices)
  139.         {
  140.             Console.Write("Adjacent Vertices: ");
  141.             for (int i = 0; i < adjacentVertices.Count; i++)
  142.             {
  143.                 Console.Write($"{adjacentVertices[i] + 1}".PadRight(4));
  144.             }
  145.             Console.WriteLine();
  146.         }
  147.         private void printRoutes()
  148.         {
  149.             Console.WriteLine("Маршрут".PadRight(9) + "Шлях".PadRight(15) + "Довжина");
  150.  
  151.             for (int i = 0; i < currentTable.GetLength(1); i++)
  152.             {
  153.                 if (currentTable[1, i] == int.MaxValue)
  154.                 {
  155.                     if (i == 0)
  156.                     {
  157.                         Console.WriteLine($"{initialVertex + 1}-{initialVertex + 1}".PadRight(9) + "-".PadRight(15) + $"{currentTable[0, i]}");
  158.                     }
  159.                     continue;
  160.                 }
  161.  
  162.                 List<int> currentRoute = new List<int>();
  163.                 currentRoute.Add(i + 1);
  164.                 int intermediateVertex = i;
  165.                 while (intermediateVertex != initialVertex)
  166.                 {
  167.                     intermediateVertex = currentTable[1, intermediateVertex];
  168.                     currentRoute.Add(intermediateVertex + 1);
  169.                 }
  170.                 currentRoute.Reverse();
  171.                 string route = "";
  172.                 for (int j = 0; j < currentRoute.Count; j++)
  173.                 {
  174.                     route += currentRoute[j].ToString();
  175.                     if (j != currentRoute.Count - 1)
  176.                     {
  177.                         route += "->";
  178.                     }
  179.                 }
  180.                 Console.WriteLine($"{initialVertex + 1}-{i + 1}".PadRight(9) + route.PadRight(15) + $"{currentTable[0, i]}");
  181.             }
  182.         }
  183.         public void Solve()
  184.         {
  185.             printArr();
  186.             Console.WriteLine();
  187.             Console.WriteLine();
  188.             int i = 0;
  189.             bool isSolved = false;
  190.             int iter = 2;
  191.             while (!isSolved)
  192.             {
  193.                 i = selectCheapestVertex();
  194.                 if (i == -1)
  195.                 {
  196.                     break;
  197.                 }
  198.                 Console.WriteLine($"Iter {iter} selected {i + 1}");
  199.                 List<int> adjacentVertices = getAdjacentVertices(i);
  200.                 printAdjacentVertices(adjacentVertices);
  201.  
  202.                 for (int j = 0; j < adjacentVertices.Count; j++)
  203.                 {
  204.                     int currentVertex = adjacentVertices[j];
  205.                     if (currentTable[0, currentVertex] > currentTable[0, i] + weigthMatrix[i, currentVertex])
  206.                     {
  207.                         currentTable[0, currentVertex] = currentTable[0, i] + weigthMatrix[i, currentVertex];
  208.                         currentTable[1, currentVertex] = i;
  209.                     }
  210.                 }
  211.  
  212.                 printTable();
  213.                 Console.WriteLine();
  214.                 iter++;
  215.  
  216.                 isSolved = (revisedVerticies.Count == weigthMatrix.GetLength(1));
  217.             }
  218.             printRoutes();
  219.         }
  220.     }
  221. }
  222.  
Advertisement
Add Comment
Please, Sign In to add comment