private void exactCheckBtn_Click(object sender, EventArgs e) { HierarchicExactChecker checker = new HierarchicExactChecker(); bool result = checker.IsHierarchic(this.exactFilePathTxt.Text); this.exactResultTxt.Text = result ? "Is Hierarchic" : "Is Not Hierarchic"; }
// Строится граф на основе матрицы смежности. public override void SetMatrix(string fileName) { ArrayList matrix = MatrixFileReader.MatrixReader(fileName); log.Info("Checking if given matrix is block-hierarchic."); // проверка на правильность входной матрицы (она должна быть иерархической) List<List<bool>> matrixInList = new List<List<bool>>(); ArrayList arr; for (int i = 0; i < matrix.Count; ++i) { arr = (ArrayList)matrix[i]; matrixInList.Add(new List<bool>()); for(int j = 0; j < arr.Count - 1; ++j) matrixInList[i].Add((bool)arr[j]); } HierarchicExactChecker checker = new HierarchicExactChecker(); if (!checker.IsHierarchic(matrixInList)) { log.Info("Given matrix is not block-hierarchic."); throw new SystemException("Not correct matrix."); } else { log.Info("Given matrix is block-hierarchic."); branchIndex = checker.BranchIndex; level = checker.Level; log.Info("Creating HierarchicContainer object from given matrix."); treeMatrix = new BitArray[level][]; // начиная снизу, для каждого уровня создаются и заполняются данные int nodeDataLength = branchIndex * (branchIndex - 1) / 2; int[] nIndexes = new int[nodeDataLength]; int[] mIndexes = new int[nodeDataLength]; for (int gamma = level; gamma > 0; --gamma) { // get current level data length and bitArrays count long dataLength = Convert.ToInt64(Math.Pow(branchIndex, gamma - 1) * nodeDataLength); int arrCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(dataLength) / ARRAY_MAX_SIZE)); treeMatrix[gamma - 1] = new BitArray[arrCount]; int j; for (j = 0; j < arrCount - 1; j++) { treeMatrix[gamma - 1][j] = new BitArray(ARRAY_MAX_SIZE); } treeMatrix[gamma - 1][j] = new BitArray(Convert.ToInt32(dataLength - (arrCount - 1) * ARRAY_MAX_SIZE)); // fills data for current level nodes // loop over all elements of given level and fill him values int lim1 = branchIndex - 1, lim2 = 0, nt = 0; nIndexes[0] = 0; for (int nIndex = 1; nIndex < branchIndex; ++nIndex) { while (nt < lim1) { if (nIndex == 1) nIndexes[nt] = nIndexes[lim2]; else nIndexes[nt] = nIndexes[lim2] + Convert.ToInt32(Math.Pow(branchIndex, level - gamma)); ++nt; } lim2 = lim1 - 1; lim1 = lim1 + branchIndex - 1 - nIndex; } mIndexes[0] = Convert.ToInt32(Math.Pow(branchIndex, level - gamma)); int mt = 0; int fIndex = 1; while (mt < nodeDataLength) { for (int i = fIndex; i <= branchIndex - 1; ++i) { mIndexes[mt] = i * mIndexes[0]; ++mt; } ++fIndex; } for (int f = 0; f < treeMatrix[gamma - 1].Length; f++) { for (int g = 0; g < treeMatrix[gamma - 1][f].Length; g++) { int currentIndex = g % nodeDataLength; int add = Convert.ToInt32((g / nodeDataLength)) * Convert.ToInt32(Math.Pow(branchIndex, level - gamma + 1)); treeMatrix[gamma - 1][f][g] = matrixInList[nIndexes[currentIndex] + add][mIndexes[currentIndex] + add]; } } } } }
public override void SetMatrix(ArrayList matrix) { List<List<bool>> matrixInList = new List<List<bool>>(); ArrayList arr; for (int i = 0; i < matrix.Count; ++i) { arr = (ArrayList)matrix[i]; matrixInList.Add(new List<bool>()); for(int j = 0; j < arr.Count; ++j) matrixInList[i].Add((bool)arr[j]); } HierarchicExactChecker checker = new HierarchicExactChecker(); if (!checker.IsHierarchic(matrixInList)) { throw new MatrixFormatException(); } else { size = (UInt32)matrix.Count; branchingIndex = (UInt32)checker.BranchIndex; level = (UInt32)checker.Level; hierarchicTree = new BitArray[level][]; // Initializing and filling data for each level, beginning from root. uint nodeDataLength = branchingIndex * (branchingIndex - 1) / 2; int[] nIndexes = new int[nodeDataLength]; int[] mIndexes = new int[nodeDataLength]; for (int gamma = (int)level; gamma > 0; --gamma) { // get current level data length and bitArrays count long levelDataLength = Convert.ToInt64(Math.Pow(branchingIndex, gamma - 1) * nodeDataLength); int arrayCountForLevel = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(levelDataLength) / ARRAY_MAX_SIZE)); hierarchicTree[gamma - 1] = new BitArray[arrayCountForLevel]; int j; for (j = 0; j < arrayCountForLevel - 1; j++) { hierarchicTree[gamma - 1][j] = new BitArray(ARRAY_MAX_SIZE); } hierarchicTree[gamma - 1][j] = new BitArray(Convert.ToInt32(levelDataLength - (arrayCountForLevel - 1) * ARRAY_MAX_SIZE)); // fills data for current level nodes // loop over all elements of given level and fill him values uint lim1 = branchingIndex - 1, lim2 = 0, nt = 0; nIndexes[0] = 0; for (uint nIndex = 1; nIndex < branchingIndex; ++nIndex) { while (nt < lim1) { if (nIndex == 1) nIndexes[nt] = nIndexes[lim2]; else nIndexes[nt] = nIndexes[lim2] + Convert.ToInt32(Math.Pow(branchingIndex, level - gamma)); ++nt; } lim2 = lim1 - 1; lim1 = lim1 + branchingIndex - 1 - nIndex; } mIndexes[0] = Convert.ToInt32(Math.Pow(branchingIndex, level - gamma)); int mt = 0; int fIndex = 1; while (mt < nodeDataLength) { for (int i = fIndex; i <= branchingIndex - 1; ++i) { mIndexes[mt] = i * mIndexes[0]; ++mt; } ++fIndex; } for (int f = 0; f < hierarchicTree[gamma - 1].Length; f++) { for (int g = 0; g < hierarchicTree[gamma - 1][f].Length; g++) { int currentIndex = g % (int)nodeDataLength; int add = Convert.ToInt32((g / nodeDataLength)) * Convert.ToInt32(Math.Pow(branchingIndex, level - gamma + 1)); hierarchicTree[gamma - 1][f][g] = matrixInList[nIndexes[currentIndex] + add][mIndexes[currentIndex] + add]; } } } } }
public override void SetMatrix(BitArray[] matrix) { // maybe better List <List <bool> > matrixInList = new List <List <bool> >(); for (int i = 0; i < matrix.Length; ++i) { matrixInList.Add(new List <bool>()); for (int j = 0; j < matrix[i].Length; ++j) { matrixInList[i].Add(matrix[i][j]); } } HierarchicExactChecker checker = new HierarchicExactChecker(); if (!checker.IsHierarchic(matrixInList)) { throw new MatrixFormatException(); } else { size = matrix.Length; branchingIndex = checker.BranchIndex; level = checker.Level; hierarchicTree = new BitArray[level][]; // Initializing and filling data for each level, beginning from root. int nodeDataLength = branchingIndex * (branchingIndex - 1) / 2; int[] nIndexes = new int[nodeDataLength]; int[] mIndexes = new int[nodeDataLength]; for (int gamma = level; gamma > 0; --gamma) { // get current level data length and bitArrays count long levelDataLength = Convert.ToInt64(Math.Pow(branchingIndex, gamma - 1) * nodeDataLength); int arrayCountForLevel = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(levelDataLength) / ARRAY_MAX_SIZE)); hierarchicTree[gamma - 1] = new BitArray[arrayCountForLevel]; int j = 0; for (; j < arrayCountForLevel - 1; j++) { hierarchicTree[gamma - 1][j] = new BitArray(ARRAY_MAX_SIZE); } hierarchicTree[gamma - 1][j] = new BitArray(Convert.ToInt32(levelDataLength - (arrayCountForLevel - 1) * ARRAY_MAX_SIZE)); // fills data for current level nodes // loop over all elements of given level and fill him values int lim1 = branchingIndex - 1, lim2 = 0, nt = 0; nIndexes[0] = 0; for (int nIndex = 1; nIndex < branchingIndex; ++nIndex) { while (nt < lim1) { if (nIndex == 1) { nIndexes[nt] = nIndexes[lim2]; } else { nIndexes[nt] = nIndexes[lim2] + Convert.ToInt32(Math.Pow(branchingIndex, level - gamma)); } ++nt; } lim2 = lim1 - 1; lim1 = lim1 + branchingIndex - 1 - nIndex; } mIndexes[0] = Convert.ToInt32(Math.Pow(branchingIndex, level - gamma)); int mt = 0; int fIndex = 1; while (mt < nodeDataLength) { for (int i = fIndex; i <= branchingIndex - 1; ++i) { mIndexes[mt] = i * mIndexes[0]; ++mt; } ++fIndex; } for (int f = 0; f < hierarchicTree[gamma - 1].Length; f++) { for (int g = 0; g < hierarchicTree[gamma - 1][f].Length; g++) { int currentIndex = g % nodeDataLength; int add = Convert.ToInt32((g / nodeDataLength)) * Convert.ToInt32(Math.Pow(branchingIndex, level - gamma + 1)); hierarchicTree[gamma - 1][f][g] = matrixInList[nIndexes[currentIndex] + add][mIndexes[currentIndex] + add]; } } } } }