Esempio n. 1
0
        public void GraphTests_ShouldReturnException_ForInvalidVertix_Z()
        {
            var vertices = new int[] { 'A', 'B', 'C', 'D', 'E' };
            var graph    = new AdjacencyMatrix <int>(vertices);

            graph.AddEdge('A', 'B');
            graph.AddEdge('B', 'Z');
        }
Esempio n. 2
0
        public void AddEdgeTest()
        {
            var matrix = new AdjacencyMatrix(3);

            // Add edges
            Assert.True(matrix.AddEdge(0, 1));
            Assert.True(matrix.AddEdge(0, 2));
            Assert.True(matrix.AddEdge(1, 2));

            // Cannot add duplicate edges
            Assert.False(matrix.AddEdge(1, 2));
        }
Esempio n. 3
0
        public void GraphTests_ShowGive10Record()
        {
            var vertices = new int[] { 'A', 'B', 'C', 'D', 'E' };
            var graph    = new AdjacencyMatrix <int>(vertices);

            graph.AddEdge('A', 'B');
            graph.AddEdge('B', 'C');
            graph.AddEdge('C', 'D');
            graph.AddEdge('D', 'E');
            graph.AddEdge('E', 'A');

            Assert.AreEqual(10, graph.GetEdges().Count);
        }
Esempio n. 4
0
        public void ReflectiveEdgeTest()
        {
            var matrix = new AdjacencyMatrix(3);

            // Add edges
            Assert.True(matrix.AddEdge(0, 1));
            Assert.True(matrix.AddEdge(0, 2));
            Assert.True(matrix.AddEdge(1, 2));

            // Has Edges
            Assert.True(matrix.HasEdge(1, 0));
            Assert.True(matrix.HasEdge(2, 0));
            Assert.True(matrix.HasEdge(2, 1));
        }
Esempio n. 5
0
        public void HasEdgeTest()
        {
            var matrix = new AdjacencyMatrix(3);

            matrix.AddEdge(0, 1);
            matrix.AddEdge(0, 2);
            matrix.AddEdge(1, 2);

            Assert.True(matrix.HasEdge(0, 1));
            Assert.True(matrix.HasEdge(0, 2));
            Assert.True(matrix.HasEdge(1, 2));
            // Out of bounds edge
            Assert.False(matrix.HasEdge(3, 1));
        }
Esempio n. 6
0
        public void RemoveEdgeTest()
        {
            var matrix = new AdjacencyMatrix(3);

            matrix.AddEdge(0, 1);
            matrix.AddEdge(0, 2);
            matrix.AddEdge(1, 2);

            Assert.True(matrix.RemoveEdge(0, 1));
            Assert.True(matrix.RemoveEdge(0, 2));
            Assert.True(matrix.RemoveEdge(1, 2));
            // Cannot remove again
            Assert.False(matrix.RemoveEdge(0, 1));
        }
Esempio n. 7
0
        public void Add(Edge edge, Vertex vertex1, Vertex vertex2)
        {
            Edges?.Add(edge);

            //добавить изменние в матрице инциндетности
            AdjacencyMatrix.AddEdge(vertex1.Id - 1, vertex2.Id - 1);
        }
Esempio n. 8
0
        public void GraphTests_ShowGive25Records()
        {
            var vertices = new int[] { 'A', 'B', 'C', 'D', 'E' };
            var graph    = new AdjacencyMatrix <int>(vertices);

            for (int i = 0; i < vertices.Length; i++)
            {
                for (int j = 0; j < vertices.Length; j++)
                {
                    graph.AddEdge(vertices[i], vertices[j]);
                }
            }

            Assert.AreEqual(25, graph.GetEdges().Count);

            var matrix = graph.GetAdjacencyMatrix();
            var length = matrix.GetUpperBound(0);

            for (int i = 0; i <= length; i++)
            {
                for (int j = 0; j <= length; j++)
                {
                    Trace.Write($"{matrix[i, j]} ");
                }
                Trace.WriteLine(string.Empty);
            }
        }
Esempio n. 9
0
        public void OutEdgesTest()
        {
            var matrix = new AdjacencyMatrix(3);

            matrix.AddEdge(0, 1);
            matrix.AddEdge(0, 2);
            matrix.AddEdge(1, 2);

            // Outgoing Edges
            Assert.Equal(new List <int>()
            {
                1, 2
            }, matrix.OutEdges(0));
            Assert.Equal(new List <int>()
            {
                0, 2
            }, matrix.OutEdges(1));
            Assert.Equal(new List <int>()
            {
                0, 1
            }, matrix.OutEdges(2));
        }
Esempio n. 10
0
        public void GraphTests_PrintAdjacencyMatrix()
        {
            var vertices = new int[] { 'A', 'B', 'C', 'D', 'E' };
            var graph    = new AdjacencyMatrix <int>(vertices);

            graph.AddEdge('A', 'B');
            graph.AddEdge('B', 'C');
            graph.AddEdge('C', 'D');
            graph.AddEdge('D', 'E');
            graph.AddEdge('E', 'A');

            var matrix = graph.GetAdjacencyMatrix();
            var length = matrix.GetUpperBound(0);

            for (int i = 0; i <= length; i++)
            {
                for (int j = 0; j <= length; j++)
                {
                    Trace.Write($"{matrix[i, j]} ");
                }
                Trace.WriteLine(string.Empty);
            }
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            AdjacencyMatrix <char> t = new AdjacencyMatrix <char>((u, v) => u.CompareTo(v), true);

            t.AddVertex('s');
            t.AddVertex('t');
            t.AddVertex('x');
            t.AddVertex('y');
            t.AddVertex('z');

            t.AddEdge('s', 't', 10);
            t.AddEdge('s', 'y', 5);

            t.AddEdge('t', 'x', 1);
            t.AddEdge('t', 'y', 2);

            t.AddEdge('x', 'z', 4);

            t.AddEdge('y', 't', 3);
            t.AddEdge('y', 'z', 2);
            t.AddEdge('y', 'x', 9);

            t.AddEdge('z', 'x', 6);

            AdjacencyList <char> l = (AdjacencyList <char>)t;

            Console.WriteLine(t);
            Console.WriteLine(l);

            Console.WriteLine(TreeAlgorithms <char> .Dijkstra(t, 's'));
            Console.WriteLine((AdjacencyList <char>)TreeAlgorithms <char> .Dijkstra(l, 's'));

            TreeAlgorithms <char> .BellmanFord(t, 's', out Graph <char> to);

            TreeAlgorithms <char> .BellmanFord(t, 's', out Graph <char> lo);

            AdjacencyList <char> ttt = (AdjacencyMatrix <char>)lo;

            Console.WriteLine(to);
            Console.WriteLine(ttt);
        }