Slavik9510

Untitled

May 9th, 2023
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.11 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.         public DijkstraMethod(string path, int initialVertex = 1)
  15.         {
  16.             string[] data = File.ReadAllLines(path);
  17.             weigthMatrix = new int[data.Length, data[0].Split(' ').Count()];
  18.             currentTable = new int[2, weigthMatrix.GetLength(1)];
  19.             revisedVerticies = new List<int>();
  20.  
  21.             for (int i = 0; i < weigthMatrix.GetLength(0); i++)
  22.             {
  23.                 string[] current = data[i].Split(" ");
  24.                 for (int j = 0; j < weigthMatrix.GetLength(1); j++)
  25.                 {
  26.                     if (current[j] == "-")
  27.                     {
  28.                         weigthMatrix[i, j] = -1;
  29.                     }
  30.                     else
  31.                     {
  32.                         weigthMatrix[i, j] = int.Parse(current[j]);
  33.                     }
  34.                 }
  35.             }
  36.  
  37.             for (int i = 0; i < currentTable.GetLength(0); i++)
  38.             {
  39.                 for (int j = 0; j < currentTable.GetLength(1); j++)
  40.                 {
  41.                     currentTable[i, j] = int.MaxValue;
  42.                 }
  43.             }
  44.             currentTable[0, initialVertex - 1] = 0;
  45.         }
  46.         private void printArr()
  47.         {
  48.             Console.Write("".PadRight(4));
  49.             for (int i = 0; i < weigthMatrix.GetLength(0); i++)
  50.             {
  51.                 Console.Write($"{i + 1}".PadRight(4));
  52.             }
  53.             Console.WriteLine();
  54.  
  55.             for (int i = 0; i < weigthMatrix.GetLength(0); i++)
  56.             {
  57.                 Console.Write($"{i + 1}".PadRight(4));
  58.  
  59.                 for (int j = 0; j < weigthMatrix.GetLength(1); j++)
  60.                 {
  61.                     if (weigthMatrix[i, j] == -1)
  62.                     {
  63.                         Console.Write($"-".PadRight(4));
  64.                     }
  65.                     else
  66.                     {
  67.                         Console.Write($"{weigthMatrix[i, j]}".PadRight(4));
  68.                     }
  69.                 }
  70.                 Console.WriteLine();
  71.             }
  72.         }
  73.         private void printTable()
  74.         {
  75.             for (int i = 0; i < weigthMatrix.GetLength(0); i++)
  76.             {
  77.                 Console.Write($"{i + 1}".PadRight(4));
  78.             }
  79.             Console.WriteLine();
  80.  
  81.             for (int i = 0; i < currentTable.GetLength(0); i++)
  82.             {
  83.                 for (int j = 0; j < currentTable.GetLength(1); j++)
  84.                 {
  85.                     if (currentTable[i, j] == int.MaxValue)
  86.                     {
  87.                         Console.Write($"-".PadRight(4));
  88.                     }
  89.                     else
  90.                     {
  91.                         if (i == 0)
  92.                         {
  93.                             Console.Write($"{currentTable[i, j]}".PadRight(4));
  94.                         }
  95.                         else
  96.                         {
  97.                             Console.Write($"{currentTable[i, j] + 1}".PadRight(4));
  98.                         }
  99.                     }
  100.                 }
  101.                 Console.WriteLine();
  102.             }
  103.         }
  104.         private List<int> getAdjacentVertices(int vertexIndex)
  105.         {
  106.             List<int> adjacentVertices = new List<int>();
  107.  
  108.             for (int j = 0; j < weigthMatrix.GetLength(1); j++)
  109.             {
  110.                 if (weigthMatrix[vertexIndex, j] > 0)
  111.                 {
  112.                     adjacentVertices.Add(j);
  113.                 }
  114.             }
  115.             return adjacentVertices;
  116.         }
  117.         private int selectCheapestVertex()
  118.         {
  119.             int cheapest = int.MaxValue;
  120.             int selected = -1;
  121.             for (int j = 0; j < currentTable.GetLength(1); j++)
  122.             {
  123.                 if (currentTable[0, j] < cheapest && !revisedVerticies.Contains(j))
  124.                 {
  125.                     cheapest = currentTable[0, j];
  126.                     selected = j;
  127.                 }
  128.             }
  129.             if (selected != -1)
  130.             {
  131.                 revisedVerticies.Add(selected);
  132.             }
  133.  
  134.             return selected;
  135.         }
  136.         private void printAdjacentVertices(List<int> adjacentVertices)
  137.         {
  138.             Console.Write("Adjacent Vertices: ");
  139.             for (int i = 0; i < adjacentVertices.Count; i++)
  140.             {
  141.                 Console.Write($"{adjacentVertices[i] + 1}".PadRight(4));
  142.             }
  143.             Console.WriteLine();
  144.         }
  145.         private void printRoutes()
  146.         {
  147.             Console.WriteLine("Маршрут".PadRight(5) + "Шлях".PadRight();
  148.         }
  149.         public void Solve()
  150.         {
  151.             printArr();
  152.             Console.WriteLine();
  153.             Console.WriteLine();
  154.             int i = 0;
  155.             bool isSolved = false;
  156.             int iter = 2;
  157.             while (!isSolved)
  158.             {
  159.                 i = selectCheapestVertex();
  160.                 if (i == -1)
  161.                 {
  162.                     break;
  163.                 }
  164.                 Console.WriteLine($"Iter {iter} selected {i + 1}");
  165.                 List<int> adjacentVertices = getAdjacentVertices(i);
  166.                 printAdjacentVertices(adjacentVertices);
  167.  
  168.                 for (int j = 0; j < adjacentVertices.Count; j++)
  169.                 {
  170.                     int currentVertex = adjacentVertices[j];
  171.                     if (currentTable[0, currentVertex] > currentTable[0, i] + weigthMatrix[i, currentVertex])
  172.                     {
  173.                         currentTable[0, currentVertex] = currentTable[0, i] + weigthMatrix[i, currentVertex];
  174.                         currentTable[1, currentVertex] = i;
  175.                     }
  176.                 }
  177.  
  178.                 printTable();
  179.                 Console.WriteLine();
  180.                 iter++;
  181.  
  182.                 isSolved = (revisedVerticies.Count == weigthMatrix.GetLength(1));
  183.             }
  184.             ;
  185.         }
  186.     }
  187. }
  188.  
Advertisement
Add Comment
Please, Sign In to add comment