Esempio n. 1
0
        public void Tree()
        {
            var reader = new DotReader(new StringReader(@"
strict graph {
    1 -- 2
    2 -- 3
    2 -- 4
    1 -- 5
}
strict graph {
    1 -- 2
    2 -- 3
    2 -- 4
    1 -- 5
}"));
            var g      = reader.Read();
            var h      = reader.Read();

            var coloring = ColorRefinement.FindColoring(g, h);

            Assert.Equal(10, coloring.Count);
            Assert.Equal(4, coloring.Values.Distinct().Count());
            Assert.Equal(coloring[g.Nodes["1"]], coloring[h.Nodes["1"]]);
            Assert.Equal(coloring[g.Nodes["2"]], coloring[h.Nodes["2"]]);
            Assert.Equal(coloring[g.Nodes["3"]], coloring[h.Nodes["3"]]);
            Assert.Equal(coloring[g.Nodes["4"]], coloring[h.Nodes["4"]]);
            Assert.Equal(coloring[g.Nodes["5"]], coloring[h.Nodes["5"]]);
        }
Esempio n. 2
0
        public void GraphAttributes()
        {
            var g = new Graph();

            g.Nodes.Add("A");
            g.Nodes.Add("B");
            g.Nodes.Add("C");

            g.Edges.Add("A", "B");
            g.Edges.Add("B", "C");
            g.Edges.Add("C", "A");

            g.UserData["color"] = "blue";
            g.UserData["label"] = "Test";

            var reader = new StringReader(
                @"strict digraph {
color=blue
A -> B -> C -> A
label=Test
}");

            var dotReader = new DotReader(reader);
            var h         = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer()
            {
                IncludeUserData = true
            });
        }
Esempio n. 3
0
        public void NodeChain()
        {
            var g = new Graph();

            g.Nodes.Add("A");
            g.Nodes.Add("B");
            g.Nodes.Add("C");

            g.Edges.Add("A", "B");
            g.Edges.Add("B", "C");

            var reader = new StringReader(
                @"strict digraph { 

A -> B -> C

}");
            var dotReader = new DotReader(reader);
            var h         = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer()
            {
                NodeComparer = new NodeComparer()
                {
                    IncludeUserData = true
                }
            });
        }
Esempio n. 4
0
        public void SpecialCharacters()
        {
            var g = new Graph();

            var n0 = g.Nodes.Add("IL_0000");
            var n1 = g.Nodes.Add("IL_0001");
            var n2 = g.Nodes.Add("IL_0002");
            var n3 = g.Nodes.Add("IL_0003");

            g.Edges.Add(n0, n1);
            g.Edges.Add(n0, n2);
            g.Edges.Add(n1, n3);
            g.Edges.Add(n2, n3);

            var reader    = new StringReader(@"
strict digraph {
    IL_0000 -> IL_0001
    IL_0001 -> IL_0003
    IL_0000 -> IL_0002
    IL_0002 -> IL_0003
}");
            var dotReader = new DotReader(reader);
            var h         = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer());
        }
Esempio n. 5
0
        public void SubGraphToNode()
        {
            var g = new Graph();

            g.Nodes.Add("A");
            g.Nodes.Add("B");
            g.Nodes.Add("C");

            g.Edges.Add("B", "C");
            g.Edges.Add("B", "A");
            g.Edges.Add("C", "A");

            g.SubGraphs.Add(new SubGraph(g.Nodes["B"], g.Nodes["C"]));

            var reader = new StringReader(
                @"strict digraph { 

{ B -> C } -> A

}");
            var dotReader = new DotReader(reader);
            var h         = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer()
            {
                IncludeSubGraphs = true,
                NodeComparer     = new NodeComparer()
                {
                    IncludeUserData = true
                }
            });
        }
Esempio n. 6
0
        public void EdgeAttributes()
        {
            var g = new Graph();

            g.Nodes.Add("A");
            g.Nodes.Add("B");
            g.Nodes.Add("C");

            g.Edges.Add("A", "B").UserData["color"]           = "red";
            g.Edges.Add("B", "C").UserData["color"]           = "red";
            g.Nodes["B"].OutgoingEdges["C"].UserData["style"] = "dashed";

            var reader = new StringReader(
                @"strict digraph { 
A -> B [color=red]
B -> C [color=green, style=dashed]
}");
            var dotReader = new DotReader(reader);

            var h = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer()
            {
                NodeComparer = new NodeComparer()
                {
                    IncludeUserData = true
                }
            });
        }
Esempio n. 7
0
        public void NamedSubGraph()
        {
            var g = new Graph();

            g.Nodes.Add("A");
            g.Nodes.Add("B");
            g.Nodes.Add("C");
            g.Nodes.Add("D");

            g.Edges.Add("A", "B");
            g.Edges.Add("C", "D");

            g.SubGraphs.Add(new SubGraph("H", g.Nodes["A"], g.Nodes["B"]));

            var reader = new StringReader(
                @"strict digraph { 

subgraph H { A -> B }

C -> D

}");
            var dotReader = new DotReader(reader);
            var h         = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer()
            {
                IncludeSubGraphs = true,
                NodeComparer     = new NodeComparer()
                {
                    IncludeUserData = true
                }
            });
        }
Esempio n. 8
0
        public void CustomSerializer()
        {
            var g = new Graph();
            var a = g.Nodes.Add("A");
            var b = g.Nodes.Add("B");

            a.UserData["MyIntegerList"] = new List <int> {
                1, 2, 3
            };
            b.UserData["MyProperty"] = "SomeValue";

            var reader = new StringReader(
                @"strict digraph {
A [MyIntegerList=""1,2,3""]
B [MyProperty=""SomeValue""]
}");

            var dotReader = new DotReader(reader, new CustomUserDataSerializer());
            var h         = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer()
            {
                NodeComparer = new NodeComparer {
                    IncludeUserData = true
                }
            });
        }
Esempio n. 9
0
        public void SimpleUndirectedEdges()
        {
            var g = new Graph(false);

            g.Nodes.Add("A");
            g.Nodes.Add("B");
            g.Nodes.Add("C");

            g.Edges.Add("A", "B");
            g.Edges.Add("B", "C");

            var reader = new StringReader(
                @"strict graph { 
    A
    B
    C

    A -- B
    B -- C
}");
            var dotReader = new DotReader(reader);

            var h = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer());
        }
Esempio n. 10
0
        public void CoarsestNotABijection()
        {
            var reader = new DotReader(new StringReader(@"
            strict graph {
                1 -- 2 -- 3 -- 4 -- 5 -- 6 -- 7
                3 -- 5
            }
            strict graph {
                1 -- 2 -- 3 -- 4 -- 5 -- 6 -- 7
                3 -- 5
            }"));
            var g      = reader.Read();
            var h      = reader.Read();

            var isomorphismFinder = new ColorIsomorphismFinder();

            var isomorphisms = isomorphismFinder.FindIsomorphisms(g, h).ToArray();

            Assert.Equal(2, isomorphisms.Length);

            var correctIsomorphisms = new[]
            {
                new Dictionary <Node, Node>
                {
                    [g.Nodes["1"]] = h.Nodes["1"],
                    [g.Nodes["2"]] = h.Nodes["2"],
                    [g.Nodes["3"]] = h.Nodes["3"],
                    [g.Nodes["4"]] = h.Nodes["4"],
                    [g.Nodes["5"]] = h.Nodes["5"],
                    [g.Nodes["6"]] = h.Nodes["6"],
                    [g.Nodes["7"]] = h.Nodes["7"],
                },
                new Dictionary <Node, Node>
                {
                    [g.Nodes["1"]] = h.Nodes["7"],
                    [g.Nodes["2"]] = h.Nodes["6"],
                    [g.Nodes["3"]] = h.Nodes["5"],
                    [g.Nodes["4"]] = h.Nodes["4"],
                    [g.Nodes["5"]] = h.Nodes["3"],
                    [g.Nodes["6"]] = h.Nodes["2"],
                    [g.Nodes["7"]] = h.Nodes["1"],
                },
            };

            Assert.All(isomorphisms, x => Assert.Contains(x, correctIsomorphisms));
        }
Esempio n. 11
0
        public void EmptyGraph()
        {
            var g = new Graph(false);

            var reader    = new StringReader("strict graph { }");
            var dotReader = new DotReader(reader);

            var h = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer());
        }
Esempio n. 12
0
        public void Simple()
        {
            var reader = new DotReader(new StringReader(@"
strict graph {
    1 -- 2 -- 3
}
strict graph {
    3 -- 2 -- 1
}"));
            var g      = reader.Read();
            var h      = reader.Read();

            var coloring = ColorRefinement.FindColoring(g, h);

            Assert.Equal(6, coloring.Count);
            Assert.Equal(2, coloring.Values.Distinct().Count());
            Assert.Equal(coloring[g.Nodes["1"]], coloring[h.Nodes["1"]]);
            Assert.Equal(coloring[g.Nodes["2"]], coloring[h.Nodes["2"]]);
            Assert.Equal(coloring[g.Nodes["3"]], coloring[h.Nodes["3"]]);
        }
Esempio n. 13
0
        private static void Validate(Graph g)
        {
            var writer    = new StringWriter();
            var dotWriter = new DotWriter(writer);

            dotWriter.Write(g);

            var reader    = new StringReader(writer.ToString());
            var dotReader = new DotReader(reader);
            var h         = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer());
        }
Esempio n. 14
0
        public void Branching()
        {
            var reader = new DotReader(new StringReader(@"
strict graph {
    1 -- 2 -- 4
    1 -- 3 -- 4
}
strict graph {
    4 -- 2 -- 1
    4 -- 3 -- 1
}"));
            var g      = reader.Read();
            var h      = reader.Read();

            var coloring = ColorRefinement.FindColoring(g, h);

            Assert.Equal(8, coloring.Count);
            Assert.Single(coloring.Values.Distinct());
            Assert.Equal(coloring[g.Nodes["1"]], coloring[h.Nodes["1"]]);
            Assert.Equal(coloring[g.Nodes["2"]], coloring[h.Nodes["2"]]);
            Assert.Equal(coloring[g.Nodes["3"]], coloring[h.Nodes["3"]]);
            Assert.Equal(coloring[g.Nodes["4"]], coloring[h.Nodes["4"]]);
        }
Esempio n. 15
0
        public void UndirectedEdgesInDirectedGraph()
        {
            var reader = new StringReader(
                @"strict digraph { 
    A
    B
    C

    A -- B
    B -- C
}");
            var dotReader = new DotReader(reader);

            Assert.Throws <SyntaxException>(() => dotReader.Read());
        }
Esempio n. 16
0
        public void SinglePath()
        {
            var reader = new StringReader(
                @"graph {
A -- B -- C
}");
            var dotReader = new DotReader(reader);
            var g         = dotReader.Read();

            g.Edges["A", "B"].UserData[DistanceProperty] = 1;
            g.Edges["B", "C"].UserData[DistanceProperty] = 2;

            var finder = new DijkstraPathFinder(DistanceProperty);
            var path   = finder.FindPath(g.Nodes["A"], g.Nodes["C"]).ToArray();

            Assert.Equal(new[] { g.Nodes["A"], g.Nodes["B"], g.Nodes["C"] }, path);
        }
Esempio n. 17
0
        public void UnreachableNode()
        {
            var reader = new StringReader(
                @"graph {
A -- B -- C -- D
E
}");
            var dotReader = new DotReader(reader);
            var g         = dotReader.Read();

            g.Edges["A", "B"].UserData[DistanceProperty] = 1;
            g.Edges["B", "C"].UserData[DistanceProperty] = 2;
            g.Edges["C", "D"].UserData[DistanceProperty] = 3;

            var finder = new DijkstraPathFinder(DistanceProperty);

            Assert.Null(finder.FindPath(g.Nodes["A"], g.Nodes["E"]));
        }
Esempio n. 18
0
        private static void Validate(Graph g, bool separate, bool semicolons)
        {
            var writer    = new StringWriter();
            var dotWriter = new DotWriter(writer);

            dotWriter.SeparateNodesAndEdges = separate;
            dotWriter.IncludeSemicolons     = semicolons;
            dotWriter.Write(g);

            var reader    = new StringReader(writer.ToString());
            var dotReader = new DotReader(reader);
            var h         = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer
            {
                IncludeUserData = true
            });
        }
Esempio n. 19
0
        public void IdentifierStartingWithDigit()
        {
            // https://github.com/Washi1337/Rivers/issues/7

            var g = new Graph();

            g.Nodes.Add("0");
            g.Nodes.Add("x0");
            g.Nodes.Add("x1");
            g.Edges.Add("x0", "0");

            var reader    = new StringReader(@"
strict digraph {
    0x0 -> 0x1
}");
            var dotReader = new DotReader(reader);
            var h         = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer());
        }
Esempio n. 20
0
        public void EscapedIdentifiers()
        {
            var g = new Graph();

            g.Nodes.Add("A\"A");
            g.Nodes.Add("B\"B");
            g.Nodes.Add("C\"C");

            g.Edges.Add("B\"B", "C\"C");

            var reader = new StringReader(
                @"strict digraph { 
""A\""A""
""B\""B"" -> ""C\""C""
}");
            var dotReader = new DotReader(reader);

            var h = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer());
        }
Esempio n. 21
0
        public void SimpleNodes()
        {
            var g = new Graph();

            g.Nodes.Add("A");
            g.Nodes.Add("B");
            g.Nodes.Add("C");

            var reader = new StringReader(
                @"strict digraph { 
    A
    B
    C

}");
            var dotReader = new DotReader(reader);

            var h = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer());
        }
Esempio n. 22
0
        public void OnlyEdges()
        {
            var g = new Graph();

            g.Nodes.Add("A");
            g.Nodes.Add("B");
            g.Nodes.Add("C");

            g.Edges.Add("A", "B");
            g.Edges.Add("B", "C");

            var reader = new StringReader(
                @"strict digraph { 
    A -> B
    B -> C
}");
            var dotReader = new DotReader(reader);

            var h = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer());
        }
Esempio n. 23
0
        public void NamedGraph()
        {
            var g = new Graph();

            var n0 = g.Nodes.Add("0");
            var n1 = g.Nodes.Add("1");

            g.Edges.Add(n0, n1);

            var reader    = new StringReader(@"
strict digraph G {
    0 -> 1
}");
            var dotReader = new DotReader(reader);
            var h         = dotReader.Read();

            Assert.Equal(g, h, new GraphComparer()
            {
                NodeComparer = new NodeComparer {
                    IncludeUserData = true
                }
            });
        }
Esempio n. 24
0
        public void ComplexLoopTest()
        {
            var reader = new DotReader(new StringReader(@"
            strict digraph {
                1 -> 2 -> 3 -> 4 -> 3 -> 5 -> 6 -> 7
            }"));

            var cfg = reader.Read();

            var info = new DominatorInfo(cfg.Nodes["1"]);

            Assert.True(info.Dominates(cfg.Nodes["1"], cfg.Nodes["1"]));
            Assert.True(info.Dominates(cfg.Nodes["1"], cfg.Nodes["2"]));
            Assert.True(info.Dominates(cfg.Nodes["1"], cfg.Nodes["3"]));
            Assert.True(info.Dominates(cfg.Nodes["1"], cfg.Nodes["4"]));
            Assert.True(info.Dominates(cfg.Nodes["1"], cfg.Nodes["5"]));
            Assert.True(info.Dominates(cfg.Nodes["1"], cfg.Nodes["6"]));
            Assert.True(info.Dominates(cfg.Nodes["1"], cfg.Nodes["7"]));

            Assert.False(info.Dominates(cfg.Nodes["2"], cfg.Nodes["1"]));
            Assert.True(info.Dominates(cfg.Nodes["2"], cfg.Nodes["2"]));
            Assert.True(info.Dominates(cfg.Nodes["2"], cfg.Nodes["3"]));
            Assert.True(info.Dominates(cfg.Nodes["2"], cfg.Nodes["4"]));
            Assert.True(info.Dominates(cfg.Nodes["2"], cfg.Nodes["5"]));
            Assert.True(info.Dominates(cfg.Nodes["2"], cfg.Nodes["6"]));
            Assert.True(info.Dominates(cfg.Nodes["2"], cfg.Nodes["7"]));

            Assert.False(info.Dominates(cfg.Nodes["3"], cfg.Nodes["1"]));
            Assert.False(info.Dominates(cfg.Nodes["3"], cfg.Nodes["2"]));
            Assert.True(info.Dominates(cfg.Nodes["3"], cfg.Nodes["3"]));
            Assert.True(info.Dominates(cfg.Nodes["3"], cfg.Nodes["4"]));
            Assert.True(info.Dominates(cfg.Nodes["3"], cfg.Nodes["5"]));
            Assert.True(info.Dominates(cfg.Nodes["3"], cfg.Nodes["6"]));
            Assert.True(info.Dominates(cfg.Nodes["3"], cfg.Nodes["7"]));

            Assert.False(info.Dominates(cfg.Nodes["4"], cfg.Nodes["1"]));
            Assert.False(info.Dominates(cfg.Nodes["4"], cfg.Nodes["2"]));
            Assert.False(info.Dominates(cfg.Nodes["4"], cfg.Nodes["3"]));
            Assert.True(info.Dominates(cfg.Nodes["4"], cfg.Nodes["4"]));
            Assert.False(info.Dominates(cfg.Nodes["4"], cfg.Nodes["5"]));
            Assert.False(info.Dominates(cfg.Nodes["4"], cfg.Nodes["6"]));
            Assert.False(info.Dominates(cfg.Nodes["4"], cfg.Nodes["7"]));

            Assert.False(info.Dominates(cfg.Nodes["5"], cfg.Nodes["1"]));
            Assert.False(info.Dominates(cfg.Nodes["5"], cfg.Nodes["2"]));
            Assert.False(info.Dominates(cfg.Nodes["5"], cfg.Nodes["3"]));
            Assert.False(info.Dominates(cfg.Nodes["5"], cfg.Nodes["4"]));
            Assert.True(info.Dominates(cfg.Nodes["5"], cfg.Nodes["5"]));
            Assert.True(info.Dominates(cfg.Nodes["5"], cfg.Nodes["6"]));
            Assert.True(info.Dominates(cfg.Nodes["5"], cfg.Nodes["7"]));

            Assert.False(info.Dominates(cfg.Nodes["6"], cfg.Nodes["1"]));
            Assert.False(info.Dominates(cfg.Nodes["6"], cfg.Nodes["2"]));
            Assert.False(info.Dominates(cfg.Nodes["6"], cfg.Nodes["3"]));
            Assert.False(info.Dominates(cfg.Nodes["6"], cfg.Nodes["4"]));
            Assert.False(info.Dominates(cfg.Nodes["6"], cfg.Nodes["5"]));
            Assert.True(info.Dominates(cfg.Nodes["6"], cfg.Nodes["6"]));
            Assert.True(info.Dominates(cfg.Nodes["6"], cfg.Nodes["7"]));

            Assert.False(info.Dominates(cfg.Nodes["7"], cfg.Nodes["1"]));
            Assert.False(info.Dominates(cfg.Nodes["7"], cfg.Nodes["2"]));
            Assert.False(info.Dominates(cfg.Nodes["7"], cfg.Nodes["3"]));
            Assert.False(info.Dominates(cfg.Nodes["7"], cfg.Nodes["4"]));
            Assert.False(info.Dominates(cfg.Nodes["7"], cfg.Nodes["5"]));
            Assert.False(info.Dominates(cfg.Nodes["7"], cfg.Nodes["6"]));
            Assert.True(info.Dominates(cfg.Nodes["7"], cfg.Nodes["7"]));
        }