public Salesman(int[,] m, int n, Salesman par) { matrix = m; N = n; reductionRows = new int[N]; reductionColumns = new int[N]; secondSanctionColumns = new int[N]; secondSanctionRows = new int[N]; rootEstimation = 0; childs = new List<Salesman>(); parent = par; }
static void branches(int[,] matrix, CompareResults cr) { Stopwatch branchesWatch = new Stopwatch(); branchesWatch.Start(); List<Salesman> ways = new List<Salesman>(); Salesman slm = new Salesman(matrix, N, null); Salesman best_way = slm; slm.step = N; slm.Prepare(); bool end = false; int min = INF; while (!end) { //включаем вершину Salesman sal1 = new Salesman(decreaseMatrix(slm), N, slm); sal1.matrix[slm.index_i, slm.index_j] = INF; sal1.step = slm.step - 1; slm.childs.Add(sal1); sal1.Prepare(); sal1.rootEstimation += slm.rootEstimation; //Console.WriteLine(sal1.rootEstimation + " " + sal1.step.ToString()); ways.Add(sal1); //не включаем вершину Salesman sal2 = new Salesman(slm.matrix, N, slm); sal2.matrix[slm.index_i, slm.index_j] = INF; sal2.step = slm.step - 1; slm.childs.Add(sal2); sal2.Prepare(); sal2.rootEstimation = slm.secondSanction + slm.rootEstimation; //Console.WriteLine(sal2.rootEstimation + " " + sal2.step.ToString()); ways.Add(sal2); //проверка min = INF; foreach (Salesman s in ways) { if ((s.rootEstimation < min) && (s.childs.Count == 0)) { slm = s; min = s.rootEstimation; } } if ((slm.step == 0))// && (slm == sal1))//&& (sal2.rootEstimation>=INF)) end = true; } branchesWatch.Stop(); cr.branchesTime = branchesWatch.Elapsed.TotalMilliseconds; cr.branchesWight = slm.rootEstimation; }
static int[,] decreaseMatrix(Salesman s) { int[,] newmatr = new int[N,N]; for (int i = 0; i < s.N; i++) { for (int j = 0; j < N; j++) { if ((i==s.index_i) || (j== s.index_j)) { newmatr[i,j] = INF; } else newmatr[i,j] = s.matrix[i,j]; } } newmatr[s.index_j, s.index_i] = INF; return newmatr; }