Ejemplo n.º 1
0
        // only works right for a directed simple graph, that is
        // there is at most one edge between any two vertices
        public IntMatrix CreateDirectedIncidenceMatrix()
        {
            // for first we need to know how many edges there are in the graph
            // somehow label the edges
            var edges     = new Dictionary <(int, int), int>();
            int edgeCount = 0;

            for (int i = 0; i < m_nodes.Length; i++)
            {
                foreach (int j in m_nodes[i].connections)
                {
                    edges.Add((i, j), edgeCount);
                    edgeCount++;
                }
            }

            // now construct the matrix where:
            // height = # of verteces
            // width = # of edges
            IntMatrix result = new IntMatrix(m_nodes.Length, edgeCount);

            foreach (var((from, to), i) in edges)
            {
                result[from, i] = 1;
                result[to, i]   = -1;
            }

            return(result);
        }
Ejemplo n.º 2
0
        // basically diplicate logic for the default list graph representation I used
        public static IntMatrix ConvertAdjacencyToUndirectedIncidence(IntMatrix adjacency)
        {
            // for first we need to know how many edges there are in the graph
            // somehow label the edges
            var edges     = new Dictionary <(int, int), int>();
            int edgeCount = 0;

            for (int i = 0; i < adjacency.Height; i++)
            {
                for (int j = 0; j < adjacency.Width; j++)
                {
                    if (adjacency[i, j] == 1 && edges.ContainsKey((j, i)) == false)
                    {
                        edges.Add((i, j), edgeCount);
                        edgeCount++;
                    }
                }
            }

            // now construct the matrix where:
            // height = # of verteces
            // width = # of edges
            IntMatrix result = new IntMatrix(adjacency.Height, edgeCount);

            foreach (var((from, to), i) in edges)
            {
                result[from, i] = 1;
                result[to, i]   = 1;
            }

            return(result);
        }
Ejemplo n.º 3
0
        public static IntMatrix ConvertAdjacencyToDirectedIncidence(IntMatrix adjacency)
        {
            // for first we need to know how many edges there are in the graph
            // somehow label the edges
            var edges = new List <(int, int)>();

            for (int i = 0; i < adjacency.Height; i++)
            {
                for (int j = 0; j < adjacency.Width; j++)
                {
                    if (adjacency[i, j] == 1)
                    {
                        edges.Add((i, j));
                    }
                }
            }

            // now construct the matrix where:
            // height = # of verteces
            // width = # of edges
            IntMatrix result = new IntMatrix(adjacency.Height, edges.Count);

            for (int i = 0; i < edges.Count; i++)
            {
                var(from, to)   = edges[i];
                result[from, i] = 1;
                result[to, i]   = -1;
            }

            return(result);
        }
Ejemplo n.º 4
0
        public static IntMatrix ConvertDirectedIncidenceToAdjacency(IntMatrix incidence)
        {
            var result = new IntMatrix(incidence.Height, incidence.Height);

            for (int i = 0; i < incidence.Width; i++)
            {
                // we need to initialize the values anyway
                int from = 0, to = 0;

                for (int j = 0; j < incidence.Height; j++)
                {
                    if (incidence[j, i] == 1)
                    {
                        from = j;
                    }
                    else if (incidence[j, i] == -1)
                    {
                        to = j;
                    }
                }
                result[from, to] = 1;
                // result[to, from] = -1;
            }
            return(result);
        }
Ejemplo n.º 5
0
        public static IntMatrix ConvertKirchhoffToAdjacency(IntMatrix kirchhoff)
        {
            var result = -kirchhoff;

            for (int i = 0; i < result.Height; i++)
            {
                result[i, i] = 0;
            }
            return(result);
        }
Ejemplo n.º 6
0
        public IntMatrix Fill(int value)
        {
            IntMatrix result = new IntMatrix(Height, Width);

            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    result.m_numbers[i, j] = value;
                }
            }
            return(result);
        }
Ejemplo n.º 7
0
        public static IntMatrix operator -(IntMatrix matrix)
        {
            var result = new IntMatrix(matrix.Height, matrix.Width);

            for (int i = 0; i < matrix.Height; i++)
            {
                for (int j = 0; j < matrix.Width; j++)
                {
                    result.m_numbers[i, j] = -matrix.m_numbers[i, j];
                }
            }
            return(result);
        }
Ejemplo n.º 8
0
        public IntMatrix CreateAdjacencyMatrix()
        {
            var result = new IntMatrix(m_nodes.Length, m_nodes.Length);

            for (int i = 0; i < m_nodes.Length; i++)
            {
                foreach (int j in m_nodes[i].connections)
                {
                    result[i, j] = 1;
                }
            }
            return(result);
        }
Ejemplo n.º 9
0
        /*
         *  The definition of Kirchoff for a directed graph is inconsistent
         *  https://www.wikiwand.com/en/Laplacian_matrix#/Definition
         *  By this definition, if i != j, the entry in the matrix is
         *  -1 if vi and vj are adjacent. Now, the way adjacency is defined
         *  for vertices doesn't state whether, in a directed graph, if there
         *  is a connection vi -> vj, vj is to be considered adjacent to vi.
         *  In my mind, it is not.
         *  However, when I apply the formula at
         *  https://www.wikiwand.com/en/Laplacian_matrix#/Incidence_matrix
         *  it seems to always give the result as though they were both adjacent,
         *  which is why this conversion diverges in its results from
         *  Incidence -> Kirchhoff conversion function (that one always results
         *  in a simmetric matrix).
         */
        public static IntMatrix ConvertAdjacencyToKirchhoff(IntMatrix adjacency)
        {
            var result = -adjacency;

            for (int i = 0; i < adjacency.Height; i++)
            {
                for (int j = 0; j < adjacency.Width; j++)
                {
                    result[i, i] += adjacency[i, j];
                }
            }
            return(result);
        }
Ejemplo n.º 10
0
        public IntMatrix CreateKirchhoffMatrix()
        {
            var result = new IntMatrix(m_nodes.Length, m_nodes.Length);

            for (int i = 0; i < m_nodes.Length; i++)
            {
                foreach (int j in m_nodes[i].connections)
                {
                    result[i, j] = -1;
                }

                result[i, i] = m_nodes[i].connections.Length;
            }
            return(result);
        }
Ejemplo n.º 11
0
        public static IntMatrix operator -(IntMatrix lhs, IntMatrix rhs)
        {
            if (lhs.Width != rhs.Width || lhs.Height != rhs.Height)
            {
                throw new System.Exception("Dimensions didn't match, couldn't subtract the two matrices");
            }
            var result = new IntMatrix(lhs.Height, lhs.Width);

            for (int i = 0; i < lhs.Height; i++)
            {
                for (int j = 0; j < lhs.Width; j++)
                {
                    result.m_numbers[i, j] = lhs.m_numbers[i, j] - rhs.m_numbers[i, j];
                }
            }
            return(result);
        }
Ejemplo n.º 12
0
        public IntMatrix Mult(IntMatrix mat)
        {
            if (Width != mat.Height)
            {
                throw new System.Exception("The width doesn't match the height of the matrix multiplying into");
            }
            var result = new IntMatrix(Height, mat.Width);

            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < mat.Width; j++)
                {
                    for (int k = 0; k < Width; k++)
                    {
                        result.m_numbers[i, j] += m_numbers[i, k] * mat.m_numbers[k, j];
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 13
0
        public static IntMatrix ConvertUndirectedIncidenceToAdjacency(IntMatrix incidence)
        {
            var        result      = new IntMatrix(incidence.Height, incidence.Height);
            List <int> currentEdge = new List <int>(2);

            for (int j = 0; j < incidence.Width; j++)
            {
                for (int i = 0; i < incidence.Height; i++)
                {
                    if (incidence[i, j] == 1)
                    {
                        currentEdge.Add(i);
                    }
                }
                result[currentEdge[0], currentEdge[1]] = 1;
                result[currentEdge[1], currentEdge[0]] = 1;
                currentEdge.Clear();
            }
            return(result);
        }
Ejemplo n.º 14
0
 public IntMatrix(IntMatrix matrix)
 {
     m_numbers = (int[, ])matrix.m_numbers.Clone();
 }
Ejemplo n.º 15
0
 // K = M_t * M
 public static IntMatrix ConvertDirectedIncidenceToKirchhoff(IntMatrix incidence)
 {
     return(incidence.Mult(incidence.Transpose));
 }