ConnectionMatrix

The connection matrix is a (true, false, false)-adjacency matrix, implemented in Mendz.Graphs.Representation.Matrices namespace as the ConnectionMatrix.

The ConnectionMatrix is a sealed class that derives directly from the GraphMatrixBase class.

ConnectionMatrix.cs
using Mendz.Library;
using System;
using System.Collections.Generic;

namespace Mendz.Graphs.Representations.Matrices
{
    public sealed class ConnectionMatrix 
        : GraphMatrixBase<HashSet<(int row, int column)>>
    {
        public bool this[int row, int column]
        {
            get
            {
                Matrix<bool>.CheckCoordinates(Size, row, column);
                return Matrix.Contains((row, column));
            }
        }

        public ConnectionMatrix(Graph graph)
            : base(graph, (graph.Order, graph.Order))
        {

        }

        public override void Initialize()
        {
            Matrix = new HashSet<(int row, int column)>();
        }

        public override HashSet<(int row, int column)> Fill()
        {
            Initialize();
            var indexedVertices = Graph.IndexVertices();
            var indexedEdges = Graph.IndexEdges();
            for (int z = 0; z < indexedEdges.Length; z++)
            {
                Edge edge = indexedEdges[z];
                SetEntries(
                    Array.BinarySearch<Vertex>(indexedVertices, edge.Tail),
                    Array.BinarySearch<Vertex>(indexedVertices, edge.Head),
                    edge.Directed);
            }
            return Matrix;
        }

        private void SetEntries(int x, int y, bool directed)
        {
            Matrix.Add((x, y));
            if (x != y)
            {
                if (!directed)
                {
                    Matrix.Add((y, x));
                }
            }
        }
    }
}

The ConnectionMatrix uses a HashSet of coordinates stored as (int row, int column) tuples.

Note that the indexer has been overridden to simulate matrix-like access to the entries (ex. matrix[i, j]).

The connection matrix can be used in solutions that apply Boolean algebra with linear algebra's Matrix Theory, for example. In mathematics and mathematical logic, Boolean algebra is the branch in algebra where the variables are true or false, instead of 1 or 0 respectively. The following lists some online sources that can help you learn more about Boolean algebra: Remember that Mendz.Graphs is not intended to be a Math library. Mendz.Graphs is all about graph data and representing them. Thus, the focus of Mendz.Graphs.Representations.Matrices is how that "rectangular array" mentioned in the matrix definition can be created, initialized and filled with graph data. The matrices that you create using Mendz.Graphs can be passed to Math libraries that support operations with matrices. There are many .Net compatible Math/numerical libraries available like Math.NET, ALGLIB and MATLAB. See also this list from Wikipedia. A quick search works to find them, too!

Mendz.Graphs intentionally does not include algorithms. I encourage developers to build their own algorithm library or libraries based on Mendz.Graphs.

Comments