Ejemplo n.º 1
0
        public void Test1()
        {
            var graph = new Graph();

            var a = new StringVertex("A");
            var b = new StringVertex("B");
            var c = new StringVertex("C");
            var d = new StringVertex("D");

            graph.AddVertex(a);
            graph.AddVertex(b);
            graph.AddVertex(c);
            graph.AddVertex(d);

            graph.AddEdge(new Edge(a, b));
            //graph.AddEdge(new Edge(a, c));
            graph.AddEdge(new Edge(a, d));
            graph.AddEdge(new Edge(b, c));
            graph.AddEdge(new Edge(b, d));
            graph.AddEdge(new Edge(c, d));

            Console.WriteLine(GraphPrinter.ToDot(graph));

            var cycles = graph.GetCycles(true);

            Assert.IsTrue(cycles.Count == 2);
            Assert.IsTrue(cycles[0].EdgeCount == 3);
            Assert.IsTrue(cycles[1].EdgeCount == 3);

            List <List <Edge> > overlap = cycles[0].Overlap(cycles[1]);

            Assert.IsTrue(overlap.Count == 1);
            Assert.IsTrue(overlap[0].Count == 1);
        }
Ejemplo n.º 2
0
        public void Print2()
        {
            var graph = new Graph();

            var v1 = new StringVertex("blue");

            graph.AddVertex(v1);
            var v2 = new StringVertex("green");

            graph.AddVertex(v2);
            var v3 = new StringVertex("yellow");

            graph.AddVertex(v3);
            var v4 = new StringVertex("yellow");

            graph.AddVertex(v4);
            var v5 = new StringVertex("blue");

            graph.AddVertex(v5);
            var v6 = new StringVertex("green");

            graph.AddVertex(v6);

            graph.AddEdge(new StringEdge(v1, v4, "green"));
            graph.AddEdge(new StringEdge(v2, v5, "yellow"));
            graph.AddEdge(new StringEdge(v3, v6, "blue"));
            graph.AddEdge(new StringEdge(v4, v5, "green"));
            graph.AddEdge(new StringEdge(v5, v6, "yellow"));
            graph.AddEdge(new StringEdge(v6, v4, "blue"));

            string graphString = GraphPrinter.ToDot(graph, true, true);

            File.WriteAllText("print2.gv", graphString);
        }
Ejemplo n.º 3
0
        public void TestCreateEdge()
        {
            var graph = new Graph();

            var v1 = new StringVertex("a");

            graph.AddVertex(v1);
            var v2 = new StringVertex("b");

            graph.AddVertex(v2);

            // should fail if vertices are the same
            Assert.ThrowsException <ArgumentException>(() => new Edge(v1, v1));

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

            // should fail if either vertex is not in graph
            var v1_2 = new StringVertex("a");
            var v2_2 = new StringVertex("b");
            var e1   = new Edge(v1, v1_2);
            var e2   = new Edge(v1_2, v1);
            var e3   = new Edge(v1_2, v2_2);

            Assert.ThrowsException <ArgumentException>(() => graph.AddEdge(e1));
            Assert.ThrowsException <ArgumentException>(() => graph.AddEdge(e2));
            Assert.ThrowsException <ArgumentException>(() => graph.AddEdge(e3));
        }
Ejemplo n.º 4
0
        public void TestRemovEdge()
        {
            var graph = new Graph();

            var v1 = new StringVertex("a");
            var v2 = new StringVertex("b");
            var e  = new StringEdge(v1, v2, "c");

            // should fail if edge is not in graph
            Assert.ThrowsException <ArgumentException>(() => graph.RemoveEdge(e));
        }
Ejemplo n.º 5
0
        public void Test3()
        {
            var graph = new Graph();

            var v1 = new StringVertex("blue");

            graph.AddVertex(v1);
            var v2 = new StringVertex("green");

            graph.AddVertex(v2);
            var v3 = new StringVertex("yellow");

            graph.AddVertex(v3);
            var v4 = new StringVertex("yellow");

            graph.AddVertex(v4);
            var v5 = new StringVertex("blue");

            graph.AddVertex(v5);
            var v6 = new StringVertex("green");

            graph.AddVertex(v6);

            graph.AddEdge(new StringEdge(v1, v4, "green"));
            graph.AddEdge(new StringEdge(v2, v5, "yellow"));
            graph.AddEdge(new StringEdge(v3, v6, "blue"));
            graph.AddEdge(new StringEdge(v4, v5, "green"));
            graph.AddEdge(new StringEdge(v5, v6, "yellow"));
            graph.AddEdge(new StringEdge(v6, v4, "blue"));

            var pattern = new Graph();

            var vp1 = new StringVertex("yellow");

            pattern.AddVertex(vp1);
            var vp2 = new StringVertex("blue");

            pattern.AddVertex(vp2);
            var vp3 = new StringVertex("green");

            pattern.AddVertex(vp3);

            pattern.AddEdge(new StringEdge(vp1, vp2, "green"));
            pattern.AddEdge(new StringEdge(vp2, vp3, "yellow"));
            pattern.AddEdge(new StringEdge(vp3, vp1, "blue"));

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

            var vp4 = new StringVertex("green");

            pattern.AddVertex(vp4);
            // wrong edge color
            var ef = new StringEdge(vp2, vp4, "blue");

            pattern.AddEdge(ef);

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

            Assert.ThrowsException <System.ArgumentException>(() => pattern.AddVertex(vp4));
            var ef2 = new DataEdge <float>(vp2, vp4, 4.2f);

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