Ejemplo 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"]]);
        }
Ejemplo n.º 2
0
        private static IEnumerable <IDictionary <int, ISet <Node> > > FindIsomorphisms(Graph g, IList <Node> d, IList <Node> i)
        {
            var refinement = new ColorRefinement(g);

            refinement.D.AddRange(d);
            refinement.I.AddRange(i);
            refinement.RefinePartitioning();

            // Unbalanced colorings can never result in isomorphisms.
            if (!IsBalanced(refinement.ColorSets.Values))
            {
                yield break;
            }

            // Base case: If we found a bijection, then this is an isomorphism!
            if (IsBijection(refinement.ColorSets.Values))
            {
                yield return(refinement.ColorSets);

                yield break;
            }

            // Find any of the cells that can be refined further by choosing one element.
            var bigSet = refinement.ColorSets.Values.First(x => x.Count >= 4);
            var nodeA  = bigSet.First(x => x.Name.StartsWith(GraphAPrefix));
            var d1     = new List <Node>(d)
            {
                nodeA
            };

            // See if the selected node can be mapped to any of the other graph's nodes.
            foreach (var nodeB in bigSet.Where(x => x.Name.StartsWith(GraphBPrefix)))
            {
                var i1 = new List <Node>(i)
                {
                    nodeB
                };
                foreach (var isomorphism in FindIsomorphisms(g, d1, i1))
                {
                    yield return(isomorphism);
                }
            }
        }
Ejemplo n.º 3
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"]]);
        }
Ejemplo n.º 4
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"]]);
        }