123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Threading.Tasks;
- namespace matModelirovanie
- {
- internal class GrafKommivoizer
- {
- int?[,] arrayAll = { { 0, 1, 2, 3, 4 }, { 1, -1, 2, 1, 5 }, { 2, 3, -1, 2, 1 }, { 3, 4, 1, -1, 2 }, { 4, 5, 3, 3, -1 } };
- public GrafKommivoizer(int countGrafs)
- {
- // arrayAll = new int[countGrafs + 1, countGrafs + 1];
- //for (int i = 1; i < arrayAll.GetLength(0); i++)
- //{
- // Console.WriteLine($"Введите значения строки {i} через пробел");
- // string rowValue = Console.ReadLine();
- // string[] elementRow = rowValue.Split(" ");
- // for (int j = 1; j < arrayAll.GetLength(1); j++)
- // {
- // arrayAll[i, j] = Convert.ToInt32(elementRow[j - 1]);
- // }
- //}
- //for (int i = 0; i < arrayAll.GetLength(0); i++)
- //{
- // arrayAll[0, i] = i;
- // arrayAll[i, 0] = i;
- //}
- PrintTable();
- }
- void PrintTable()
- {
- for (int i = 0; i < arrayAll.GetLength(0); i++)
- {
- for (int j = 0; j < arrayAll.GetLength(1); j++)
- {
- Console.Write("{0,5:0}", arrayAll[i, j]);
- }
- Console.WriteLine();
- }
- Console.WriteLine("----------------------------------------");
- }
- List<int?> StringReduction()
- {
- List<int?> listReductionRow = new List<int?>();
- for (int i = 1; i < arrayAll.GetLength(0); i++)
- {
- List<int?> listRowValue = new List<int?>();
- for (int j = 1; j < arrayAll.GetLength(1); j++)
- {
- if (arrayAll[i, j] == -1 || arrayAll[i, j] == null) continue;
- else listRowValue.Add(arrayAll[i, j]);
- }
- int minValue = 0 ;
- if (listRowValue.Count != 0)
- {
- minValue = (int)listRowValue.Min();
- }
- listReductionRow.Add(minValue);
- for (int j = 1; j < arrayAll.GetLength(1); j++)
- {
- if (arrayAll[i, j] == -1) continue;
- arrayAll[i, j] -= minValue;
- }
- }
- PrintTable();
- return listReductionRow;
- }
- List<int?> ColumnReduction()
- {
- List<int?> listReductionColumn = new List<int?>();
- for (int i = 1; i < arrayAll.GetLength(1); i++)
- {
- List<int?> listColumnValue = new List<int?>();
- for (int j = 1; j < arrayAll.GetLength(0); j++)
- {
- if (arrayAll[j, i] == -1 || arrayAll[j, i] == null) continue;
- else listColumnValue.Add(arrayAll[j, i]);
- }
- int minValue = 0;
- if (listColumnValue.Count != 0)
- {
- minValue = (int)listColumnValue.Min();
- }
- listReductionColumn.Add(minValue);
- for (int j = 1; j < arrayAll.GetLength(0); j++)
- {
- if (arrayAll[j, i] == -1) continue;
- arrayAll[j, i] -= minValue;
- }
- }
- PrintTable();
- return listReductionColumn;
- }
- int CountingRoot(List<int?> listReductionRow, List<int?> listReductionColumn)
- {
- return (int)(listReductionRow.Sum() + listReductionColumn.Sum());
- }
- int[,] CountZeroCells()
- {
- int[,] result = new int[arrayAll.GetLength(0), arrayAll.GetLength(1)];
- for (int i = 1; i < result.GetLength(0); i++)
- {
- for (int j = 1; j < result.GetLength(1); j++)
- {
- if (arrayAll[i, j] == 0)
- {
- result[i, j] = CountSumMinValueForZeroCells(i, j);
- }
- }
- }
- return result;
- }
- int CountSumMinValueForZeroCells(int indexRow, int indexColumn)
- {
- List<int?> row = new List<int?>();
- List<int?> column = new List<int?>();
- for (int i = 1; i < arrayAll.GetLength(0); i++)
- {
- if (arrayAll[indexRow, i] == -1 || i == indexColumn) continue;
- row.Add(arrayAll[indexRow, i]);
- }
- for (int i = 1; i < arrayAll.GetLength(1); i++)
- {
- if (arrayAll[i, indexColumn] == -1 || i == indexRow) continue;
- column.Add(arrayAll[i, indexColumn]);
- }
- return (int)(row.Min() + column.Min());
- }
- List<int> ConvertToListZeroCells(int[,] evaluationOfZeroCells)
- {
- List<int> result = new List<int>();
- for (int i = 0; i < evaluationOfZeroCells.GetLength(0); i++)
- {
- for (int j = 0; j < evaluationOfZeroCells.GetLength(1); j++)
- {
- result.Add(evaluationOfZeroCells[i, j]);
- }
- }
- return result;
- }
- int[] SerchMaxElem(int maxCell, int[,] evaluationOfZeroCells)
- {
- for (int i = 1; i < evaluationOfZeroCells.GetLength(0); i++)
- {
- for (int j = 1; j < evaluationOfZeroCells.GetLength(1); j++)
- {
- if (evaluationOfZeroCells[i, j] == maxCell)
- {
- return new int[] { i, j };
- }
- }
- }
- return null;
- }
- int[] WorkWothTable()
- {
- List<int?> listReductionRow = StringReduction();
- List<int?> listReductionColumn = ColumnReduction();
- int root = CountingRoot(listReductionRow, listReductionColumn);
- int[,] evaluationOfZeroCells = CountZeroCells();
- List<int> listEvaluationOfZeroCells = ConvertToListZeroCells(evaluationOfZeroCells);
- int maxCell = listEvaluationOfZeroCells.Max();
- int[] indexRowAndColumn = SerchMaxElem(maxCell, evaluationOfZeroCells);
- ResizeArrayAll(indexRowAndColumn);
- return new int[] { root, maxCell, indexRowAndColumn[0], indexRowAndColumn[1] };
- }
- void PrintGraf(int root, int indexRow, int indexColumn)
- {
- Console.WriteLine($"Стоимость пути - {root}. Идем из графа {indexRow} -> {indexColumn}");
- }
- void ResizeArrayAll(int[] indexRowAndColumn)
- {
- for (int i = 0; i < arrayAll.GetLength(0); i++)
- {
- if (i == indexRowAndColumn[0])
- {
- for (int j = 0; j < arrayAll.GetLength(1); j++)
- {
- arrayAll[i, j] = null;
- }
- }
- }
- for (int i = 0; i < arrayAll.GetLength(1); i++)
- {
- if (i == indexRowAndColumn[1])
- {
- for (int j = 0; j < arrayAll.GetLength(0); j++)
- {
- arrayAll[j, i] = null;
- }
- }
- }
- }
- int CountWithoutGraf(int maxCell, int root)
- {
- return root + maxCell;
- }
- bool CheckArrayAllNotNullElements()
- {
- int countNotNullElem = 0;
- for(int i = 0;i < arrayAll.GetLength(0);i++)
- {
- for(int j = 0;j < arrayAll.GetLength(1);j++)
- {
- if (arrayAll[i,j] != null)
- {
- countNotNullElem += 1;
- }
- }
- }
- if (countNotNullElem > 1) return true;
- else return false;
- }
- public void MainCicle()
- {
- int[] valueRootAndMaxCell = WorkWothTable();
- PrintGraf(valueRootAndMaxCell[0], valueRootAndMaxCell[2], valueRootAndMaxCell[3]);
- while (CheckArrayAllNotNullElements())
- {
- int withoutGraf = CountWithoutGraf(valueRootAndMaxCell[1], valueRootAndMaxCell[0]);
- valueRootAndMaxCell = WorkWothTable();
- if (valueRootAndMaxCell[0] > withoutGraf)
- {
- PrintGraf(valueRootAndMaxCell[0], valueRootAndMaxCell[2], valueRootAndMaxCell[3]);
- }
- }
- }
- }
- }
|