Esempio n. 1
0
        private Dictionary <Component, int> Bfs(Component a, Component b, DesireComponent avaliableComponents = null)
        {
            var queue = new Queue <Component>();

            queue.Enqueue(a);
            var distance = new Dictionary <Component, int>();

            distance[a] = 0;

            while (queue.Any())
            {
                var current    = queue.Dequeue();
                var dist       = distance[current];
                var neighbours = avaliableComponents == null
                    ? GetEdges(current)
                    : GetEdges(current).Where(x => avaliableComponents.Components.Contains(x.Item1));

                foreach (var neighbour in neighbours)
                {
                    if (!distance.ContainsKey(neighbour.Item1))
                    {
                        distance[neighbour.Item1] = dist + 1;
                        queue.Enqueue(neighbour.Item1);
                    }
                    if (neighbour.Item1 == b)
                    {
                        return(distance);
                    }
                }
            }
            return(distance);
        }
Esempio n. 2
0
        public Edge GetFragileEdge(DesireComponent desire)
        {
            var edgePower = new Dictionary <Edge, (int, Component)>();
            var grey      = new List <(Component, Edge)>();
            var color     = new Dictionary <Component, int>();

            calculatePower(desire.Root, desire, edgePower, grey, color);

            return(!edgePower.Any()
                ? GetMostExpensiveEdge()
                : edgePower.OrderBy(x => x.Value).First().Key);
        }
Esempio n. 3
0
        public DesireComponent FindGreedyFullComponent(int size)
        {
            var result = new DesireComponent();

            result.Root = GetBestComponentByChart(size / 2);
            result.Components.Add(result.Root);

            var desireScore = result.Root.Score.Clone();

            var S = new SortedSet <(long, Component)>();

            foreach (var c in GetEdges(result.Root).Select(x => x.Item1))
            {
                S.Add((desireScore.Clone().Add(c.Score).SelfScore, c));
            }

            for (int i = 0; i < size - 1; ++i)
            {
                while (S.Count != 0 && result.Components.Contains(S.Max.Item2))
                {
                    S.Remove(S.Max);
                }
                if (S.Count == 0)
                {
                    break;
                }
                var newComponent = S.Max.Item2;
                result.Components.Add(newComponent);
                desireScore.Add(newComponent.Score);
                S.Remove(S.Max);

                if (newComponent.Score.Mines.Any())
                {
                    foreach (var c in state.components.Where(c => !result.Components.Contains(c)))
                    {
                        S.Add((desireScore.Clone().Add(c.Score).SelfScore - desireScore.SelfScore, c));
                    }
                }
                else
                {
                    foreach (var c in newComponent.Nodes.SelectMany(n => state.map.GetAvaliableEdges(n.Id, state.punter).Select(e => nodeToComponent[e.Item1.Id])).Distinct().Where(x => !result.Components.Contains(x)))
                    {
                        S.Add((desireScore.Clone().Add(c.Score).SelfScore - desireScore.SelfScore, c));
                    }
                }
            }

            return(result);
        }
Esempio n. 4
0
        public DesireComponent FindShortestPath(Component a, Component b, out long resultScore)
        {
            var distanceFromA = Bfs(a, b);
            var distanceFromB = Bfs(b, a);

            var length = distanceFromA[b];

            var nodes = distanceFromA
                        .Where(x => distanceFromB.ContainsKey(x.Key) && x.Value + distanceFromB[x.Key] == length)
                        .OrderBy(x => x.Value)
                        .Select(x => x.Key)
                        .ToArray();

            var dp     = new Dictionary <Component, ComponentScore>();
            var parent = new Dictionary <Component, Component>();

            dp[a] = a.Score;
            foreach (var node in nodes.Skip(1))
            {
                var prevs = GetEdges(node)
                            .Select(x => x.Item1)
                            .Where(x => dp.ContainsKey(x) && distanceFromA[x] == distanceFromA[node] - 1);

                foreach (var prev in prevs)
                {
                    var score = dp[prev].Clone().Add(node.Score).Add(b.Score);
                    if (!dp.ContainsKey(node) || dp[node].SelfScore < score.SelfScore)
                    {
                        dp[node]     = score;
                        parent[node] = prev;
                    }
                }
            }

            resultScore = dp[b].SelfScore;

            var result = new DesireComponent();

            result.Components.Add(a);
            while (a != b)
            {
                result.Components.Add(b);
                b = parent[b];
            }

            return(result);
        }
Esempio n. 5
0
        public void ClaimEdge(Node a, Node b, DesireComponent desire = null)
        {
            var sourceComponent = nodeToComponent[a.Id];
            var targetComponent = nodeToComponent[b.Id];

            if (sourceComponent != targetComponent)
            {
                sourceComponent.Union(targetComponent);
                state.components.Remove(targetComponent);
                foreach (var node in targetComponent.Nodes)
                {
                    nodeToComponent[node.Id] = sourceComponent;
                }
                if (desire != null)
                {
                    desire.Components.Remove(targetComponent);
                    if (desire.Root == targetComponent)
                    {
                        desire.Root = sourceComponent;
                    }
                }
            }
        }
Esempio n. 6
0
 private void calculatePower(Component v, DesireComponent desireComponent, Dictionary <Edge, (int, Component)> edgePower, List <(Component, Edge)> grey, Dictionary <Component, int> color)
Esempio n. 7
0
 public Edge GetMaxSubtreeEdge(DesireComponent desire)
 {
     throw new NotImplementedException();
 }
Esempio n. 8
0
        public bool IsConnected(DesireComponent component)
        {
            var visited = Bfs(component.Components.First(), null, component);

            return(visited.Count == component.Components.Count);
        }