Dense - Degree Matrix and Variations

The degree matrix is defined as a diagonal matrix which contains information about the degree of each vertex — that is, the number of edges attached to each vertex (Wikipedia). The degree matrix can be easily defined using Mendz.Graphs..DenseGraphMatrixBase.

The degree matrices D can be used with an adjacency matrix A to create a Laplacian adjacency matrix L = D - A.

DegreeMatrix

By definition:

DegreeMatrix.cs
using Mendz.Library;
using System;

namespace Mendz.Graphs.Representations.Matrices.Dense
{
    public class DegreeMatrix : DenseGraphMatrixBase<int>
    {
        public DegreeMatrix(Graph graph)
            : base(graph, (graph.Order, graph.Order))
        {

        }

        public override void Initialize()
        {
            Matrix = SquareMatrix<int>.Create(Size.rows);
        }

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

        protected virtual void SetEntries(int[,] matrix, int x, int y, bool directed)
        {
            matrix[x, x] += 1;
            matrix[y, y] += 1;
        }
    }
}

Note that the DegreeMatrix does not care if the edge is directed or undirected.

InDegreeMatrix

In a directed graph, the indegree matrix counts the edges that "points" in to a vertex.

InDegreeMatrix.cs
using System;

namespace Mendz.Graphs.Representations.Matrices.Dense
{
    public class InDegreeMatrix : DegreeMatrix
    {
        public InDegreeMatrix(Graph graph)
            : base(graph)
        {

        }

        protected override void SetEntries(int[,] matrix, int x, int y, bool directed)
        {
            if (!directed)
            {
                throw new InvalidOperationException("Undirected edges are not supported.");
            }
            matrix[y, y] += 1;
        }
    }
}

Note that it is invalid to use InDegreeMatrix with undirected graphs (or mixed graphs containing undirected edges).

OutDegreeMatrix

In a directed graph, the outdegree matrix counts the edges that "points" out from a vertex.

OutDegreeMatrix.cs
using System;

namespace Mendz.Graphs.Representations.Matrices.Dense
{
    public class OutDegreeMatrix : DegreeMatrix
    {
        public OutDegreeMatrix (Graph graph)
            : base(graph)
        {

        }

        protected override void SetEntries(int[,] matrix, int x, int y, bool directed)
        {
            if (!directed)
            {
                throw new InvalidOperationException("Undirected edges are not supported.");
            }
            matrix[x, x] += 1;
        }
    }
}

Note that it is invalid to use OutDegreeMatrix with undirected graphs (or mixed graphs containing undirected edges).

Comments