GrafKommivoizer.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http.Headers;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace matModelirovanie
  8. {
  9. internal class GrafKommivoizer
  10. {
  11. 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 } };
  12. public GrafKommivoizer(int countGrafs)
  13. {
  14. // arrayAll = new int[countGrafs + 1, countGrafs + 1];
  15. //for (int i = 1; i < arrayAll.GetLength(0); i++)
  16. //{
  17. // Console.WriteLine($"Введите значения строки {i} через пробел");
  18. // string rowValue = Console.ReadLine();
  19. // string[] elementRow = rowValue.Split(" ");
  20. // for (int j = 1; j < arrayAll.GetLength(1); j++)
  21. // {
  22. // arrayAll[i, j] = Convert.ToInt32(elementRow[j - 1]);
  23. // }
  24. //}
  25. //for (int i = 0; i < arrayAll.GetLength(0); i++)
  26. //{
  27. // arrayAll[0, i] = i;
  28. // arrayAll[i, 0] = i;
  29. //}
  30. PrintTable();
  31. }
  32. void PrintTable()
  33. {
  34. for (int i = 0; i < arrayAll.GetLength(0); i++)
  35. {
  36. for (int j = 0; j < arrayAll.GetLength(1); j++)
  37. {
  38. Console.Write("{0,5:0}", arrayAll[i, j]);
  39. }
  40. Console.WriteLine();
  41. }
  42. Console.WriteLine("----------------------------------------");
  43. }
  44. List<int?> StringReduction()
  45. {
  46. List<int?> listReductionRow = new List<int?>();
  47. for (int i = 1; i < arrayAll.GetLength(0); i++)
  48. {
  49. List<int?> listRowValue = new List<int?>();
  50. for (int j = 1; j < arrayAll.GetLength(1); j++)
  51. {
  52. if (arrayAll[i, j] == -1 || arrayAll[i, j] == null) continue;
  53. else listRowValue.Add(arrayAll[i, j]);
  54. }
  55. int minValue = 0 ;
  56. if (listRowValue.Count != 0)
  57. {
  58. minValue = (int)listRowValue.Min();
  59. }
  60. listReductionRow.Add(minValue);
  61. for (int j = 1; j < arrayAll.GetLength(1); j++)
  62. {
  63. if (arrayAll[i, j] == -1) continue;
  64. arrayAll[i, j] -= minValue;
  65. }
  66. }
  67. PrintTable();
  68. return listReductionRow;
  69. }
  70. List<int?> ColumnReduction()
  71. {
  72. List<int?> listReductionColumn = new List<int?>();
  73. for (int i = 1; i < arrayAll.GetLength(1); i++)
  74. {
  75. List<int?> listColumnValue = new List<int?>();
  76. for (int j = 1; j < arrayAll.GetLength(0); j++)
  77. {
  78. if (arrayAll[j, i] == -1 || arrayAll[j, i] == null) continue;
  79. else listColumnValue.Add(arrayAll[j, i]);
  80. }
  81. int minValue = 0;
  82. if (listColumnValue.Count != 0)
  83. {
  84. minValue = (int)listColumnValue.Min();
  85. }
  86. listReductionColumn.Add(minValue);
  87. for (int j = 1; j < arrayAll.GetLength(0); j++)
  88. {
  89. if (arrayAll[j, i] == -1) continue;
  90. arrayAll[j, i] -= minValue;
  91. }
  92. }
  93. PrintTable();
  94. return listReductionColumn;
  95. }
  96. int CountingRoot(List<int?> listReductionRow, List<int?> listReductionColumn)
  97. {
  98. return (int)(listReductionRow.Sum() + listReductionColumn.Sum());
  99. }
  100. int[,] CountZeroCells()
  101. {
  102. int[,] result = new int[arrayAll.GetLength(0), arrayAll.GetLength(1)];
  103. for (int i = 1; i < result.GetLength(0); i++)
  104. {
  105. for (int j = 1; j < result.GetLength(1); j++)
  106. {
  107. if (arrayAll[i, j] == 0)
  108. {
  109. result[i, j] = CountSumMinValueForZeroCells(i, j);
  110. }
  111. }
  112. }
  113. return result;
  114. }
  115. int CountSumMinValueForZeroCells(int indexRow, int indexColumn)
  116. {
  117. List<int?> row = new List<int?>();
  118. List<int?> column = new List<int?>();
  119. for (int i = 1; i < arrayAll.GetLength(0); i++)
  120. {
  121. if (arrayAll[indexRow, i] == -1 || i == indexColumn) continue;
  122. row.Add(arrayAll[indexRow, i]);
  123. }
  124. for (int i = 1; i < arrayAll.GetLength(1); i++)
  125. {
  126. if (arrayAll[i, indexColumn] == -1 || i == indexRow) continue;
  127. column.Add(arrayAll[i, indexColumn]);
  128. }
  129. return (int)(row.Min() + column.Min());
  130. }
  131. List<int> ConvertToListZeroCells(int[,] evaluationOfZeroCells)
  132. {
  133. List<int> result = new List<int>();
  134. for (int i = 0; i < evaluationOfZeroCells.GetLength(0); i++)
  135. {
  136. for (int j = 0; j < evaluationOfZeroCells.GetLength(1); j++)
  137. {
  138. result.Add(evaluationOfZeroCells[i, j]);
  139. }
  140. }
  141. return result;
  142. }
  143. int[] SerchMaxElem(int maxCell, int[,] evaluationOfZeroCells)
  144. {
  145. for (int i = 1; i < evaluationOfZeroCells.GetLength(0); i++)
  146. {
  147. for (int j = 1; j < evaluationOfZeroCells.GetLength(1); j++)
  148. {
  149. if (evaluationOfZeroCells[i, j] == maxCell)
  150. {
  151. return new int[] { i, j };
  152. }
  153. }
  154. }
  155. return null;
  156. }
  157. int[] WorkWothTable()
  158. {
  159. List<int?> listReductionRow = StringReduction();
  160. List<int?> listReductionColumn = ColumnReduction();
  161. int root = CountingRoot(listReductionRow, listReductionColumn);
  162. int[,] evaluationOfZeroCells = CountZeroCells();
  163. List<int> listEvaluationOfZeroCells = ConvertToListZeroCells(evaluationOfZeroCells);
  164. int maxCell = listEvaluationOfZeroCells.Max();
  165. int[] indexRowAndColumn = SerchMaxElem(maxCell, evaluationOfZeroCells);
  166. ResizeArrayAll(indexRowAndColumn);
  167. return new int[] { root, maxCell, indexRowAndColumn[0], indexRowAndColumn[1] };
  168. }
  169. void PrintGraf(int root, int indexRow, int indexColumn)
  170. {
  171. Console.WriteLine($"Стоимость пути - {root}. Идем из графа {indexRow} -> {indexColumn}");
  172. }
  173. void ResizeArrayAll(int[] indexRowAndColumn)
  174. {
  175. for (int i = 0; i < arrayAll.GetLength(0); i++)
  176. {
  177. if (i == indexRowAndColumn[0])
  178. {
  179. for (int j = 0; j < arrayAll.GetLength(1); j++)
  180. {
  181. arrayAll[i, j] = null;
  182. }
  183. }
  184. }
  185. for (int i = 0; i < arrayAll.GetLength(1); i++)
  186. {
  187. if (i == indexRowAndColumn[1])
  188. {
  189. for (int j = 0; j < arrayAll.GetLength(0); j++)
  190. {
  191. arrayAll[j, i] = null;
  192. }
  193. }
  194. }
  195. }
  196. int CountWithoutGraf(int maxCell, int root)
  197. {
  198. return root + maxCell;
  199. }
  200. bool CheckArrayAllNotNullElements()
  201. {
  202. int countNotNullElem = 0;
  203. for(int i = 0;i < arrayAll.GetLength(0);i++)
  204. {
  205. for(int j = 0;j < arrayAll.GetLength(1);j++)
  206. {
  207. if (arrayAll[i,j] != null)
  208. {
  209. countNotNullElem += 1;
  210. }
  211. }
  212. }
  213. if (countNotNullElem > 1) return true;
  214. else return false;
  215. }
  216. public void MainCicle()
  217. {
  218. int[] valueRootAndMaxCell = WorkWothTable();
  219. PrintGraf(valueRootAndMaxCell[0], valueRootAndMaxCell[2], valueRootAndMaxCell[3]);
  220. while (CheckArrayAllNotNullElements())
  221. {
  222. int withoutGraf = CountWithoutGraf(valueRootAndMaxCell[1], valueRootAndMaxCell[0]);
  223. valueRootAndMaxCell = WorkWothTable();
  224. if (valueRootAndMaxCell[0] > withoutGraf)
  225. {
  226. PrintGraf(valueRootAndMaxCell[0], valueRootAndMaxCell[2], valueRootAndMaxCell[3]);
  227. }
  228. }
  229. }
  230. }
  231. }