public void TestRemoveVertex()
        {
            // (2)---(1)---(3)
            var graph = new Graph();
            var v1    = new IntVertex(1);

            graph.AddVertex(v1);
            var v2 = new IntVertex(2);

            graph.AddVertex(v2);
            var v3 = new IntVertex(3);

            graph.AddVertex(v3);
            var e1 = new Edge(v1, v2);

            graph.AddEdge(e1);
            var e2 = new Edge(v1, v3);

            graph.AddEdge(e2);

            graph.RemoveVertex(v1);
            Assert.IsFalse(graph.Vertices.Contains(v1));
            Assert.IsTrue(v1.Edges.Count == 0);
            Assert.IsFalse(v2.Edges.Contains(e1));
            Assert.IsFalse(v3.Edges.Contains(e2));

            // Assert.ThrowsException<ArgumentException>(() => graph.RemoveVertex(v1));
        }
        public void TestGetNeighbours()
        {
            // (2)---(1)---(3)
            var graph = new Graph();
            var v1    = new IntVertex(1);

            graph.AddVertex(v1);
            var v2 = new IntVertex(2);

            graph.AddVertex(v2);
            var v3 = new IntVertex(3);

            graph.AddVertex(v3);
            var e1 = new Edge(v1, v2);

            graph.AddEdge(e1);
            var e2 = new Edge(v1, v3);

            graph.AddEdge(e2);

            var neighbours = graph.GetNeighbours(v1);

            Assert.IsNotNull(neighbours);
            Assert.IsTrue(neighbours.Count == 2);
            Assert.IsTrue(neighbours.Contains(v2));
            Assert.IsTrue(neighbours.Contains(v3));
        }
Esempio n. 3
0
        public void TestGetNeighbours()
        {
            var graph = new Graph();

            var v = new IntVertex(1);

            // should fail if vertex is null or not in graph
            Assert.ThrowsException <ArgumentException>(() => graph.GetNeighbours(null));
            Assert.ThrowsException <ArgumentException>(() => graph.GetNeighbours(v));
        }
Esempio n. 4
0
        public void TestRemoveVertex()
        {
            var graph = new Graph();
            var v1    = new IntVertex(1);

            graph.AddVertex(v1);

            var v1_2 = new IntVertex(1);

            // should fail if vertex is null or not in graph
            Assert.ThrowsException <ArgumentException>(() => graph.RemoveVertex(null));
            Assert.ThrowsException <ArgumentException>(() => graph.RemoveVertex(v1_2));
        }
        public void TestCreateVertex()
        {
            var graph = new Graph();

            int vertexData = 42;
            var v1         = new IntVertex(vertexData);

            graph.AddVertex(v1);
            Assert.IsNotNull(v1);
            Assert.IsTrue(v1.Data == vertexData);
            Assert.IsNotNull(v1.Edges);
            Assert.IsTrue(v1.Edges.Count == 0);
            Assert.IsTrue(graph.Vertices.Count == 1);
        }
Esempio n. 6
0
        public void Print1()
        {
            var graph = new Graph();

            var v1 = new IntVertex(1);

            graph.AddVertex(v1);
            var v2 = new IntVertex(2);

            graph.AddVertex(v2);
            var v3 = new IntVertex(3);

            graph.AddVertex(v3);
            var v4 = new IntVertex(2);

            graph.AddVertex(v4);
            var v5 = new IntVertex(3);

            graph.AddVertex(v5);
            var v6 = new IntVertex(2);

            graph.AddVertex(v6);

            var e1 = new Edge(v1, v2);

            graph.AddEdge(e1);
            var e2 = new Edge(v2, v3);

            graph.AddEdge(e2);
            var e3 = new Edge(v1, v4);

            graph.AddEdge(e3);
            var e4 = new Edge(v2, v5);

            graph.AddEdge(e4);
            var e5 = new Edge(v3, v6);

            graph.AddEdge(e5);
            var e6 = new Edge(v4, v5);

            graph.AddEdge(e6);
            var e7 = new Edge(v5, v6);

            graph.AddEdge(e7);

            string graphString = GraphPrinter.ToDot(graph);

            File.WriteAllText("print1.gv", graphString);
        }
        public void TestRemovEdge()
        {
            // (2)---(1)
            var graph = new Graph();
            var v1    = new IntVertex(1);

            graph.AddVertex(v1);
            var v2 = new IntVertex(2);

            graph.AddVertex(v2);
            var e1 = new Edge(v1, v2);

            graph.AddEdge(e1);

            graph.RemoveEdge(e1);
            Assert.IsFalse(v1.Edges.Contains(e1));
            Assert.IsFalse(v2.Edges.Contains(e1));
        }
Esempio n. 8
0
        public void TestAdjacent()
        {
            var graph = new Graph();
            var v1    = new IntVertex(1);

            graph.AddVertex(v1);

            var v1_2 = new IntVertex(1);
            var v2_2 = new IntVertex(2);

            // should fail if either vertex is null
            Assert.ThrowsException <ArgumentException>(() => graph.Adjacent(v1, null));
            Assert.ThrowsException <ArgumentException>(() => graph.Adjacent(null, v1));
            Assert.ThrowsException <ArgumentException>(() => graph.Adjacent(null, null));

            // should fail if either vertex is not in graph
            Assert.ThrowsException <ArgumentException>(() => graph.Adjacent(v1, v1_2));
            Assert.ThrowsException <ArgumentException>(() => graph.Adjacent(v1_2, v1));
            Assert.ThrowsException <ArgumentException>(() => graph.Adjacent(v1_2, v2_2));
        }
        public void TestCreateEdge()
        {
            var graph = new Graph();

            var v1 = new IntVertex(0);

            graph.AddVertex(v1);
            var v2 = new IntVertex(1);

            graph.AddVertex(v2);

            int edgeData = 69;
            var e        = new DataEdge <int>(v1, v2, edgeData);

            graph.AddEdge(e);
            Assert.IsNotNull(e);
            Assert.IsTrue(e.Data == edgeData);
            Assert.IsTrue(v1.Edges.Contains(e));
            Assert.IsTrue(v2.Edges.Contains(e));
        }
Esempio n. 10
0
        public void TestAdjacent()
        {
            // (2)---(1)---(3)
            var graph = new Graph();
            var v1    = new IntVertex(1);

            graph.AddVertex(v1);
            var v2 = new IntVertex(2);

            graph.AddVertex(v2);
            var v3 = new IntVertex(3);

            graph.AddVertex(v3);
            var e1 = new Edge(v1, v2);

            graph.AddEdge(e1);
            var e2 = new Edge(v1, v3);

            graph.AddEdge(e2);

            Assert.IsTrue(graph.Adjacent(v1, v2));
            Assert.IsTrue(graph.Adjacent(v2, v1));
            Assert.IsFalse(graph.Adjacent(v2, v3));
        }
Esempio n. 11
0
        public void Test2()
        {
            var graph = new Graph();
            var v1    = new IntVertex(1);

            graph.AddVertex(v1);
            var v2 = new IntVertex(2);

            graph.AddVertex(v2);
            var v3 = new IntVertex(3);

            graph.AddVertex(v3);
            var v4 = new IntVertex(2);

            graph.AddVertex(v4);
            var v5 = new IntVertex(3);

            graph.AddVertex(v5);
            var v6 = new IntVertex(2);

            graph.AddVertex(v6);

            var e1 = new Edge(v1, v2);

            graph.AddEdge(e1);
            var e2 = new Edge(v2, v3);

            graph.AddEdge(e2);
            var e3 = new Edge(v1, v4);

            graph.AddEdge(e3);
            var e4 = new Edge(v2, v5);

            graph.AddEdge(e4);
            var e5 = new Edge(v3, v6);

            graph.AddEdge(e5);
            var e6 = new Edge(v4, v5);

            graph.AddEdge(e6);
            var e7 = new Edge(v5, v6);

            graph.AddEdge(e7);

            var pattern = new Graph();

            var vp1 = new IntVertex(2);

            pattern.AddVertex(vp1);
            var vp2 = new IntVertex(3);

            pattern.AddVertex(vp2);
            var vp3 = new IntVertex(2);

            pattern.AddVertex(vp3);
            var vp4 = new IntVertex(1);

            pattern.AddVertex(vp4);

            var ep1 = new Edge(vp1, vp2);

            pattern.AddEdge(ep1);
            var ep2 = new Edge(vp2, vp3);

            pattern.AddEdge(ep2);
            var ep3 = new Edge(vp3, vp4);

            pattern.AddEdge(ep3);

            Assert.IsNotNull(graph.FindPattern(pattern));

            var ef = new Edge(vp1, vp3);

            pattern.AddEdge(ef);

            Assert.IsNull(graph.FindPattern(pattern));
        }
Esempio n. 12
0
        public void Test1()
        {
            var graph = new Graph();

            var v1 = new IntVertex(1);

            graph.AddVertex(v1);
            var v2 = new IntVertex(2);

            graph.AddVertex(v2);
            var v3 = new IntVertex(3);

            graph.AddVertex(v3);
            var v4 = new IntVertex(2);

            graph.AddVertex(v4);
            var v5 = new IntVertex(3);

            graph.AddVertex(v5);
            var v6 = new IntVertex(2);

            graph.AddVertex(v6);

            var e1 = new Edge(v1, v2);

            graph.AddEdge(e1);
            var e2 = new Edge(v2, v3);

            graph.AddEdge(e2);
            var e3 = new Edge(v1, v4);

            graph.AddEdge(e3);
            var e4 = new Edge(v2, v5);

            graph.AddEdge(e4);
            var e5 = new Edge(v3, v6);

            graph.AddEdge(e5);
            var e6 = new Edge(v4, v5);

            graph.AddEdge(e6);
            var e7 = new Edge(v5, v6);

            graph.AddEdge(e7);

            var pattern = new Graph();
            var pv1     = new IntVertex(1);

            pattern.AddVertex(pv1);

            MatchResult result = graph.FindPattern(pattern);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Vertices.Count == pattern.Vertices.Count);
            Assert.IsTrue(result.Vertices[pv1] == v1);

            var pv3 = new IntVertex(3);

            pattern.AddVertex(pv3);
            var e = new Edge(pv1, pv3);

            pattern.AddEdge(e);

            Assert.IsNull(graph.FindPattern(pattern));
            pattern.RemoveEdge(e);

            var pv2 = new IntVertex(2);

            pattern.AddVertex(pv2);
            var pv4 = new IntVertex(2);

            pattern.AddVertex(pv4);
            var ep1 = new Edge(pv1, pv2);

            pattern.AddEdge(ep1);
            var ep2 = new Edge(pv2, pv3);

            pattern.AddEdge(ep2);
            var ep3 = new Edge(pv3, pv4);

            pattern.AddEdge(ep3);
            var ep4 = new Edge(pv1, pv4);

            pattern.AddEdge(ep4);

            result = graph.FindPattern(pattern);
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Vertices.Count == pattern.Vertices.Count);
            Assert.IsTrue(result.Edges.Count == 4);

            var pv5 = new IntVertex(3);

            pattern.AddVertex(pv5);
            var ep5 = new Edge(pv2, pv5);

            pattern.AddEdge(ep5);

            result = graph.FindPattern(pattern);
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Vertices.Count == pattern.Vertices.Count);
            Assert.IsTrue(result.Edges.Count == 5);

            // see if vertices/edges are there
            Assert.IsTrue(result.Vertices.ContainsKey(pv1));
            Assert.IsTrue(result.Vertices.ContainsKey(pv3));
            Assert.IsTrue(result.Vertices[pv1] == v1);
            Assert.IsTrue(result.Vertices[pv3] == v5);

            Assert.IsTrue(result.Edges.ContainsKey(ep2));
            Assert.IsTrue(result.Edges.ContainsKey(ep4));


            var pv6 = new IntVertex(2);

            pattern.AddVertex(pv6);
            var ep6 = new Edge(pv2, pv6);

            pattern.AddEdge(ep6);

            Assert.IsNull(graph.FindPattern(pattern));
        }