static void Main(string[] args) { SquareMatrix2d wm = new SquareMatrix2d("g46.txt"); SquareMatrix2d minSpanTree = Algorithms.MakeMinSpannedTree(wm); SquareMatrix2d K = Algorithms.MakeKirchhoffMatrix(wm); double det = new SquareMatrix2d(K.Cofactor(0, 0)).Determinant(); Console.WriteLine("Source matrix\n"); Console.WriteLine(wm); Console.WriteLine("Kirchhoff matrix\n"); Console.WriteLine(K.ToString()); Console.WriteLine("determinant of Kirchhoff matrix: {0}\n", det); Console.WriteLine("Min spanned tree matrix\n"); Console.WriteLine(minSpanTree); Console.ReadKey(); }
/// <summary> /// Calculate a determinant of square matrix /// http://en.wikipedia.org/wiki/Determinant /// </summary> /// <returns>det of matrix</returns> public double Determinant() { double det = 1; double EPS = 0.00000000001; SquareMatrix2d m = new SquareMatrix2d(this); int n = XSize; for (int i = 0; i < n; i++) { int k = i; for (int j = i + 1; j < n; ++j) if (Math.Abs(m[j, i]) > Math.Abs(m[k, i])) k = j; if (Math.Abs(m[k, i]) < EPS) return 0; for (int it = 0; it < n; ++it) { double temp = m[i, it]; m[i, it] = m[k, it]; m[k, it] = temp; } if (i != k) det = -det; det *= m[i, i]; for (int j = i + 1; j < n; ++j) m[i, j] = m[i, j] / m[i, i]; for (int j = 0; j < n; ++j) if (j != i && Math.Abs(m[j, i]) > EPS) for (k = i + 1; k < n; ++k) m[j, k] = m[j, k] - (m[i, k] * m[j, i]); } return det; }
public SquareMatrix2d(SquareMatrix2d m) : base(m) { }
private void UpdateGraphInfo() { SquareMatrix2d K = Algorithms.MakeKirchhoffMatrix(srcMatrixModel); double det = new SquareMatrix2d(K.Cofactor(0, 0)).Determinant(); statusBar.Text = "this graph contains the " + det.ToString() + " spanned trees"; }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { if (openMatrixFile.ShowDialog() == DialogResult.OK) { srcMatrixModel = new SquareMatrix2d(openMatrixFile.FileName); srcMatrixView = InitSrcMatrixView(srcMatrixModel, srcMatrixView); } }
private void newToolStripMenuItem_Click(object sender, EventArgs e) { NewGraphCreateDialog creator = new NewGraphCreateDialog(); creator.Owner = this; creator.ShowDialog(); if (IntData.Value >= 2) { srcMatrixModel = new SquareMatrix2d(IntData.Value, 0); srcMatrixView = InitSrcMatrixView(srcMatrixModel, srcMatrixView); } this.Refresh(); }
private void makeMinSpannedTreeToolStripMenuItem_Click(object sender, EventArgs e) { minSpannedTreeModel = MakeMinSpannedTree(srcMatrixView); spannedTreeView = InitSpannedTreeView(minSpannedTreeModel, spannedTreeView); }
private SquareMatrix2d MakeMinSpannedTree(DataGridView matrixView) { matrixView = srcMatrixView; SquareMatrix2d m = new SquareMatrix2d(matrixView.ColumnCount, 0.0); SquareMatrix2d tree = new SquareMatrix2d(matrixView.ColumnCount, 0.0); for (int i = 0; i < m.XSize; i++) { for (int j = 0; j < m.YSize; j++) { m[i, j] = Convert.ToDouble(matrixView.Rows[i].Cells[j].Value); } } tree = Algorithms.MakeMinSpannedTree(m); return tree; }