public void UndirectedGetNeighborsPointingToReciprocalTest()
        {
            MyGraph <string> graph = new MyGraph <string>();

            graph.AddVertex("A");
            graph.AddVertex("B");

            Vertex <string> vertRef  = null;
            Vertex <string> vertRef2 = null;

            foreach (var pair in graph.AdjacencyList)
            {
                if (pair.Key.Data == "A")
                {
                    vertRef = pair.Key;
                }
                if (pair.Key.Data == "B")
                {
                    vertRef2 = pair.Key;
                }
            }

            graph.AddUndirectedEdge(vertRef, vertRef2, 1);

            foreach (var pair in graph.AdjacencyList)
            {
                if (pair.Key.Data == "B")
                {
                    List <Tuple <Vertex <string>, int> > neighbors = graph.InDegree(pair.Key);
                    Assert.Equal("A", neighbors[0].Item1.Data);
                }
            }
        }
        public void GraphCounter2()
        {
            MyGraph <string> graph = new MyGraph <string>();

            graph.AddVertex("A");
            graph.AddVertex("A");
            Assert.Equal(2, graph._size);
        }
Beispiel #3
0
        public void CreateNewSwitch()
        {
            if (_graph.Vertices.Any(vertex => vertex.Name == NewSwitchName))
            {
                return;
            }

            _graph.AddVertex(new Switch(NewSwitchName, Generated.False));
            OnPropertyChanged("SwitchNames");
        }
        public void GetVertices(string a, int b)
        {
            MyGraph <string> graph = new MyGraph <string>();

            graph.AddVertex("A");
            graph.AddVertex("B");
            graph.AddVertex("C");
            List <Vertex <string> > list = graph.GetVertices();

            Assert.Equal(list[b].Data, a);
        }
Beispiel #5
0
        public void Should_AddVertex_Throw_If_Out_Of_Range()
        {
            //arrange
            var graph = new MyGraph <int>(5);

            //act
            Action actLower  = () => graph.AddVertex(-1, 1);
            Action actHigher = () => graph.AddVertex(6, 1);

            //assert
            actLower.ShouldThrow <ArgumentOutOfRangeException>();
            actHigher.ShouldThrow <ArgumentOutOfRangeException>();

            graph.Capacity.ShouldBeEquivalentTo(5);
            graph.Count.ShouldBeEquivalentTo(0);
        }
        private MyGraph MyGraph_Setup(DeterministicFiniteAutomaton dfa)
        {
            var dataGraph = new MyGraph();

            for (uint i = 0; i < dfa.States; i++)
            {
                var dataVertex = new DataVertex("q" + i.ToString())
                {
                    ID = i
                };
                dataGraph.AddVertex(dataVertex);
            }

            var vlist = dataGraph.Vertices.ToList();

            for (uint i = 0; i < dfa.States; i++)
            {
                for (uint j = 0; j < dfa.Alphabet; j++)
                {
                    var dataEdge = new DataEdge(vlist[(int)i], vlist[(int)dfa.GetNextState(i, j)])
                    {
                        Text = j.ToString()
                    };
                }
            }

            return(dataGraph);
        }
        public void DirectedGetAllNeighbors()
        {
            MyGraph <string> graph = new MyGraph <string>();

            var a = graph.AddVertex("a");
            var b = graph.AddVertex("b");
            var c = graph.AddVertex("c");
            var d = graph.AddVertex("d");

            graph.AddDirectedEdge(d, a, 5);
            graph.AddDirectedEdge(a, b, 5);
            graph.AddDirectedEdge(a, c, 5);

            List <Tuple <Vertex <string>, int> > pointingToA = graph.GetNeighborsDirected(a);

            Assert.Equal(3, pointingToA.Count);
        }
Beispiel #8
0
        public void RouteBetweenNodes_Should_Check_False()
        {
            //arrange
            var graph = new MyGraph <int>(7);

            graph.AddVertex(0, 0);
            graph.AddVertex(1, 1);
            graph.AddVertex(2, 2);
            graph.AddVertex(3, 3);
            graph.AddVertex(4, 4);
            graph.AddVertex(5, 5);
            graph.AddVertex(6, 6);

            graph.AddEdge(0, 4);
            graph.AddEdge(1, 4);
            graph.AddEdge(2, 5);
            graph.AddEdge(3, 5);
            graph.AddEdge(4, 0);
            graph.AddEdge(4, 1);
            graph.AddEdge(5, 2);
            graph.AddEdge(5, 3);

            //act
            var result = _treesGraphs.RouteBetweenNodes(graph, 0, 3);

            //assert
            result.ShouldBeEquivalentTo(false);
        }
        public void DirectedGetNeighborsPointingFromC()
        {
            MyGraph <string> graph = new MyGraph <string>();

            var a = graph.AddVertex("a");
            var b = graph.AddVertex("b");
            var c = graph.AddVertex("c");
            var d = graph.AddVertex("d");

            graph.AddDirectedEdge(a, b, 5);
            graph.AddDirectedEdge(a, c, 5);
            graph.AddDirectedEdge(a, d, 5);
            graph.AddDirectedEdge(c, a, 1);

            List <Tuple <Vertex <string>, int> > pointingToC = graph.OutDegree(c);

            Assert.Single(pointingToC);
        }
Beispiel #10
0
        public void BuildOrder_Should_Check_For_Cycle()
        {
            //arrange
            var graph = new MyGraph <char>(6);

            graph.AddVertex(0, 'a');
            graph.AddVertex(1, 'b');
            graph.AddVertex(2, 'c');

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

            //act
            Action act = () => _treesGraphs.BuildOrder(graph);

            //assert
            act.ShouldThrow <ArgumentException>();
        }
        public void AddVert()
        {
            MyGraph <string> graph = new MyGraph <string>();

            graph.AddVertex("123");
            foreach (var pair in graph.AdjacencyList)
            {
                Assert.Equal("123", pair.Key.Data);
            }
        }
Beispiel #12
0
        public void Should_AddVertex()
        {
            //arrange
            var graph = new MyGraph <int>(5);

            //act
            graph.AddVertex(0, 1);

            //assert
            graph.Capacity.ShouldBeEquivalentTo(5);
            graph.Count.ShouldBeEquivalentTo(1);
        }
        public void ClusteringCoefficientUndirectedFullConnection()
        {
            MyGraph <string> graph = new MyGraph <string>();

            var aa = graph.AddVertex("aa");
            var bb = graph.AddVertex("bb");
            var cc = graph.AddVertex("cc");
            var dd = graph.AddVertex("dd");

            graph.AddUndirectedEdge(aa, bb, 5);
            graph.AddUndirectedEdge(aa, cc, 5);
            graph.AddUndirectedEdge(aa, dd, 5);

            graph.AddUndirectedEdge(bb, cc, 5);
            graph.AddUndirectedEdge(cc, dd, 5);
            graph.AddUndirectedEdge(dd, bb, 5);

            decimal output = graph.ClusteringCoefficientUndirected(aa);

            Assert.Equal(1.ToString(), Math.Round(output, 2).ToString());
        }
Beispiel #14
0
        public void BuildOrder_Should_Check_Order_Reversed()
        {
            //arrange
            var graph = new MyGraph <char>(6);

            graph.AddVertex(0, 'a');
            graph.AddVertex(1, 'b');
            graph.AddVertex(2, 'c');
            graph.AddVertex(3, 'd');
            graph.AddVertex(4, 'e');
            graph.AddVertex(5, 'f');

            graph.AddEdge(5, 0);
            graph.AddEdge(4, 0);
            graph.AddEdge(4, 0);
            graph.AddEdge(3, 1);
            graph.AddEdge(5, 2);
            graph.AddEdge(2, 3);

            var order = new[] { 'e', 'f', 'a', 'c', 'd', 'b' };

            //act
            var result = _treesGraphs.BuildOrder(graph).ToArray();

            //assert
            result.Length.ShouldBeEquivalentTo(order.Length);

            for (int i = 0; i < result.Length; i++)
            {
                result[i].ShouldBeEquivalentTo(order[i]);
            }
        }
Beispiel #15
0
        public void Should_Remove()
        {
            //arrange
            var graph = new MyGraph <int>(4);

            graph.AddVertex(0, 0);
            graph.AddVertex(1, 1);
            graph.AddVertex(2, 2);
            graph.AddVertex(3, 3);

            graph.AddEdge(0, 2);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 0);
            graph.AddEdge(2, 1);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 2);

            //act
            graph.Remove(2);

            var    first  = graph.BreadthFirstSearch(0).ToArray();
            var    second = graph.BreadthFirstSearch(1).ToArray();
            var    third  = graph.BreadthFirstSearch(3).ToArray();
            Action act    = () => graph.BreadthFirstSearch(2).ToArray();

            //assert
            first.Length.ShouldBeEquivalentTo(1);
            second.Length.ShouldBeEquivalentTo(1);
            third.Length.ShouldBeEquivalentTo(1);

            first[0].ShouldBeEquivalentTo(0);
            second[0].ShouldBeEquivalentTo(1);
            third[0].ShouldBeEquivalentTo(3);

            act.ShouldThrow <ArgumentException>();

            graph.Capacity.ShouldBeEquivalentTo(4);
            graph.Count.ShouldBeEquivalentTo(3);
        }
Beispiel #16
0
        public void BuildOrder_Should_Check_Length_One()
        {
            //arrange
            var graph = new MyGraph <char>(6);

            graph.AddVertex(0, 'a');

            //act
            var result = _treesGraphs.BuildOrder(graph).ToArray();

            //assert
            result.Length.ShouldBeEquivalentTo(1);
            result[0].ShouldBeEquivalentTo('a');
        }
Beispiel #17
0
        public void Should_DepthFirstSearch_Length_One()
        {
            //arrange
            var graph = new MyGraph <int>(6);

            graph.AddVertex(0, 0);

            //act
            var result = graph.DepthFirstSearch(0).ToArray();

            //assert
            result.Length.ShouldBeEquivalentTo(1);
            result[0].ShouldBeEquivalentTo(0);

            graph.Capacity.ShouldBeEquivalentTo(6);
            graph.Count.ShouldBeEquivalentTo(1);
        }
Beispiel #18
0
        public void Should_BidirectionalSearch_Throw_If_Node_Is_Null()
        {
            //arrange
            var graph = new MyGraph <int>(5);

            graph.AddVertex(0, 0);

            //act
            Action actFrom = () => graph.BidirectionalSearch(1, 0).ToArray();
            Action actTo   = () => graph.BidirectionalSearch(0, 1).ToArray();

            //assert
            actFrom.ShouldThrow <ArgumentException>();
            actTo.ShouldThrow <ArgumentException>();

            graph.Capacity.ShouldBeEquivalentTo(5);
            graph.Count.ShouldBeEquivalentTo(1);
        }
Beispiel #19
0
        public void Should_BidirectionalSearch_Length_One()
        {
            //arrange
            var graph = new MyGraph <int>(1);

            graph.AddVertex(0, 0);

            //act
            var result = graph.BidirectionalSearch(0, 0);

            //assert
            result[0].Count().ShouldBeEquivalentTo(1);
            result[1].Count().ShouldBeEquivalentTo(1);

            result[0].ElementAt(0).ShouldBeEquivalentTo(0);
            result[1].ElementAt(0).ShouldBeEquivalentTo(0);

            graph.Capacity.ShouldBeEquivalentTo(1);
            graph.Count.ShouldBeEquivalentTo(1);
        }
Beispiel #20
0
        public void Should_RemoveEdge()
        {
            //arrange
            var graph = new MyGraph <int>(6);

            graph.AddVertex(0, 0);
            graph.AddVertex(1, 1);
            graph.AddVertex(2, 2);
            graph.AddVertex(3, 3);
            graph.AddVertex(4, 4);
            graph.AddVertex(5, 5);

            graph.AddEdge(0, 5);
            graph.AddEdge(0, 1);
            graph.AddEdge(0, 4);
            graph.AddEdge(1, 3);
            graph.AddEdge(1, 4);
            graph.AddEdge(2, 1);
            graph.AddEdge(3, 2);
            graph.AddEdge(3, 4);

            var resBfs = new[] { 0, 5, 4 };

            //act
            graph.RemoveEdge(0, 1);
            var result = graph.BreadthFirstSearch(0).ToArray();

            //assert
            result.Length.ShouldBeEquivalentTo(resBfs.Length);
            for (int i = 0; i < result.Length; i++)
            {
                result[i].ShouldBeEquivalentTo(resBfs[i]);
            }

            graph.Capacity.ShouldBeEquivalentTo(6);
            graph.Count.ShouldBeEquivalentTo(6);
        }
        static void Main(string[] args)
        {
            MyGraph <string> graph = new MyGraph <string>();

            var a = graph.AddVertex("a");
            var b = graph.AddVertex("b");
            var c = graph.AddVertex("c");
            var d = graph.AddVertex("d");
            var e = graph.AddVertex("e");
            var f = graph.AddVertex("f");
            var g = graph.AddVertex("g");

            graph.AddUndirectedEdge(a, b, 5);
            graph.AddUndirectedEdge(b, c, 5);
            graph.AddUndirectedEdge(c, d, 5);
            graph.AddUndirectedEdge(d, e, 5);
            graph.AddUndirectedEdge(e, f, 5);
            graph.AddUndirectedEdge(f, g, 5);
            graph.AddUndirectedEdge(g, a, 5);

            graph.AddDirectedEdge(a, c, 1);


            graph.Print();

            //graph.GetNeighborsPointingTo(a);
            //graph.GetNeighborsPointingFrom(a);
            //both run by:
            graph.GetNeighborsDirected(a);

            MyGraph <string> graph1 = new MyGraph <string>();

            var j = graph1.AddVertex("A");
            var k = graph1.AddVertex("B");
            var l = graph1.AddVertex("C");
            var m = graph1.AddVertex("D");
            var n = graph1.AddVertex("E");
            var o = graph1.AddVertex("F");

            graph1.AddUndirectedEdge(j, k, 5);
            graph1.AddUndirectedEdge(k, l, 5);
            graph1.AddUndirectedEdge(k, m, 5);
            graph1.AddUndirectedEdge(l, n, 5);
            graph1.AddUndirectedEdge(l, o, 5);
            graph1.AddUndirectedEdge(m, o, 5);

            graph1.BreadthFirst(j);

            MyGraph <string> graph2 = new MyGraph <string>();

            var aa = graph2.AddVertex("aa");
            var bb = graph2.AddVertex("bb");
            var cc = graph2.AddVertex("cc");
            var dd = graph2.AddVertex("dd");
            var ee = graph2.AddVertex("ee");

            graph2.AddUndirectedEdge(aa, bb, 5);
            graph2.AddUndirectedEdge(aa, cc, 5);
            graph2.AddUndirectedEdge(aa, dd, 5);

            graph2.AddUndirectedEdge(bb, cc, 5);
            graph2.AddUndirectedEdge(cc, dd, 5);
            graph2.AddUndirectedEdge(dd, bb, 5);

            Console.WriteLine(graph2.ClusteringCoefficientUndirected(aa));
        }
Beispiel #22
0
        public void Should_BidirectionalSearch_False()
        {
            //arrange
            var graph = new MyGraph <int>(15);

            graph.AddVertex(0, 0);
            graph.AddVertex(1, 1);
            graph.AddVertex(2, 2);
            graph.AddVertex(3, 3);
            graph.AddVertex(4, 4);
            graph.AddVertex(5, 5);
            graph.AddVertex(6, 6);

            graph.AddVertex(8, 8);
            graph.AddVertex(9, 9);
            graph.AddVertex(10, 10);
            graph.AddVertex(11, 11);
            graph.AddVertex(12, 12);
            graph.AddVertex(13, 13);
            graph.AddVertex(14, 14);

            graph.AddEdge(0, 4);
            graph.AddEdge(1, 4);
            graph.AddEdge(2, 5);
            graph.AddEdge(3, 5);
            graph.AddEdge(4, 0);
            graph.AddEdge(4, 1);
            graph.AddEdge(4, 6);
            graph.AddEdge(5, 2);
            graph.AddEdge(5, 3);
            graph.AddEdge(5, 6);
            graph.AddEdge(6, 4);
            graph.AddEdge(6, 5);
            graph.AddEdge(8, 9);
            graph.AddEdge(8, 10);
            graph.AddEdge(9, 8);
            graph.AddEdge(9, 11);
            graph.AddEdge(9, 12);
            graph.AddEdge(10, 8);
            graph.AddEdge(10, 13);
            graph.AddEdge(10, 14);
            graph.AddEdge(11, 9);
            graph.AddEdge(12, 9);
            graph.AddEdge(13, 10);
            graph.AddEdge(14, 10);

            //act
            var result = graph.BidirectionalSearch(1, 14);

            //assert
            result.ShouldBeEquivalentTo(null);

            graph.Capacity.ShouldBeEquivalentTo(15);
            graph.Count.ShouldBeEquivalentTo(14);
        }
Beispiel #23
0
        public void Should_BidirectionalSearch()
        {
            //arrange
            var graph = new MyGraph <int>(15);

            graph.AddVertex(0, 0);
            graph.AddVertex(1, 1);
            graph.AddVertex(2, 2);
            graph.AddVertex(3, 3);
            graph.AddVertex(4, 4);
            graph.AddVertex(5, 5);
            graph.AddVertex(6, 6);
            graph.AddVertex(7, 7);
            graph.AddVertex(8, 8);
            graph.AddVertex(9, 9);
            graph.AddVertex(10, 10);
            graph.AddVertex(11, 11);
            graph.AddVertex(12, 12);
            graph.AddVertex(13, 13);
            graph.AddVertex(14, 14);

            graph.AddEdge(0, 4);
            graph.AddEdge(1, 4);
            graph.AddEdge(2, 5);
            graph.AddEdge(3, 5);
            graph.AddEdge(4, 0);
            graph.AddEdge(4, 1);
            graph.AddEdge(4, 6);
            graph.AddEdge(5, 2);
            graph.AddEdge(5, 3);
            graph.AddEdge(5, 6);
            graph.AddEdge(6, 4);
            graph.AddEdge(6, 5);
            graph.AddEdge(6, 7);
            graph.AddEdge(7, 6);
            graph.AddEdge(7, 8);
            graph.AddEdge(8, 7);
            graph.AddEdge(8, 9);
            graph.AddEdge(8, 10);
            graph.AddEdge(9, 8);
            graph.AddEdge(9, 11);
            graph.AddEdge(9, 12);
            graph.AddEdge(10, 8);
            graph.AddEdge(10, 13);
            graph.AddEdge(10, 14);
            graph.AddEdge(11, 9);
            graph.AddEdge(12, 9);
            graph.AddEdge(13, 10);
            graph.AddEdge(14, 10);

            var fromSearch = new[] { 1, 4, 0, 6, 5, 7 };
            var toSearch   = new[] { 14, 10, 8, 13, 7, 9 };

            //act
            var result = graph.BidirectionalSearch(1, 14);

            //assert
            result[0].Count().ShouldBeEquivalentTo(fromSearch.Length);
            result[1].Count().ShouldBeEquivalentTo(toSearch.Length);

            for (int i = 0; i < fromSearch.Length; i++)
            {
                fromSearch[i].ShouldBeEquivalentTo(result[0].ElementAt(i));
            }
            for (int i = 0; i < toSearch.Length; i++)
            {
                toSearch[i].ShouldBeEquivalentTo(result[1].ElementAt(i));
            }

            graph.Capacity.ShouldBeEquivalentTo(15);
            graph.Count.ShouldBeEquivalentTo(15);
        }