コード例 #1
0
        public static bool DegreeCondition(this Graph g, List <long> stacks, long pot, Func <List <int>, int> missingEdges = null)
        {
            if (missingEdges == null)
            {
                missingEdges = Y => 0;
            }

            var colorGraphs = GetColorGraphs(stacks, pot);

            foreach (var X in ListUtility.EnumerateSublists(g.Vertices))
            {
                var e = g.EdgesOn(X) - missingEdges(X);

                if (e <= 0)
                {
                    continue;
                }

                var value = colorGraphs.Sum(cg => cg.IntersectionCount(X) / 2);

                if (value < e)
                {
                    return(false);
                }
            }

            return(true);
        }
コード例 #2
0
ファイル: NonCrossing.cs プロジェクト: landon/WebGraphs
        public static List <Graph> LoadGraphs(string path, int n)
        {
            var lll = Load(path);

            var gs = new List <Graph>();

            foreach (var ll in lll)
            {
                var g = Choosability.Graphs.C(n);
                foreach (var l in ll)
                {
                    if (l.Count <= 1)
                    {
                        continue;
                    }

                    foreach (var pair in ListUtility.EnumerateSublists(l, 2))
                    {
                        g = g.AddEdge(pair[0], pair[1]);
                    }
                }

                g.VertexWeight = g.Vertices.ToList();
                gs.Add(g);
            }
            return(gs);
        }
コード例 #3
0
        public static IEnumerable <Graph> EnumerateClass(List <Graph> excluded, int maxVertices = int.MaxValue)
        {
            excluded.Sort((g1, g2) => g1.N.CompareTo(g2.N));

            if (excluded.Count > 0 && excluded[0].N <= 1)
            {
                yield break;
            }
            yield return(new Graph(new List <int>()));

            var lastLevel = new List <Graph>()
            {
                new Graph(new List <int>())
            };

            while (lastLevel.Count > 0)
            {
                var currentLevel = new List <Graph>();

                foreach (var G in lastLevel)
                {
                    List <List <int> > neighborSets;

                    if (G.IsComplete())
                    {
                        neighborSets = ListUtility.EnumerateSublists(Enumerable.Range(0, G.N).ToList()).Distinct(_sizeComparer).ToList();
                    }
                    else
                    {
                        neighborSets = ListUtility.GenerateSublists(Enumerable.Range(0, G.N).ToList());
                    }

                    foreach (var neighborSet in neighborSets.Where(l => l.Count > 0))
                    {
                        var H = G.AttachNewVertex(neighborSet);

                        if (currentLevel.Any(W => Graph.Isomorphic(H, W)))
                        {
                            continue;
                        }
                        if (excluded.Any(W => H.ContainsInduced(W)))
                        {
                            continue;
                        }

                        currentLevel.Add(H);
                        yield return(H);
                    }
                }

                if (currentLevel.Count > 0 && currentLevel[0].N >= maxVertices)
                {
                    yield break;
                }
                lastLevel = currentLevel;
            }
        }
コード例 #4
0
        HashSet <Board> GenerateAllBoards(Template template, int colorCount, Action <ThoughtProgress> progress = null)
        {
            var boards = new HashSet <Board>();

            var vertices = Enumerable.Range(0, template.Sizes.Count).ToList();
            var potSet   = Enumerable.Range(0, colorCount).ToList();
            var pot      = potSet.ToInt64();

            var fix = vertices.OrderBy(v => Math.Abs(template.Sizes[v] - colorCount / 2)).First();

            var boardsChunk = new List <Board>();

            foreach (var assignmentSets in vertices.Select(v =>
            {
                if (v == fix)
                {
                    return(potSet.Take(template.Sizes[v]).ToList().EnList());
                }

                return(ListUtility.EnumerateSublists(potSet, template.Sizes[v]));
            }).CartesianProduct())
            {
                var sets        = assignmentSets.ToList();
                var totalColors = sets.SelectMany(set => set).Distinct().Count();
                if (totalColors < colorCount)
                {
                    continue;
                }

                var stacks = sets.Select(list => list.ToInt64()).ToList();

                var superAbundant = Knowledge.GraphKnowledge.Graph.DegreeCondition(stacks, pot);
                var board         = new Board(stacks, pot);

                if (SuperabundantOnly && !superAbundant)
                {
                    continue;
                }

                boards.Add(board);
                boardsChunk.Add(board);

                if (boardsChunk.Count > 100)
                {
                    if (progress != null)
                    {
                        progress(new ThoughtProgress()
                        {
                            BoardsAdded = boardsChunk.ToList(), WinLength = -1
                        });
                    }

                    boardsChunk.Clear();
                }
            }

            if (boardsChunk.Count > 0 && progress != null)
            {
                progress(new ThoughtProgress()
                {
                    BoardsAdded = boardsChunk.ToList(), WinLength = -1
                });
            }

            return(boards);
        }
コード例 #5
0
ファイル: SlimMind.cs プロジェクト: landon/Playground
        void GenerateAllBoards(Template template, int colorCount, Action <Tuple <string, int> > progress = null)
        {
            var boards = new HashSet <Board>();

            var vertices = Enumerable.Range(0, template.Sizes.Count).ToList();
            var potSet   = Enumerable.Range(0, colorCount).ToList();
            var pot      = potSet.ToInt64();

            var fix     = vertices.OrderBy(v => Math.Abs(template.Sizes[v] - colorCount / 2)).First();
            var lastP   = -1;
            var current = 0;
            var total   = vertices.Aggregate(1L, (x, v) =>
            {
                if (v == fix)
                {
                    return(x);
                }

                return(x * ListUtility.BinomialCoefficient(potSet.Count, template.Sizes[v]));
            });

            foreach (var assignmentSets in vertices.Select(v =>
            {
                if (v == fix)
                {
                    return(potSet.Take(template.Sizes[v]).ToList().EnList());
                }

                return(ListUtility.EnumerateSublists(potSet, template.Sizes[v]));
            }).CartesianProduct())
            {
                var sets        = assignmentSets.ToList();
                var totalColors = sets.SelectMany(set => set).Distinct().Count();
                if (totalColors < colorCount)
                {
                    continue;
                }

                var stacks = sets.Select(list => list.ToInt64()).ToList();

                if (SuperabundantOnly && !Knowledge.GraphKnowledge.Graph.DegreeCondition(stacks, pot))
                {
                    continue;
                }

                var board = new Board(stacks, pot);

                var count = boards.Count;
                boards.Add(board);

                if (boards.Count > count)
                {
                    BoardLookup[NextBoardID] = board;
                    BoardIDLookup[board]     = NextBoardID;

                    NextBoardID++;
                }

                if (progress != null)
                {
                    var p = (int)(100 * current / total);
                    if (p > lastP)
                    {
                        progress(new Tuple <string, int>("Finding all positions...", p));
                        lastP = p;
                    }
                }

                current++;
            }
        }
コード例 #6
0
ファイル: SuperDuperLocal.cs プロジェクト: landon/WebGraphs
        static int GammaInfinity(Graph G, int r)
        {
            var RCliques = G.N >= r?ListUtility.EnumerateSublists(G.Vertices, r).Where(X => G.IsClique(X)) : Enumerable.Empty <List <int> >();

            return(ReedAverage(G, G.EnumerateMaximalCliques().Concat(RCliques)));
        }